chenxin
2020-12-25 dcd118c76ba7a035b3c6b25a0bed9abeceac7251
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using Core.Health;
using Core.Utilities;
using TowerDefense.Agents;
using UnityEngine;
using Random = UnityEngine.Random;
using TowerDefense.Towers;
 
namespace ActionGameFramework.Health
{
    /// <summary>
    /// A component that does damage to damageables
    /// </summary>
    public class Damager : MonoBehaviour
    {
        /// <summary>
        /// The amount of damage damager does
        /// </summary>
        public float damage;
 
        /// <summary>
        /// 强化子弹的多倍攻击
        /// </summary>
        public float damageMulti = 1.0f;
 
        /// <summary>
        /// 是否是增强的子弹
        /// </summary>
        public bool IsEnhancedBullet { get; set; }
 
        /// <summary>
        /// 是否击退过小怪
        /// </summary>
        public bool IsRepeled { get; set; }
 
        /// <summary>
        /// TEST CODE TO TOWER_NAME
        /// </summary>
        public string towerName = "";
 
        public bool bSet = false;
 
        /// <summary>
        /// The event that fires off when the damager has been damaged
        /// </summary>
        public Action<Vector3> hasDamaged;
 
        /// <summary>
        /// 设置局内升级的对应伤害.
        /// </summary>
        protected float mInSceneUpGradeDamage = 0;
 
        /// <summary>
        /// Random chance to spawn collision projectile prefab
        /// </summary>
        [Range(0, 1)]
        public float chanceToSpawnCollisionPrefab = 1.0f;
 
        /// <summary>
        /// The particle system to fire off when the damager attacks
        /// </summary>
        public GameObject collisionObj;
 
        /// <summary>
        /// The alignment of the damager
        /// </summary>
        public SerializableIAlignmentProvider alignment;
 
        public Tower TowerPtr;
 
        /// <summary>
        /// Gets the alignment of the damager
        /// </summary>
        public IAlignmentProvider alignmentProvider
        {
            get { return alignment != null ? alignment.GetInterface() : null; }
        }
 
        protected void Awake()
        {
            doubleHit = false;
        }
 
        public float inSceneUpGradeDamage
        {
            get { return mInSceneUpGradeDamage; }
            set { mInSceneUpGradeDamage = value; }
        }
 
        /// <summary>
        /// The logic to set the value of the damage
        /// </summary>
        /// <param name="damageAmount">
        /// The amount to set the damage by, 
        /// will not be set for values less than zero
        /// </param>
        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;
            }
        }
 
        /// <summary>
        /// 用于精英怪双击.
        /// </summary>
        public bool doubleHit { get; set; }
 
        /// <summary>
        /// 得到最终的Damage数据,WORK START:
        /// </summary>
        public float finalDamage
        {
            get
            {
                float fd = damage * damageMulti;
                damageMulti = 1.0f;
                return fd + inSceneUpGradeDamage;
            }
        }
 
        /// <summary>
        /// Damagable will tell damager that it has successful hurt the damagable
        /// </summary>
        public void HasDamaged(Vector3 point, IAlignmentProvider otherAlignment)
        {
            if (hasDamaged != null)
            {
                hasDamaged(point);
            }
        }
 
        /// <summary>
        /// Instantiate particle system and play it
        /// </summary>
        void OnCollisionEnter(Collision other)
        {
            if (collisionObj == null || Random.value > chanceToSpawnCollisionPrefab)
            {
                return;
            }
        }
 
        /// <summary>
        /// TEST CODE:把Agent的isTrigger勾上了,所以加入了相关的其它处理。
        /// </summary>
        /// <param name="other"></param>
        protected void OnTriggerEnter(Collider other)
        {
            var ag = other.gameObject.GetComponent<Agent>();
            if (ag == null) return;
 
            if (collisionObj == null || Random.value > chanceToSpawnCollisionPrefab)
            {
                return;
            }
 
            // 炮弹击中特效
            GameObject obj = Poolable.TryGetPoolable(collisionObj);
            ParticleSystem ps = obj.GetComponent<ParticleSystem>();
            if (ps == null)
                ps = obj.transform.GetChild(0).GetComponent<ParticleSystem>();
            ps.transform.position = transform.position;
            ps.Play();
        }
    }
}