forked from 2121578/gai-ca2
Added demo for behavior tree
parent
db933cd620
commit
e9fba43aec
|
@ -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/global/GameManager.gd" id="1_eeg2d"]
|
||||||
[ext_resource type="Script" path="res://scripts/tilemap/World.gd" id="1_k0rw8"]
|
[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/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/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/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"]
|
[node name="Island-scene" type="Node2D"]
|
||||||
script = ExtResource("1_eeg2d")
|
script = ExtResource("1_eeg2d")
|
||||||
|
@ -41,3 +43,9 @@ script = ExtResource("6_efs30")
|
||||||
|
|
||||||
[node name="sl_Root" type="Node" parent="PlayerManager/BehaviorTree"]
|
[node name="sl_Root" type="Node" parent="PlayerManager/BehaviorTree"]
|
||||||
script = ExtResource("7_1jajd")
|
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")
|
||||||
|
|
|
@ -25,5 +25,7 @@ func populate_blackboard():
|
||||||
|
|
||||||
|
|
||||||
func game_tick() -> void:
|
func game_tick() -> void:
|
||||||
|
print("game_tick:")
|
||||||
populate_blackboard()
|
populate_blackboard()
|
||||||
behavior_tree.internal_run(blackboard)
|
behavior_tree.internal_run(blackboard)
|
||||||
|
print(" ==> [active state=", blackboard["current_task"], "] [status=", behavior_tree.status, "]")
|
||||||
|
|
|
@ -5,19 +5,40 @@ enum {FAILURE = -1, SUCCESS = 1, RUNNING = 0}
|
||||||
var status: int = FAILURE
|
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:
|
func internal_run(p_blackboard: Dictionary) -> void:
|
||||||
p_blackboard["current_task"] = self
|
p_blackboard["current_task"] = self
|
||||||
|
|
||||||
if status == RUNNING:
|
if status == RUNNING:
|
||||||
for c in self.get_children():
|
var running_child: Task = find_running_child()
|
||||||
if not c.status == RUNNING:
|
if running_child != null:
|
||||||
continue
|
running_child.internal_run(p_blackboard)
|
||||||
c.internal_run(p_blackboard)
|
status = running_child.status
|
||||||
if c.status != SUCCESS:
|
return
|
||||||
status = c.status
|
else:
|
||||||
return
|
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:
|
func run(p_blackboard: Dictionary) -> void:
|
||||||
|
|
|
@ -3,7 +3,7 @@ extends Task
|
||||||
|
|
||||||
func run(blackboard: Dictionary) -> void:
|
func run(blackboard: Dictionary) -> void:
|
||||||
for c in self.get_children():
|
for c in self.get_children():
|
||||||
c.internal_run(blackboard)
|
run_child(blackboard, c)
|
||||||
if c.status != FAILURE:
|
if c.status != FAILURE:
|
||||||
status = c.status
|
status = c.status
|
||||||
return
|
return
|
||||||
|
|
|
@ -3,7 +3,7 @@ extends Task
|
||||||
|
|
||||||
func run(blackboard: Dictionary) -> void:
|
func run(blackboard: Dictionary) -> void:
|
||||||
for c in self.get_children():
|
for c in self.get_children():
|
||||||
c.internal_run(blackboard)
|
run_child(blackboard, c)
|
||||||
if c.status != SUCCESS:
|
if c.status != SUCCESS:
|
||||||
status = c.status
|
status = c.status
|
||||||
return
|
return
|
||||||
|
|
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue