少年修仙传客户端代码仓库
hch
2025-06-12 204ef05a831c9484e2abc561d27ecbff7c797453
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
using UnityEngine;
using System.Collections.Generic;
 
public class SampleAI
{
    protected enum E_AIStatus
    {
        Idle,
        Patrol,// 巡逻
        MoveToTarget,// 向目标移动
        Attack,// 攻击
        MoveToBornPos,// 向出生点移动
        RunAway,// 逃离
        //后续IL开发添加预设
        default1,
        default2,
        default3,
        default4,
        default5,
        default6,
        default7,
        default8,
        default9,
        default10,
    }
 
    public Vector3 BornPos;
    protected float m_KeepBornDistSqrt;
 
    protected ushort m_BaseAtkSkillID = 10016;
    protected List<int> m_SkillList = new List<int>();
    protected float m_SleepTime;
    protected float m_LastThinkTime;
    protected float m_LastAttackTime;
    protected float m_PatrolDist;
 
    protected GA_NpcClientFightNorm m_Owner;
    protected bool m_CanCastSkill = true;
 
    protected E_AIStatus m_AIStatus;
 
    public SampleAI(GA_NpcClientFightNorm owner, Vector3 bornPosition)
    {
        m_Owner = owner;
        BornPos = bornPosition;
 
        // NPC类型的视野读取配置
        m_KeepBornDistSqrt = m_Owner.NpcConfig.Sight * .5f;
        m_KeepBornDistSqrt *= m_KeepBornDistSqrt;
        m_PatrolDist = m_Owner.NpcConfig.MoveArea * .5f;
 
        m_SleepTime = 0.5f;
 
        m_LastThinkTime = 0;
        m_PatrolStartTime = 0;
 
        if (owner.NpcConfig.Skill1 > 0)
        {
            m_SkillList.Add(owner.NpcConfig.Skill1);
        }
        if (owner.NpcConfig.Skill2 > 0)
        {
            m_SkillList.Add(owner.NpcConfig.Skill2);
        }
        if (owner.NpcConfig.Skill3 > 0)
        {
            m_SkillList.Add(owner.NpcConfig.Skill3);
        }
        if (owner.NpcConfig.Skill4 > 0)
        {
            m_SkillList.Add(owner.NpcConfig.Skill4);
        }
        if (owner.NpcConfig.Skill5 > 0)
        {
            m_SkillList.Add(owner.NpcConfig.Skill5);
        }
        if (owner.NpcConfig.Skill6 > 0)
        {
            m_SkillList.Add(owner.NpcConfig.Skill6);
        }
        if (owner.NpcConfig.Skill7 > 0)
        {
            m_SkillList.Add(owner.NpcConfig.Skill7);
        }
        if (owner.NpcConfig.Skill8 > 0)
        {
            m_SkillList.Add(owner.NpcConfig.Skill8);
        }
        SearchActor = true;
        if (owner.NpcConfig.AtkType == 1)
        {
            SearchActor = false;
            owner.ActorInfo.OnHpChange += OnHpChange;
        }
    }
 
    protected bool SearchActor = false;
 
    private void OnHpChange(ulong value)
    {
        SearchActor = true;
    }
 
    public virtual void Update()
    {
        if (GA_Hero.s_MapSwitching)
        {
            return;
        }
 
        OutOfSleepUpdate();
 
        if (Time.time - m_LastThinkTime < m_SleepTime)
        {
            return;
        }
 
        m_LastThinkTime = Time.time;
 
        Thinking();
 
        //if (m_Owner.ActorID == 1007)
        //{
        //    Debug.Log("决定要: " + m_AIStatus.ToString());
        //}
 
        OnUpdate();
    }
 
    protected virtual void OutOfSleepUpdate() { }
 
    protected virtual void OnUpdate() { }
 
    protected float m_PatrolWaitingTime = 5f;
    protected float m_PatrolStartTime;
 
    protected void Patrol()
    {
        if (Time.time - m_PatrolStartTime < m_PatrolWaitingTime)
        {
            return;
        }
 
        m_PatrolStartTime = Time.time;
 
        // 随机一个出生点周围的点
        Vector3 _randomPosition = BornPos + Random.rotation * Vector3.forward * m_PatrolDist;
 
        m_Owner.MoveToPosition(_randomPosition);
    }
 
    protected void Thinking()
    {
        // 英雄是否存在
        GA_Hero _hero = PlayerDatas.Instance.hero;
 
        if (_hero == null)
        {
            return;
        }
 
        // 持有者是否已经死亡
        if (m_Owner == null
         || m_Owner.ActorInfo == null
         || m_Owner.ActorInfo.serverDie)
        {
            return;
        }
 
        // 是否正在释放技能
        if (m_Owner.SkillMgr.CurCastSkill != null)
        {
            if (m_Owner.SkillMgr.CurCastSkill.SkillCompelete == false)
            {
                if (m_Owner.IsIdle())
                {
                    m_Owner.SkillMgr.CurCastSkill.SkillCompelete = true;
                }
                return;
            }
 
            if (m_Owner.SkillMgr.CurCastSkill.SkillPreparing)
            {
                return;
            }
 
            if (m_Owner.NextAction == 300)
            {
                return;
            }
        }
 
        // 计算视野距离
        float _distSqrt = MathUtility.DistanceSqrtXZ(_hero.Pos, m_Owner.Pos);
        // 是否在攻击距离内
        if (_distSqrt <= 4)
        {
            m_AIStatus = E_AIStatus.Attack;
            return;
        }
 
        m_AIStatus = E_AIStatus.Patrol;
 
        if (m_Owner.NpcConfig.NPCID == 1007)
        {
            Vector3 _position = m_Owner.Pos;
            _position.y = 0;
            Vector3 _targetPosition = m_Owner.Pos - m_Owner.Forward;
            UnityEngine.AI.NavMeshHit _hit;
            if (UnityEngine.AI.NavMesh.Raycast(_position, _targetPosition, out _hit, -1))
            {
                _distSqrt = MathUtility.DistanceSqrtXZ(_position, _hit.position);
                if (_distSqrt < 2)
                {
                    m_MinPos = m_Owner.Pos + (BornPos - m_Owner.Pos).normalized * 2f;
                    m_AIStatus = E_AIStatus.MoveToBornPos;
                    return;
                }
            }
        }
 
        if (SearchActor)
        {
            m_AIStatus = E_AIStatus.MoveToTarget;
        }
 
    }
 
    private Vector3 m_MinPos;
 
    protected void MoveToBornPosMin()
    {
 
        //Debug.LogFormat(".{0}................执行: {1}", m_Owner.NpcConfig.NPCID, m_AIStatus.ToString());
        m_Owner.MoveToPosition(m_MinPos);
 
        // 判断是否离开出生点过远
        float _distSqrt = MathUtility.DistanceSqrtXZ(m_MinPos, m_Owner.Pos);
        if (_distSqrt < 0.5f)
        {
            m_AIStatus = E_AIStatus.Patrol;
        }
    }
 
    protected void MoveToTarget()
    {
        //Debug.LogFormat(".{0}................执行: {1}", m_Owner.NpcConfig.NPCID, m_AIStatus.ToString());
        GA_Hero _hero = PlayerDatas.Instance.hero;
 
        if (m_Owner.State != E_ActorState.AutoRun)
        {
            // 在目标周围随机找一个点进行移动
            Vector3 _randomPosition = _hero.Pos + Random.rotation * Vector3.forward * 1.5f;
            if (m_Owner.NpcConfig.NPCID == 1007)
            {
                _randomPosition = MathUtility.ForwardXZ(m_Owner.Pos, _hero.Pos);
                _randomPosition = _hero.Pos + _randomPosition * 1.5f;
            }
 
            m_Owner.MoveToPosition(_randomPosition);
        }
    }
 
    protected void BaseAttack()
    {
        if (Time.time - m_LastAttackTime < m_Owner.NpcConfig.AtkInterval * Constants.F_GAMMA)
        {
            return;
        }
 
        m_LastAttackTime = Time.time;
 
        Skill _skill = null;
 
        if (m_CanCastSkill)
        {
            for (int i = 0; i < m_SkillList.Count; ++i)
            {
                _skill = m_Owner.SkillMgr.Get(m_SkillList[i]);
 
                if (_skill.IsValid() == false)
                {
                    _skill = null;
                    continue;
                }
 
                break;
            }
        }
 
        if (_skill == null)
        {
            _skill = m_Owner.SkillMgr.Get(m_BaseAtkSkillID);
        }
 
        m_Owner.CastSkill(_skill.id);
 
    }
 
    public virtual void UnInit() { }
}