update AI and doc
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user