1
0
Fork 0

Added demo for behavior tree

Yan Wittmann 2025-01-04 17:09:43 +01:00
parent db933cd620
commit e9fba43aec
7 changed files with 60 additions and 11 deletions

View File

@ -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")

View File

@ -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, "]")

View File

@ -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
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)
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:

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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