-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStateMachine.gd
43 lines (28 loc) · 850 Bytes
/
StateMachine.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
extends Node2D
class_name StateMachine
onready var parent: Object = get_parent()
var current_state : State= null
var previous_state : State = null
func _ready() -> void:
var first_child = get_child(0)
assert(first_child is State)
current_state = first_child
_enter_state()
func change_to(new_state_name) -> void:
var new_state = get_node(new_state_name)
assert(new_state is State)
previous_state = current_state
current_state = new_state
_enter_state()
func back() -> void:
change_to(previous_state.name)
func _enter_state() -> void:
#print(parent.name, " is entering state: ", current_state.name)
#current_state.host = parent
current_state.enter()
func _physics_process(delta):
current_state.physics_process(delta)
func _process(delta):
current_state.process(delta)
func _input(event):
current_state.input(event)