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

86 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
if status == RUNNING:
var running_child: Task = find_running_child()
if running_child != null:
print(" -> ", human_readable(running_child.name))
running_child.internal_run(blackboard)
print(" <- ", human_readable(running_child.name))
status = running_child.status
return
else:
print(" -> ", human_readable())
run(blackboard)
print(" <- ", human_readable())
else:
print(" -> ", human_readable())
run(blackboard)
print(" <- ", human_readable())
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 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