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() { }
|
}
|