Skip to content
Snippets Groups Projects
Commit 17ea9123 authored by geraldabrhm's avatar geraldabrhm
Browse files

feat: handle canvas element show-hide pet health

parent 739fa259
Branches
No related merge requests found
...@@ -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 // 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)
{ {
......
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CanvasElementsManager : MonoBehaviour
{
GameObject[] petUIElements;
void Awake()
{
petUIElements = GameObject.FindGameObjectsWithTag("PetUI");
}
void Start()
{
GameObject pet = GameObject.FindGameObjectWithTag ("Pet");
if(pet != null)
{
setPetUIDisplayed(true);
}
else
{
setPetUIDisplayed(false);
}
}
public void setPetUIDisplayed(bool isDisplayed)
{
Debug.Log($"Pet UI Displayed: {isDisplayed}");
foreach(var petUIElement in petUIElements)
{
petUIElement.SetActive(isDisplayed);
}
}
}
fileFormatVersion: 2
guid: 1a6efe4df810cf5448c99498bd01e9a6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
...@@ -49,15 +49,14 @@ public class PetAttack : MonoBehaviour ...@@ -49,15 +49,14 @@ public class PetAttack : MonoBehaviour
GameObject PickEnemyToAttacked() GameObject PickEnemyToAttacked()
{ {
string currentTag = GetCurrentEnemyTag(); string currentTag = GetCurrentEnemyTag();
Debug.Log($"Current enemy tag: {currentTag}"); // Debug.Log($"Current enemy tag: {currentTag}"); // ! Debug
GameObject[] enemies = GameObject.FindGameObjectsWithTag(currentTag); GameObject[] enemies = GameObject.FindGameObjectsWithTag(currentTag);
Debug.Log($"Skeleton watch {enemies.Length} enemies"); // ! Debug // Debug.Log($"Skeleton watch {enemies.Length} enemies"); // ! Debug
if(enemies.Length > 0) if(enemies.Length > 0)
{ {
GameObject pickedEnemy = enemies[0]; GameObject pickedEnemy = enemies[0];
float nearestDistToEnemy = Vector3.Distance(gameObject.transform.position, pickedEnemy.transform.position); 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) foreach(var enemy in enemies)
{ {
float distToCurrentEnemy = Vector3.Distance(gameObject.transform.position, enemy.transform.position); float distToCurrentEnemy = Vector3.Distance(gameObject.transform.position, enemy.transform.position);
...@@ -71,7 +70,7 @@ public class PetAttack : MonoBehaviour ...@@ -71,7 +70,7 @@ public class PetAttack : MonoBehaviour
Debug.Log($"Nearest enemy from skeleton: {pickedEnemy.name}"); // ! Debug Debug.Log($"Nearest enemy from skeleton: {pickedEnemy.name}"); // ! Debug
return pickedEnemy; return pickedEnemy;
} }
Debug.Log("There are no enemy"); Debug.Log("There are no enemy"); // ! Debug
return null; return null;
} }
......
...@@ -11,25 +11,25 @@ public class PetHealth : MonoBehaviour ...@@ -11,25 +11,25 @@ public class PetHealth : MonoBehaviour
Animator anim; Animator anim;
AudioSource petAudio; AudioSource petAudio;
CanvasElementsManager canvasManager;
bool isDead; bool isDead;
bool damaged; bool damaged;
void Awake() void Awake()
{ {
anim = GetComponent<Animator>(); anim = GetComponent<Animator>();
petAudio = GetComponent<AudioSource>(); petAudio = GetComponent<AudioSource>();
Debug.Log("PetHealth script loaded");
GameObject[] petUIElements = GameObject.FindGameObjectsWithTag("PetUI"); GameObject hudCanvas = GameObject.Find("HUDCanvas");
foreach(var petUIElement in petUIElements) canvasManager = hudCanvas.GetComponent<CanvasElementsManager>();
{
Debug.Log(petUIElement.name);
petUIElement.SetActive(true);
}
currentHealth = startingHealth; currentHealth = startingHealth;
} }
void Start()
{
canvasManager.setPetUIDisplayed(true);
}
void Update() void Update()
{ {
...@@ -74,5 +74,6 @@ public class PetHealth : MonoBehaviour ...@@ -74,5 +74,6 @@ public class PetHealth : MonoBehaviour
enemyMovement.petExist = false; enemyMovement.petExist = false;
} }
} }
canvasManager.setPetUIDisplayed(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