add audio_manager and code refactoring
This commit is contained in:
20
core/ai/framework/state.gd
Normal file
20
core/ai/framework/state.gd
Normal file
@@ -0,0 +1,20 @@
|
||||
extends Node
|
||||
|
||||
class_name State
|
||||
|
||||
@export var state_id: String = ""
|
||||
|
||||
@warning_ignore("unused_signal")
|
||||
signal transitioned(state, new_state_name)
|
||||
|
||||
func enter() -> void:
|
||||
pass
|
||||
|
||||
func exit() -> void:
|
||||
pass
|
||||
|
||||
func update(_delta) -> void:
|
||||
pass
|
||||
|
||||
func physics_update(_delta) -> void:
|
||||
pass
|
||||
1
core/ai/framework/state.gd.uid
Normal file
1
core/ai/framework/state.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dpso7abi5ftyw
|
||||
46
core/ai/framework/state_machine.gd
Normal file
46
core/ai/framework/state_machine.gd
Normal file
@@ -0,0 +1,46 @@
|
||||
extends Node
|
||||
|
||||
class_name StateMachine
|
||||
|
||||
@export var initial_state: State
|
||||
|
||||
var current_state: State
|
||||
var states: Dictionary = {}
|
||||
var enable: bool = true:
|
||||
set(value):
|
||||
enable = value
|
||||
toggle_enable()
|
||||
|
||||
func toggle_enable() -> void:
|
||||
if !enable:
|
||||
return
|
||||
|
||||
if !states.is_empty():
|
||||
return
|
||||
|
||||
for state in get_children():
|
||||
if state is State:
|
||||
states[state.state_id] = state
|
||||
state.transitioned.connect(_on_state_transition)
|
||||
|
||||
if initial_state:
|
||||
current_state = initial_state
|
||||
current_state.enter()
|
||||
|
||||
func physics_process(delta) -> void:
|
||||
if current_state and enable:
|
||||
current_state.update(delta)
|
||||
|
||||
func _on_state_transition(state: State, new_state_id: String) -> void:
|
||||
if state != current_state:
|
||||
return
|
||||
|
||||
var new_state = states.get(new_state_id)
|
||||
if not new_state:
|
||||
return
|
||||
|
||||
if current_state:
|
||||
current_state.exit()
|
||||
|
||||
current_state = new_state
|
||||
current_state.enter()
|
||||
1
core/ai/framework/state_machine.gd.uid
Normal file
1
core/ai/framework/state_machine.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://ps3vlu2qvmop
|
||||
Reference in New Issue
Block a user