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

@@ -3,10 +3,13 @@ extends CharacterBody3D
class_name AIBase
@export var speed: float = 4.0
@export var enable_state_machine: bool = true:
var _enable_state_machine: bool = true
@export var enable_state_machine: bool:
set(value):
enable_state_machine = value
_enable_state_machine = value
toggle_enable_state_machine()
get:
return _enable_state_machine
@onready var patrol_radius_shape: CollisionShape3D = $%PatrolRadiusShape
@@ -19,10 +22,10 @@ func _ready() -> void:
toggle_enable_state_machine()
func toggle_enable_state_machine() -> void:
state_machine.enable = enable_state_machine
state_machine.enable = _enable_state_machine
func _physics_process(delta: float) -> void:
if !enable_state_machine:
if !_enable_state_machine:
return
state_machine.physics_process(delta)

View File

@@ -31,11 +31,11 @@ initial_state = NodePath("IdleState")
[node name="IdleState" type="Node" parent="StateMachine" unique_id=763668255]
script = ExtResource("3_26faq")
state_id = "idle"
state_id = &"idle"
[node name="PatrolState" type="Node" parent="StateMachine" unique_id=2054606629]
script = ExtResource("4_lim44")
state_id = "patrol"
state_id = &"patrol"
[node name="PatrolRadiusShape" type="CollisionShape3D" parent="." unique_id=1379515938]
unique_name_in_owner = true

View File

@@ -1,5 +1,7 @@
extends State
const PATROL_STATE_ID: StringName = &"patrol"
@export var wait_time: float = 3.0
var runtime_timer: Timer
@@ -21,4 +23,4 @@ func exit() -> void:
runtime_timer = null
func _on_timer_timeout() -> void:
transitioned.emit(self, "patrol")
transitioned.emit(self, PATROL_STATE_ID)

View File

@@ -1,10 +1,12 @@
extends State
const IDLE_STATE_ID: StringName = &"idle"
@onready var agent: AIBase = owner
func enter() -> void:
agent.navigate_to_random_point()
func update(_delta) -> void:
func physics_update(_delta) -> void:
if agent.nav_agent.is_navigation_finished():
transitioned.emit(self, "idle")
transitioned.emit(self, IDLE_STATE_ID)

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