This repository has been archived on 2026-05-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
instructions-clear/scripts/states/state_machine.gd
T
2025-02-02 01:08:44 +09:00

52 lines
1.0 KiB
GDScript

extends Node
@export var current_state: State
var subject: Node
func set_subject(new_subject: Node) -> void:
subject = new_subject
for state in get_children():
state.subject = subject
func _change_state(new_state: State) -> void:
if current_state:
#print("Exiting: %s" % current_state.name)
current_state.exit()
current_state = new_state
print("Entering: %s" % current_state.name)
current_state.enter()
func process_physics(delta: float) -> void:
if not current_state:
return
if not current_state.subject:
return
var new_state = current_state.process_physics(delta)
if new_state:
_change_state(new_state)
func process_input(event: InputEvent) -> void:
if not current_state:
return
if not current_state.subject:
return
var new_state = current_state.process_input(event)
if new_state:
_change_state(new_state)
func process_frame(delta: float) -> void:
if not current_state:
return
if not current_state.subject:
return
var new_state = current_state.process_frame(delta)
if new_state:
_change_state(new_state)