yyl
3 天以前 cec8b67d82c2c2c1662d55c818c4a46bcc0487db
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
using System.Collections.Generic;
using UnityEngine;
using System;
using LitJson;
using DG.Tweening;
 
public class BattleField
{
    public BattleObjMgr battleObjMgr;
 
    public BattleEffectMgr battleEffectMgr;
 
    public BattleTweenMgr battleTweenMgr;
 
    public RecordPlayer recordPlayer;
 
    public IOperationAgent operationAgent;
 
    public int round = 0;
 
    public string guid = string.Empty;//等于string.Empty的时候代表是StoryBattleField 是主线副本
 
    public int MapID = 0;
 
    public int FuncLineID = 0;
 
    public JsonData extendData;
 
    public bool IsBattleFinish
    {
        get;
        protected set;
    }
 
    private bool m_IsPause = false;
 
    public bool IsPause
    {
        get
        {
            return m_IsPause;
        }
        set
        {
            m_IsPause = value;
 
            if (m_IsPause)
            {
                PauseGame();
            }
            else
            {
                ResumeGame();
            }
 
            OnBattlePause?.Invoke(m_IsPause);
        }
    }
 
    public BattleRootNode battleRootNode;
 
    private BattleMode battleMode;
 
    public Action<bool> OnBattlePause;
 
    protected List<TeamBase> redTeamList = null;
 
    protected List<TeamBase> blueTeamList = null;
 
    protected int redTeamIndex = 0;
 
    protected int blueTeamIndex = 0;
 
    public BattleField(string _guid)
    {
        guid = _guid;
 
        GameObject go = ResManager.Instance.LoadAsset<GameObject>("Battle/Prefabs", "BattleRootNode");
        GameObject battleRootNodeGO = GameObject.Instantiate(go);
        battleRootNode = battleRootNodeGO.GetComponent<BattleRootNode>();
        battleRootNodeGO.name = this.GetType().Name;
 
        battleObjMgr = new BattleObjMgr();
        battleEffectMgr = new BattleEffectMgr();
        battleTweenMgr = new BattleTweenMgr();
        recordPlayer = new RecordPlayer();
 
        battleEffectMgr.Init(this);
        battleTweenMgr.Init(this);
        recordPlayer.Init(this);
    }
 
    public virtual void Init(int _MapID, int _FuncLineID, JsonData _extendData,
        List<TeamBase> _redTeamList, List<TeamBase> _blueTeamList)
    {
        MapID = _MapID;
        redTeamList = _redTeamList;
        blueTeamList = _blueTeamList;
        FuncLineID = _FuncLineID;
        extendData = _extendData;
 
        redTeamIndex = 0;
        blueTeamIndex = 0;
 
        if (blueTeamList == null)
        {
            battleObjMgr.Init(this, redTeamList[redTeamIndex], null);
            HaveRest();
        }
        else
        {
            battleObjMgr.Init(this, redTeamList[redTeamIndex], blueTeamList[blueTeamIndex]);
        }
 
        battleRootNode.SetBackground(ResManager.Instance.LoadAsset<Texture>("Texture/FullScreenBg", "battlebg1"));
    }
 
    //  在Run之前要设置完毕 要创建Agent
    public void SetBattleMode(BattleMode _battleMode)
    {
        battleMode = _battleMode;
        CreateAgent();
    }
 
    public virtual void Release()
    {
        battleObjMgr.Release();
        battleEffectMgr.Release();
        battleTweenMgr.Release();
        recordPlayer.Release();
    }
 
    public virtual void Run()
    {
        if (IsPause)
            return;
 
        if (IsRoundReachLimit())
        {
            return;
        }
        if (recordPlayer == null || battleObjMgr == null)
            return;
        recordPlayer.Run();
        battleObjMgr.Run();
 
        if (operationAgent == null)
        {
            BattleDebug.LogError("you should SetBattleMode before Run");
            return;
        }
 
        operationAgent.Run();
    }
 
 
    protected virtual void CreateAgent()
    {
        // Hand,//手动战斗
        // Auto,//自动战斗
        // Record,//战报
        switch (battleMode)
        {
            case BattleMode.Hand:
                operationAgent = new HandModeOperationAgent(this);
                break;
            case BattleMode.Auto:
                operationAgent = new AutoModeOperationAgent(this);
                break;
            case BattleMode.Record:
                operationAgent = new RecordModeOperationAgent(this);
                break;
            case BattleMode.Stop:
                operationAgent = new StopModeOperationAgent(this);
                break;
            default:
                operationAgent = new HandModeOperationAgent(this);
                break;
        }
 
        BattleDebug.LogError("battleMode is " + battleMode.ToString());
    }
 
    public virtual void AutoSetBattleMode()
    {
        //  除了主线都是战报 主线的内容在StoryBattleField里
        SetBattleMode(BattleMode.Record);
    }
 
    public virtual void PlayRecord(RecordAction recordAction)
    {
        recordPlayer.PlayRecord(recordAction);
    }
 
    public virtual void PlayRecord(List<RecordAction> recordList)
    {
        recordPlayer.PlayRecord(recordList);
    }
 
    public virtual void ResumeGame()
    {
        battleObjMgr.ResumeGame();
        recordPlayer.ResumeGame();
        battleEffectMgr.ResumeGame();
        battleTweenMgr.ResumeGame();
    }
 
    public virtual void PauseGame()
    {
        //  怎么通知界面暂停了呢?
 
        battleObjMgr.PauseGame();
        recordPlayer.PauseGame();
        battleEffectMgr.PauseGame();
        battleTweenMgr.PauseGame();
    }
 
    public virtual void TurnFightState(int TurnNum, int State,
        uint FuncLineID, JsonData extendData)
    {
        round = TurnNum;
    }
 
    public virtual void OnTurnFightObjAction(int turnNum, int ObjID)
    {
 
    }
 
    public virtual void OnTurnFightState(int turnNum, int State, int FuncLineID, JsonData turnFightStateData)
    {
        //  切换回合
        //  是每个战斗开始/每个回合的第一个战斗包
        //  MapID = MapID
 
        //  MapID;    // 自定义地图ID,可用于绑定战斗地图场景功能(如主线关卡、主线boss、爬塔、竞技场等)
        //  FuncLineID;    // MapID对应的扩展值,如具体某个关卡等  章节*10000+关卡编号*100+第x波,如第一章,第10关卡的boss值 = 11001
        //  State;    // 0-起始状态标记;1-准备完毕;2-战斗中;3-战斗结束;4-结算奖励;5-结束状态标记
        //  TurnNum;    // 当前轮次
        //  Len;
        //  Msg;    //size = Len   +
        if (State == 4)
        {
            //已经结束并结算
            OnBattleEnd(turnFightStateData);
            return;
        }
 
        //  做表现
        if (turnNum == 1)
        {
            if (State == 2)
            {
                Debug.Log("战斗开始");
            }
        }
        else
        {
            Debug.Log("战斗回合 : " + turnNum + ",状态 " + State);
        }
 
        DistributeNextPackage();
 
        // 做一个Action 通知UI翻下牌子 然后结束Action
        // TurnFightStateAction turnFightStateAction = new TurnFightStateAction(this, turnNum, State, FuncLineID, extendData);
        // recordPlayer.PlayRecord(turnFightStateAction);
    }
 
 
    public void ObjInfoRefresh(H0418_tagObjInfoRefresh _refreshInfo)
    {
        BattleObject battleObj = battleObjMgr.GetBattleObject((int)_refreshInfo.ObjID);
 
        if (null != battleObj)
        {
            battleObj.OnObjInfoRefresh(_refreshInfo);
        }
    }
 
    public void ObjPropertyRefreshView(HB418_tagSCObjPropertyRefreshView vNetData)
    {
 
    }
 
    public virtual void OnObjsDead(List<HB422_tagMCTurnFightObjDead> deadPackList)
    {
        if (deadPackList.Count > 0)
        {
            DeathRecordAction recordAction = new DeathRecordAction(this, deadPackList);
            recordPlayer.PlayRecord(recordAction);
        }
    }
 
    public virtual void Destroy()
    {
        //  销毁全部内容
        BattleManager.Instance.DestroyBattleField(this);
    }
 
    public void NPCDisappear(uint[] ObjIDArr)
    {
        //  让npc消失
        battleObjMgr.DestroyObjIds(ObjIDArr);
    }
 
 
    public RectTransform GetTeamNode(BattleCamp battleCamp)
    {
        if (battleCamp == BattleCamp.Red)
        {
            return battleRootNode.redTeamNode;
        }
        else
        {
            return battleRootNode.blueTeamNode;
        }
    }
 
    public RectTransform GetTeamNode(BattleCamp battleCamp, BattleObject target)
    {
        int index = target.teamHero.positionNum;
        return GetTeamNode(battleCamp, index);
    }
 
    public RectTransform GetTeamNode(BattleCamp battleCamp, SkillConfig skillConfig)
    {
        int index = skillConfig.CastIndexNum - 1; // 技能配置的index是从1开始的,所以要减1
        return GetTeamNode(battleCamp, index);
    }
 
    public RectTransform GetTeamNode(BattleCamp battleCamp, int index)
    {
        if (index < 0 || index >= battleRootNode.redTeamNodeList.Count)
        {
            BattleDebug.LogError($"GetTeamNode: Index {index} is out of range for {battleCamp} camp.");
            return null;
        }
 
        if (battleCamp == BattleCamp.Red)
        {
            return battleRootNode.redTeamNodeList[index].transform as RectTransform;
        }
        else
        {
            return battleRootNode.blueTeamNodeList[index].transform as RectTransform;
        }
    }
 
    public bool IsRoundReachLimit()
    {
        // return round > xxx;
        return false;
    }
 
    public void UpdateCanvas(Canvas canvas)
    {
        EffectPenetrationBlocker[] blockers = battleRootNode.GetComponentsInChildren<EffectPenetrationBlocker>(true);
 
        if (null != blockers)
        {
            foreach (var blocker in blockers)
            {
                blocker.SetParentCanvas(canvas);
            }
        }
    }
 
    public void StartBattle(Action onMoveComplete)
    {
        List<BattleObject> redTeam = battleObjMgr.GetBattleObjList(BattleCamp.Red);
 
        Tween tween = null;
 
        foreach (var obj in redTeam)
        {
            obj.motionBase.PlayAnimation(MotionName.run, true);
            RectTransform trans = obj.heroRectTrans;
            trans.anchoredPosition = new Vector2(-800, 0);
            tween = trans.DOAnchorPos(Vector2.zero, 1f).SetEase(Ease.Linear);
            battleTweenMgr.OnPlayTween(tween);
        }
 
        tween.onComplete = () =>
        {
            foreach (var obj in redTeam)
            {
                obj.motionBase.PlayAnimation(MotionName.idle, true);
            }
 
            // 播放战斗开始的特效
            // var efplayer = battleEffectMgr.PlayEffect(0, BattleConst.BattleStartEffectID, battleRootNode.transform);
            // efplayer.onDestroy += a => onMoveComplete();
 
            onMoveComplete?.Invoke();
        };
    }
 
 
    public void OnObjReborn(HB423_tagMCTurnFightObjReborn vNetData)
    {
        // 处理复活逻辑
        BattleObject battleObj = battleObjMgr.GetBattleObject((int)vNetData.ObjID);
        if (battleObj != null)
        {
            battleObj.OnReborn(vNetData);
        }
        else
        {
            BattleDebug.LogError($"BattleObject with ID {vNetData.ObjID} not found for reborn.");
        }
    }
 
    protected virtual void OnSettlement(JsonData turnFightStateData)
    {
 
    }
 
 
    public virtual void OnBattleEnd(JsonData turnFightStateData)
    {
        BattleEndAction battleEndAction = new BattleEndAction(this, turnFightStateData, () =>
        {
            BattleDebug.LogError(turnFightStateData.ToJson());
            // 这里可以添加战斗结束的具体逻辑
            OnSettlement(turnFightStateData);
 
            int winFaction = (int)turnFightStateData["winFaction"];
            //获胜阵营:   一般为1或者2,当玩家发起的战斗时,如果获胜阵营不等于1代表玩家失败了
 
            if (winFaction == 1)
            {
                Debug.LogError("战斗胜利");
                //  战斗胜利
 
                //  如果是自动战斗转自动战斗?
            }
            else
            {
                //  战斗失败
                Debug.LogError("战斗失败");
                HaveRest();
            }
 
            IsBattleFinish = true;
            
        });
        recordPlayer.PlayRecord(battleEndAction);
        // 处理战斗结束逻辑
        // IsBattleFinish = true;
        // 结算逻辑
 
            // {
            // "itemInfo": [],
            // "winFaction": 1,//获胜阵营:   一般为1或者2,当玩家发起的战斗时,如果获胜阵营不等于1代表玩家失败了
            // "statInfo": {
            //     "1": {
            //     "1": {
            //         "5": {
            //         "NPCID": 0,
            //         "DefHurt": 633,
            //         "CureHP": 0,
            //         "AtkHurt": 169247,
            //         "ObjID": 1,
            //         "HeroID": 510006
            //         }
            //     }
            //     },
            //     "2": {
            //     "1": {
            //         "2": {
            //         "NPCID": 10101001,
            //         "DefHurt": 169246,
            //         "CureHP": 143096,
            //         "AtkHurt": 999952,
            //         "ObjID": 2,
            //         "HeroID": 0
            //         },
            //         "4": {
            //         "NPCID": 10101001,
            //         "DefHurt": 0,
            //         "CureHP": 0,
            //         "AtkHurt": 0,
            //         "ObjID": 3,
            //         "HeroID": 0
            //         },
            //         "6": {
            //         "NPCID": 10101001,
            //         "DefHurt": 1,
            //         "CureHP": 0,
            //         "AtkHurt": 0,
            //         "ObjID": 4,
            //         "HeroID": 0
            //         }
            //     }
            //     }
            // }
            // }
    }
 
    public virtual void HaveRest()
    {
        //  休息状态
        battleObjMgr.HaveRest(BattleCamp.Red);
        battleObjMgr.DestroyTeam(BattleCamp.Blue);
 
        battleEffectMgr.HaveRest();
        battleTweenMgr.HaveRest();
 
        SetBattleMode(BattleMode.Stop);
    }
 
    public bool IsBattleEnd()
    {
        return IsBattleFinish;
    }
 
    public virtual void DistributeNextPackage()
    {
 
    }
}