gai-ca2/project/scripts/player/tree/Task.gd

88 lines
2.2 KiB
GDScript

class_name Task
extends Node
enum {FAILURE = -1, SUCCESS = 1, RUNNING = 0, SUCCESS_STOP = 2}
var status: int = FAILURE
var status_reason: String = ""
var tilemap_types: TileMapTileTypes = TileMapTileTypes.new()
func _ready() -> void:
for c in get_children():
if not c is Task:
push_error("Child is not a task: " + c.name + " in " + name)
return
func internal_run(blackboard: Dictionary) -> void:
blackboard["current_task"] = self
var running_child: Task = find_running_child()
var extra_string: String = ""
if running_child != null:
extra_string = running_child.name
print(" -> ", human_readable(extra_string))
run(blackboard)
print(" <- ", human_readable(extra_string))
func find_running_child() -> Task:
for c in get_children():
if c.status == RUNNING:
return c
return null
func run_child(blackboard: Dictionary, p_child: Task) -> void:
p_child.internal_run(blackboard)
if p_child.status != RUNNING:
status = RUNNING
func slice_at_child(start_child: Task) -> Array:
var children: Array[Node] = get_children()
if start_child == null:
return children
var start_index: int = children.find(start_child)
if start_index == -1:
return children
return children.slice(start_index, children.size())
func run(blackboard: Dictionary) -> void:
pass
func cancel(blackboard: Dictionary):
pass
func get_first_child() -> Task:
if get_child_count() == 0:
push_error("Task does not have any children: " + name)
return null
return get_children()[0] as Task
func human_readable(addon: String = "") -> String:
var clear_status: String = "UNKNOWN"
if status == FAILURE:
clear_status = "FAILURE"
elif status == SUCCESS:
clear_status = "SUCCESS"
elif status == RUNNING:
clear_status = "RUNNING"
elif status == SUCCESS_STOP:
clear_status = "SUCCESS_STOP"
var ret: String = name;
if addon != "":
ret += " " + addon
if status_reason != "":
ret += " [" + clear_status + ", " + status_reason + "]"
else:
ret += " [" + clear_status + "]"
return ret