yyl
15 小时以前 5d3366f2e0f687995eb7ad2107c4379fe7acd4e8
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using DG.Tweening.Core;
using DG.Tweening.Plugins.Options;
using Spine.Unity;
using UnityEngine.UI;
using System.Linq;
 
public enum BattleCamp
{
    Red,
    Blue
}
 
public enum BattleState
{
    None = 0,
    Stunned = 1 << 0,
    Poisoned = 1 << 1,
    Bleeding = 1 << 2,
    Silenced = 1 << 3,
    Frozen = 1 << 4,
    Burned = 1 << 5
}
 
public class BattleObject
{
    public BattleField battleField;
 
    public int BattleObjectId { get; set; }
 
    public BattleCamp Camp { get; protected set; }
 
    public TeamHero teamHero { get; protected set; }
 
    // public BuffMgr buffMgr;
 
    protected MotionBase motionBase;
 
    public GameObject heroGo
    {
        get;
        private set;
    }
 
    public BattleObject(BattleField _battleField)
    {
        battleField = _battleField;
    }
 
    public virtual void Init(GameObject _heroGo, TeamHero _teamHero, BattleCamp _camp)
    {
        heroGo = _heroGo;
        teamHero = _teamHero;
        Camp = _camp;
        motionBase = new MotionBase();
        motionBase.Init(heroGo.GetComponentInChildren<SkeletonGraphic>(true));
    }
 
 
 
    public virtual void Run()
    {
        motionBase.Run();
    }
 
    public virtual void Pause()
    {
        motionBase.Pause();
    }
 
    public virtual void Resume()
    {
        motionBase.Resume();
    }
 
    public virtual void Destroy()
    {
        if (heroGo != null)
        {
            GameObject.DestroyImmediate(heroGo);
            heroGo = null;
        }
 
        motionBase.Release();
        motionBase = null;
        teamHero = null;
        BattleObjectId = 0;
    }
 
    //  眩晕
    public bool IsStunned()
    {
        return teamHero.isStunned;
    }
 
    //  冰冻
    public bool IsFrozen()
    {
        return teamHero.isFrozen;
    }
 
    //  石化
    public bool IsStoned()
    {
        return teamHero.isStoned;
    }
 
    // //   禁锢
    // public bool IsConfined()
    // {
    //     return false;
    // }
 
    //  被沉默
    public bool IsSlient()
    {
        return teamHero.isSlient;
    }
 
    //  被缴械
    public bool IsDisarmed()
    {
        return teamHero.isDisarmed;
    }
 
    //  是否无敌
    public bool IsInvincable()
    {
        return teamHero.isInvinceble;
    }
 
    //  是否死亡
    public bool IsDead()
    {
        return teamHero.isDead;
    }
 
    //  是否被控住了
    public bool IsCrowdControlled()
    {
        return IsStunned() || IsStoned() || IsFrozen();
    }
 
    public virtual bool IsCanCastSkill()
    {
        //  被控住
        if (IsCrowdControlled())
        {
            return false;
        }
 
        //  被沉默
        if (IsSlient())
        {
            return false;
        }
 
        //  看看怒气是否达到释放要求
        return teamHero.rage >= 100;
    }
 
    public virtual bool IsCanNormalAttack()
    {
        //  被控住
        if (IsCrowdControlled())
        {
            return false;
        }
 
        //  缴械
        if (IsDisarmed())
        {
            return false;
        }
 
        return true;
    }
 
    public virtual void TakeDamage(List<int> damageValues)
    {
        if (IsDead())
            return;
 
        PopDamage(damageValues);
 
        motionBase.PlayAnimation(MotionName.hit, false);
 
        //  计算伤害
        int totalDamage = 0;
        foreach (var damage in damageValues)
        {
            totalDamage += damage;
        }
 
 
        //  扣血
        teamHero.curHp -= totalDamage;
 
        //  其实这里应该是等服务器发death的action
        // if (IsDead())
        // {
        //     OnDeath();
        // }
        
    }
 
    //  闪避开始
    public virtual void OnDodgeBegin()
    {
        float pingpongTime = 0.2f;
        RectTransform rectTrans = heroGo.GetComponent<RectTransform>();
        rectTrans.DOAnchorPos(new Vector3(-50, 50, 0), pingpongTime)
            .SetEase(Ease.OutCubic);
    }
 
    //  闪避结束
    public virtual void OnDodgeEnd()
    {
        float pingpongTime = 0.2f;
        RectTransform rectTrans = heroGo.GetComponent<RectTransform>();
        rectTrans.DOAnchorPos(Vector3.zero, pingpongTime)
                            .SetEase(Ease.OutCubic);
    }
 
    protected virtual void OnDeath()
    {
        motionBase.OnOtherAnimationComplete = OnOtherAnimationComplete;
        motionBase.PlayAnimation(MotionName.dead, false);
    }
 
    protected virtual void OnOtherAnimationComplete(MotionName motionName)
    {
        if (motionName == MotionName.dead)
        {
            OnDeadAnimationComplete();
        }
    }
 
    protected virtual void OnDeadAnimationComplete()
    {
        //  或许看看溶解特效? YYL TODO
        heroGo.SetActive(false);
    }
 
     // 伤害还要看 是否闪避 暴击 and so on 需要有一个DamageType 服务器应该会给
    protected virtual void PopDamage(List<int> damageValues)
    {
        //  其实应该通知出去给UI界面解耦 让UI界面自己来显示的 YYL TODO
        //  播放伤害数字
        //  这里可以实现一个伤害数字的弹出效果
        //  比如使用一个UI组件来显示伤害数字
        foreach (var damage in damageValues)
        {
            Debug.Log($"Damage: {damage}");
        }
 
        EventBroadcast.Instance.Broadcast<BattleObject, List<int>>(EventName.BATTLE_DAMAGE_TAKEN, this, damageValues);
    }
 
    public void PlaySkill(SkillConfig skillConfig, List<Dictionary<int, List<int>>> damageList, Action _onComplete)
    {
        bool moveToTarget = true;
 
        if (moveToTarget)
        {
            int targetId = damageList[0].First().Key;
            BattleObject _targetObj = battleField.battleObjMgr.GetBattleObject(targetId);
 
            RectTransform selfRect = heroGo.GetComponent<RectTransform>();
            RectTransform targetRect = _targetObj.heroGo.GetComponent<RectTransform>();
            Vector2 curAnchoredPos = selfRect.anchoredPosition;
 
            MoveToTargetUI(selfRect, targetRect, new Vector2(100f, 0f), () =>
            {
                PlaySkillAnimation(skillConfig, damageList, () =>
                {
                    // 回到原位置
                    selfRect.DOAnchorPos(curAnchoredPos, 0.2f)
                        .SetEase(Ease.Linear)
                        .OnComplete(() => {
                            _onComplete?.Invoke();
                        });
                });
            });
        }
        else
        {
            PlaySkillAnimation(skillConfig, damageList, _onComplete);
        }
    }
 
    protected void MoveToTargetUI(RectTransform selfRect, RectTransform targetRect, Vector2 offset, Action _onComplete)
    {
        // 1. 目标的本地坐标转为世界坐标
        Vector3 targetWorldPos = targetRect.TransformPoint(targetRect.anchoredPosition + offset);
 
        // 2. 世界坐标转为自己父节点下的本地坐标
        RectTransform parentRect = selfRect.parent as RectTransform;
        Vector2 targetAnchoredPos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(
            parentRect,
            RectTransformUtility.WorldToScreenPoint(null, targetWorldPos),
            null,
            out targetAnchoredPos);
 
        // 3. DOTween 移动
        selfRect.DOAnchorPos(targetAnchoredPos, 0.2f)
            .SetEase(Ease.Linear)
            .OnComplete(() => _onComplete?.Invoke());
    }
 
 
    protected void PlaySkillAnimation(SkillConfig skillConfig, List<Dictionary<int, List<int>>> damageList, Action _onComplete)
    {
 
        //  关键帧列表
        List<int> keyFrameList = new List<int>() { 15 };
        motionBase.OnAttackHitEvent = (int _frame) =>
        {
            Dictionary<int, List<int>> oneRoundDamage = damageList[keyFrameList.IndexOf(_frame)];
 
            foreach (var kvp in oneRoundDamage)
            {
                int targetId = kvp.Key;
                List<int> damageValues = kvp.Value;
 
                BattleObject targetObj = battleField.battleObjMgr.GetBattleObject(targetId);
                if (targetObj != null && !targetObj.IsDead())
                {
                    targetObj.TakeDamage(damageValues);
                }
            }
        };
 
        motionBase.OnAttackAnimationComplete = () =>
        {
            _onComplete?.Invoke();
 
            motionBase.OnAttackHitEvent = null;
            motionBase.OnAttackAnimationComplete = null;
 
            //  死亡确定其实不应该在这里进行触发 应该由服务器下发 YYL TODO
 
#if UNITY_EDITOR
            //  暂时的处理
            HashSet<int> hitTargets = new HashSet<int>();
 
            foreach (var dmgDict in damageList)
            {
                foreach (var kvp in dmgDict)
                {
                    int targetId = kvp.Key;
                    hitTargets.Add(targetId);
                }
            }
 
            foreach (int targetId in hitTargets)
            {
                BattleObject targetObj = battleField.battleObjMgr.GetBattleObject(targetId);
                if (targetObj != null && targetObj.IsDead())
                {
                    targetObj.OnDeath();
                }
            }
#endif
        };
 
        motionBase.PlayAnimationEx(MotionName.attack, false, keyFrameList);
    }
 
#if UNITY_EDITOR
    public void EditorRevive()
    {
        teamHero.curHp = 100;
        heroGo.SetActive(true);
        motionBase.PlayAnimation(MotionName.idle, true);
    }
 
    public List<int> TryAttack(BattleObject obj, SkillConfig skillConfig)
    {
        List<int> damageList = new List<int>();
 
        int totalDamage = teamHero.attack - obj.teamHero.defense;
 
        int damage1 = (int)((float)totalDamage * 0.3f);
 
        int damage2 = (int)((float)totalDamage * 0.25f);
 
        int damage3 = totalDamage - damage1 - damage2;
 
        damageList.Add(damage1);
        damageList.Add(damage2);
        damageList.Add(damage3);
 
        return damageList;
    }
#endif
 
}