少年修仙传客户端代码仓库
client_Hale
2019-04-16 2f723e5320ef618985cc9ea8f71deafd3e4bec36
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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Snxxz.UI
{
    public class ClientHazyGrassStage : DungeonStage
    {
        static readonly Vector3 PlayerBornPosition = new Vector3(17.25f, 5.12f, 3.70f);
 
        static List<HazyMapNpcScriptableObject.NpcInfo> s_NpcInfos = new List<HazyMapNpcScriptableObject.NpcInfo>();
        static Dictionary<Vector3, GA_NpcClientCollect> s_CollectNpcs = new Dictionary<Vector3, GA_NpcClientCollect>();
        static Dictionary<uint, Vector3> s_Sid2NpcPos = new Dictionary<uint, Vector3>();
        static Dictionary<uint, int> s_Sid2NpcIds = new Dictionary<uint, int>();
 
        static int grassRefreshCount = 0;
 
        bool mapLoadFinish = false;
 
        HazyGrassModel model { get { return ModelCenter.Instance.GetModel<HazyGrassModel>(); } }
 
        public override void Initialize()
        {
            base.Initialize();
 
            s_NpcInfos.Clear();
            s_Sid2NpcIds.Clear();
            s_Sid2NpcPos.Clear();
 
            mapLoadFinish = false;
 
            grassRefreshCount = 0;
 
            UnloadAllNpc();
 
            GA_NpcClientCollect.OnCollectFinished += OnCollectFinished;
        }
 
        protected override void OnStageLoadFinish()
        {
            base.OnStageLoadFinish();
 
            mapLoadFinish = true;
            model.RefreshGrassBornTime(TimeUtility.ServerNow);
 
            InitialPlayer();
            InitializeNpc();
        }
 
        protected override void OnUpdate()
        {
            base.OnUpdate();
 
            if (mapLoadFinish)
            {
                var used = Mathf.Max(0, (int)(TimeUtility.ServerNow - model.grassBornTime).TotalSeconds);
                var count = used / model.grassRefreshSeconds;
                if (count != grassRefreshCount)
                {
                    RebornCollectedNpc();
                    grassRefreshCount = count;
                }
            }
        }
 
        public override void UnInitialize()
        {
            base.UnInitialize();
 
            UnloadAllNpc();
 
            GA_NpcClientCollect.OnCollectFinished -= OnCollectFinished;
        }
 
        private void OnCollectFinished(uint _sid)
        {
            if (s_Sid2NpcIds.ContainsKey(_sid))
            {
                var npcId = s_Sid2NpcIds[_sid];
                Debug.Log("采集了Npc:--" + npcId);
            }
 
            if (s_Sid2NpcPos.ContainsKey(_sid))
            {
                var pos = s_Sid2NpcPos[_sid];
                if (s_CollectNpcs.ContainsKey(pos))
                {
                    s_CollectNpcs.Remove(pos);
                }
            }
        }
 
        void InitializeNpc()
        {
            s_NpcInfos.Clear();
 
            var config = ScriptableObjectLoader.LoadSoHazyMapNpc(ClientDungeonStageUtility.clientMapId);
            var npcInfos = config.GetAllNpcInfos();
            if (npcInfos != null)
            {
                s_NpcInfos.AddRange(npcInfos);
                RebornCollectedNpc();
            }
        }
 
        void InitialPlayer()
        {
            var hero = PlayerDatas.Instance.hero;
            hero.Pos = PlayerBornPosition;
            CameraController.Instance.Apply();
        }
 
        void RebornCollectedNpc()
        {
            foreach (var npcInfo in s_NpcInfos)
            {
                switch (npcInfo.npcType)
                {
                    case E_NpcType.Collect:
                        GA_NpcClientCollect _npc = null;
                        if (!s_CollectNpcs.TryGetValue(npcInfo.position, out _npc)
                            || _npc == null || _npc.ActorInfo.serverDie)
                        {
                            _npc = GAMgr.Instance.ReqClntNoFightNpc<GA_NpcClientCollect>((uint)npcInfo.npcId,
                                E_ActorGroup.FuncNpc);
                            if (_npc != null)
                            {
                                _npc.Pos = npcInfo.position;
                                s_CollectNpcs[npcInfo.position] = _npc;
 
                                s_Sid2NpcIds[_npc.ServerInstID] = npcInfo.npcId;
                                s_Sid2NpcPos[_npc.ServerInstID] = npcInfo.position;
                            }
                        }
                        break;
                }
            }
        }
 
        void UnloadAllNpc()
        {
            foreach (var _npc in s_CollectNpcs.Values)
            {
                if (_npc != null)
                {
                    _npc.KillerServerInstID = PlayerDatas.Instance.PlayerId;
                    _npc.ActorInfo.serverDie = true;
                    GAMgr.Instance.ServerDie(_npc.ServerInstID);
                    GAMgr.Instance.Release(_npc);
                }
            }
            s_CollectNpcs.Clear();
        }
 
#if UNITY_EDITOR
        private void OnGUI()
        {
            if (GUILayout.Button("Exit"))
            {
                model.RequestExitClientDungeon();
            }
        }
#endif
    }
}