using System; using Core.Health; using Core.Utilities; using TowerDefense.Agents; using UnityEngine; using Random = UnityEngine.Random; namespace ActionGameFramework.Health { /// /// A component that does damage to damageables /// public class Damager : MonoBehaviour { /// /// The amount of damage damager does /// public float damage; /// /// 用于最后一颗子弹的多倍攻击 /// public float damageMulti = 1.0f; /// /// TEST CODE TO TOWER_NAME /// public string towerName = ""; public bool bSet = false; /// /// The event that fires off when the damager has been damaged /// public Action hasDamaged; /// /// 设置局内升级的对应伤害. /// protected float mInSceneUpGradeDamage = 0; /// /// Random chance to spawn collision projectile prefab /// [Range(0, 1)] public float chanceToSpawnCollisionPrefab = 1.0f; /// /// The particle system to fire off when the damager attacks /// public GameObject collisionObj; /// /// The alignment of the damager /// public SerializableIAlignmentProvider alignment; /// /// Gets the alignment of the damager /// public IAlignmentProvider alignmentProvider { get { return alignment != null ? alignment.GetInterface() : null; } } protected void Awake() { doubleHit = false; } public float inSceneUpGradeDamage { get { return mInSceneUpGradeDamage; } set { mInSceneUpGradeDamage = value; } } /// /// The logic to set the value of the damage /// /// /// The amount to set the damage by, /// will not be set for values less than zero /// public void SetDamage(float damageAmount) { if (damageAmount < 0) return; damage = damageAmount; } public bool isCrit { get { System.Random mRandom = new System.Random(); int val = mRandom.Next(0, 15); if (val <= 1) return true; else return false; } } /// /// 用于精英怪双击. /// public bool doubleHit { get; set; } /// /// 得到最终的Damage数据,WORK START: /// public float finalDamage { get { float fd = damage * damageMulti; if (damageMulti > 1.0f) Debug.Log("hello,world:" + damageMulti.ToString() ); damageMulti = 1.0f; return fd + inSceneUpGradeDamage; } } /// /// Damagable will tell damager that it has successful hurt the damagable /// public void HasDamaged(Vector3 point, IAlignmentProvider otherAlignment) { if (hasDamaged != null) { hasDamaged(point); } } /// /// Instantiate particle system and play it /// void OnCollisionEnter(Collision other) { if (collisionObj == null || Random.value > chanceToSpawnCollisionPrefab) { return; } } /// /// TEST CODE:把Agent的isTrigger勾上了,所以加入了相关的其它处理。 /// /// protected void OnTriggerEnter(Collider other) { var ag = other.gameObject.GetComponent(); if (ag == null) return; if (collisionObj == null || Random.value > chanceToSpawnCollisionPrefab) { return; } // 炮弹击中特效 GameObject obj = Poolable.TryGetPoolable(collisionObj); ParticleSystem ps = obj.GetComponent(); if (ps == null) ps = obj.transform.GetChild(0).GetComponent(); ps.transform.position = transform.position; ps.Play(); } } }