using System;
|
using UnityEngine;
|
using KTGMGemClient;
|
using TowerDefense.Agents;
|
|
namespace Core.Health
|
{
|
/// <summary>
|
/// Abstract class for any MonoBehaviours that can take damage
|
/// 任何会受到伤害的MonoBehaviour的抽象类
|
/// </summary>
|
public class DamageableBehaviour : MonoBehaviour
|
{
|
/// <summary>
|
/// The Damageable object
|
/// </summary>
|
public Damageable configuration;
|
|
/// <summary>
|
/// Gets whether this <see cref="DamageableBehaviour" /> is dead.
|
/// </summary>
|
/// <value>True if dead</value>
|
public bool isDead
|
{
|
get { return configuration.isDead; }
|
}
|
|
/// <summary>
|
/// The position of the transform
|
/// </summary>
|
public virtual Vector3 position
|
{
|
get { return transform.position; }
|
}
|
|
/// <summary>
|
/// 获取当前Agent对应的HealthValue.
|
/// </summary>
|
public float healthVal
|
{
|
get { return this.configuration.currentHealth; }
|
}
|
|
/// <summary>
|
/// Occurs when damage is taken
|
/// </summary>
|
public event Action<HitInfo> hit;
|
|
/// <summary>
|
/// Event that is fired when this instance is removed, such as when pooled or destroyed
|
/// </summary>
|
public event Action<DamageableBehaviour> removed;
|
|
/// <summary>
|
/// Event that is fired when this instance is killed
|
/// </summary>
|
public event Action<DamageableBehaviour> died;
|
|
/// <summary>
|
/// Takes the damage and also provides a position for the damage being dealt
|
/// </summary>
|
/// <param name="damageValue">Damage value.</param>
|
/// <param name="damagePoint">Damage point.</param>
|
/// <param name="alignment">Alignment value</param>
|
/// <param name="attributeId">标注产生伤害的塔位属性ID</param>
|
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;
|
}
|
|
/// <summary>
|
/// Kills this damageable
|
/// </summary>
|
protected virtual void Kill()
|
{
|
HealthChangeInfo healthChangeInfo = new HealthChangeInfo();
|
configuration.TakeDamage(configuration.currentHealth, null, ref healthChangeInfo);
|
}
|
|
|
/// <summary>
|
/// Removes this damageable without killing it
|
/// </summary>
|
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();
|
}
|
|
/// <summary>
|
/// Fires kill events
|
/// </summary>
|
protected virtual void OnDeath()
|
{
|
if (died != null)
|
{
|
died(this);
|
}
|
}
|
|
/// <summary>
|
/// Fires the removed event
|
/// </summary>
|
protected void OnRemoved()
|
{
|
if (removed != null)
|
{
|
removed(this);
|
}
|
}
|
|
public virtual bool isAgent()
|
{
|
return false;
|
}
|
|
public virtual void PlayDeath()
|
{
|
return;
|
}
|
|
/// <summary>
|
/// Event fired when Damageable takes critical damage
|
/// </summary>
|
void OnConfigurationDied(HealthChangeInfo changeInfo)
|
{
|
OnDeath();
|
|
if (!this.isAgent())
|
Remove();
|
else
|
this.PlayDeath();
|
}
|
}
|
}
|