2024-06-30 23:55:58 +00:00
using ColdMint.scripts.character ;
using ColdMint.scripts.debug ;
2024-07-17 12:04:13 +00:00
using ColdMint.scripts.weapon ;
2024-06-30 23:55:58 +00:00
using Godot ;
namespace ColdMint.scripts.stateMachine.StateProcessor ;
/// <summary>
/// <para>PatrolStateProcessor</para>
/// <para>巡逻状态处理器</para>
/// </summary>
public class PatrolStateProcessor : StateProcessorTemplate
{
2024-07-02 15:16:04 +00:00
public Vector2 [ ] ? Points { get ; set ; }
2024-07-17 12:04:13 +00:00
2024-07-06 14:55:07 +00:00
/// <summary>
/// <para>Whether to guard the origin</para>
/// <para>是否需要守护原点</para>
/// </summary>
/// <remarks>
///<para>When empty by default, PatrolStateProcessor will take the first point where the character touches the ground as the origin. This property handles whether or not the origin is "guarded" when the character is attracted to another character, such as chasing an enemy, and switches back to patrol mode. If set to true, the role tries to return to the origin, otherwise, a new origin is assigned.</para>
///<para>默认清空下, PatrolStateProcessor会将角色与地面接触的第一个位置当作原点。这个属性用来处理当角色被其他角色所吸引, ( 例如追击敌人) 转换回巡逻模式, 是否“守护”原点。如果设置为true, 则角色会尝试返回原点, 否则, 将分配新的原点。</para>
/// </remarks>
public bool Guard { get ; set ; }
2024-07-02 15:16:04 +00:00
private int _index ;
private Vector2 ? _originPosition ;
2024-07-06 14:55:07 +00:00
public override void Enter ( StateContext context )
{
if ( ! Guard )
{
//Reset the origin when transitioning to patrol.
//转换到巡逻状态时重置原点。
_originPosition = null ;
}
}
2024-06-30 23:55:58 +00:00
protected override void OnExecute ( StateContext context , Node owner )
{
if ( owner is not AiCharacter aiCharacter )
{
return ;
}
2024-07-02 15:16:04 +00:00
2024-07-07 15:10:25 +00:00
if ( aiCharacter . ScoutEnemyDetected ( ) )
2024-07-06 14:55:07 +00:00
{
2024-07-10 15:23:04 +00:00
//Once the enemy enters the reconnaissance range, we first see if the character has a weapon, if so, then pursue the enemy, otherwise, the patrol state changes to looking for weapons.
//发现敌人进入侦察范围后,我们先看角色是否持有武器,如果有,那么追击敌人,否则,巡逻状态转换为寻找武器。
2024-07-17 12:04:13 +00:00
if ( aiCharacter . CurrentItem is WeaponTemplate weaponTemplate )
{
context . CurrentState = State . Chase ;
}
else
{
context . CurrentState = State . LookForWeapon ;
}
2024-07-10 15:23:04 +00:00
2024-07-06 14:55:07 +00:00
LogCat . Log ( "patrol_enemy_detected" , label : LogCat . LogLabel . PatrolStateProcessor ) ;
return ;
}
2024-07-02 15:16:04 +00:00
if ( Points = = null | | Points . Length = = 0 )
{
2024-07-07 15:10:25 +00:00
//There are no patrol points.
//没有巡逻点。
2024-07-02 15:16:04 +00:00
LogCat . LogError ( "no_points" , label : LogCat . LogLabel . PatrolStateProcessor ) ;
return ;
}
if ( _originPosition = = null )
{
2024-07-07 15:10:25 +00:00
//If there is no origin, then wait for the character to collide with the ground to set it as the origin.
//如果没有原点,那么等待角色与地面碰撞时将其设置为原点。
2024-07-02 15:16:04 +00:00
if ( ! aiCharacter . IsOnFloor ( ) )
{
LogCat . LogWarning ( "patrol_not_on_floor" , LogCat . LogLabel . PatrolStateProcessor ) ;
return ;
}
_originPosition = aiCharacter . GlobalPosition ;
LogCat . LogWithFormat ( "patrol_origin_position" , LogCat . LogLabel . PatrolStateProcessor ,
2024-07-17 12:04:13 +00:00
LogCat . UploadFormat , _originPosition ) ;
2024-07-02 15:16:04 +00:00
}
var point = _originPosition + Points [ _index ] ;
var distance = aiCharacter . GlobalPosition . DistanceTo ( point . Value ) ;
2024-07-07 15:10:25 +00:00
if ( distance < 5 )
2024-07-02 15:16:04 +00:00
{
2024-07-07 15:10:25 +00:00
//No need to actually come to the patrol point, we just need a distance to get close.
//无需真正的来到巡逻点,我们只需要一个距离接近了就可以了。
2024-07-17 12:04:13 +00:00
LogCat . LogWithFormat ( "patrol_arrival_point" , LogCat . LogLabel . PatrolStateProcessor , LogCat . UploadFormat ,
point ) ;
2024-07-02 15:16:04 +00:00
_index + + ;
if ( _index > = Points . Length )
{
_index = 0 ;
}
}
else
{
2024-07-17 12:04:13 +00:00
LogCat . LogWithFormat ( "patrol_to_next_point" , label : LogCat . LogLabel . PatrolStateProcessor ,
LogCat . UploadFormat , point ,
2024-07-02 15:16:04 +00:00
aiCharacter . GlobalPosition , Points [ _index ] ,
distance ) ;
aiCharacter . SetTargetPosition ( point . Value ) ;
}
2024-06-30 23:55:58 +00:00
}
2024-07-02 15:16:04 +00:00
2024-06-30 23:55:58 +00:00
public override State State = > State . Patrol ;
}