Skip to content
Snippets Groups Projects
Commit 7e7006b1 authored by geraldabrhm's avatar geraldabrhm
Browse files

feat: handle skeleton attack logic

parent 43cce998
Branches
Tags v2.0.0
No related merge requests found
This diff is collapsed.
fileFormatVersion: 2 fileFormatVersion: 2
guid: ff832e88aba4451479e8f2936edea27b guid: 94a59137dd731b74498b8af02444751b
PrefabImporter: PrefabImporter:
externalObjects: {} externalObjects: {}
userData: userData:
......
...@@ -90,7 +90,7 @@ public class EnemyAttack : MonoBehaviour ...@@ -90,7 +90,7 @@ public class EnemyAttack : MonoBehaviour
if(animating == false && timer >= attackOffset && (playerInRange || petInRange) && enemyHealth.currentHealth > 0) if(animating == false && timer >= attackOffset && (playerInRange || petInRange) && enemyHealth.currentHealth > 0)
{ {
// ... attack. // ... attack.
Debug.Log("Initiate attack"); // Debug.Log("Initiate attack"); // ! Debug
Attack (); Attack ();
} }
if (animating == true) { if (animating == true) {
...@@ -111,10 +111,10 @@ public class EnemyAttack : MonoBehaviour ...@@ -111,10 +111,10 @@ public class EnemyAttack : MonoBehaviour
void Attack () void Attack ()
{ {
Transform nearestAttackable = enemyMovement.getNearestAttackable(); Transform nearestAttackable = enemyMovement.getNearestAttackable();
Debug.Log("Enemy try to attack"); // Debug.Log("Enemy try to attack"); // ! Debug
if(nearestAttackable.gameObject == player) if(nearestAttackable.gameObject == player)
{ {
Debug.Log("Enemy try to attack player"); // Debug.Log("Enemy try to attack player"); // ! Debug
// If the player has health to lose... // If the player has health to lose...
if (playerHealth.currentHealth > 0) if (playerHealth.currentHealth > 0)
{ {
...@@ -125,7 +125,7 @@ public class EnemyAttack : MonoBehaviour ...@@ -125,7 +125,7 @@ public class EnemyAttack : MonoBehaviour
timer = 0f; timer = 0f;
} }
else if(nearestAttackable.gameObject == pet) { else if(nearestAttackable.gameObject == pet) {
Debug.Log("Enemy try to attack pet"); Debug.Log("Enemy try to attack pet"); // ! Debug
// If the pet has health to lose... // If the pet has health to lose...
if (petHealth.currentHealth > 0) if (petHealth.currentHealth > 0)
{ {
......
...@@ -60,7 +60,6 @@ public class EnemyMovement : MonoBehaviour ...@@ -60,7 +60,6 @@ public class EnemyMovement : MonoBehaviour
{ {
distToPlayer = Vector3.Distance(gameObject.transform.position, player.position); distToPlayer = Vector3.Distance(gameObject.transform.position, player.position);
distToPet = Vector3.Distance(gameObject.transform.position, pet.position); distToPet = Vector3.Distance(gameObject.transform.position, pet.position);
if(distToPlayer > distToPet) if(distToPlayer > distToPet)
{ {
return pet; return pet;
......
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class PetAttack : MonoBehaviour
{
public int attackDamage = 5;
public float intervalBetweenAtk = 5f;
public float atkRange = 15f;
public AudioClip arrowClip;
float timer;
AudioSource arrowAudio;
void Awake ()
{
arrowAudio = GetComponent<AudioSource>();
}
void Update ()
{
timer += Time.deltaTime;
if(timer > intervalBetweenAtk)
{
Attack();
}
}
// Assume there is no attack range
void Attack ()
{
GameObject enemyTarget = PickEnemyToAttacked();
float distToEnemyTarget = Vector3.Distance(gameObject.transform.position, enemyTarget.transform.position);
if(enemyTarget != null && distToEnemyTarget < atkRange);
{
EnemyHealth enemyHealth = enemyTarget.GetComponent<EnemyHealth>();
enemyHealth.TakeDamage(attackDamage, Vector3.zero); // TODO hitPoint is still zero vector
arrowAudio.clip = arrowClip;
arrowAudio.Play ();
// Reset timer
timer = 0f;
}
}
GameObject PickEnemyToAttacked()
{
string currentTag = GetCurrentEnemyTag();
Debug.Log($"Current enemy tag: {currentTag}");
GameObject[] enemies = GameObject.FindGameObjectsWithTag(currentTag);
Debug.Log($"Skeleton watch {enemies.Length} enemies"); // ! Debug
if(enemies.Length > 0)
{
GameObject pickedEnemy = enemies[0];
float nearestDistToEnemy = Vector3.Distance(gameObject.transform.position, pickedEnemy.transform.position);
Debug.Log($"First enemy in skeleton iteration: {pickedEnemy.name}"); // ! Debug
foreach(var enemy in enemies)
{
float distToCurrentEnemy = Vector3.Distance(gameObject.transform.position, enemy.transform.position);
if(distToCurrentEnemy < nearestDistToEnemy)
{
pickedEnemy = enemy;
nearestDistToEnemy = distToCurrentEnemy;
}
}
Debug.Log($"Nearest enemy from skeleton: {pickedEnemy.name}"); // ! Debug
return pickedEnemy;
}
Debug.Log("There are no enemy");
return null;
}
string GetCurrentEnemyTag()
{
// TODO Complete the case (if there is case unhandled)
switch(SceneManager.GetActiveScene().name)
{
case "Level_01": return "EnemyQ1";
case "Level_02": return "EnemyQ2";
case "Level_03": return "EnemyQ2";
default: return "Enemy";
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 0dce99aabd0c25d47b2c13d6eba0fd69
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
...@@ -66,14 +66,14 @@ public class PetBuff : MonoBehaviour ...@@ -66,14 +66,14 @@ public class PetBuff : MonoBehaviour
if(petType == PetTypeEnum.Healer) if(petType == PetTypeEnum.Healer)
{ {
Heal(); Heal();
if (playerHealth.currentHealth > 0)
{
// ... tell the animator to make groot have a healing animation
anim.SetTrigger ("HealPlayer");
}
} }
} }
if (playerHealth.currentHealth > 0)
{
// ... tell the animator to make groot have a healing animation
anim.SetTrigger ("HealPlayer");
}
} }
......
...@@ -6,7 +6,7 @@ public class PetHealth : MonoBehaviour ...@@ -6,7 +6,7 @@ public class PetHealth : MonoBehaviour
{ {
public int startingHealth = 100; public int startingHealth = 100;
public int currentHealth; public int currentHealth;
private Slider healthSlider; public Slider healthSlider;
public AudioClip deathClip; public AudioClip deathClip;
Animator anim; Animator anim;
...@@ -19,11 +19,11 @@ public class PetHealth : MonoBehaviour ...@@ -19,11 +19,11 @@ public class PetHealth : MonoBehaviour
{ {
anim = GetComponent<Animator>(); anim = GetComponent<Animator>();
petAudio = GetComponent<AudioSource>(); petAudio = GetComponent<AudioSource>();
Debug.Log("PetHealth script loaded");
GameObject[] petUIElements = GameObject.FindGameObjectsWithTag("PetUI"); GameObject[] petUIElements = GameObject.FindGameObjectsWithTag("PetUI");
foreach(var petUIElement in petUIElements) foreach(var petUIElement in petUIElements)
{ {
// Debug.Log(petUIElement); Debug.Log(petUIElement.name);
petUIElement.SetActive(true); petUIElement.SetActive(true);
} }
...@@ -66,10 +66,13 @@ public class PetHealth : MonoBehaviour ...@@ -66,10 +66,13 @@ public class PetHealth : MonoBehaviour
Destroy(pet); Destroy(pet);
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy"); GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach(var enemy in enemies) if(enemies.Length > 0)
{ {
EnemyMovement enemyMovement = enemy.GetComponent<EnemyMovement>(); foreach(var enemy in enemies)
enemyMovement.petExist = false; {
EnemyMovement enemyMovement = enemy.GetComponent<EnemyMovement>();
enemyMovement.petExist = false;
}
} }
} }
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment