mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-09 22:15:55 +00:00
FSM Test
This commit is contained in:
parent
f1f40a91dd
commit
af46098aca
9 changed files with 414 additions and 145 deletions
|
|
@ -3,7 +3,7 @@ using Godot.Collections;
|
|||
|
||||
namespace Cirno.Scripts.Components.FSM;
|
||||
|
||||
public partial class ActorStateMachine : Node2D
|
||||
public partial class ActorStateMachine : CharacterBody2D
|
||||
{
|
||||
public Dictionary<int, State> States { get; private set; } = new();
|
||||
|
||||
|
|
|
|||
128
Scripts/Components/FSM/Player/Active.cs
Normal file
128
Scripts/Components/FSM/Player/Active.cs
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
using System;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM.Player;
|
||||
|
||||
public partial class Active : PlayerFSMState
|
||||
{
|
||||
private Vector2 _movementDirection { get; set; }
|
||||
private Vector2 _facingDirection { get; set; }
|
||||
|
||||
[Export] public Sprite2D HitboxSprite { get; set; }
|
||||
|
||||
[ExportGroup("Action Names")]
|
||||
[Export] private string _shootActionName = "shoot";
|
||||
[Export] private string _useActionName = "Use";
|
||||
[Export] private string _strafeActionName = "strafe";
|
||||
[Export] private string _nextWeaponActionName = "next_weapon";
|
||||
[Export] private string _previousWeaponActionName = "previous_weapon";
|
||||
|
||||
private bool _isStrafing { get; set; }
|
||||
|
||||
public override void EnterState()
|
||||
{
|
||||
// enable sprite
|
||||
// enable crosshair
|
||||
}
|
||||
|
||||
public override void ExitState()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void PhysicsProcessState(double delta)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void ProcessState(double delta)
|
||||
{
|
||||
SetAnimation();
|
||||
|
||||
_movementDirection = GetInput();
|
||||
_isStrafing = Input.IsActionPressed(_strafeActionName);
|
||||
|
||||
// Toggle visibility of the hitbox sprite based on strafing
|
||||
if (HitboxSprite != null)
|
||||
{
|
||||
HitboxSprite.Visible = _isStrafing;
|
||||
}
|
||||
|
||||
var rightStickInput = GetRightStickInput();
|
||||
|
||||
// Update Facing Direction
|
||||
if (!_isStrafing)
|
||||
{
|
||||
if (rightStickInput.Length() > 0.1f) // If the right stick is moved
|
||||
{
|
||||
_facingDirection = rightStickInput.Normalized();
|
||||
}
|
||||
else if (_movementDirection != Vector2.Zero) // Fall back to movement direction
|
||||
{
|
||||
_facingDirection = _movementDirection;
|
||||
}
|
||||
}
|
||||
|
||||
// HandleShoot();
|
||||
// FindInteractable();
|
||||
|
||||
// if (Input.IsActionJustPressed(_nextWeaponActionName))
|
||||
// {
|
||||
// NextWeapon();
|
||||
// }
|
||||
|
||||
// if (Input.IsActionJustPressed(_previousWeaponActionName))
|
||||
// {
|
||||
// PreviousWeapon();
|
||||
// }
|
||||
|
||||
// _crosshair.Position = CalculateCrosshairPosition();
|
||||
|
||||
}
|
||||
|
||||
private void SetAnimation()
|
||||
{
|
||||
|
||||
// if (Velocity.X == 0 && Velocity.Y == 0)
|
||||
// {
|
||||
// _animatedSprite.SpeedScale = 0;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// _animatedSprite.SpeedScale = 1;
|
||||
// }
|
||||
|
||||
// if (Velocity.X > 0)
|
||||
// {
|
||||
// _animatedSprite.Play("walk_right");
|
||||
// }
|
||||
// else if (Velocity.X < 0)
|
||||
// {
|
||||
// _animatedSprite.Play("walk_left");
|
||||
// }
|
||||
// else if (Velocity.Y > 0)
|
||||
// {
|
||||
// _animatedSprite.Play("walk_down");
|
||||
// }
|
||||
// else if (Velocity.Y < 0)
|
||||
// {
|
||||
// _animatedSprite.Play("walk_up");
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
||||
public Vector2 GetInput()
|
||||
{
|
||||
return Input.GetVector("left", "right", "up", "down");
|
||||
}
|
||||
|
||||
private Vector2 GetRightStickInput()
|
||||
{
|
||||
return new Vector2(
|
||||
Input.GetAxis("aim_left", "aim_right"),
|
||||
Input.GetAxis("aim_up", "aim_down")
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -5,18 +5,11 @@ namespace Cirno.Scripts.Components.FSM;
|
|||
public abstract partial class PlayerFSMState : State
|
||||
{
|
||||
[Export]
|
||||
public PlayerState State { get; private set; }
|
||||
public PlayerState State { get; private set; }
|
||||
public override int StateId => (int)State;
|
||||
|
||||
|
||||
protected void ChangeState(PlayerState newState)
|
||||
{
|
||||
_stateMachine.SetState((int)newState);
|
||||
}
|
||||
}
|
||||
|
||||
public enum PlayerState
|
||||
{
|
||||
Idle,
|
||||
Walking,
|
||||
Cutscene
|
||||
}
|
||||
12
Scripts/Components/FSM/PlayerStateMachine.cs
Normal file
12
Scripts/Components/FSM/PlayerStateMachine.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
using System;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM;
|
||||
|
||||
public partial class PlayerStateMachine : ActorStateMachine
|
||||
{
|
||||
public override void _Ready()
|
||||
{
|
||||
base._Ready();
|
||||
}
|
||||
}
|
||||
|
|
@ -5,28 +5,28 @@ namespace Cirno.Scripts.Components.FSM;
|
|||
|
||||
public abstract partial class State : Node2D
|
||||
{
|
||||
[Export]
|
||||
public Array<FSMStateModule> Modules { get; private set; } = new();
|
||||
|
||||
// [Export]
|
||||
// public Array<FSMStateModule> Modules { get; private set; } = new();
|
||||
|
||||
public virtual int StateId { get; }
|
||||
|
||||
protected ActorStateMachine _stateMachine;
|
||||
|
||||
|
||||
public virtual void Init(ActorStateMachine stateMachine)
|
||||
{
|
||||
_stateMachine = stateMachine;
|
||||
|
||||
foreach (var module in Modules)
|
||||
{
|
||||
//module.Init()
|
||||
}
|
||||
// foreach (var module in Modules)
|
||||
// {
|
||||
// //module.Init()
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
public abstract void EnterState();
|
||||
|
||||
public abstract void ExitState();
|
||||
|
||||
|
||||
public abstract void ProcessState(double delta);
|
||||
public abstract void PhysicsProcessState(double delta);
|
||||
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue