mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-13 20:55:55 +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
|
|
@ -1,5 +1,4 @@
|
|||
using System.Threading.Tasks;
|
||||
using Cirno.Scripts.Components.Actors;
|
||||
using Cirno.Scripts.Components.Actors;
|
||||
using Cirno.Scripts.Components.Actors._3D;
|
||||
using Cirno.Scripts.Utils;
|
||||
using Godot;
|
||||
|
|
@ -14,39 +13,34 @@ public partial class Active : BaseState<PlayerState, CharacterBody3D>
|
|||
[Export] private InputProvider _inputProvider;
|
||||
[Export] public PlayerAnimationProvider3D AnimationProvider { get; private set; }
|
||||
|
||||
[Export] public IsoPlayerStorageModule Storage { get; private set; }
|
||||
private IsoPlayerStorageModule Storage { get; set; }
|
||||
|
||||
[Export] public PlayerDamageReceiver3D DamageReceiver { get; private set; }
|
||||
|
||||
//[Export] public PlayerHitboxSpriteProvider3D HitboxSpriteProvider { get; private set; }
|
||||
|
||||
public override void Init(IStateMachine<PlayerState, CharacterBody3D> machine)
|
||||
{
|
||||
base.Init(machine);
|
||||
|
||||
//_hud = Hud.Instance;
|
||||
if (machine is IsoPlayerStateMachine sm)
|
||||
Storage = sm.Storage;
|
||||
|
||||
DamageReceiver.Death += () => { ChangeState(PlayerState.Dead); };
|
||||
|
||||
DamageReceiver.HealthDecreased += (value, newValue, maxValue) =>
|
||||
{
|
||||
AnimationProvider.Blink();
|
||||
};
|
||||
DamageReceiver.HealthDecreased += (_, _, _) =>
|
||||
{
|
||||
AnimationProvider.Blink();
|
||||
};
|
||||
|
||||
DamageReceiver.ShieldDecreased += (value, newValue, maxValue) =>
|
||||
{
|
||||
AnimationProvider.PlayShieldAnimation();
|
||||
|
||||
};
|
||||
DamageReceiver.ShieldDecreased += (_, _, _) =>
|
||||
{
|
||||
AnimationProvider.PlayShieldAnimation();
|
||||
};
|
||||
|
||||
DamageReceiver.Init(StateMachine);
|
||||
|
||||
DamageReceiver.RefillHealth();
|
||||
DamageReceiver.RefillShield();
|
||||
|
||||
//_activationProvider.Init(MainObject);
|
||||
|
||||
//_weaponProvider.Init(MainObject);
|
||||
}
|
||||
|
||||
public override void EnterState()
|
||||
|
|
@ -78,19 +72,10 @@ public partial class Active : BaseState<PlayerState, CharacterBody3D>
|
|||
DamageReceiver.Enabled = false;
|
||||
|
||||
_canOpenInventory = true;
|
||||
// _activationProvider.Enabled = false;
|
||||
// _activation_provider.Enabled = false;
|
||||
// _interactionController.Enabled = false;
|
||||
}
|
||||
|
||||
public override void PhysicsProcessState(double delta)
|
||||
{
|
||||
// Reset at start of frame
|
||||
//MainObject.Velocity = Vector2.Zero;
|
||||
|
||||
// Process modules
|
||||
base.PhysicsProcessState(delta);
|
||||
}
|
||||
|
||||
public override void ProcessState(double delta)
|
||||
{
|
||||
base.ProcessState(delta);
|
||||
|
|
@ -102,14 +87,13 @@ public partial class Active : BaseState<PlayerState, CharacterBody3D>
|
|||
}
|
||||
|
||||
private bool _canOpenInventory = true;
|
||||
private double _inventoryCooldown = 0;
|
||||
private double _inventoryCooldown;
|
||||
|
||||
private void HandleInputHotkeys(double delta)
|
||||
{
|
||||
if (_canOpenInventory && _inputProvider.GetInventoryJustPressed())
|
||||
{
|
||||
_canOpenInventory = false;
|
||||
|
||||
GameStateManager.SetState(GameState.Inventory);
|
||||
return;
|
||||
}
|
||||
|
|
@ -127,7 +111,6 @@ public partial class Active : BaseState<PlayerState, CharacterBody3D>
|
|||
if (_inputProvider.GetPauseJustPressed())
|
||||
{
|
||||
GameStateManager.Instance.Pause();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using Cirno.Scripts.Components.Actors._3D;
|
||||
using Cirno.Scripts.Components.FSM.Player;
|
||||
using Cirno.Scripts.Utils;
|
||||
using Godot;
|
||||
|
||||
|
|
@ -11,11 +10,13 @@ public partial class Cutscene : BaseState<PlayerState, CharacterBody3D>
|
|||
|
||||
[Export] public PlayerAnimationProvider3D AnimationProvider { get; set; }
|
||||
|
||||
[Export] public IsoPlayerStorageModule PlayerStorage { get; private set; }
|
||||
private IsoPlayerStorageModule PlayerStorage { get; set; }
|
||||
|
||||
public override void Init(IStateMachine<PlayerState, CharacterBody3D> machine)
|
||||
{
|
||||
base.Init(machine);
|
||||
if (machine is IsoPlayerStateMachine sm)
|
||||
PlayerStorage = sm.Storage;
|
||||
}
|
||||
|
||||
public override void EnterState()
|
||||
|
|
@ -40,15 +41,4 @@ public partial class Cutscene : BaseState<PlayerState, CharacterBody3D>
|
|||
AnimationProvider.SetAnimation(MainObject.Velocity.ToVector2());
|
||||
}
|
||||
|
||||
public override void PhysicsProcessState(double delta)
|
||||
{
|
||||
// Reset at start of frame
|
||||
//MainObject.Velocity = Vector2.Zero;
|
||||
|
||||
// Process modules
|
||||
base.PhysicsProcessState(delta);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -9,19 +9,34 @@ public partial class Dead : BaseState<PlayerState, CharacterBody3D>
|
|||
{
|
||||
public override PlayerState StateId => PlayerState.Dead;
|
||||
|
||||
[Export]
|
||||
private ActorResourceProvider _motivationProvider;
|
||||
[Export]
|
||||
private InputProvider _inputProvider;
|
||||
|
||||
[Export]
|
||||
private ActorResourceProvider _healthProvider;
|
||||
|
||||
[Export]
|
||||
private PlayerAnimationProvider3D _animationProvider;
|
||||
|
||||
private bool _isGameOver = false;
|
||||
|
||||
private bool _isGameOver;
|
||||
|
||||
public override void Init(IStateMachine<PlayerState, CharacterBody3D> machine)
|
||||
{
|
||||
base.Init(machine);
|
||||
// try to obtain common providers via modules/storage
|
||||
if (machine is IsoPlayerStateMachine sm)
|
||||
{
|
||||
var storage = sm.Storage;
|
||||
// store references if present on storage
|
||||
_motivationProvider = storage.Root.GetNodeOrNull<ActorResourceProvider>("MotivationProvider");
|
||||
_healthProvider = storage.Root.GetNodeOrNull<ActorResourceProvider>("HealthProvider");
|
||||
}
|
||||
// fallback to searching on the actor
|
||||
_motivationProvider ??= StateMachine.GetModule<ActorResourceProvider>();
|
||||
_healthProvider ??= StateMachine.GetModule<ActorResourceProvider>();
|
||||
|
||||
_animationProvider ??= StateMachine.GetModule<PlayerAnimationProvider3D>();
|
||||
|
||||
}
|
||||
|
||||
public override void EnterState()
|
||||
{
|
||||
base.EnterState();
|
||||
|
|
@ -50,10 +65,6 @@ public partial class Dead : BaseState<PlayerState, CharacterBody3D>
|
|||
Hud.Instance.HideTerminated();
|
||||
}
|
||||
|
||||
public override void PhysicsProcessState(double delta)
|
||||
{
|
||||
base.PhysicsProcessState(delta);
|
||||
}
|
||||
|
||||
public override void ProcessState(double delta)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -7,9 +7,14 @@ public partial class Drowning : BaseState<PlayerState, CharacterBody3D>
|
|||
{
|
||||
public override PlayerState StateId => PlayerState.Drowning;
|
||||
|
||||
[Export]
|
||||
private PlayerAnimationProvider3D _animationProvider;
|
||||
|
||||
public override void Init(IStateMachine<PlayerState, CharacterBody3D> machine)
|
||||
{
|
||||
base.Init(machine);
|
||||
_animationProvider ??= StateMachine.GetModule<PlayerAnimationProvider3D>();
|
||||
}
|
||||
|
||||
public override void EnterState()
|
||||
{
|
||||
_animationProvider.PlayDrowningAnimation();
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using System.Threading.Tasks;
|
||||
using Godot;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM._3DPlayer;
|
||||
|
||||
|
|
@ -9,9 +8,9 @@ public partial class Init : BaseState<PlayerState, CharacterBody3D>
|
|||
|
||||
public override void EnterState()
|
||||
{
|
||||
// _storageModule.FacingDirection = ((PlayerStateMachine)StateMachine).StartingDirection;
|
||||
// _animationProvider.PlayUnteleportAnimation();
|
||||
_ = AutoSwitchToStart();
|
||||
// Use Godot timer instead of Task.Delay to stay on main thread
|
||||
var timer = GetTree().CreateTimer(0.5f);
|
||||
timer.Timeout += () => StateMachine.SetState(PlayerState.Active);
|
||||
}
|
||||
|
||||
public override void ExitState()
|
||||
|
|
@ -28,10 +27,4 @@ public partial class Init : BaseState<PlayerState, CharacterBody3D>
|
|||
{
|
||||
|
||||
}
|
||||
|
||||
private async Task AutoSwitchToStart()
|
||||
{
|
||||
await Task.Delay(500);
|
||||
StateMachine.SetState(PlayerState.Active);
|
||||
}
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@ namespace Cirno.Scripts.Components.FSM._3DPlayer;
|
|||
|
||||
public partial class IsoMovementModule : ModuleBase<PlayerState, CharacterBody3D>
|
||||
{
|
||||
[Export] public IsoPlayerStorageModule PlayerStorage { get; private set; }
|
||||
private IsoPlayerStorageModule PlayerStorage { get; set; }
|
||||
[Export] private InputProvider _inputProvider;
|
||||
|
||||
[Export] public PlayerHitboxSpriteProvider3D HitboxSpriteProvider { get; private set; }
|
||||
|
|
@ -29,9 +29,6 @@ public partial class IsoMovementModule : ModuleBase<PlayerState, CharacterBody3D
|
|||
|
||||
public int MovementSpeed => _isStrafing ? StrafeSpeed : Speed;
|
||||
|
||||
private IStateMachine<PlayerState, CharacterBody3D> _stateMachine;
|
||||
private CharacterBody3D MainObject => _stateMachine.MainObject;
|
||||
|
||||
public override void EnterState(PlayerState state)
|
||||
{
|
||||
_accelerationPerSecond = Speed / Acceleration;
|
||||
|
|
@ -44,7 +41,8 @@ public partial class IsoMovementModule : ModuleBase<PlayerState, CharacterBody3D
|
|||
|
||||
public override void Init(IStateMachine<PlayerState, CharacterBody3D> machine)
|
||||
{
|
||||
_stateMachine = machine;
|
||||
base.Init(machine);
|
||||
PlayerStorage ??= StateMachine.GetModule<IsoPlayerStorageModule>();
|
||||
}
|
||||
|
||||
public override void Process(double delta)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
using Cirno.Scripts.Components.Actors;
|
||||
using Godot;
|
||||
using Cirno.Scripts.Resources.Loot;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM._3DPlayer;
|
||||
|
||||
public partial class IsoPlayerStorageModule : Node
|
||||
public partial class IsoPlayerStorageModule : Node, IFSMStorage, IActorStorage
|
||||
{
|
||||
[Export] public IsoPlayerFSMProxy Root { get; private set; }
|
||||
|
||||
|
|
@ -14,4 +16,7 @@ public partial class IsoPlayerStorageModule : Node
|
|||
public Vector2 FacingDirection { get; set; } = Vector2.Down;
|
||||
public Vector2 AimingDirection { get; set; } = Vector2.Down;
|
||||
public Vector2 MovementDirection { get; set; } = Vector2.Zero;
|
||||
|
||||
// Implement LootDrops for IFSMStorage (players may not drop loot but expose empty list)
|
||||
public IEnumerable<LootDrop> LootDrops => new LootDrop[0];
|
||||
}
|
||||
|
|
@ -4,9 +4,6 @@ namespace Cirno.Scripts.Components.FSM._3DPlayer;
|
|||
|
||||
public partial class PlayerAcidDeathModule : ModuleBase<PlayerState, CharacterBody3D>
|
||||
{
|
||||
private IStateMachine<PlayerState, CharacterBody3D> _stateMachine;
|
||||
private CharacterBody3D MainObject => _stateMachine.MainObject;
|
||||
|
||||
private bool _enabled = false;
|
||||
|
||||
public override void EnterState(PlayerState state)
|
||||
|
|
@ -21,7 +18,7 @@ public partial class PlayerAcidDeathModule : ModuleBase<PlayerState, CharacterBo
|
|||
|
||||
public override void Init(IStateMachine<PlayerState, CharacterBody3D> machine)
|
||||
{
|
||||
_stateMachine = machine;
|
||||
base.Init(machine);
|
||||
}
|
||||
|
||||
public override void Process(double delta)
|
||||
|
|
@ -38,6 +35,6 @@ public partial class PlayerAcidDeathModule : ModuleBase<PlayerState, CharacterBo
|
|||
{
|
||||
if (!_enabled) return;
|
||||
GD.Print("Oh no acid");
|
||||
_stateMachine.SetState(PlayerState.Dead);
|
||||
StateMachine.SetState(PlayerState.Dead);
|
||||
}
|
||||
}
|
||||
13
Scripts/Components/FSM/3DPlayer/PlayerState.cs
Normal file
13
Scripts/Components/FSM/3DPlayer/PlayerState.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
namespace Cirno.Scripts.Components.FSM;
|
||||
|
||||
public enum PlayerState
|
||||
{
|
||||
Init,
|
||||
Active,
|
||||
Cutscene,
|
||||
Teleporting,
|
||||
UnTeleporting,
|
||||
Controlling,
|
||||
Dead,
|
||||
Drowning,
|
||||
}
|
||||
1
Scripts/Components/FSM/3DPlayer/PlayerState.cs.uid
Normal file
1
Scripts/Components/FSM/3DPlayer/PlayerState.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://7thusbxv3r2n
|
||||
|
|
@ -8,10 +8,7 @@ public partial class PlayerWeaponModule3D : ModuleBase<PlayerState, CharacterBod
|
|||
[Export] public PlayerWeaponProvider3D WeaponProvider { get; private set; }
|
||||
|
||||
[Export] public InputProvider InputProvider { get; private set; }
|
||||
[Export] public IsoPlayerStorageModule Storage { get; private set; }
|
||||
|
||||
private IStateMachine<PlayerState, CharacterBody3D> _stateMachine;
|
||||
private CharacterBody3D MainObject => _stateMachine.MainObject;
|
||||
private IsoPlayerStorageModule Storage { get; set; }
|
||||
|
||||
public override void EnterState(PlayerState state)
|
||||
{
|
||||
|
|
@ -25,8 +22,8 @@ public partial class PlayerWeaponModule3D : ModuleBase<PlayerState, CharacterBod
|
|||
|
||||
public override void Init(IStateMachine<PlayerState, CharacterBody3D> machine)
|
||||
{
|
||||
_stateMachine = machine;
|
||||
|
||||
base.Init(machine);
|
||||
Storage ??= StateMachine.GetModule<IsoPlayerStorageModule>();
|
||||
WeaponProvider.Init(MainObject);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,4 @@
|
|||
using Cirno.Scripts.Components.Actors._3D;
|
||||
using Cirno.Scripts.Components.FSM.Player;
|
||||
using Cirno.Scripts.Utils;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM._3DPlayer;
|
||||
|
|
@ -9,38 +7,24 @@ public partial class Teleporting : BaseState<PlayerState, CharacterBody3D>
|
|||
{
|
||||
public override PlayerState StateId => PlayerState.Teleporting;
|
||||
|
||||
[Export] public PlayerAnimationProvider3D AnimationProvider { get; set; }
|
||||
private PlayerAnimationProvider3D AnimationProvider { get; set; }
|
||||
|
||||
public override void Init(IStateMachine<PlayerState, CharacterBody3D> machine)
|
||||
{
|
||||
base.Init(machine);
|
||||
AnimationProvider ??= StateMachine.GetModule<PlayerAnimationProvider3D>();
|
||||
}
|
||||
|
||||
public override void EnterState()
|
||||
{
|
||||
base.EnterState();
|
||||
AnimationProvider.PlayTeleportAnimation();
|
||||
AnimationProvider?.PlayTeleportAnimation();
|
||||
MainObject.Velocity = Vector3.Zero;
|
||||
}
|
||||
|
||||
public override void ExitState()
|
||||
{
|
||||
base.ExitState();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override void ProcessState(double delta)
|
||||
{
|
||||
base.ProcessState(delta);
|
||||
|
||||
}
|
||||
|
||||
public override void PhysicsProcessState(double delta)
|
||||
{
|
||||
// Process modules
|
||||
base.PhysicsProcessState(delta);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,6 +1,4 @@
|
|||
using Cirno.Scripts.Components.Actors._3D;
|
||||
using Cirno.Scripts.Components.FSM.Player;
|
||||
using Cirno.Scripts.Utils;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM._3DPlayer;
|
||||
|
|
@ -9,37 +7,17 @@ public partial class UnTeleporting : BaseState<PlayerState, CharacterBody3D>
|
|||
{
|
||||
public override PlayerState StateId => PlayerState.UnTeleporting;
|
||||
|
||||
[Export] public PlayerAnimationProvider3D AnimationProvider { get; set; }
|
||||
private PlayerAnimationProvider3D AnimationProvider { get; set; }
|
||||
|
||||
public override void Init(IStateMachine<PlayerState, CharacterBody3D> machine)
|
||||
{
|
||||
base.Init(machine);
|
||||
AnimationProvider ??= StateMachine.GetModule<PlayerAnimationProvider3D>();
|
||||
}
|
||||
|
||||
public override void EnterState()
|
||||
{
|
||||
base.EnterState();
|
||||
AnimationProvider.PlayUnteleportAnimation();
|
||||
AnimationProvider?.PlayUnteleportAnimation();
|
||||
}
|
||||
|
||||
public override void ExitState()
|
||||
{
|
||||
base.ExitState();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override void ProcessState(double delta)
|
||||
{
|
||||
base.ProcessState(delta);
|
||||
|
||||
}
|
||||
|
||||
public override void PhysicsProcessState(double delta)
|
||||
{
|
||||
// Process modules
|
||||
base.PhysicsProcessState(delta);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue