using System;
using ColdMint.scripts.character;
using Godot;
namespace ColdMint.scripts.stateMachine.StateProcessor;
///
/// Escape state processor
/// 逃跑状态处理器
///
public class FleeProcessor : StateProcessorTemplate
{
///
/// When to return to enemy free status
/// 何时恢复到没有敌人的状态
///
private DateTime? _endTime;
///
/// When away from the enemy, how long to return to normal state
/// 当远离敌人后,多长时间恢复到正常状态
///
public TimeSpan RecoveryTimeSpan { get; set; } = TimeSpan.FromMilliseconds(300);
protected override void OnExecute(StateContext context, Node owner)
{
if (owner is not AiCharacter aiCharacter)
{
return;
}
var enemy = aiCharacter.GetFirstEnemyInScoutArea();
if (enemy == null)
{
//There are no enemies left.
//没有敌人了
if (_endTime == null)
{
_endTime = DateTime.Now + RecoveryTimeSpan;
return;
}
if (DateTime.Now > _endTime)
{
//Recovery time, end status.
//恢复时间,结束状态。
context.CurrentState = State.Patrol;
}
}
else
{
//Enemies
//有敌人
//To calculate the escape direction, the vector of the enemy pointing to the character is the escape direction.
//计算逃跑方向,敌人指向角色的向量为逃跑方向。
_endTime = null;
var direction = aiCharacter.GlobalPosition - enemy.GlobalPosition;
aiCharacter.SetTargetPosition(aiCharacter.GlobalPosition + direction);
}
}
public override State State => State.Flee;
}