mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-13 01:05:55 +00:00
Enemy AI
This commit is contained in:
parent
383fc740df
commit
ede8f2028a
34 changed files with 1418 additions and 417 deletions
96
Scripts/Components/FSM/Enemy/3D/Alert.cs
Normal file
96
Scripts/Components/FSM/Enemy/3D/Alert.cs
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
using Cirno.Scripts.Enums;
|
||||
using Cirno.Scripts.Utils;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM.Enemy._3D;
|
||||
|
||||
public partial class Alert : EnemyStateBase3D
|
||||
{
|
||||
public override EnemyState StateId => EnemyState.Alert;
|
||||
|
||||
[Export] public EnemyStorage3D Storage { get; private set; }
|
||||
[Export] public PlayerDetection3D PlayerDetection { get; private set; }
|
||||
[Export] public NavigationProvider3D NavigationModule { get; private set; }
|
||||
[Export] public bool DebugEnabled { get; set; } = false;
|
||||
|
||||
private bool _isPlayerInRange = false;
|
||||
|
||||
public override void EnterState()
|
||||
{
|
||||
base.EnterState();
|
||||
// player detection
|
||||
// damage receiver will be a module
|
||||
|
||||
NavigationModule.Init(MainObject);
|
||||
|
||||
PlayerDetection.SetRange(Storage.Root.EnemyResource.PlayerDetectionRange);
|
||||
|
||||
PlayerDetection.PlayerInRange += PlayerDetectionOnPlayerInRange;
|
||||
|
||||
PlayerDetection.PlayerOutOfRange += PlayerDetectionOnPlayerOutOfRange;
|
||||
|
||||
GD.Print("Entered alert");
|
||||
}
|
||||
|
||||
private void PlayerDetectionOnPlayerOutOfRange()
|
||||
{
|
||||
_isPlayerInRange = false;
|
||||
}
|
||||
|
||||
public override void ExitState()
|
||||
{
|
||||
base.ExitState();
|
||||
PlayerDetection.PlayerInRange -= PlayerDetectionOnPlayerInRange;
|
||||
|
||||
PlayerDetection.PlayerOutOfRange -= PlayerDetectionOnPlayerOutOfRange;
|
||||
NavigationModule.SetTarget(null);
|
||||
// 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(Storage.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(GameController.Instance.PlayerPosition.Value) >=
|
||||
Storage.Root.EnemyResource.PlayerDisengageRange)
|
||||
{
|
||||
StateMachine.SetState(EnemyState.Idle);
|
||||
return;
|
||||
}
|
||||
|
||||
// Move towards last known position
|
||||
if (PlayerDetection.LastKnownPlayerPosition.HasValue)
|
||||
{
|
||||
MoveTowardsPosition(PlayerDetection.LastKnownPlayerPosition.Value);
|
||||
}
|
||||
|
||||
NavigationModule.Move(Storage.EnemyData.MovementSpeed);
|
||||
|
||||
//Storage.FacingDirection = MainObject.Velocity.SnapToCardinal().Normalized();
|
||||
Storage.FacingDirection = MainObject.Velocity.ToVector2().Normalized();
|
||||
Storage.AimingDirection = Storage.FacingDirection;
|
||||
}
|
||||
|
||||
private void MoveTowardsPosition(Vector3 position)
|
||||
{
|
||||
NavigationModule.SetTarget(position);
|
||||
}
|
||||
|
||||
public override void ProcessState(double delta)
|
||||
{
|
||||
base.ProcessState(delta);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue