83 lines
2.0 KiB
GDScript
83 lines
2.0 KiB
GDScript
@icon("res://icons/turn_action.svg")
|
|
class_name ActionDecider
|
|
extends Node
|
|
|
|
var actor: TurnActor
|
|
var current_action: Action
|
|
|
|
func get_actor():
|
|
var parent = get_parent()
|
|
if parent is TurnActor:
|
|
return parent
|
|
else:
|
|
return null
|
|
|
|
func connect_to_actor():
|
|
actor.connect("turn_started", handle_turn_start)
|
|
actor.connect("deciding_action", handle_decide_action)
|
|
actor.connect("performing_action", handle_perform_action)
|
|
actor.connect("turn_ended", handle_turn_end)
|
|
|
|
func handle_turn_start():
|
|
#print("turn start!")
|
|
pass
|
|
|
|
func handle_decide_action():
|
|
#print("deciding action...")
|
|
pass
|
|
|
|
func handle_perform_action():
|
|
#print("performing action: ", actor.get_current_action())
|
|
pass
|
|
|
|
func handle_turn_end():
|
|
#print("turn end!")
|
|
pass
|
|
|
|
func is_deciding() -> bool:
|
|
return actor.is_deciding()
|
|
|
|
func try_perform(action_node: Action) -> bool:
|
|
if current_action:
|
|
push_error("Tried to start an action while another one is performing! Current action: ", current_action.name)
|
|
return false
|
|
add_child(action_node)
|
|
if action_node.predicate():
|
|
inner_perform(action_node)
|
|
return true
|
|
else:
|
|
action_node.queue_free()
|
|
return false
|
|
|
|
func inner_perform(action_node: Action):
|
|
current_action = action_node
|
|
actor.perform_action(action_node.name)
|
|
current_action.done.connect(_on_action_done, CONNECT_ONE_SHOT)
|
|
current_action.abort.connect(_on_action_abort, CONNECT_ONE_SHOT)
|
|
current_action.action_ready()
|
|
|
|
func _on_action_done():
|
|
current_action.abort.disconnect(_on_action_abort)
|
|
inner_action_cleanup()
|
|
|
|
func _on_action_abort():
|
|
push_error("Action aborted: ", current_action.get_path())
|
|
current_action.done.disconnect(_on_action_done)
|
|
inner_action_cleanup()
|
|
|
|
func inner_action_cleanup():
|
|
current_action.queue_free()
|
|
current_action = null
|
|
actor.end_turn()
|
|
|
|
func _ready():
|
|
actor = get_actor()
|
|
if not actor is TurnActor:
|
|
push_error("Couldn't get TurnActor from TurnAction")
|
|
else:
|
|
connect_to_actor()
|
|
|
|
func _process(delta: float):
|
|
if current_action:
|
|
current_action.action_process(delta)
|