From 5e46d7279058af19c8ad2c64d099f3c58f9f066d Mon Sep 17 00:00:00 2001 From: yuki Date: Sat, 22 Nov 2025 10:45:04 -0300 Subject: [PATCH] add basic state --- classes/state/state.gd | 58 ++++++++++++++++++++++++++++++++++++++ classes/state/state.gd.uid | 1 + 2 files changed, 59 insertions(+) create mode 100644 classes/state/state.gd create mode 100644 classes/state/state.gd.uid diff --git a/classes/state/state.gd b/classes/state/state.gd new file mode 100644 index 0000000..7f77f43 --- /dev/null +++ b/classes/state/state.gd @@ -0,0 +1,58 @@ +## Virtual base class for all states. +## Extend this class and override its methods to implement a state. +@abstract class_name State extends Node + +## Emitted when the state finishes and wants to transition to another state. +@warning_ignore("unused_signal") +signal finished(next_state_path: String, data: Dictionary) + + +## For 'ghost frame' update fix +var is_active: bool = false: + set(value): + is_active = value + get: + return is_active + + +## Called by the state machine when receiving unhandled input events. +@abstract func _handle_input(event: InputEvent) -> void + +## Called by the state machine on the engine's main loop tick. +@abstract func _state_update(delta: float) -> void + +## Called by the state machine on the engine's physics update tick. +@abstract func _state_physics_update(delta: float) -> void + +## Called by the state machine upon changing the active state. The `data` parameter +## is a dictionary with arbitrary data the state can use to initialize itself. +@abstract func _enter(previous_state_path: String, data: Dictionary = {}) -> void + +## Called by the state machine before changing the active state. Use this function +## to clean up the state. +@abstract func _exit() -> void + + +func handle_input(event: InputEvent) -> void: + if not is_active: return + _handle_input(event) + + +func state_update(delta: float) -> void: + if not is_active: return + _state_update(delta) + + +func state_physics_update(delta: float) -> void: + if not is_active: return + _state_physics_update(delta) + + +func enter(previous_state_path: String, data: Dictionary = {}) -> void: + is_active = true + _enter(previous_state_path, data) + + +func exit() -> void: + is_active = false + _exit() diff --git a/classes/state/state.gd.uid b/classes/state/state.gd.uid new file mode 100644 index 0000000..55d56a5 --- /dev/null +++ b/classes/state/state.gd.uid @@ -0,0 +1 @@ +uid://c204s7yw0rmoa