Compare commits

3 Commits

Author SHA1 Message Date
be5a146c9a added state machine 2025-09-17 18:53:24 +02:00
4af96b6ac3 finally adding something here 2025-09-17 18:29:38 +02:00
f6a954508a test commit 2025-09-17 18:15:24 +02:00
2 changed files with 46 additions and 0 deletions

45
scripts/state_machine.gd Normal file
View File

@@ -0,0 +1,45 @@
extends Node
@export var initial_state : State
var states : Dictionary = {}
var current_state : State = null
func _ready() -> void:
for child in get_children():
if child is State:
states[child.name.to_lower()] = child
child.Transitioned.connect(_on_state_transitioned)
if initial_state:
initial_state.enter()
current_state = initial_state
func _process(delta: float) -> void:
if current_state:
current_state.frame_update(delta)
func _physics_process(delta: float) -> void:
if current_state:
current_state.physics_update(delta)
func _on_state_transitioned(state: State, new_state_name: String) -> void:
if state != current_state:
push_warning("Attempting transition from nonactive state!")
return
change_state(new_state_name)
func change_state(new_state_name: String) -> void:
var key := new_state_name.to_lower()
var new_state: State = states.get(key)
if new_state == null:
push_error("New state not found: %s" % key)
return
elif current_state == new_state:
push_warning("Already in state: %s" % key)
return
if current_state:
current_state.exit()
new_state.enter()
current_state = new_state

1
secret_test_files/hi Normal file
View File

@@ -0,0 +1 @@
i am a balloon