Started State Machine Implementation
This commit is contained in:
parent
cb164ef859
commit
376bf912c7
3 changed files with 81 additions and 21 deletions
|
|
@ -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
|
||||
|
|
|
|||
16
Systems/StateMachine/State.gd
Normal file
16
Systems/StateMachine/State.gd
Normal file
|
|
@ -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
|
||||
34
Systems/StateMachine/StateMachine.gd
Normal file
34
Systems/StateMachine/StateMachine.gd
Normal file
|
|
@ -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)
|
||||
|
||||
Loading…
Reference in a new issue