From c1aaa9492f933da9c7e1b511e5b200d89ff1ac43 Mon Sep 17 00:00:00 2001 From: yuki Date: Fri, 14 Nov 2025 15:29:06 -0300 Subject: [PATCH] create abstract state class --- scenes/classes/state.gd | 24 ++++++++++++++++++++++++ scenes/classes/state.gd.uid | 1 + scenes/classes/state.tscn | 6 ++++++ 3 files changed, 31 insertions(+) create mode 100644 scenes/classes/state.gd create mode 100644 scenes/classes/state.gd.uid create mode 100644 scenes/classes/state.tscn diff --git a/scenes/classes/state.gd b/scenes/classes/state.gd new file mode 100644 index 0000000..ba02391 --- /dev/null +++ b/scenes/classes/state.gd @@ -0,0 +1,24 @@ +## 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) + +## 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 diff --git a/scenes/classes/state.gd.uid b/scenes/classes/state.gd.uid new file mode 100644 index 0000000..9bce01e --- /dev/null +++ b/scenes/classes/state.gd.uid @@ -0,0 +1 @@ +uid://bpjerj832q2gf diff --git a/scenes/classes/state.tscn b/scenes/classes/state.tscn new file mode 100644 index 0000000..57a46e8 --- /dev/null +++ b/scenes/classes/state.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://kyeowhvglgj2"] + +[ext_resource type="Script" uid="uid://bpjerj832q2gf" path="res://scenes/classes/state.gd" id="1_m4c3p"] + +[node name="State" type="Node"] +script = ExtResource("1_m4c3p")