76 lines
1.8 KiB
GDScript
76 lines
1.8 KiB
GDScript
extends StateMachine
|
|
|
|
signal got_grounded
|
|
|
|
|
|
# Adds the intial states
|
|
func _ready():
|
|
add_state("deactivated")
|
|
add_state("searching")
|
|
add_state("hunting")
|
|
add_state("locking")
|
|
add_state("shooting")
|
|
state = states.searching
|
|
set_state(states.searching)
|
|
|
|
|
|
# Calls the parent behaviours according to state
|
|
func _state_logic(_delta):
|
|
var state_action_ref = "handle_deactivated_state"
|
|
match self.state:
|
|
"deactivated":
|
|
state_action_ref = funcref(self, "handle_deactivated_state")
|
|
"searching":
|
|
state_action_ref = funcref(self, "handle_searching_state")
|
|
"locking":
|
|
state_action_ref = funcref(self, "handle_locking_state")
|
|
"shooting":
|
|
state_action_ref = funcref(self, "handle_shooting_state")
|
|
_:
|
|
state_action_ref = funcref(self, "handle_deactivated_state")
|
|
print("don't panik")
|
|
|
|
state_action_ref.call_func()
|
|
|
|
|
|
func handle_searching_state():
|
|
parent.searching()
|
|
|
|
|
|
# Locking is intialized once when starting
|
|
# While the locking is handled by a Time/Turret, this method just passes
|
|
func handle_locking_state():
|
|
pass
|
|
|
|
|
|
func handle_shooting_state():
|
|
parent.shooting()
|
|
|
|
|
|
# Determines which state should be active at the moment
|
|
func _get_transition(_delta):
|
|
# TODO why only this way?
|
|
parent.get_node("StateLabel").text = self.state
|
|
var new_state
|
|
if parent.prey != null && self.state == "searching":
|
|
parent.start_locking()
|
|
new_state = "locking"
|
|
if self.state == "locking" && parent.is_locked_on_target():
|
|
new_state = "shooting"
|
|
if new_state != self.state:
|
|
return new_state
|
|
|
|
return null
|
|
|
|
|
|
func _enter_state(new_state, old_state):
|
|
if old_state == "idle" && (new_state == "walk" || new_state == "run"):
|
|
pass
|
|
# TODO This may be hard to keep track of if many states are added
|
|
if !["run", "walk", "idle"].has(old_state) && parent.is_on_floor():
|
|
emit_signal("got_grounded")
|
|
|
|
|
|
func _exit_state(old_state, new_state):
|
|
pass
|