using Core.Health;
using System;
using System.Dynamic;
using TowerDefense.UI.HUD;
using UnityEngine;
namespace ActionGameFramework.Health
{
///
/// A simple class for identifying enemies
///
public class Targetable : DamageableBehaviour
{
///
/// The transform that will be targeted
///
public Transform targetTransform;
///
/// 是否精英怪
///
public bool bElit = false;
///
/// 是否Boss怪.
///
public bool bBoss = false;
///
/// 分别对应三种不同的纹理.
///
public Texture poisonTex;
public Texture frozenTex;
public Texture commonTex;
///
/// The position of the object
///
protected Vector3 m_CurrentPosition, m_PreviousPosition;
///
/// 速度降低值.
///
protected float speedSlowRate = 0.0f;
// 中毒相关的数据
protected int poisonTimes = 0;
protected float poisonHurt = 0.0f;
protected int poisonAttid = 0;
protected float timeToPoisonHurt = 0.0f;
///
/// 是否处于 中毒状态
///
protected bool isPoison;
///
/// 是否处于减速状态
///
protected bool isSlowDown;
///
/// 是否处于冰冻状态
///
protected bool isFrost;
///
/// 中毒粒子特效
///
public ParticleSystem PoisonParticle;
///
/// 中毒结束播放的粒子特效
///
public ParticleSystem PoisonEndParticle;
///
/// 减速粒子特效
///
public ParticleSystem SlowDownParticle;
///
/// 冰冻特效
///
public ParticleSystem FrostParticle;
protected Color mMatColor;
///
/// 是否处于破甲状态.
///
public bool bShieldBreak { get; set; }
///
/// The velocity of the rigidbody
///
public virtual Vector3 velocity { get; protected set; }
///
/// 用于确认具体是哪一方的Agent.
///
public bool opponentAgent { set; get; }
///
/// The transform that objects target, which falls back to this object's transform if not set
///
public Transform targetableTransform
{
get
{
return targetTransform == null ? transform : targetTransform;
}
}
///
/// 设置当前目标的颜色数据
///
///
public void SetTargetableMatColor(Color color, bool force = false)
{
if (!force)
{
if (color == mMatColor) return;
// 无敌状态,只接受白色原贴图:
if (this.configuration.bInvincible && (color != Color.white)) return;
}
foreach (Transform t in transform.GetComponentsInChildren())
{
if (t.name == "Cube")
{
Material tMat = t.GetComponent().material;
if ((color == Color.green) && (poisonTex != null))
{
tMat.mainTexture = poisonTex;
}
if ((color == Color.blue) && (frozenTex != null))
{
tMat.mainTexture = frozenTex;
}
if ((color == Color.white) && (commonTex != null))
{
tMat.mainTexture = commonTex;
}
mMatColor = color;
}
}
}
///
/// 降低移动速度.
///
///
public void addSpeedSlowRate(float rate)
{
speedSlowRate += rate;
if (speedSlowRate >= 0.75f)
speedSlowRate = 0.75f;
}
///
/// 怪物中毒.
///
///
public void poisonAgent(float damage, int attid)
{
if (this.poisonTimes >= 1) return;
if (!isPoison)
{
isPoison = true;
if (PoisonParticle != null)
PoisonParticle.Play();
}
this.poisonTimes++;
this.poisonAttid = attid;
this.poisonHurt = (float)Math.Floor(this.configuration.maxHealth / 20.0f);
this.timeToPoisonHurt = 1.0f;
}
public bool bInPoison { get { return this.poisonHurt > 0; } }
///
/// 处理中毒相关的数据
///
///
protected void updatePoison(float time)
{
this.timeToPoisonHurt -= time;
if (this.timeToPoisonHurt <= 0)
{
Vector3 backPos = this.transform.position;
this.TakeDamage(poisonHurt, this.transform.position, null, poisonAttid);
if ((poisonHurt > 0) && (!opponentAgent))
{
if (GameUI.instanceExists)
GameUI.instance.generateBloodText(backPos, poisonHurt, false, false, true);
else if (EndlessGameUI.instanceExists)
EndlessGameUI.instance.generateBloodText(backPos, poisonHurt, false, false, true);
}
if (poisonHurt > 0)
timeToPoisonHurt = 1.0f;
}
}
///
/// Returns our targetable's transform position
///
public override Vector3 position
{
get { return targetableTransform.position; }
}
protected int mLiveID = 0;
///
/// 用于判断延迟攻击的时候,是否是同一个Agent.
///
public int liveID
{
protected set
{
mLiveID = value;
}
get { return this.mLiveID; }
}
///
/// Initialises any DamageableBehaviour logic
///
protected override void Awake()
{
base.Awake();
ResetPositionData();
bShieldBreak = false;
mMatColor = Color.white;
}
///
/// Sets up the position data so velocity can be calculated
///
protected void ResetPositionData()
{
m_CurrentPosition = position;
m_PreviousPosition = position;
}
///
/// Calculates the velocity and updates the position
///
void FixedUpdate()
{
m_CurrentPosition = position;
velocity = (m_CurrentPosition - m_PreviousPosition) / Time.fixedDeltaTime;
m_PreviousPosition = m_CurrentPosition;
}
}
}