Refactored states

This commit is contained in:
Bobby Lucero 2025-04-25 01:04:49 -04:00
parent 7bc6454ce1
commit 740c7c8ea2
8 changed files with 81 additions and 77 deletions

View File

@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ANode_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FSourcesCache_003Ff1d69ec2da76ccf9bc8a75c8e0fdca9a7ba1adf8c8c9d5047e2fa5991c02eca_003FNode_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>

View File

@ -192,3 +192,7 @@ script = ExtResource("33_4yec0")
[node name="DashState" type="Node" parent="StateMachine"]
script = ExtResource("41_2qsty")
[node name="Timer" type="Timer" parent="StateMachine/DashState"]
wait_time = 0.4
one_shot = true

View File

@ -12,12 +12,6 @@ public partial class Player : CharacterBody3D
public override void _Ready()
{
}
public override void _PhysicsProcess(double delta)
{
Velocity = new Vector3(Direction.X, 0, Direction.Y);
Velocity *= 5;
MoveAndSlide();
}
public override void _Input(InputEvent @event)
{
@ -34,4 +28,8 @@ public partial class Player : CharacterBody3D
}
}
}

View File

@ -2,41 +2,41 @@ using Godot;
using System;
using DungeonRPG.Scripts.General;
public partial class PlayerDashState : Node
public partial class PlayerDashState : PlayerState
{
private Player characterNode;
private Timer timer;
[Export] private float speed = 10;
public override void _Ready()
{
characterNode = GetOwner<Player>();
SetPhysicsProcess(false);
SetProcessInput(false);
base._Ready();
timer = GetNode<Timer>("Timer");
timer.Timeout += HandleDashTimeout;
}
public override void _PhysicsProcess(double delta)
{
characterNode.MoveAndSlide();
}
public override void _Notification(int what)
{
base._Notification(what);
if (what == GameConstants.STATE_NOTIFICATION_ENABLE)
protected override void EnterState()
{
characterNode.AnimatedSprite.Play(GameConstants.ANIM_DASH);
SetPhysicsProcess(true);
SetProcessInput(true);
characterNode.Velocity = new Vector3(characterNode.Direction.X, 0, characterNode.Direction.Y);
}
else if (what == GameConstants.STATE_NOTIFICATION_DISABLE)
if (characterNode.Velocity == Vector3.Zero)
{
SetPhysicsProcess(false);
SetProcessInput(false);
characterNode.Velocity = characterNode.AnimatedSprite.FlipH ? Vector3.Left : Vector3.Right;
}
characterNode.Velocity *= speed;
timer.Start();
}
private void HandleDashTimeout()
{
characterNode.StateMachine.SwitchState<PlayerIdleState>();
characterNode.Velocity = Vector3.Zero;
}
}

View File

@ -2,15 +2,9 @@ using Godot;
using System;
using DungeonRPG.Scripts.General;
public partial class PlayerIdleState : Node
public partial class PlayerIdleState : PlayerState
{
private Player characterNode;
public override void _Ready()
{
characterNode = GetOwner<Player>();
SetPhysicsProcess(false);
SetProcessInput(false);
}
public override void _PhysicsProcess(double delta)
{
@ -28,21 +22,10 @@ public partial class PlayerIdleState : Node
}
}
public override void _Notification(int what)
protected override void EnterState()
{
base._Notification(what);
base.EnterState();
if (what == GameConstants.STATE_NOTIFICATION_ENABLE)
{
characterNode.AnimatedSprite.Play(GameConstants.ANIM_IDLE);
SetPhysicsProcess(true);
SetProcessInput(true);
}
else if (what == GameConstants.STATE_NOTIFICATION_DISABLE)
{
SetPhysicsProcess(false);
SetProcessInput(false);
}
}
}

View File

@ -2,24 +2,19 @@ using Godot;
using System;
using DungeonRPG.Scripts.General;
public partial class PlayerMoveState : Node
public partial class PlayerMoveState : PlayerState
{
private Player characterNode;
public override void _Ready()
{
characterNode = GetOwner<Player>();
SetPhysicsProcess(false);
SetProcessInput(false);
}
public override void _PhysicsProcess(double delta)
{
if (characterNode.Direction == Vector2.Zero)
{
characterNode.StateMachine.SwitchState<PlayerIdleState>();
return;
}
characterNode.Velocity = new Vector3(characterNode.Direction.X, 0, characterNode.Direction.Y);
characterNode.Velocity *= 5;
characterNode.MoveAndSlide();
}
public override void _Input(InputEvent @event)
@ -29,23 +24,8 @@ public partial class PlayerMoveState : Node
characterNode.StateMachine.SwitchState<PlayerDashState>();
}
}
public override void _Notification(int what)
{
base._Notification(what);
if (what == GameConstants.STATE_NOTIFICATION_ENABLE)
protected override void EnterState()
{
characterNode.AnimatedSprite.Play(GameConstants.ANIM_MOVE);
SetPhysicsProcess(true);
SetProcessInput(true);
}
else if (what == GameConstants.STATE_NOTIFICATION_DISABLE)
{
SetPhysicsProcess(false);
SetProcessInput(false);
}
}
}

View File

@ -0,0 +1,36 @@
using DungeonRPG.Scripts.General;
using Godot;
public partial class PlayerState : Node
{
protected Player characterNode;
public override void _Ready()
{
characterNode = GetOwner<Player>();
SetPhysicsProcess(false);
SetProcessInput(false);
}
public override void _Notification(int what)
{
base._Notification(what);
if (what == GameConstants.STATE_NOTIFICATION_ENABLE)
{
SetPhysicsProcess(true);
SetProcessInput(true);
EnterState();
}
else if (what == GameConstants.STATE_NOTIFICATION_DISABLE)
{
SetPhysicsProcess(false);
SetProcessInput(false);
}
}
protected virtual void EnterState()
{ }
}

View File

@ -0,0 +1 @@
uid://c0jjjfwcbxi2s