44 lines
1.3 KiB
GDScript
44 lines
1.3 KiB
GDScript
class_name StateMachine extends Node
|
|
|
|
@export var initialState: ChessGameState = null
|
|
|
|
@onready var state: ChessGameState = (func getInitialState() -> State:
|
|
return initialState if initialState != null else get_child(0)
|
|
).call()
|
|
|
|
@onready var parent = get_parent()
|
|
var previouseState = null
|
|
@onready var stateString: RichTextLabel = owner.get_node("StateLabel")
|
|
|
|
|
|
func _ready() -> void:
|
|
print("Ready Statemachine")
|
|
|
|
func start() -> void:
|
|
for stateNode: ChessGameState in find_children("*", "ChessGameState"):
|
|
stateNode.game = owner
|
|
stateNode.finished.connect(transitionToNextState)
|
|
|
|
await owner.ready
|
|
state.enter("")
|
|
|
|
func unhandledInput(event: InputEvent) -> void:
|
|
# print("StateMachine received input:", event)
|
|
state.handleInput(event)
|
|
|
|
|
|
func process(delta: float) -> void: state.update(delta)
|
|
|
|
|
|
|
|
|
|
func transitionToNextState(targetStatePath: String, data: Dictionary = {}) -> void:
|
|
print("TRANSITIONING TO: ", targetStatePath)
|
|
if not has_node(targetStatePath):
|
|
printerr(owner.name + ": Trying to transition to state " + targetStatePath + " but it does not exist.")
|
|
return
|
|
previouseState = state.name
|
|
state.exit()
|
|
state = get_node(targetStatePath)
|
|
stateString.text = targetStatePath;
|
|
state.enter(previouseState, data)
|