Implemented state machine & added NPC ship template
This commit is contained in:
parent
2d4eb92751
commit
bda9232e72
15 changed files with 284 additions and 22 deletions
51
scripts/State Machine/tree.gd
Normal file
51
scripts/State Machine/tree.gd
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
extends Node
|
||||
|
||||
class_name StateTree
|
||||
|
||||
## Emitted when active state is transitioned
|
||||
signal transitioned(to_state)
|
||||
|
||||
## Active state
|
||||
@export var state : State
|
||||
|
||||
## Dictionary of all states
|
||||
var cached_states = {}
|
||||
|
||||
func _ready():
|
||||
for child in get_children():
|
||||
child.tree = self
|
||||
cached_states[child.name] = child
|
||||
|
||||
state.enter({})
|
||||
|
||||
## Make another state active. Optionally, properties can be provided with message
|
||||
func transit(to : String, message : Dictionary = {}):
|
||||
|
||||
state.exit()
|
||||
|
||||
state = cached_states[to]
|
||||
state.enter(message)
|
||||
|
||||
transitioned.emit(to)
|
||||
|
||||
## Provides properties to active state
|
||||
func send_input(message : Dictionary = {}):
|
||||
state.input(message)
|
||||
|
||||
## Provides properties to all states
|
||||
func send_input_to_system(message : Dictionary = {}):
|
||||
for state in cached_states.values():
|
||||
state.input(message)
|
||||
|
||||
## Returns state with provided name
|
||||
func get_state(state_name) -> State:
|
||||
return cached_states[state_name]
|
||||
|
||||
#region Перегруженные виртуальные функции гойды
|
||||
func _process(delta) -> void:
|
||||
state.process(delta)
|
||||
|
||||
func _physics_process(delta) -> void:
|
||||
state.physics_process(delta)
|
||||
|
||||
#endregion
|
||||
Loading…
Add table
Add a link
Reference in a new issue