cirnogodot/Scripts/Components/FSM/Enemy/Alert.cs

114 lines
No EOL
3.5 KiB
C#

using Cirno.Scripts.Components.Actors;
using Cirno.Scripts.Enums;
using Godot;
namespace Cirno.Scripts.Components.FSM.Enemy;
public partial class Alert : EnemyStateBase
{
public override EnemyState StateId => EnemyState.Alert;
private EnemyStorageModule StorageModule { get; set; }
private PlayerDetectionModule PlayerDetection { get; set; }
private GenericDamageReceiver DamageReceiver { get; set; }
private NavigationMovementModule NavigationModule { get; set; }
private bool _isPlayerInRange = false;
public override void Init(IStateMachine<EnemyState, CharacterBody2D> machine)
{
base.Init(machine);
// Try to resolve modules via the state machine so the same modules can be reused without per-state exports
StorageModule ??= StateMachine.GetModule<EnemyStorageModule>();
PlayerDetection ??= StateMachine.GetModule<PlayerDetectionModule>();
DamageReceiver ??= StateMachine.GetModule<GenericDamageReceiver>();
NavigationModule ??= StateMachine.GetModule<NavigationMovementModule>();
}
public override void EnterState()
{
base.EnterState();
NavigationModule.Init(MainObject);
PlayerDetection.SetRange(StorageModule.Root.EnemyResource.PlayerDetectionRange);
PlayerDetection.PlayerInRange += PlayerDetectionOnPlayerInRange;
PlayerDetection.PlayerOutOfRange += PlayerDetectionOnPlayerOutOfRange;
DamageReceiver.ChangeState(true);
DamageReceiver.HealthProvider.ResourceDepleted += HealthProviderOnResourceDepleted;
}
private void HealthProviderOnResourceDepleted()
{
ChangeState(EnemyState.Dead);
}
private void PlayerDetectionOnPlayerOutOfRange()
{
_isPlayerInRange = false;
}
public override void ExitState()
{
base.ExitState();
PlayerDetection.PlayerInRange -= PlayerDetectionOnPlayerInRange;
PlayerDetection.PlayerOutOfRange -= PlayerDetectionOnPlayerOutOfRange;
DamageReceiver.HealthProvider.ResourceDepleted -= HealthProviderOnResourceDepleted;
DamageReceiver.ChangeState(false);
NavigationModule.SetTarget(null);
}
private void PlayerDetectionOnPlayerInRange()
{
//_isPlayerInRange = true;
}
public override void PhysicsProcessState(double delta)
{
base.PhysicsProcessState(delta);
if (PlayerDetection.IsPlayerInRange(StorageModule.Root.EnemyResource.ViewRange) &&
PlayerDetection.IsPlayerInSight())
{
StateMachine.SetState(EnemyState.Shooting);
return;
}
// if player is outside disengage range, change to idle (later on, search)
if (MainObject.GlobalPosition.DistanceTo(GameManager.Instance.PlayerPosition.Value) >=
StorageModule.Root.EnemyResource.PlayerDisengageRange)
{
StateMachine.SetState(EnemyState.Idle);
return;
}
// Move towards last known position
if (PlayerDetection.LastKnownPlayerPosition.HasValue)
{
MoveTowardsPosition(PlayerDetection.LastKnownPlayerPosition.Value);
}
NavigationModule.Move(StorageModule.EnemyData.MovementSpeed);
StorageModule.FacingDirection = MainObject.Velocity.SnapToCardinal().Normalized();
StorageModule.AimingDirection = StorageModule.FacingDirection;
}
private void MoveTowardsPosition(Vector2 position)
{
NavigationModule.SetTarget(position);
}
public override void ProcessState(double delta)
{
base.ProcessState(delta);
}
}