using UnityEngine;
|
|
public class AI_Npc_198 : SampleAI
|
{
|
public AI_Npc_198(GA_NpcClientFightNorm owner, Vector3 bornPosition) : base(owner, bornPosition)
|
{
|
m_SleepTime = 1f;
|
owner.OnAttacked += OnAttacked;
|
m_RunAwayStep = 0;
|
}
|
|
public override void UnInit()
|
{
|
base.UnInit();
|
|
m_Owner.OnAttacked -= OnAttacked;
|
}
|
|
private float m_LastBeAtkedTime = 0;
|
|
public void OnAttacked()
|
{
|
if (m_AIStatus != E_AIStatus.RunAway)
|
{
|
m_AIStatus = E_AIStatus.RunAway;
|
m_RunAwayStep = 0;
|
}
|
m_LastBeAtkedTime = Time.realtimeSinceStartup;
|
}
|
|
public override void Update()
|
{
|
// ai休眠时间
|
if (Time.realtimeSinceStartup - m_LastThinkTime > m_SleepTime)
|
{
|
_Thinking();
|
}
|
|
switch (m_AIStatus)
|
{
|
case E_AIStatus.Patrol:
|
_Patrol();
|
break;
|
case E_AIStatus.MoveToBornPos:
|
MoveToBornPos();
|
break;
|
case E_AIStatus.RunAway:
|
_RunAway();
|
break;
|
}
|
}
|
|
private byte m_RunAwayStep = 0;
|
private Vector3 m_Dest;
|
|
private void _RunAway()
|
{
|
switch (m_RunAwayStep)
|
{
|
case 0:
|
// 选择一个逃跑点
|
var _nextForawrd = Random.rotation * m_Owner.Forward;
|
var _randomDis = Random.Range(0, m_Owner.NpcConfig.Sight * .5f);
|
m_Dest = m_Owner.BornPos + _nextForawrd * _randomDis;
|
GActor.TryGetValidPos(m_Dest, ref m_Dest);
|
m_RunAwayStep = 1;
|
break;
|
case 1:
|
var _conmareDis = MathUtility.DistanceSqrtXZ(m_Owner.Pos, m_Dest);
|
if (_conmareDis > .5f)
|
{
|
m_Owner.MoveToPosition(m_Dest);
|
}
|
else
|
{
|
m_RunAwayStep = 0;
|
m_AIStatus = E_AIStatus.Idle;
|
}
|
break;
|
}
|
}
|
|
private void _Thinking()
|
{
|
float _compareDis;
|
var _hero = PlayerDatas.Instance.hero;
|
if (_hero == null || _hero.ActorInfo.serverDie)
|
{
|
// 主角不存在或者死亡了
|
// 在出生点附近
|
_compareDis = MathUtility.DistanceSqrtXZ(BornPos, m_Owner.Pos);
|
if (_compareDis < 4)
|
{
|
m_AIStatus = E_AIStatus.Patrol;
|
}
|
// 巡逻
|
// 不在出生点附近
|
else
|
{
|
// 回到出生点
|
m_AIStatus = E_AIStatus.MoveToBornPos;
|
}
|
}
|
else
|
{
|
_compareDis = MathUtility.DistanceSqrtXZ(m_Owner.Pos, BornPos);
|
if (_compareDis > m_KeepBornDistSqrt)
|
{
|
m_AIStatus = E_AIStatus.MoveToBornPos;
|
}
|
else
|
{
|
if (m_AIStatus != E_AIStatus.RunAway
|
&& m_AIStatus != E_AIStatus.Idle)
|
{
|
m_AIStatus = E_AIStatus.Patrol;
|
}
|
}
|
}
|
|
if (m_AIStatus == E_AIStatus.Idle)
|
{
|
if (Time.realtimeSinceStartup - m_LastBeAtkedTime > 5)
|
{
|
m_AIStatus = E_AIStatus.Patrol;
|
}
|
}
|
}
|
|
private void MoveToBornPos()
|
{
|
var _dis = MathUtility.DistanceSqrtXZ(m_Owner.Pos, BornPos);
|
if (_dis > 0.1f)
|
{
|
m_Owner.MoveToPosition(BornPos);
|
}
|
else
|
{
|
m_AIStatus = E_AIStatus.Patrol;
|
}
|
}
|
|
private byte patrolStep;
|
private float patrolWaitTime;
|
private float patrolWaitRandomTime;
|
private Vector3 randomPos;
|
private void _Patrol()
|
{
|
if (m_AIStatus == E_AIStatus.MoveToBornPos)
|
{
|
return;
|
}
|
|
switch (patrolStep)
|
{
|
case 0:
|
patrolWaitRandomTime = Random.Range(2f, 4f);
|
patrolWaitTime = Time.realtimeSinceStartup;
|
patrolStep = 1;
|
break;
|
case 1:
|
if (Time.realtimeSinceStartup - patrolWaitTime > patrolWaitRandomTime)
|
{
|
patrolStep = 2;
|
patrolWaitTime = Time.realtimeSinceStartup;
|
randomPos = BornPos + new Vector3(Random.Range(-2f, 2f), 0, Random.Range(-2f, 2f));
|
GActor.TryGetValidPos(randomPos, ref randomPos);
|
m_Owner.MoveToPosition(randomPos);
|
}
|
break;
|
case 2:
|
if (Time.realtimeSinceStartup - patrolWaitTime > 2)
|
{
|
patrolStep = 1;
|
}
|
break;
|
}
|
}
|
}
|