Initial Commit
This commit is contained in:
commit
48a5e71e00
1136 changed files with 64347 additions and 0 deletions
16
lib/state_machine/state.gd
Normal file
16
lib/state_machine/state.gd
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
extends Node
|
||||
class_name State
|
||||
|
||||
signal transitioned(state: State, new_state_name: String)
|
||||
|
||||
func _enter() -> void:
|
||||
pass
|
||||
|
||||
func _exit() -> void:
|
||||
pass
|
||||
|
||||
func _update(_delta: float) -> void:
|
||||
pass
|
||||
|
||||
func _physics_update(_delta: float) -> void:
|
||||
pass
|
||||
1
lib/state_machine/state.gd.uid
Normal file
1
lib/state_machine/state.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://cb1uc3nlv2mgj
|
||||
44
lib/state_machine/state_machine.gd
Normal file
44
lib/state_machine/state_machine.gd
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
extends Node
|
||||
class_name StateMachine
|
||||
|
||||
@export var initial_state: State
|
||||
|
||||
signal state_changed(new_state: State)
|
||||
|
||||
var current_state: State
|
||||
var states: Dictionary[String, State] = {}
|
||||
|
||||
func _ready() -> void:
|
||||
for child in get_children():
|
||||
if child is State:
|
||||
states[child.name.to_lower()] = child
|
||||
child.transitioned.connect(_handle_child_transition)
|
||||
|
||||
if initial_state:
|
||||
initial_state._enter()
|
||||
current_state = initial_state
|
||||
|
||||
func _process(delta) -> void:
|
||||
if current_state:
|
||||
current_state._update(delta)
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if current_state:
|
||||
current_state._physics_update(delta)
|
||||
|
||||
func _handle_child_transition(state: State, new_state_name: String) -> void:
|
||||
if state != current_state:
|
||||
return
|
||||
|
||||
var new_state: State = states.get(new_state_name.to_lower(), null)
|
||||
|
||||
if not new_state:
|
||||
return
|
||||
|
||||
if current_state:
|
||||
current_state._exit()
|
||||
|
||||
new_state._enter()
|
||||
|
||||
current_state = new_state
|
||||
state_changed.emit(new_state)
|
||||
1
lib/state_machine/state_machine.gd.uid
Normal file
1
lib/state_machine/state_machine.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://c16ty2yx4qkvu
|
||||
Loading…
Add table
Add a link
Reference in a new issue