using System;
using ColdMint.scripts.character;
using Godot;
namespace ColdMint.scripts.stateMachine.StateProcessor;
///
/// AttackStateProcessor
/// 攻击状态处理器
///
public class AttackStateProcessor : StateProcessorTemplate
{
///
/// Consecutive attacks
/// 连续攻击次数
///
private int _consecutiveAttacks;
///
/// Max number of consecutive attacks
/// 最大连续攻击次数
///
///
///When this value is reached, the attack needs to be paused for a period of time.
///到达此值后需要暂停一段时间攻击。
///
public int MaxConsecutiveAttacks = 3;
///
/// How long to pause after the maximum number of attacks is reached
/// 到达最大攻击次数后停顿多长时间
///
public TimeSpan PauseTimeSpan = TimeSpan.FromSeconds(3);
///
/// Time of next attack
/// 下次攻击时间
///
private DateTime _nextAttackTime = DateTime.Now;
protected override void OnExecute(StateContext context, Node owner)
{
var now = DateTime.Now;
if (now < _nextAttackTime)
{
return;
}
if (owner is not AiCharacter aiCharacter)
{
return;
}
var enemy = aiCharacter.GetFirstEnemyInScoutArea();
if (enemy == null)
{
context.CurrentState = context.PreviousState;
return;
}
var canAttackEnemy = aiCharacter.GetFirstEnemyInAttackArea();
if (canAttackEnemy == null)
{
context.CurrentState = context.PreviousState;
return;
}
aiCharacter.DispladyPlaint();
aiCharacter.HideQuery();
if (aiCharacter.UseItem(enemy.GlobalPosition))
{
_consecutiveAttacks++;
if (_consecutiveAttacks >= MaxConsecutiveAttacks)
{
_consecutiveAttacks = 0;
_nextAttackTime = now + PauseTimeSpan;
}
}
aiCharacter.AimTheCurrentItemAtAPoint(enemy.GlobalPosition);
}
public override State State => State.Attack;
}