diff --git a/project/main-scenes/island.tscn b/project/main-scenes/island.tscn index 56ae467..924b5b4 100644 --- a/project/main-scenes/island.tscn +++ b/project/main-scenes/island.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=8 format=4 uid="uid://b88asko1ugyd2"] +[gd_scene load_steps=10 format=4 uid="uid://b88asko1ugyd2"] [ext_resource type="Script" path="res://scripts/global/GameManager.gd" id="1_eeg2d"] [ext_resource type="Script" path="res://scripts/tilemap/World.gd" id="1_k0rw8"] @@ -7,6 +7,8 @@ [ext_resource type="Script" path="res://scripts/player/PlayerManager.gd" id="4_1xqo1"] [ext_resource type="Script" path="res://scripts/player/tree/BehaviorTree.gd" id="6_efs30"] [ext_resource type="Script" path="res://scripts/player/tree/impl/base/TaskSelector.gd" id="7_1jajd"] +[ext_resource type="Script" path="res://scripts/player/tree/impl/context/Task5050Success.gd" id="8_xrswf"] +[ext_resource type="Script" path="res://scripts/player/tree/impl/context/Task5050Running.gd" id="9_r13cd"] [node name="Island-scene" type="Node2D"] script = ExtResource("1_eeg2d") @@ -41,3 +43,9 @@ script = ExtResource("6_efs30") [node name="sl_Root" type="Node" parent="PlayerManager/BehaviorTree"] script = ExtResource("7_1jajd") + +[node name="5050Success" type="Node" parent="PlayerManager/BehaviorTree/sl_Root"] +script = ExtResource("8_xrswf") + +[node name="5050Running" type="Node" parent="PlayerManager/BehaviorTree/sl_Root"] +script = ExtResource("9_r13cd") diff --git a/project/scripts/player/tree/BehaviorTree.gd b/project/scripts/player/tree/BehaviorTree.gd index b565371..536c007 100644 --- a/project/scripts/player/tree/BehaviorTree.gd +++ b/project/scripts/player/tree/BehaviorTree.gd @@ -25,5 +25,7 @@ func populate_blackboard(): func game_tick() -> void: + print("game_tick:") populate_blackboard() behavior_tree.internal_run(blackboard) + print(" ==> [active state=", blackboard["current_task"], "] [status=", behavior_tree.status, "]") diff --git a/project/scripts/player/tree/Task.gd b/project/scripts/player/tree/Task.gd index c45079b..ee43085 100644 --- a/project/scripts/player/tree/Task.gd +++ b/project/scripts/player/tree/Task.gd @@ -5,19 +5,40 @@ enum {FAILURE = -1, SUCCESS = 1, RUNNING = 0} var status: int = FAILURE +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(p_blackboard: Dictionary) -> void: p_blackboard["current_task"] = self if status == RUNNING: - for c in self.get_children(): - if not c.status == RUNNING: - continue - c.internal_run(p_blackboard) - if c.status != SUCCESS: - status = c.status - return + var running_child: Task = find_running_child() + if running_child != null: + running_child.internal_run(p_blackboard) + status = running_child.status + return + else: + run(p_blackboard) + else: + run(p_blackboard) + print(" - ", name, " ", status) - run(p_blackboard) + +func find_running_child() -> Task: + for c in get_children(): + if c.status == RUNNING: + return c + return null + + +func run_child(p_blackboard: Dictionary, p_child: Task) -> void: + p_child.internal_run(p_blackboard) + if p_child.status != RUNNING: + status = RUNNING func run(p_blackboard: Dictionary) -> void: diff --git a/project/scripts/player/tree/impl/base/TaskSelector.gd b/project/scripts/player/tree/impl/base/TaskSelector.gd index a71f8a8..07b705e 100644 --- a/project/scripts/player/tree/impl/base/TaskSelector.gd +++ b/project/scripts/player/tree/impl/base/TaskSelector.gd @@ -3,7 +3,7 @@ extends Task func run(blackboard: Dictionary) -> void: for c in self.get_children(): - c.internal_run(blackboard) + run_child(blackboard, c) if c.status != FAILURE: status = c.status return diff --git a/project/scripts/player/tree/impl/base/TaskSequence.gd b/project/scripts/player/tree/impl/base/TaskSequence.gd index 39f847b..f7a7b80 100644 --- a/project/scripts/player/tree/impl/base/TaskSequence.gd +++ b/project/scripts/player/tree/impl/base/TaskSequence.gd @@ -3,7 +3,7 @@ extends Task func run(blackboard: Dictionary) -> void: for c in self.get_children(): - c.internal_run(blackboard) + run_child(blackboard, c) if c.status != SUCCESS: status = c.status return diff --git a/project/scripts/player/tree/impl/context/Task5050Running.gd b/project/scripts/player/tree/impl/context/Task5050Running.gd new file mode 100644 index 0000000..17ca558 --- /dev/null +++ b/project/scripts/player/tree/impl/context/Task5050Running.gd @@ -0,0 +1,9 @@ +class_name Task5050Running +extends Task + +func run(blackboard: Dictionary) -> void: + var random: int = randi() % 2 + if random == 0: + status = RUNNING + else: + status = SUCCESS diff --git a/project/scripts/player/tree/impl/context/Task5050Success.gd b/project/scripts/player/tree/impl/context/Task5050Success.gd new file mode 100644 index 0000000..94ea92b --- /dev/null +++ b/project/scripts/player/tree/impl/context/Task5050Success.gd @@ -0,0 +1,9 @@ +class_name Task5050Success +extends Task + +func run(blackboard: Dictionary) -> void: + var random: int = randi() % 2 + if random == 0: + status = SUCCESS + else: + status = FAILURE