Created
March 23, 2024 23:42
-
-
Save lamarmarshall/f7cf4fb4c72b2292865fdb81a6205072 to your computer and use it in GitHub Desktop.
godot 4, C#, transition states
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Godot; | |
using System; | |
public partial class DashState : Node | |
{ | |
Player characterNode; | |
public override void _Ready() | |
{ | |
base._Ready(); | |
characterNode = GetOwner<Player>(); | |
characterNode.animationPlayerNode.Play(GameConstants.ANIM_DASH); | |
} | |
public override void _Notification(int what) | |
{ | |
base._Notification(what); | |
if(what == GameConstants.DASH_STATE) | |
{ | |
GD.Print("DashState Active"); | |
characterNode.animationPlayerNode.Play(GameConstants.ANIM_DASH); | |
} | |
} | |
public override void _PhysicsProcess(double delta) | |
{ | |
if(characterNode.animationPlayerNode.IsPlaying() == false) | |
{ | |
characterNode.stateMachineNode.SwitchStates<IdleState>(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
public partial class GameConstants { | |
// ANIMATION CONSTANTS | |
public const String ANIM_IDLE = "Idle"; | |
public const String ANIM_MOVE = "move"; | |
public const String ANIM_DASH = "Dash"; | |
} | |
public partial class GameConstants { | |
public const int IDLE_STATE = 5001; | |
public const int MOVE_STATE = 5002; | |
public const int DISABLE_NODE = 5003; | |
public const int DASH_STATE = 5005; | |
} | |
public partial class GameConstants { | |
// INPUT CONSTANTS | |
public const String INPUT_MOVE_LEFT = "MoveLeft"; | |
public const String INPUT_MOVE_RIGHT = "MoveRight"; | |
public const String INPUT_MOVE_FORWARD = "MoveForward"; | |
public const String INPUT_MOVE_BACKWARD = "MoveBackward"; | |
public const String INPUT_DASH = "Dash"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Godot; | |
using System; | |
public partial class IdleState : Node | |
{ | |
Player characterNode; | |
public override void _Ready() | |
{ | |
base._Ready(); | |
characterNode = GetOwner<Player>(); | |
SetPhysicsProcess(false); | |
} | |
public override void _PhysicsProcess(double delta) | |
{ | |
//GD.Print("IdleState Physics Process"); | |
base._PhysicsProcess(delta); | |
if(characterNode.direction == Vector2.Zero) | |
{ | |
if(characterNode.stateMachineNode.currentState.Name == "IdleState") | |
characterNode.stateMachineNode.SwitchStates<IdleState>(); | |
} | |
} | |
public override void _Notification(int what) | |
{ | |
base._Notification(what); | |
if (what == GameConstants.IDLE_STATE) | |
{ | |
characterNode.animationPlayerNode.Play(GameConstants.ANIM_IDLE); | |
SetPhysicsProcess(true); | |
} | |
else if (what == GameConstants.DISABLE_NODE) | |
{ | |
SetPhysicsProcess(false); | |
} | |
} | |
public override void _Input(InputEvent @event) | |
{ | |
base._Input(@event); | |
if(Input.IsActionJustPressed(GameConstants.INPUT_DASH)) | |
{ | |
characterNode.stateMachineNode.SwitchStates<DashState>(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Godot; | |
using System; | |
public partial class MoveState : Node | |
{ | |
Player characterNode; | |
public override void _Ready() | |
{ | |
base._Ready(); | |
characterNode = GetOwner<Player>(); | |
characterNode.animationPlayerNode.Play(GameConstants.ANIM_MOVE); | |
} | |
public override void _Notification(int what) | |
{ | |
base._Notification(what); | |
if(what == GameConstants.MOVE_STATE) | |
{ | |
Player characterNode = GetOwner<Player>(); | |
characterNode.animationPlayerNode.Play(GameConstants.ANIM_MOVE); | |
SetPhysicsProcess(true); | |
} | |
} | |
public override void _PhysicsProcess(double delta) | |
{ | |
base._PhysicsProcess(delta); | |
//GD.Print("Move State Physics Process"); | |
if(characterNode.direction != Vector2.Zero && | |
characterNode.stateMachineNode.currentState.Name != "DashState" ) | |
{ | |
characterNode.stateMachineNode.SwitchStates<MoveState>(); | |
} | |
else if(characterNode.stateMachineNode.currentState.Name == "DashState") | |
{ | |
characterNode.stateMachineNode.SwitchStates<DashState>(); | |
} | |
else | |
{ | |
characterNode.stateMachineNode.SwitchStates<IdleState>(); | |
} | |
} | |
public override void _Input(InputEvent @event) | |
{ | |
base._Input(@event); | |
if(Input.IsActionJustPressed(GameConstants.INPUT_DASH)) | |
{ | |
characterNode.stateMachineNode.SwitchStates<DashState>(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Godot; | |
using System; | |
public partial class StateMachine : Node | |
{ | |
[Export] public Node currentState; | |
[Export] public Node[] States; | |
public override void _Ready() | |
{ | |
currentState.Notification(GameConstants.IDLE_STATE); | |
} | |
public void SwitchStates<T>() | |
{ | |
Node newState = null; | |
foreach (Node state in States) | |
{ | |
if (state is T) | |
{ | |
newState = state; | |
} | |
} | |
if(newState == null) { return;} | |
currentState = newState; | |
if(currentState.Name == "IdleState") | |
{ | |
currentState.Notification(GameConstants.IDLE_STATE); | |
} | |
if(currentState.Name == "MoveState") | |
{ | |
currentState.Notification(GameConstants.MOVE_STATE); | |
} | |
if(currentState.Name == "DashState") | |
{ | |
GD.Print("DASH STATE"); | |
currentState.Notification(GameConstants.DASH_STATE); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment