otherworldly_simulation/tool/state_machine/state.gd
2024-10-25 15:41:39 +08:00

46 lines
1.4 KiB
GDScript

extends Node
##状态机根节点类
class_name StateMachine
##状态机进入时候的状态
@export var init_state:NodePath="stable_1"##进入时的状态
@export var owner_player:NodePath="stable_1"##拥有此状态机的节点路径
@onready var state_list=get_children() ##所拥有的状态节点的数组
@onready var state_now:State=get_node(init_state) ##当前的状态节点
@onready var player =get_node(owner_player) ##通过角色路径获取到的节点
func _ready():
set_process(false)
##启动状态机
func launch():
state_now=get_node(init_state)
state_now.enter_state(1)
set_process(true)
func _process(delta):
if state_now!=null:
state_now.update_state(delta)
func _physics_process(delta):
if state_now!=null:
state_now.update_state_phy(delta)
##判断n节点是否在状态机节点所拥有的状态中
func is_in_list(n)->bool:
if n in state_list:
return true
return false
##将状态节点切换为n,并调用对应的init stable方法和exit stable方法,传入s
func change_state(n:String,s):
var change_to=get_node(n)
if change_to&&is_in_list(change_to)&&state_now!=change_to:
state_now.exit_state()
state_now=change_to
state_now.enter_state(s)
##获取当前状态的状态名
func get_state_name()->String:
return self.state_now.state_name
func get_state(state_name:String):
return self.get_node(state_name)
func send_message(type:String,n=0):
if state_now!=null:
state_now.process_message(type,n)
pass