Files
sidescroller-project/sidescroller/scripts/State Machine.gd
2025-06-11 17:54:55 +02:00

43 lines
860 B
GDScript

extends Node
@export var player: Node # In Godot im Inspector zuweisen!
@export var initial_state : State
var current_state : State
var states : Dictionary = {}
func _ready():
for child in get_children():
if child is State:
states[child.name.to_lower()] = child
child.Transitioned.connect(on_child_transitioned)
child.player = player
if initial_state:
initial_state.enter()
current_state = initial_state
func _process(delta):
if current_state:
current_state.update(delta)
func _physics_process(delta):
if current_state:
current_state.physics_update(delta)
func on_child_transitioned(state, new_state_name):
if state != current_state:
return
var new_state = states.get(new_state_name.to_lower())
if !new_state:
return
if current_state:
current_state.exit()
new_state.enter()
current_state = new_state