godot-psd-training/sceen/people/StateMachine/StateMachine.gd
2024-06-17 13:15:00 +08:00

31 lines
734 B
GDScript

extends Node3D
## 角色状态机
class_name StateMachine
## 默认状态
@export var current_state: StateBase
func _ready() -> void:
##StateMachine中的所有子节点
for child in get_children():
if child is StateBase:
child.state_machine = self
await get_parent().ready
current_state.enter()
func _process(delta: float) -> void:
current_state.process_update(delta)
## 修改状态 对每一种状态拆分编写
func change_state(target_state_name: String) -> void:
var target_state = get_node_or_null(target_state_name)
if target_state == null:
printerr("状态传入错误")
return
##退出当前状态
current_state.exit()
##切换为目标状态
current_state = target_state
current_state.enter()