少年修仙传客户端代码仓库
client_Hale
2019-04-10 bf4044c721fed1b9389a31aded4370610558bd52
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
using H2Engine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class ClientSceneManager : Singleton<ClientSceneManager>
{
    private H2Engine.MapData m_MapData;
    private List<TransferGroup> m_TransferGroupList = new List<TransferGroup>();
    private Dictionary<int, IEventHandler> m_EventHandlerDict = new Dictionary<int, IEventHandler>();
    private List<IEventHandler> m_ReadyRemoveEventHandlerList = new List<IEventHandler>();
    private Dictionary<int, uint> m_TriggerIDToMissionIDDict = new Dictionary<int, uint>();
    private Dictionary<int, TriggerHandler> m_TriggerHandlerDict = new Dictionary<int, TriggerHandler>();
    private List<int> m_ReadyRemoveTriggerList = new List<int>();
    private Dictionary<int, List<GActor>> m_EventActorDict = new Dictionary<int, List<GActor>>();
    public bool IsClientFightMode { get; private set; }
 
    private IEnumerator DelayUnTrigger(int id)
    {
        while (m_MapData == null)
        {
            yield return null;
        }
        float _escapeTime = 0;
        while (m_TriggerHandlerDict.Count == 0)
        {
            _escapeTime += Time.deltaTime;
            if (_escapeTime > 1f)
            {
                yield break;
            }
            yield return null;
        }
 
        Debug.LogFormat("=           结束触发: {0}             =", id);
        foreach (var _trigger in m_MapData.triggers)
        {
            if (_trigger.id != id)
            {
                continue;
            }
 
            if (_trigger.evevntIDs == null
             || _trigger.evevntIDs.Length == 0)
            {
                continue;
            }
 
            Debug.LogFormat("=           触发器有: {0} 个事件             =", _trigger.evevntIDs.Length);
            foreach (var _eventID in _trigger.evevntIDs)
            {
                if (!m_EventActorDict.ContainsKey(_eventID))
                {
                    continue;
                }
 
                if (m_EventActorDict[_eventID] == null
                 || m_EventActorDict[_eventID].Count == 0)
                {
                    continue;
                }
 
                Debug.LogFormat("=           残留有: {0} 个NPC             =", m_EventActorDict[_eventID].Count);
                foreach (var _npc in m_EventActorDict[_eventID])
                {
                    _npc.KillerServerInstID = PlayerDatas.Instance.PlayerId;
                    _npc.ActorInfo.serverDie = true;
                    GAMgr.Instance.ServerDie(_npc.ServerInstID);
                    GAMgr.Instance.Release(_npc);
                }
 
                m_EventHandlerDict.Remove(_eventID);
            }
 
            break;
        }
    }
 
    public void NpcBorn(int eventID, GActor npc)
    {
        if (!m_EventActorDict.ContainsKey(eventID))
        {
            m_EventActorDict.Add(eventID, new List<GActor>());
        }
 
        if (!m_EventActorDict[eventID].Contains(npc))
        {
            m_EventActorDict[eventID].Add(npc);
            Debug.LogFormat("= 为事件: {0} 增加Npc: {1}", eventID, npc.ServerInstID);
        }
    }
 
    public void NpcDead(int eventID, GActor npc, int npcID)
    {
        if (eventID < 0 || npc == null)
        {
            return;
        }
 
        var _eventHandler = GetEventHandler(eventID) as EventRefreshNPCHandler;
        if (_eventHandler != null)
        {
            _eventHandler.NpcDead(npcID);
        }
 
        if (m_EventActorDict.ContainsKey(eventID))
        {
            if (m_EventActorDict[eventID].Contains(npc))
            {
                m_EventActorDict[eventID].Remove(npc);
            }
        }
    }
 
    public void Handle_0820(H0820_tagMissionDict package)
    {
        m_ReadyRemoveTriggerList.Clear();
 
        foreach (var _triggerID in m_TriggerIDToMissionIDDict.Keys)
        {
            if (m_TriggerIDToMissionIDDict[_triggerID] == package.MissionID)
            {
                if (package.DictKey.Equals("state"))
                {
                    if (package.DictValue > 2)
                    {
                        m_ReadyRemoveTriggerList.Add(_triggerID);
                        SnxxzGame.Instance.StartCoroutine(DelayUnTrigger(_triggerID));
                    }
                }
            }
        }
 
        foreach (var _id in m_ReadyRemoveTriggerList)
        {
            m_TriggerIDToMissionIDDict.Remove(_id);
        }
 
 
        if (package.DictKey.Equals("riggerID"))
        {
            int _triggerID = (int)package.DictValue;
            if (_triggerID == 0)
            {
                Debug.LogFormat("退出当前触发事件, 已触发数量: {0}", m_TriggerIDToMissionIDDict.Count);
                foreach (var _removeTriggerID in m_TriggerIDToMissionIDDict.Keys)
                {
                    SnxxzGame.Instance.StartCoroutine(DelayUnTrigger(_removeTriggerID));
                }
            }
            else
            {
                m_TriggerIDToMissionIDDict[_triggerID] = package.MissionID;
                SnxxzGame.Instance.StartCoroutine(DelayTrigger(package.MissionID, _triggerID));
            }
        }
        else if (package.DictKey.Equals("xinshou"))
        {
            if (package.DictValue == 0)
            {
                Debug.LogFormat("=           退出了前端模式             =");
                IsClientFightMode = false;
            }
            else
            {
                Debug.LogFormat("=           进入了前端模式             =");
                IsClientFightMode = true;
            }
        }
    }
 
    private bool m_Inited = false;
 
    public void Init()
    {
        if (m_Inited)
        {
            return;
        }
        m_Inited = true;
 
        int _mapID = PlayerDatas.Instance.baseData.MapID;
        m_MapData = MapData.LoadFormFile(_mapID);
        if (m_MapData == null)
        {
            return;
        }
 
        foreach (var _trasfer in m_MapData.transfers)
        {
            m_TransferGroupList.Add(new TransferGroup(_trasfer));
        }
 
        TriggerHandler.OnEnter += OnTriggerEnter;
        TriggerHandler.OnExit += OnTriggerExit;
    }
 
    public void UnInit()
    {
        foreach (var _t in m_TransferGroupList)
        {
            _t.UnInit();
        }
        m_TransferGroupList.Clear();
 
        ReConnectClear();
        m_MapData = null;
        TriggerHandler.OnEnter -= OnTriggerEnter;
        TriggerHandler.OnExit -= OnTriggerExit;
 
        m_Inited = false;
        IsClientFightMode = false;
        m_EventActorDict.Clear();
        m_EventHandlerDict.Clear();
    }
 
    public void ReConnectClear()
    {
        foreach (var _triggerID in m_TriggerIDToMissionIDDict.Keys)
        {
            DelayUnTrigger(_triggerID);
        }
        m_TriggerIDToMissionIDDict.Clear();
        m_TriggerHandlerDict.Clear();
    }
 
    public void Update()
    {
        foreach (var _trasfer in m_TransferGroupList)
        {
            _trasfer.Update();
        }
 
        foreach (var _triggerHandler in m_TriggerHandlerDict.Values)
        {
            _triggerHandler.Update();
        }
 
        m_ReadyRemoveEventHandlerList.Clear();
 
        foreach (var _eventHandler in m_EventHandlerDict.Values)
        {
            _eventHandler.Update();
            if (_eventHandler.IsCompelete())
            {
                m_ReadyRemoveEventHandlerList.Add(_eventHandler);
            }
        }
 
        foreach (var _eventHandler in m_ReadyRemoveEventHandlerList)
        {
            _eventHandler.UnInit();
            m_EventHandlerDict.Remove(_eventHandler.GetEventID());
        }
    }
 
    public IEventHandler GetEventHandler(int id)
    {
        if (m_EventHandlerDict.ContainsKey(id))
        {
            return m_EventHandlerDict[id];
        }
        return null;
    }
 
    private List<MapTrasfer> m_ChkedList = new List<MapTrasfer>();
    private List<MapTrasfer> m_ChkedList1 = new List<MapTrasfer>();
    private int count = 0;
    public Vector3 GetTransPoint(Vector3 start, Vector3 next)
    {
        count = 0;
        m_ChkedList.Clear();
 
        return GetNext(start, next);
    }
 
    public Vector3 GetNext1(Vector3 start, Vector3 dest)
    {
        count = 0;
        m_ChkedList1.Clear();
 
        if (PathFinder.WalkAble(start, dest))
        {
            float _dis = PathFinder.CalculateDistance(start, dest);
            //Debug.Log("得出的距离平方为: " + _dis);
 
            int _chkIndex = -1;
            float _compareDis = float.MaxValue;
            float _chkDis1;
            float _chkDis2;
            float _totalDis = 0;
            foreach (var _t in m_MapData.transfers)
            {
                if (_t.transferPoints.Length >= 2)
                {
                    if (!PathFinder.WalkAble(_t.transferPoints[0].position, dest)
                     && !PathFinder.WalkAble(_t.transferPoints[_t.transferPoints.Length - 1].position, dest))
                    {
                        continue;
                    }
 
                    _chkDis1 = Vector3.Distance(_t.transferPoints[0].position, dest);
                    _chkDis2 = Vector3.Distance(_t.transferPoints[_t.transferPoints.Length - 1].position, dest);
 
                    if (_chkDis1 > _compareDis && _chkDis2 > _compareDis)
                    {
                        continue;
                    }
 
                    if (_chkDis1 < _chkDis2)
                    {
                        _compareDis = _chkDis1;
                        _chkIndex = 0;
                    }
                    else
                    {
                        _compareDis = _chkDis2;
                        _chkIndex = _t.transferPoints.Length - 1;
                    }
                    m_ChkedList1.Add(_t);
                }
            }
            // Debug.LogFormat("离终点最近的点为飞跃组: {0} 的第 {1} 个点", _chkTransID, _chkIndex);
            if (m_ChkedList1.Count == 0)
            {
                return Vector3.zero;
            }
            var _t1 = m_ChkedList1[m_ChkedList1.Count - 1];
            for (int i = 0; i < _t1.transferPoints.Length - 1; ++i)
            {
                var _p1 = _t1.transferPoints[i].position;
                _p1.y = 0;
                var _p2 = _t1.transferPoints[i + 1].position;
                _p2.y = 0;
                _totalDis += Vector3.Distance(_p1, _p2);
                // Debug.LogFormat(" ### 2个点的距离: {0}, {1}", Vector3.Distance(_p1, _p2), _totalDis);
            }
            var _p = _t1.transferPoints[_chkIndex].position;
            _p.y = 0;
            _totalDis += Vector3.Distance(_p, dest);
            //Debug.LogFormat(" ### 终点和跳跃点的距离: {0}, {1}", Vector3.Distance(_p, dest), _totalDis);
            var _nextIdx = _chkIndex == 0 ? _t1.transferPoints.Length - 1 : 0;
            _p = _t1.transferPoints[_nextIdx].position;
            var _startDis = PathFinder.CalculateDistance(start, _p);
            _totalDis += _startDis;
            //Debug.LogFormat(" ### 起点和跳跃点的距离: {0}, {1}", _startDis, _totalDis);
            if (_totalDis < _dis)
            {
                return _p;
            }
            var _next = GetNext11(start, _p, ref _totalDis, _dis);
            if (_totalDis < _dis)
            {
                return _next;
            }
            else
            {
                return Vector3.zero;
            }
        }
        else
        {
            return GetNext(start, dest);
        }
    }
 
    private Vector3 GetNext11(Vector3 start, Vector3 dest, ref float dis, float totalDis)
    {
        float _chkDis1;
        float _chkDis2;
        float _compareDis = float.MaxValue;
        int _chkTransID = -1;
        int _chkIndex = -1;
 
        foreach (var _t in m_MapData.transfers)
        {
            if (m_ChkedList1.Contains(_t))
            {
                continue;
            }
 
            if (_t.transferPoints.Length >= 2)
            {
                if (!PathFinder.WalkAble(_t.transferPoints[0].position, dest)
                 && !PathFinder.WalkAble(_t.transferPoints[_t.transferPoints.Length - 1].position, dest))
                {
                    continue;
                }
                _chkDis1 = Vector3.Distance(_t.transferPoints[0].position, dest);
                _chkDis2 = Vector3.Distance(_t.transferPoints[_t.transferPoints.Length - 1].position, dest);
 
                if (_chkDis1 > _compareDis && _chkDis2 > _compareDis)
                {
                    continue;
                }
 
                if (_chkDis1 < _chkDis2)
                {
                    _compareDis = _chkDis1;
                    _chkIndex = 0;
                }
                else
                {
                    _compareDis = _chkDis2;
                    _chkIndex = _t.transferPoints.Length - 1;
                }
 
                m_ChkedList1.Add(_t);
                _chkTransID = _t.id;
            }
        }
 
        if (m_ChkedList1.Count == 0)
        {
            return Vector3.zero;
        }
 
        var _t1 = m_ChkedList1[m_ChkedList1.Count - 1];
 
        if (_chkTransID != -1)
        {
            for (int i = 0; i < _t1.transferPoints.Length - 1; ++i)
            {
                var _p1 = _t1.transferPoints[i].position;
                _p1.y = 0;
                var _p2 = _t1.transferPoints[i + 1].position;
                _p2.y = 0;
                dis += Vector3.Distance(_p1, _p2);
                // Debug.LogFormat(" ### 2个点的距离: {0}", Vector3.Distance(_p1, _p2));
            }
            var _p = _t1.transferPoints[_chkIndex].position;
            _p.y = 0;
            dis += Vector3.Distance(_p, dest);
            _p = _t1.transferPoints[_chkIndex].position;
            _p.y = 0;
            //Debug.LogFormat(" ### 终点和跳跃点的距离: {0}", Vector3.Distance(_p, dest));
            int _nextIdx = _chkIndex == 0 ? _t1.transferPoints.Length - 1 : 0;
            return GetNext11(start, _t1.transferPoints[_nextIdx].position, ref dis, totalDis);
        }
        else
        {
            int _nextIdx = _chkIndex == 0 ? _t1.transferPoints.Length - 1 : 0;
            return _t1.transferPoints[_nextIdx].position;
        }
    }
 
    private Vector3 GetNext(Vector3 start, Vector3 next)
    {
        count += 1;
        if (count > 20 || next == Vector3.zero)
        {
            return Vector3.zero;
        }
        Vector3 _targetPos = Vector3.zero;
        // 遍历寻找到可移动至目标点的跳跃点
        foreach (var _t in m_MapData.transfers)
        {
            if (m_ChkedList.Contains(_t))
            {
                continue;
            }
 
            for (int i = 0; i < _t.transferPoints.Length; ++i)
            {
                var _tp = _t.transferPoints[i];
                if (PathFinder.WalkAble(next, _tp.position))
                {
                    //Debug.Log("_tp.position : " + _tp.position);
                    if (i == 0)
                    {
                        _targetPos = _t.transferPoints[_t.transferPoints.Length - 1].position;
                    }
                    else
                    {
                        _targetPos = _t.transferPoints[0].position;
                    }
 
                    m_ChkedList.Add(_t);
                    break;
                }
            }
            if (_targetPos != Vector3.zero)
            {
                break;
            }
        }
 
        if (PathFinder.WalkAble(start, _targetPos))
        {
            return _targetPos;
        }
        else
        {
            _targetPos = GetNext(start, _targetPos);
        }
        //Debug.Log("_targetPos: " + _targetPos);
 
        return _targetPos;
    }
 
    public Vector3 GetCloseTransPoint(Vector3 pos)
    {
        Vector3 _targetPos = Vector3.zero;
 
        foreach (var _t in m_MapData.transfers)
        {
            for (int i = 0; i < _t.transferPoints.Length; ++i)
            {
                if (PathFinder.WalkAble(pos, _t.transferPoints[i].position))
                {
                    if (i == 0)
                    {
                        _targetPos = _t.transferPoints[_t.transferPoints.Length - 1].position;
                    }
                    else
                    {
                        _targetPos = _t.transferPoints[0].position;
                    }
                    break;
                }
            }
            if (_targetPos != Vector3.zero)
            {
                break;
            }
        }
 
        if (_targetPos == Vector3.zero)
        {
            Debug.LogWarningFormat("寻找最近的寻路飞跃点, 没有找到任何结果...");
        }
 
        return _targetPos;
    }
 
    public void TriggerTest(int triggerID)
    {
        SnxxzGame.Instance.StartCoroutine(DelayTrigger(99999999, triggerID));
    }
 
    private IEnumerator DelayTrigger(uint missionID, int triggerID)
    {
        // ID为0, 约定为清空触发
        if (triggerID == 0)
        {
            yield return SnxxzGame.Instance.StartCoroutine(DelayUnTrigger(triggerID));
            yield break;
        }
 
        while (StageLoad.Instance.isLoading)
        {
            yield return null;
        }
 
        Debug.LogFormat("=           任务: {0} 触发: {1}             =", missionID, triggerID);
 
        if (m_MapData == null)
        {
            Init();
        }
 
        if (m_MapData.triggers == null
         || m_MapData.triggers.Length == 0)
        {
            yield break;
        }
 
        foreach (var _trigger in m_MapData.triggers)
        {
            if (_trigger.id != triggerID)
            {
                continue;
            }
 
            if (_trigger.evevntIDs == null
             || _trigger.evevntIDs.Length == 0)
            {
                continue;
            }
 
            var _triggerHandler = new TriggerHandler(_trigger);
            m_TriggerHandlerDict[_trigger.id] = _triggerHandler;
            foreach (var _eventID in _trigger.evevntIDs)
            {
                if (!m_MapData.eventDict.ContainsKey(_eventID))
                {
                    continue;
                }
 
                var _event = m_MapData.eventDict[_eventID];
                if (_event.type == Evt.E_EventType.Enemy)
                {
                    var _refreshEvent = new EventRefreshNPCHandler();
                    _refreshEvent.Init(_event as Evt_RefreshMonster);
                    if (!m_EventHandlerDict.ContainsKey(_refreshEvent.GetEventID()))
                    {
                        m_EventHandlerDict.Add(_refreshEvent.GetEventID(), _refreshEvent);
                    }
                }
            }
 
            break;
        }
    }
 
    private void OnTriggerEnter(int id)
    {
        //Debug.Log("进入了触发器范围: " + id);
    }
 
    private void OnTriggerExit(int id)
    {
        if (m_TriggerHandlerDict.ContainsKey(id))
        {
            //Debug.Log("退出了触发器范围: " + id);
            CA225_tagCMClientTaskCount _a225 = new CA225_tagCMClientTaskCount
            {
                CountID = 10000
            };
            GameNetSystem.Instance.SendInfo(_a225);
        }
    }
}