This commit is contained in:
Maddo 2025-02-28 13:50:52 +01:00
commit af46098aca
9 changed files with 414 additions and 145 deletions

View file

@ -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();

View 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")
);
}
}

View file

@ -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
}

View 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();
}
}

View file

@ -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);
}

View file

@ -64,6 +64,8 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
[Export] private GpuParticles2D _shieldParticles;
public Weapon EquippedWeapon { get; set; }
private PlayerState _state;
public Array<Weapon> EquippedWeapons { get; set; } = new Array<Weapon>();
public int CurrentWeaponIndex { get; set; } = 0;
@ -124,6 +126,8 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
// CurrentHealth = MaxHealth;
// CurrentShield = MaxShield;
_state = PlayerState.Active;
_animatedSprite = GetNode<AnimatedSprite2D>("./Smoothing2D/AnimatedSprite2D");
_crosshair = GetNode<Sprite2D>("./Smoothing2D/Crosshair");
@ -171,17 +175,22 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
switch (state)
{
case GameState.Menu:
break;
case GameState.Paused:
_canMove = false;
_state = PlayerState.Paused;
break;
case GameState.Playing:
_canMove = true;
_state = PlayerState.Active;
break;
case GameState.Dialogue:
_canMove = false;
_state = PlayerState.Paused;
break;
case GameState.Controlling:
_canMove = false;
_state = PlayerState.Controlling;
break;
}
}
@ -601,3 +610,11 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
}
}
public enum PlayerState
{
Active,
Paused,
Controlling,
Dead,
}