using System;
using UnityEngine;
using KTGMGemClient;
using TowerDefense.Agents;
namespace Core.Health
{
///
/// Abstract class for any MonoBehaviours that can take damage
/// 任何会受到伤害的MonoBehaviour的抽象类
///
public class DamageableBehaviour : MonoBehaviour
{
///
/// The Damageable object
///
public Damageable configuration;
///
/// Gets whether this is dead.
///
/// True if dead
public bool isDead
{
get { return configuration.isDead; }
}
///
/// The position of the transform
///
public virtual Vector3 position
{
get { return transform.position; }
}
///
/// 获取当前Agent对应的HealthValue.
///
public float healthVal
{
get { return this.configuration.currentHealth; }
}
///
/// Occurs when damage is taken
///
public event Action hit;
///
/// Event that is fired when this instance is removed, such as when pooled or destroyed
///
public event Action removed;
///
/// Event that is fired when this instance is killed
///
public event Action died;
///
/// Takes the damage and also provides a position for the damage being dealt
///
/// Damage value.
/// Damage point.
/// Alignment value
/// 标注产生伤害的塔位属性ID
public virtual void TakeDamage(float damageValue, Vector3 damagePoint, IAlignmentProvider alignment, int attributeId = 0)
{
HealthChangeInfo info = new HealthChangeInfo();
info.attributeId = attributeId;
configuration.TakeDamage(damageValue, alignment, ref info);
var damageInfo = new HitInfo(info, damagePoint);
EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessAgentTaskDamage, damageInfo.healthChangeInfo.absHealthDifference);
if (attributeId == 0)
EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessOneHit);
if (hit != null)
{
hit(damageInfo);
}
}
protected virtual void Awake()
{
configuration.Init();
configuration.died += OnConfigurationDied;
}
///
/// Kills this damageable
///
protected virtual void Kill()
{
HealthChangeInfo healthChangeInfo = new HealthChangeInfo();
configuration.TakeDamage(configuration.currentHealth, null, ref healthChangeInfo);
}
///
/// Removes this damageable without killing it
///
public virtual void Remove()
{
// Set health to zero so that this behaviour appears to be dead. This will not fire death events
configuration.SetHealth(0);
OnRemoved();
}
///
/// Fires kill events
///
protected virtual void OnDeath()
{
if (died != null)
{
died(this);
}
}
///
/// Fires the removed event
///
protected void OnRemoved()
{
if (removed != null)
{
removed(this);
}
}
public virtual bool isAgent()
{
return false;
}
public virtual void PlayDeath()
{
return;
}
///
/// Event fired when Damageable takes critical damage
///
void OnConfigurationDied(HealthChangeInfo changeInfo)
{
OnDeath();
if (!this.isAgent())
Remove();
else
this.PlayDeath();
}
}
}