Update test project with waypoints example

This commit is contained in:
Serhii Snitsaruk 2022-12-15 14:49:38 +01:00
parent dbc9a7ad31
commit e72f7698fb
17 changed files with 266 additions and 75 deletions

View File

@ -1,19 +0,0 @@
extends KinematicBody2D
onready var bt_player: BTPlayer = $BTPlayer
func _ready() -> void:
_configure_ai()
func _configure_ai() -> void:
var tree := BehaviorTree.new()
var seq := BTSequence.new()
var print_task := BTPrintLine.new("Hello world!")
seq.add_child(print_task)
tree.root_task = seq
bt_player.behavior_tree = tree
print("Assigning tree second time")
bt_player.behavior_tree = tree

View File

@ -1,9 +0,0 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://Agent.gd" type="Script" id=2]
[node name="Agent" type="KinematicBody2D"]
script = ExtResource( 2 )
[node name="BTPlayer" type="BTPlayer" parent="."]
active = true

View File

@ -1,7 +1,7 @@
[gd_scene load_steps=2 format=2] [gd_scene load_steps=2 format=3 uid="uid://pgnoaj17gaew"]
[ext_resource path="res://Agent.tscn" type="PackedScene" id=1] [ext_resource type="PackedScene" uid="uid://c26b8c8dndtop" path="res://tests/waypoints/Agent.tscn" id="1"]
[node name="Test" type="Node2D"] [node name="Test" type="Node2D"]
[node name="Agent" parent="." instance=ExtResource( 1 )] [node name="Agent" parent="." instance=ExtResource("1")]

View File

@ -1,14 +0,0 @@
class_name BTPrintLine
extends BTTask
export var line: String
func _init(p_line: String = "") -> void:
line = p_line
func _tick(_delta: float) -> int:
print(line)
return SUCCESS

View File

@ -0,0 +1,24 @@
@tool
extends BTAction
@icon("res://icon.png")
@export var target_position_var := "target_position"
@export var speed_var := "speed"
@export var tolerance := 50.0
func _generate_name() -> String:
return "Arrive pos: %s speed: %s" % [
LimboUtility.decorate_var(target_position_var),
LimboUtility.decorate_var(speed_var),
]
func _tick(p_delta: float) -> int:
var target_pos: Vector2 = blackboard.get_var(target_position_var, Vector2.ZERO)
if target_pos.distance_to(agent.global_position) < tolerance:
return SUCCESS
var speed: float = blackboard.get_var(speed_var, 10.0)
var dir: Vector2 = agent.global_position.direction_to(target_pos)
agent.global_position += dir * speed * p_delta
return RUNNING

View File

@ -0,0 +1,35 @@
@tool
extends BTAction
@export var animation_name: String
@export var player_path: NodePath
var _player: AnimationPlayer
var _finished: bool
func _generate_name() -> String:
return "PlayAnimation \"%s\"" % animation_name
func _setup() -> void:
_player = agent.get_node(player_path)
func _enter() -> void:
if _player.has_animation(animation_name):
_finished = false
_player.play(animation_name)
_player.animation_finished.connect(_on_animation_finished, CONNECT_ONE_SHOT)
else:
_finished = true
func _tick(_delta: float) -> int:
if _finished:
return SUCCESS
return RUNNING
func _on_animation_finished(_anim):
_finished = true

View File

@ -0,0 +1,20 @@
@tool
extends BTAction
@export var animation_name: String
@export var player_path: NodePath
var _player: AnimationPlayer
func _generate_name() -> String:
return "StartAnimation \"%s\"" % animation_name
func _setup() -> void:
_player = agent.get_node(player_path)
func _tick(p_delta: float) -> int:
_player.play(animation_name)
return SUCCESS

View File

@ -1,6 +1,6 @@
[gd_resource type="BehaviorTree" load_steps=2 format=2] [gd_resource type="BehaviorTree" load_steps=2 format=3 uid="uid://6l0b3cyvwy56"]
[sub_resource type="BTSequence" id=1] [sub_resource type="BTSequence" id="1"]
[resource] [resource]
root_task = SubResource( 1 ) root_task = SubResource("1")

View File

@ -0,0 +1,33 @@
[gd_resource type="BehaviorTree" load_steps=9 format=3 uid="uid://c5cp2lpww1qcc"]
[ext_resource type="Script" path="res://ai/tasks/arrive_pos.gd" id="1_6wvrl"]
[ext_resource type="Script" path="res://ai/tasks/play_animation.gd" id="2_hcuvn"]
[sub_resource type="BTAction" id="BTAction_3xal7"]
script = ExtResource("1_6wvrl")
target_position_var = "wp"
speed_var = "speed"
tolerance = 50.0
[sub_resource type="BTAction" id="BTAction_yq1vl"]
script = ExtResource("2_hcuvn")
animation_name = "bounce"
player_path = NodePath("AnimationPlayer")
[sub_resource type="BTSequence" id="BTSequence_a2ng0"]
children = [SubResource("BTAction_3xal7"), SubResource("BTAction_yq1vl")]
[sub_resource type="BTForEach" id="BTForEach_0cp04"]
children = [SubResource("BTSequence_a2ng0")]
array_var = "waypoints"
save_var = "wp"
[sub_resource type="BTRepeat" id="BTRepeat_xljhp"]
children = [SubResource("BTForEach_0cp04")]
times = 999
[sub_resource type="BTSelector" id="BTSelector_5dclr"]
children = [SubResource("BTRepeat_xljhp")]
[resource]
root_task = SubResource("BTSelector_5dclr")

View File

@ -1,6 +1,6 @@
[gd_resource type="Environment" load_steps=2 format=2] [gd_resource type="Environment" load_steps=2 format=2]
[sub_resource type="ProceduralSky" id=1] [sub_resource type="Sky" id=1]
[resource] [resource]
background_mode = 2 background_mode = 2

View File

@ -1,8 +1,9 @@
[remap] [remap]
importer="texture" importer="texture"
type="StreamTexture" type="CompressedTexture2D"
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" uid="uid://d0mht3ntak7e5"
path="res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"
metadata={ metadata={
"vram_texture": false "vram_texture": false
} }
@ -10,26 +11,24 @@ metadata={
[deps] [deps]
source_file="res://icon.png" source_file="res://icon.png"
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ] dest_files=["res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"]
[params] [params]
compress/mode=0 compress/mode=0
compress/lossy_quality=0.7 compress/lossy_quality=0.7
compress/hdr_mode=0 compress/hdr_compression=1
compress/bptc_ldr=0 compress/bptc_ldr=0
compress/normal_map=0 compress/normal_map=0
flags/repeat=0 compress/channel_pack=0
flags/filter=true mipmaps/generate=false
flags/mipmaps=false mipmaps/limit=-1
flags/anisotropic=false roughness/mode=0
flags/srgb=2 roughness/src_normal=""
process/fix_alpha_border=true process/fix_alpha_border=true
process/premult_alpha=false process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false process/normal_map_invert_y=false
stream=false process/hdr_as_srgb=false
size_limit=0 process/hdr_clamp_exposure=false
detect_3d=true process/size_limit=0
svg/scale=1.0 detect_3d/compress_to=1

View File

@ -6,22 +6,13 @@
; [section] ; section goes between [] ; [section] ; section goes between []
; param=value ; assign values to parameters ; param=value ; assign values to parameters
config_version=4 config_version=5
_global_script_classes=[ {
"base": "BTTask",
"class": "BTPrintLine",
"language": "GDScript",
"path": "res://ai/tasks/BTPrintLine.gd"
} ]
_global_script_class_icons={
"BTPrintLine": ""
}
[application] [application]
config/name="LimboAI Test" config/name="LimboAI Test"
run/main_scene="res://Test.tscn" run/main_scene="res://Test.tscn"
config/features=PackedStringArray("4.0")
config/icon="res://icon.png" config/icon="res://icon.png"
[gui] [gui]

View File

@ -0,0 +1,12 @@
extends CharacterBody2D
@onready var bt_player: BTPlayer = $BTPlayer
func _ready() -> void:
bt_player.blackboard.set_var("waypoints", [])
func add_waypoint(p_waypoint: Vector2) -> void:
(bt_player.blackboard.get_var("waypoints") as Array).append(p_waypoint)

View File

@ -0,0 +1,60 @@
[gd_scene load_steps=7 format=3 uid="uid://c26b8c8dndtop"]
[ext_resource type="Script" path="res://tests/waypoints/Agent.gd" id="1_1l3ql"]
[ext_resource type="BehaviorTree" uid="uid://c5cp2lpww1qcc" path="res://ai/trees/waypoints_agent.tres" id="2_58ujo"]
[ext_resource type="Texture2D" uid="uid://d0mht3ntak7e5" path="res://icon.png" id="2_xqtrc"]
[sub_resource type="Animation" id="Animation_5id00"]
length = 0.001
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Sprite2D:scale")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector2(1, 1)]
}
[sub_resource type="Animation" id="Animation_4w16c"]
resource_name = "bounce"
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Sprite2D:scale")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 0.5, 1),
"transitions": PackedFloat32Array(1, 1, 1),
"update": 0,
"values": [Vector2(1, 1), Vector2(2, 2), Vector2(1, 1)]
}
[sub_resource type="AnimationLibrary" id="AnimationLibrary_l438t"]
_data = {
"RESET": SubResource("Animation_5id00"),
"bounce": SubResource("Animation_4w16c")
}
[node name="Agent" type="CharacterBody2D"]
script = ExtResource("1_1l3ql")
[node name="BTPlayer" type="BTPlayer" parent="."]
behavior_tree = ExtResource("2_58ujo")
update_mode = 1
active = true
_blackboard_data = {
"speed": 500.0
}
[node name="Sprite2D" type="Sprite2D" parent="."]
texture = ExtResource("2_xqtrc")
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
libraries = {
"": SubResource("AnimationLibrary_l438t")
}

View File

@ -0,0 +1,17 @@
extends Node2D
@onready var agent: CharacterBody2D = $Agent
@onready var agent_2: CharacterBody2D = $Agent2
func _ready() -> void:
var waypoints: Array[Node] = $Waypoints.get_children()
for wp in waypoints:
agent.add_waypoint(wp.global_position)
waypoints.reverse()
for wp in waypoints:
agent_2.add_waypoint(wp.global_position)

View File

@ -0,0 +1,32 @@
[gd_scene load_steps=4 format=3 uid="uid://p5i7fdku3hqs"]
[ext_resource type="PackedScene" uid="uid://c26b8c8dndtop" path="res://tests/waypoints/Agent.tscn" id="1_esee5"]
[ext_resource type="Script" path="res://tests/waypoints/test_waypoints.gd" id="1_kmjjq"]
[ext_resource type="Script" path="res://tests/waypoints/waypoint_marker.gd" id="3_1aub7"]
[node name="test_waypoints" type="Node2D"]
script = ExtResource("1_kmjjq")
[node name="Waypoints" type="Node2D" parent="."]
[node name="WP1" type="Marker2D" parent="Waypoints"]
position = Vector2(283, 184)
script = ExtResource("3_1aub7")
[node name="WP2" type="Marker2D" parent="Waypoints"]
position = Vector2(938, 100)
script = ExtResource("3_1aub7")
[node name="WP3" type="Marker2D" parent="Waypoints"]
position = Vector2(838, 490)
script = ExtResource("3_1aub7")
[node name="WP4" type="Marker2D" parent="Waypoints"]
position = Vector2(177, 515)
script = ExtResource("3_1aub7")
[node name="Agent" parent="." instance=ExtResource("1_esee5")]
position = Vector2(57, 59)
[node name="Agent2" parent="." instance=ExtResource("1_esee5")]
position = Vector2(316, 590)

View File

@ -0,0 +1,10 @@
@tool
extends Marker2D
func _ready() -> void:
queue_redraw()
func _draw() -> void:
draw_circle(Vector2.ZERO, 50.0, Color.CHARTREUSE)