2025-03-20 18:22:40 +01:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
|
|
public EnemyStorageModule StorageModule { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
|
|
public PlayerDetectionModule PlayerDetection { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
|
|
public GenericDamageReceiver DamageReceiver { get; private set; }
|
|
|
|
|
|
|
2025-03-20 19:28:40 +01:00
|
|
|
|
[Export]
|
|
|
|
|
|
public NavigationMovementModule NavigationModule { get; private set; }
|
|
|
|
|
|
|
2025-03-20 18:22:40 +01:00
|
|
|
|
private bool _isPlayerInRange = false;
|
|
|
|
|
|
|
|
|
|
|
|
public override void EnterState()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.EnterState();
|
2025-03-20 19:28:40 +01:00
|
|
|
|
NavigationModule.Init(MainObject);
|
2025-03-21 10:30:44 +01:00
|
|
|
|
|
2025-03-20 18:22:40 +01:00
|
|
|
|
PlayerDetection.SetRange(StorageModule.Root.EnemyResource.PlayerDetectionRange);
|
|
|
|
|
|
|
2025-03-21 10:30:44 +01:00
|
|
|
|
//_isPlayerInRange = PlayerDetection.IsPlayerInRange(StorageModule.Root.EnemyResource.ViewRange);
|
|
|
|
|
|
//GD.Print($"Player In Range: {_isPlayerInRange}");
|
2025-03-20 18:22:40 +01:00
|
|
|
|
|
|
|
|
|
|
PlayerDetection.PlayerInRange += PlayerDetectionOnPlayerInRange;
|
|
|
|
|
|
|
|
|
|
|
|
PlayerDetection.PlayerOutOfRange += PlayerDetectionOnPlayerOutOfRange;
|
|
|
|
|
|
|
|
|
|
|
|
DamageReceiver.ChangeState(true);
|
|
|
|
|
|
|
|
|
|
|
|
DamageReceiver.HealthProvider.ResourceDepleted += HealthProviderOnResourceDepleted;
|
|
|
|
|
|
}
|
2025-03-21 10:30:44 +01:00
|
|
|
|
|
2025-03-20 18:22:40 +01:00
|
|
|
|
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);
|
2025-03-20 19:28:40 +01:00
|
|
|
|
NavigationModule.SetTarget(null);
|
2025-03-20 18:22:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void PlayerDetectionOnPlayerInRange()
|
|
|
|
|
|
{
|
2025-03-21 10:30:44 +01:00
|
|
|
|
//_isPlayerInRange = true;
|
2025-03-20 18:22:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void PhysicsProcessState(double delta)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.PhysicsProcessState(delta);
|
2025-03-21 10:30:44 +01:00
|
|
|
|
if (PlayerDetection.IsPlayerInRange(StorageModule.Root.EnemyResource.ViewRange) && PlayerDetection.IsPlayerInSight())
|
2025-03-20 18:22:40 +01:00
|
|
|
|
{
|
|
|
|
|
|
StateMachine.SetState(EnemyState.Shooting);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// if player is outside disengage range, change to idle (later on, search)
|
2025-06-11 15:28:26 +02:00
|
|
|
|
if (MainObject.GlobalPosition.DistanceTo(GameManager.Instance.PlayerPosition.Value) >=
|
2025-03-20 18:22:40 +01:00
|
|
|
|
StorageModule.Root.EnemyResource.PlayerDisengageRange)
|
|
|
|
|
|
{
|
|
|
|
|
|
StateMachine.SetState(EnemyState.Idle);
|
2025-03-20 19:28:40 +01:00
|
|
|
|
return;
|
2025-03-20 18:22:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Move towards last known position
|
|
|
|
|
|
if (PlayerDetection.LastKnownPlayerPosition.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
MoveTowardsPosition(PlayerDetection.LastKnownPlayerPosition.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-25 15:15:04 +01:00
|
|
|
|
NavigationModule.Move(StorageModule.EnemyData.MovementSpeed);
|
2025-03-21 15:16:50 +01:00
|
|
|
|
|
|
|
|
|
|
StorageModule.FacingDirection = MainObject.Velocity.SnapToCardinal().Normalized();
|
2025-05-08 10:23:00 +02:00
|
|
|
|
StorageModule.AimingDirection = StorageModule.FacingDirection;
|
2025-03-20 18:22:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void MoveTowardsPosition(Vector2 position)
|
|
|
|
|
|
{
|
2025-03-20 19:28:40 +01:00
|
|
|
|
NavigationModule.SetTarget(position);
|
2025-03-20 18:22:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void ProcessState(double delta)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.ProcessState(delta);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|