mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-18 09:23:46 +00:00
Add FSM components for player and enemy state management, including initialization and module resolution
This commit is contained in:
parent
18683c0680
commit
b6cc5a00e8
57 changed files with 526 additions and 506 deletions
|
|
@ -9,20 +9,30 @@ public partial class Shooting : EnemyStateBase3D
|
|||
{
|
||||
public override EnemyState StateId => EnemyState.Shooting;
|
||||
|
||||
[Export] public EnemyStorage3D Storage { get; private set; }
|
||||
private EnemyStorage3D Storage { get; set; }
|
||||
|
||||
[Export] public PlayerDetection3D PlayerDetection { get; private set; }
|
||||
private PlayerDetection3D PlayerDetection { get; set; }
|
||||
|
||||
[Export] public Weapon3D EquippedWeapon;
|
||||
[Export] public NavigationProvider3D NavigationModule { get; private set; }
|
||||
private NavigationProvider3D NavigationModule { get; set; }
|
||||
|
||||
[Export] public GravityProvider GravityProvider { get; private set; }
|
||||
private GravityProvider GravityProvider { get; set; }
|
||||
|
||||
private bool _isPlayerInRange = false;
|
||||
private Vector3? _currentStrafeTarget = null;
|
||||
private float _strafeSpeed => Storage.EnemyData.StrafeSpeed;
|
||||
private bool _isPlayerInRange;
|
||||
private Vector3? _currentStrafeTarget;
|
||||
private float StrafeSpeed => Storage.EnemyData.StrafeSpeed;
|
||||
|
||||
private double _responseTimer;
|
||||
|
||||
public override void Init(IStateMachine<EnemyState, CharacterBody3D> machine)
|
||||
{
|
||||
base.Init(machine);
|
||||
Storage ??= StateMachine.GetModule<EnemyStorage3D>();
|
||||
PlayerDetection ??= StateMachine.GetModule<PlayerDetection3D>();
|
||||
NavigationModule ??= StateMachine.GetModule<NavigationProvider3D>();
|
||||
GravityProvider ??= StateMachine.GetModule<GravityProvider>();
|
||||
}
|
||||
|
||||
private double _responseTimer = 0;
|
||||
public override void EnterState()
|
||||
{
|
||||
base.EnterState();
|
||||
|
|
@ -31,12 +41,13 @@ public partial class Shooting : EnemyStateBase3D
|
|||
|
||||
PlayerDetection.PlayerOutOfRange += PlayerDetectionOnPlayerOutOfRange;
|
||||
|
||||
//DamageReceiver.ChangeState(true);
|
||||
|
||||
//DamageReceiver.HealthProvider.ResourceDepleted += HealthProviderOnResourceDepleted;
|
||||
if (Storage is null || PlayerDetection is null) return;
|
||||
|
||||
EquippedWeapon.WeaponData = Storage.Root.EnemyResource.Weapon;
|
||||
EquippedWeapon.Init();
|
||||
if (EquippedWeapon != null)
|
||||
{
|
||||
EquippedWeapon.WeaponData = Storage.Root.EnemyResource.Weapon;
|
||||
EquippedWeapon.Init();
|
||||
}
|
||||
|
||||
_currentStrafeTarget = null;
|
||||
|
||||
|
|
@ -56,24 +67,22 @@ public partial class Shooting : EnemyStateBase3D
|
|||
|
||||
PlayerDetection.PlayerOutOfRange -= PlayerDetectionOnPlayerOutOfRange;
|
||||
|
||||
//DamageReceiver.HealthProvider.ResourceDepleted -= HealthProviderOnResourceDepleted;
|
||||
|
||||
_currentStrafeTarget = null;
|
||||
|
||||
//DamageReceiver.ChangeState(false);
|
||||
|
||||
}
|
||||
|
||||
public override void PhysicsProcessState(double delta)
|
||||
{
|
||||
base.PhysicsProcessState(delta);
|
||||
|
||||
if (Storage is null || PlayerDetection is null || NavigationModule is null || GravityProvider is null)
|
||||
return;
|
||||
|
||||
if (PlayerDetection.IsPlayerInRange(Storage.Root.EnemyResource.ViewRange) && PlayerDetection.IsPlayerInSight())
|
||||
{
|
||||
// SHOOT
|
||||
Shoot();
|
||||
|
||||
if (_strafeSpeed > 0)
|
||||
if (StrafeSpeed > 0)
|
||||
{
|
||||
// Check if a strafe position is needed
|
||||
if (!_currentStrafeTarget.HasValue || NavigationModule.IsNavigationFinished())
|
||||
|
|
@ -100,7 +109,7 @@ public partial class Shooting : EnemyStateBase3D
|
|||
if (_currentStrafeTarget.HasValue)
|
||||
{
|
||||
NavigationModule.SetTarget(_currentStrafeTarget.Value);
|
||||
NavigationModule.Move(_strafeSpeed);
|
||||
NavigationModule.Move(StrafeSpeed);
|
||||
}
|
||||
|
||||
// Calculate gravity
|
||||
|
|
@ -111,6 +120,8 @@ public partial class Shooting : EnemyStateBase3D
|
|||
|
||||
private Vector3? CalculateStrafePosition()
|
||||
{
|
||||
if (!PlayerDetection.LastKnownPlayerPosition.HasValue) return null;
|
||||
|
||||
var playerPos = PlayerDetection.LastKnownPlayerPosition.Value;
|
||||
var enemyPos = MainObject.GlobalPosition;
|
||||
|
||||
|
|
@ -149,16 +160,10 @@ public partial class Shooting : EnemyStateBase3D
|
|||
|
||||
private Vector2 GetShootDirection()
|
||||
{
|
||||
// if (Storage.EnemyData.PredictPlayer && PlayerDetection.LastKnowPlayerVelocity.HasValue)
|
||||
// {
|
||||
// var predictedDirection = MathFunctions.PredictInterceptPosition(MainObject.GlobalPosition,
|
||||
// PlayerDetection.LastKnownPlayerPosition.Value, PlayerDetection.LastKnowPlayerVelocity.Value,
|
||||
// EquippedWeapon.WeaponData.BulletData.BulletSpeed);
|
||||
// if (predictedDirection.HasValue) return (predictedDirection.Value - MainObject.GlobalPosition).Normalized();
|
||||
//
|
||||
// }
|
||||
|
||||
return ( PlayerDetection.LastKnownPlayerPosition.Value - MainObject.GlobalPosition).ToVector2().Normalized();
|
||||
if (!PlayerDetection.LastKnownPlayerPosition.HasValue)
|
||||
return Vector2.Zero;
|
||||
|
||||
return (PlayerDetection.LastKnownPlayerPosition.Value - MainObject.GlobalPosition).ToVector2().Normalized();
|
||||
}
|
||||
|
||||
private void Shoot()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue