From 376bf912c7f077b894f0a947597e1a8248fd02b0 Mon Sep 17 00:00:00 2001 From: 2ManyProjects Date: Sat, 25 Jan 2025 02:03:19 -0600 Subject: [PATCH] Started State Machine Implementation --- Diagrams/Game Machine.mmd | 52 +++++++++++++++++----------- Systems/StateMachine/State.gd | 16 +++++++++ Systems/StateMachine/StateMachine.gd | 34 ++++++++++++++++++ 3 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 Systems/StateMachine/State.gd create mode 100644 Systems/StateMachine/StateMachine.gd diff --git a/Diagrams/Game Machine.mmd b/Diagrams/Game Machine.mmd index d358912..8071ab6 100644 --- a/Diagrams/Game Machine.mmd +++ b/Diagrams/Game Machine.mmd @@ -5,27 +5,37 @@ config: stateDiagram [*] --> GameSetup GameSetup --> WhiteTurn - state TurnPhases { - WhiteTurn --> HandSetup - BlackTurn --> HandSetup - HandSetup --> ResolvePersistentEffects : Draw Cards - ResolvePersistentEffects --> ApplyTileEffects - ApplyTileEffects --> AttachCards - AttachCards --> ApplyCardEffects - ApplyCardEffects --> Movement - Movement --> EvaluatePosition - state EvaluatePosition { - [*] --> CheckStatus - CheckStatus --> Checkmate : In Checkmate - CheckStatus --> Draw : In Draw - CheckStatus --> ValidMove : Game Continues - ValidMove --> SpecialMoves - SpecialMoves --> [*] - } - EvaluatePosition --> WhiteTurn : White's Turn - EvaluatePosition --> BlackTurn : Black's Turn - EvaluatePosition --> RoundEnd : Game Over - } + state ChessGame { + WhiteTurn --> HandSetup + BlackTurn --> HandSetup + + HandSetup --> DrawPhase + DrawPhase --> ResolvePersistentEffects : Draw/Discard + + ResolvePersistentEffects --> ApplyTileEffects : Update Durations + ApplyTileEffects --> PreMovePhase + + PreMovePhase --> AttachCards : Play Cards + AttachCards --> ApplyCardEffects + ApplyCardEffects --> Movement + + Movement --> PostMovePhase + PostMovePhase --> EvaluatePosition : Resolve Move Effects + + state EvaluatePosition { + [*] --> CheckStatus + CheckStatus --> Checkmate : In Checkmate + CheckStatus --> Draw : In Draw + CheckStatus --> ValidMove : Game Continues + ValidMove --> SpecialMoves : En Passant/Castle/Promote + SpecialMoves --> [*] + } + + EvaluatePosition --> CleanupPhase + CleanupPhase --> WhiteTurn : White's Turn + CleanupPhase --> BlackTurn : Black's Turn + CleanupPhase --> RoundEnd : Game Over + } RoundEnd --> ShopPhase ShopPhase --> GameSetup : Next Round RoundEnd --> [*] : End Game diff --git a/Systems/StateMachine/State.gd b/Systems/StateMachine/State.gd new file mode 100644 index 0000000..5223cfc --- /dev/null +++ b/Systems/StateMachine/State.gd @@ -0,0 +1,16 @@ +class_name State extends Node + + +signal finished(nextStatePath: String, data: Dictionary) + +func handleInput(_event: InputEvent) -> void: + pass + +func update(_delta: float) -> void: + pass + +func enter(previousStatePath: String, data := {}) -> void: + pass + +func exit() -> void: + pass \ No newline at end of file diff --git a/Systems/StateMachine/StateMachine.gd b/Systems/StateMachine/StateMachine.gd new file mode 100644 index 0000000..48e40b5 --- /dev/null +++ b/Systems/StateMachine/StateMachine.gd @@ -0,0 +1,34 @@ +class_name StateMachine extends Node + +@export var initialState: State = null + +@onready var state: State = (func getInitialState() -> State: + return initialState if initialState != null else get_child(0) +).call() + + +func ready() -> void: + for stateNode: State in find_children("*", "State"): + stateNode.finished.connect(transitionToNextState) + + await owner.ready + state.enter("") + + +func unhandledInput(event: InputEvent) -> void: + state.handleInput(event) + + +func process(delta: float) -> void: + state.update(delta) + + +func transitionToNextState(targetStatePath: String, data: Dictionary = {}) -> void: + if not has_node(targetStatePath): + printerr(owner.name + ": Trying to transition to state " + targetStatePath + " but it does not exist.") + return + var previousStatePath := state.name + state.exit() + state = get_node(targetStatePath) + state.enter(previousStatePath, data) +