44 lines
1.3 KiB
C#
44 lines
1.3 KiB
C#
using Code.Infrastructure.EventBus;
|
|
using Reflex.Attributes;
|
|
using UnityEngine;
|
|
|
|
namespace Code.Example.Platformer.Entity
|
|
{
|
|
public class EntityHealth : MonoBehaviour
|
|
{
|
|
private int _health;
|
|
private int _maxHealth;
|
|
public void SetMaxHealth(int maxHealth) => _maxHealth = maxHealth;
|
|
public bool IsDead { get; private set; }
|
|
|
|
[Inject] private EventBus _eventBus;
|
|
[SerializeField] private Entity entity;
|
|
|
|
public void GiveDamage(int damage)
|
|
{
|
|
_health -= damage;
|
|
|
|
if(_health <= 0) IsDead = true;
|
|
|
|
_eventBus.RaiseEvent<IEntityDamagedHandler>(x=> x.EntityDamaged(gameObject, damage, IsDead));
|
|
}
|
|
|
|
public void Heal(int heal)
|
|
{
|
|
if (IsDead) return;
|
|
if (_health + heal > _maxHealth) _health = _maxHealth;
|
|
else _health += heal;
|
|
|
|
_eventBus.RaiseEvent<IEntityHealedHandler>(x=> x.EntityHealed(gameObject, heal));
|
|
}
|
|
}
|
|
|
|
public interface IEntityDamagedHandler : IGlobalSubscriber
|
|
{
|
|
void EntityDamaged(GameObject entity, int damageCount, bool isDied);
|
|
}
|
|
public interface IEntityHealedHandler : IGlobalSubscriber
|
|
{
|
|
void EntityHealed(GameObject entity, int healCount);
|
|
}
|
|
} |