refactor to state machine
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
extends Node
|
||||
|
||||
@export var current_state: State
|
||||
var subject: Node
|
||||
|
||||
func set_subject(new_subject: Node) -> void:
|
||||
subject = new_subject
|
||||
for state in get_children():
|
||||
state.subject = subject
|
||||
|
||||
func _change_state(new_state: State) -> void:
|
||||
if current_state:
|
||||
#print("Exiting: %s" % current_state.name)
|
||||
current_state.exit()
|
||||
|
||||
current_state = new_state
|
||||
print("Entering: %s" % current_state.name)
|
||||
current_state.enter()
|
||||
|
||||
func process_physics(delta: float) -> void:
|
||||
if not current_state:
|
||||
return
|
||||
|
||||
if not current_state.subject:
|
||||
return
|
||||
|
||||
var new_state = current_state.process_physics(delta)
|
||||
if new_state:
|
||||
_change_state(new_state)
|
||||
|
||||
func process_input(event: InputEvent) -> void:
|
||||
if not current_state:
|
||||
return
|
||||
|
||||
if not current_state.subject:
|
||||
return
|
||||
|
||||
var new_state = current_state.process_input(event)
|
||||
if new_state:
|
||||
_change_state(new_state)
|
||||
|
||||
func process_frame(delta: float) -> void:
|
||||
if not current_state:
|
||||
return
|
||||
|
||||
if not current_state.subject:
|
||||
return
|
||||
|
||||
var new_state = current_state.process_frame(delta)
|
||||
if new_state:
|
||||
_change_state(new_state)
|
||||
Reference in New Issue
Block a user