chenxin
2020-10-28 56f231f1f6523d7920cf32f033f9bb6f0015550f
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using Core.Health;
using System;
using System.Dynamic;
using TowerDefense.UI.HUD;
using UnityEngine;
 
namespace ActionGameFramework.Health
{
    /// <summary>
    /// A simple class for identifying enemies
    /// </summary>
    public class Targetable : DamageableBehaviour
    {
        /// <summary>
        /// The transform that will be targeted
        /// </summary>
        public Transform targetTransform;
 
        /// <summary>
        /// 是否精英怪
        /// </summary>
        public bool bElit = false;
 
        /// <summary>
        /// 是否Boss怪.
        /// </summary>
        public bool bBoss = false;
 
        /// <summary>
        /// 分别对应三种不同的纹理.
        /// </summary>
        public Texture poisonTex;
        public Texture frozenTex;
        public Texture commonTex;
 
        /// <summary>
        /// The position of the object
        /// </summary>
        protected Vector3 m_CurrentPosition, m_PreviousPosition;
 
 
        /// <summary>
        /// 速度降低值.
        /// </summary>
        protected float speedSlowRate = 0.0f;
 
        // 中毒相关的数据
        protected int poisonTimes = 0;
        protected float poisonHurt = 0.0f;
        protected int poisonAttid = 0;
        protected float timeToPoisonHurt = 0.0f;
 
        /// <summary>
        /// 是否处于 中毒状态
        /// </summary>
        protected bool isPoison;
 
        /// <summary>
        /// 是否处于减速状态
        /// </summary>
        protected bool isSlowDown;
 
        /// <summary>
        /// 是否处于冰冻状态
        /// </summary>
        protected bool isFrost;
 
        /// <summary>
        /// 中毒粒子特效
        /// </summary>
        public ParticleSystem PoisonParticle;
 
        /// <summary>
        /// 中毒结束播放的粒子特效
        /// </summary>
        public ParticleSystem PoisonEndParticle;
 
        /// <summary>
        /// 减速粒子特效
        /// </summary>
        public ParticleSystem SlowDownParticle;
 
        /// <summary>
        /// 冰冻特效
        /// </summary>
        public ParticleSystem FrostParticle;
 
        protected Color mMatColor;
 
        /// <summary>
        /// 是否处于破甲状态.
        /// </summary>
        public bool bShieldBreak { get; set; }
 
        /// <summary>
        /// The velocity of the rigidbody
        /// </summary>
        public virtual Vector3 velocity { get; protected set; }
 
        /// <summary>
        /// 用于确认具体是哪一方的Agent.
        /// </summary>
        public bool opponentAgent { set; get; }
 
        /// <summary>
        /// The transform that objects target, which falls back to this object's transform if not set
        /// </summary>
        public Transform targetableTransform
        {
            get
            {
                return targetTransform == null ? transform : targetTransform;
            }
        }
 
        /// <summary>
        /// 设置当前目标的颜色数据
        /// </summary>
        /// <param name="color"></param>
        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<Transform>())
            {
                if (t.name == "Cube")
                {
                    Material tMat = t.GetComponent<MeshRenderer>().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;
                }
 
            }
        }
 
 
        /// <summary>
        /// 降低移动速度.
        /// </summary>
        /// <param name="rate"></param>
        public void addSpeedSlowRate(float rate)
        {
            speedSlowRate += rate;
            if (speedSlowRate >= 0.5f)
                speedSlowRate = 0.5f;
        }
 
        /// <summary>
        /// 怪物中毒.
        /// </summary>
        /// <param name="damage"></param>
        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; } }
 
        /// <summary>
        /// 处理中毒相关的数据
        /// </summary>
        /// <param name="time"></param>
        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;
            }
        }
 
 
        /// <summary>
        /// Returns our targetable's transform position
        /// </summary>
        public override Vector3 position
        {
            get { return targetableTransform.position; }
        }
 
        protected int mLiveID = 0;
        /// <summary>
        /// 用于判断延迟攻击的时候,是否是同一个Agent.
        /// </summary>
        public int liveID
        {
            protected set
            {
                mLiveID = value;
            }
            get { return this.mLiveID; }
        }
 
        /// <summary>
        /// Initialises any DamageableBehaviour logic
        /// </summary>
        protected override void Awake()
        {
            base.Awake();
            ResetPositionData();
            bShieldBreak = false;
            mMatColor = Color.white;
        }
 
        /// <summary>
        /// Sets up the position data so velocity can be calculated
        /// </summary>
        protected void ResetPositionData()
        {
            m_CurrentPosition = position;
            m_PreviousPosition = position;
        }
 
        /// <summary>
        /// Calculates the velocity and updates the position
        /// </summary>
        void FixedUpdate()
        {
            m_CurrentPosition = position;
            velocity = (m_CurrentPosition - m_PreviousPosition) / Time.fixedDeltaTime;
            m_PreviousPosition = m_CurrentPosition;
        }
    }
}