update AI and doc

This commit is contained in:
2026-04-16 15:51:10 +02:00
parent cf285153cf
commit a51e94d4d3
9 changed files with 66 additions and 43 deletions

View File

@@ -2,10 +2,10 @@ extends Node
class_name State
@export var state_id: String = ""
@export var state_id: StringName = &""
@warning_ignore("unused_signal")
signal transitioned(state, new_state_name)
signal transitioned(state: State, new_state_id: StringName)
func enter() -> void:
pass

View File

@@ -5,33 +5,48 @@ class_name StateMachine
@export var initial_state: State
var current_state: State
var states: Dictionary = {}
var enable: bool = true:
var states: Dictionary[StringName, State] = {}
var is_initialized: bool = false
var _enabled: bool = true
var enable: bool:
set(value):
enable = value
toggle_enable()
_enabled = value
_set_enabled(value)
get:
return _enabled
func toggle_enable() -> void:
if !enable:
return
if !states.is_empty():
func _ready() -> void:
_initialize_states()
_set_enabled(enable)
func _initialize_states() -> void:
if is_initialized:
return
for state in get_children():
if state is State:
states[state.state_id] = state
state.transitioned.connect(_on_state_transition)
if initial_state:
is_initialized = true
func _set_enabled(value: bool) -> void:
_enabled = value
if not _enabled:
return
_initialize_states()
if initial_state and current_state == null:
current_state = initial_state
current_state.enter()
func physics_process(delta) -> void:
if current_state and enable:
current_state.update(delta)
if current_state and _enabled:
current_state.physics_update(delta)
func _on_state_transition(state: State, new_state_id: String) -> void:
func _on_state_transition(state: State, new_state_id: StringName) -> void:
if state != current_state:
return