private void Awake()
{
health = currentHealth.initialValue;
}
private void TakeDamage(float damage)
{
health -= damage;
currentHealth.RuntimeValue = health;
EnemyHealthSignal.Raise();
if (health <= 0)
{
DeathEffect();
MakeLoot1();
MakeLoot2();
MakeLoot3();
// if(roomSignal != null){
// roomSignal.Raise();
// }
this.gameObject.SetActive(false);
}
}
public class EnemyHeartManager : MonoBehaviour
{
public SpriteRenderer[] hearts;
public Sprite fullHeart;
public Sprite halfFullHeart;
public Sprite emptyHeart;
public FloatValue EnemyHeartContainer;
public FloatValue EnemyCurrentHealth;
void Start()
{
InitHearts();
}
public void InitHearts()
{
for (int i = 0; i < EnemyHeartContainer.initialValue; i++)
{
hearts[i].gameObject.SetActive(true);
hearts[i].GetComponent<SpriteRenderer>().sprite = fullHeart;
}
}
public void UpdateHearts()
{
float tempHealth = EnemyCurrentHealth.RuntimeValue / 2;
for (int i = 0; i < EnemyHeartContainer.initialValue; i++)
{
if (i <= tempHealth - 1)
{
hearts[i].GetComponent<SpriteRenderer>().sprite = fullHeart;
}
else if (i >= tempHealth)
{
hearts[i].GetComponent<SpriteRenderer>().sprite = emptyHeart;
}
else
{
hearts[i].GetComponent<SpriteRenderer>().sprite = halfFullHeart;
}
}
}
}
[SerializedField] private int hitpoints = 100;
public Action<int, int> OnChange;
public void Change(int amount)
{
hitpoints += amount;
OnChange?.Invoke(hitpoints, amount);
}
private void OnEnable()
{
GetComponent<Health>().OnChange += OnChange;
}
private void OnDisable()
{
GetComponent<Health>().OnChange -= OnChange;
}
private void OnChange(int hitpoints, int amount)
{
//здесь уже делай всю визуальную часть, основанную на значениях выше.
}