少年修仙传客户端代码仓库
hch
2025-03-03 28785d6ddf9c08e49527ede9405c7b6c93c6ed32
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
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.AI;
 
 
public class HeroRoundGird : Singleton<HeroRoundGird>
{
    
    public class GirdNode
    {
        public int index;
        public int column;
        public int row;
        public float dis;
        public uint ownerSID;
    }
 
    private List<GirdNode> m_1 = new List<GirdNode>();
    private List<GirdNode> m_2 = new List<GirdNode>();
    private List<GirdNode> m_3 = new List<GirdNode>();
    private List<GirdNode> m_4 = new List<GirdNode>();
    private Dictionary<int, GirdNode> m_NodeDict = new Dictionary<int, GirdNode>();
    private Dictionary<int, bool> m_NodeEmpty = new Dictionary<int, bool>();
    private float m_Width = 0.5f;
    private float m_Height = 0.5f;
    private int m_Row;
    private int m_Column;
    private float m_OffsetX;
    private float m_OffsetY;
    private float m_Sin45 = .7071f;
    private int m_Half;
 
    public Vector3 GetRealPos(GirdNode node)
    {
        var _hero = PlayerDatas.Instance.hero;
        if (_hero != null)
        {
            return new Vector3(node.column * m_Width, 0, node.row * m_Height)
                    + _hero.Pos
                    - new Vector3(m_Half * m_Width, 0, m_Half * m_Height);
        }
        return Vector3.zero;
    }
 
    public GirdNode Get(int row, int column)
    {
        var _index = row * m_Column + column;
        if (m_NodeDict.ContainsKey(_index))
        {
            return m_NodeDict[_index];
        }
        return null;
    }
 
    public void UnInit()
    {
        m_1.Clear();
        m_2.Clear();
        m_3.Clear();
        m_4.Clear();
        m_NodeDict.Clear();
        m_NodeEmpty.Clear();
    }
    private bool m_Inited = false;
 
    public void Init(int row, int column)
    {
        if (m_Inited)
        {
            return;
        }
        m_Inited = true;
        m_1.Clear();
        m_2.Clear();
        m_3.Clear();
        m_4.Clear();
 
        m_Row = row;
        m_Column = column;
 
        m_Half = m_Row / 2;
        int _count = m_Half * m_Half;
 
        m_OffsetX = column * m_Width * .5f;
        m_OffsetY = row * m_Height * .5f;
 
        for (int i = m_Half; i < m_Row; ++i)
        {
            for (int j = m_Half; j < m_Column; ++j)
            {
                var _node = new GirdNode
                {
                    index = i * m_Column + j,
                    column = j,
                    row = i,
                };
                _node.dis = GetDistance(_node);
                m_1.Add(_node);
                m_NodeDict[_node.index] = _node;
                m_NodeEmpty[_node.index] = true;
            };
        }
        m_1.Sort(_Sort);
        for (int i = m_Half; i < m_Row; ++i)
        {
            for (int j = 0; j < m_Half; ++j)
            {
                var _node = new GirdNode
                {
                    index = i * m_Column + j,
                    column = j,
                    row = i
                };
                _node.dis = GetDistance(_node);
                m_2.Add(_node);
                m_NodeDict[_node.index] = _node;
                m_NodeEmpty[_node.index] = true;
            }
        }
        m_2.Sort(_Sort);
        for (int i = 0; i < m_Half; ++i)
        {
            for (int j = 0; j < m_Half; ++j)
            {
                var _node = new GirdNode
                {
                    index = i * m_Column + j,
                    column = j,
                    row = i
                };
                _node.dis = GetDistance(_node);
                m_3.Add(_node);
                m_NodeDict[_node.index] = _node;
                m_NodeEmpty[_node.index] = true;
            }
        }
        m_3.Sort(_Sort);
        for (int i = 0; i < m_Half; ++i)
        {
            for (int j = m_Half; j < m_Column; ++j)
            {
                var _node = new GirdNode
                {
                    index = i * m_Column + j,
                    column = j,
                    row = i
                };
                _node.dis = GetDistance(_node);
                m_4.Add(_node);
                m_NodeDict[_node.index] = _node;
                m_NodeEmpty[_node.index] = true;
            }
        }
        m_4.Sort(_Sort);
    }
 
    private float GetDistance(GirdNode n)
    {
        var _r = (n.row - m_Half) * m_Height;
        var _c = (n.column - m_Half) * m_Width;
        return Mathf.Sqrt(_c * _c + _r * _r);
    }
 
    private int _Sort(GirdNode a, GirdNode b)
    {
        if (a.dis > b.dis)
        {
            return -1;
        }
        else if (a.dis < b.dis)
        {
            return 1;
        }
        return 0;
    }
 
    public GirdNode Request(uint sid, Vector3 pos, float limitDis)
    {
        var _hero = PlayerDatas.Instance.hero;
        if (_hero == null)
        {
            return null;
        }
        Vector3 _chkCenter = _hero.Pos;
 
        // 获取行列
        float _x = pos.x - _chkCenter.x + m_OffsetX;
        float _y = pos.z - _chkCenter.z + m_OffsetY;
 
        int _column = (int)(_x / m_Width);
        int _row = (int)(_y / m_Height);
 
        // Debug.LogFormat("计算得出: row: {1}, column: {0}", _row, _column);
 
        int _index = _row * m_Column + _column;
 
        // Debug.LogFormat("监测点: {0}, 所处行: {1}, 列: {2}, 当前对应索引 {3}", pos, _row, _column, _index);
 
        // 距离满足的情况下, 则计算索引
        float _currentDistance = MathUtility.DistanceSqrtXZ(pos, _chkCenter);
        if (_currentDistance < limitDis * limitDis)
        {
            if (_index >= 0 && _index < m_NodeDict.Count)
            {
                if (m_NodeEmpty[_index])
                {
                    m_NodeEmpty[_index] = false;
                    m_NodeDict[_index].ownerSID = sid;
                    return m_NodeDict[_index];
                }
                else
                {
                    if (m_NodeDict[_index].ownerSID == sid)
                    {
                        return m_NodeDict[_index];
                    }
                    // Debug.LogFormat("<color=yellow>监测点: {0}, 索引 {1} 已经被占用...</color>", pos, _index);
                }
            }
            else
            {
                // Debug.LogFormat("<color=yellow>监测点: {0}, 索引 {1} 非法...</color>", pos, _index);
            }
        }
        else
        {
            // Debug.LogFormat("<color=yellow>监测点: {0}, 距离超出限定距离...</color>", pos, _index);
        }
 
        byte _quadrant = 0;
 
        if (pos.x >= _chkCenter.x && pos.z >= _chkCenter.z)
        {
            _quadrant = 0;
        }
        else if (pos.x <= _chkCenter.x && pos.z >= _chkCenter.z)
        {
            _quadrant = 1;
        }
        else if (pos.x <= _chkCenter.x && pos.z <= _chkCenter.z)
        {
            _quadrant = 2;
        }
        else
        {
            _quadrant = 3;
        }
 
        // Debug.LogFormat("监测点: {0}, 相对于玩家处于第 {1} 象限.", pos, (_quadrant + 1));
 
        GirdNode _node;
 
        switch (_quadrant)
        {
            case 0:
                //   □ │ ■
                //  ───┼───
                //   □ │ □
                for (int i = 0; i < m_1.Count; ++i)
                {
                    _node = m_1[i];
                    if (_node.dis >= limitDis)
                    {
                        continue;
                    }
                    if (m_NodeEmpty[_node.index])
                    {
                        m_NodeEmpty[_node.index] = false;
 
                        var _real = GetRealPos(_node);
                        _real.y = 0;
                        NavMeshHit _hit;
                        if (NavMesh.SamplePosition(_real, out _hit, 0.02f, NavMesh.AllAreas))
                        {
                            _node.ownerSID = sid;
                            return _node;
                        }
                    }
                    else
                    {
                        if (!m_NodeEmpty[_node.index])
                        {
                            if (_node.ownerSID == 0)
                            {
                                var _real = GetRealPos(_node);
                                _real.y = 0;
                                NavMeshHit _hit;
                                if (NavMesh.SamplePosition(_real, out _hit, 0.02f, NavMesh.AllAreas))
                                {
                                    m_NodeEmpty[_node.index] = true;
                                }
                            }
                        }
 
                        if (m_NodeDict[_node.index].ownerSID == sid)
                        {
                            return m_NodeDict[_node.index];
                        }
                    }
                }
 
                break;
            case 1:
                //   ■ │ □
                //  ───┼───
                //   □ │ □
                for (int i = 0; i < m_2.Count; ++i)
                {
                    _node = m_2[i];
                    if (_node.dis >= limitDis)
                    {
                        continue;
                    }
                    if (m_NodeEmpty[_node.index])
                    {
                        m_NodeEmpty[_node.index] = false;
 
                        var _real = GetRealPos(_node);
                        _real.y = 0;
                        NavMeshHit _hit;
                        if (NavMesh.SamplePosition(_real, out _hit, 0.02f, NavMesh.AllAreas))
                        {
                            _node.ownerSID = sid;
                            return _node;
                        }
                    }
                    else
                    {
                        if (!m_NodeEmpty[_node.index])
                        {
                            if (_node.ownerSID == 0)
                            {
                                var _real = GetRealPos(_node);
                                _real.y = 0;
                                NavMeshHit _hit;
                                if (NavMesh.SamplePosition(_real, out _hit, 0.02f, NavMesh.AllAreas))
                                {
                                    m_NodeEmpty[_node.index] = true;
                                }
                            }
                        }
 
                        if (m_NodeDict[_node.index].ownerSID == sid)
                        {
                            return m_NodeDict[_node.index];
                        }
                    }
                }
 
                break;
            case 2:
                //   □ │ □
                //  ───┼───
                //   ■ │ □
                for (int i = 0; i < m_3.Count; ++i)
                {
                    _node = m_3[i];
                    if (_node.dis >= limitDis)
                    {
                        continue;
                    }
                    if (m_NodeEmpty[_node.index])
                    {
                        m_NodeEmpty[_node.index] = false;
 
                        var _real = GetRealPos(_node);
                        _real.y = 0;
                        NavMeshHit _hit;
                        if (NavMesh.SamplePosition(_real, out _hit, 0.02f, NavMesh.AllAreas))
                        {
                            _node.ownerSID = sid;
                            return _node;
                        }
                    }
                    else
                    {
                        if (!m_NodeEmpty[_node.index])
                        {
                            if (_node.ownerSID == 0)
                            {
                                var _real = GetRealPos(_node);
                                _real.y = 0;
                                NavMeshHit _hit;
                                if (NavMesh.SamplePosition(_real, out _hit, 0.02f, NavMesh.AllAreas))
                                {
                                    m_NodeEmpty[_node.index] = true;
                                }
                            }
                        }
 
                        if (m_NodeDict[_node.index].ownerSID == sid)
                        {
                            return m_NodeDict[_node.index];
                        }
                    }
                }
                break;
            case 3:
                //   □ │ □
                //  ───┼───
                //   □ │ ■
                for (int i = 0; i < m_4.Count; ++i)
                {
                    _node = m_4[i];
                    if (_node.dis >= limitDis)
                    {
                        continue;
                    }
                    if (m_NodeEmpty[_node.index])
                    {
                        m_NodeEmpty[_node.index] = false;
 
                        var _real = GetRealPos(_node);
                        _real.y = 0;
                        NavMeshHit _hit;
                        if (NavMesh.SamplePosition(_real, out _hit, 0.02f, NavMesh.AllAreas))
                        {
                            _node.ownerSID = sid;
                            return _node;
                        }
                    }
                    else
                    {
                        if (!m_NodeEmpty[_node.index])
                        {
                            if (_node.ownerSID == 0)
                            {
                                var _real = GetRealPos(_node);
                                _real.y = 0;
                                NavMeshHit _hit;
                                if (NavMesh.SamplePosition(_real, out _hit, 0.02f, NavMesh.AllAreas))
                                {
                                    m_NodeEmpty[_node.index] = true;
                                }
                            }
                        }
 
                        if (m_NodeDict[_node.index].ownerSID == sid)
                        {
                            return m_NodeDict[_node.index];
                        }
                    }
                }
                break;
        }
 
        return null;
    }
 
    public void Release(int id)
    {
        if (!m_NodeEmpty.ContainsKey(id))
        {
            return;
        }
        m_NodeEmpty[id] = true;
        m_NodeDict[id].ownerSID = 0;
    }
}