From 79813d57a8b1f7f7951bda2fbbf5fec475799288 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Thu, 1 Feb 2024 17:35:55 +0100 Subject: [PATCH] Demo: Implement combo enemy BT --- demo/demo/agents/agent_base.tscn | 3 +- demo/demo/agents/agent_fencer.tscn | 24 --- demo/demo/agents/agent_melee_combo.tscn | 41 +++++ demo/demo/ai/tasks/get_first_in_group.gd | 2 +- demo/demo/ai/tasks/leap_forward.gd | 32 ++++ demo/demo/ai/tasks/select_flanking_pos.gd | 2 +- demo/demo/ai/trees/enemy_melee_combo.tres | 147 ++++++++++++++++++ demo/demo/ai/trees/enemy_melee_simple.tres | 6 +- .../{agent_fencer.png => agent_combo.png} | Bin ...ncer.png.import => agent_combo.png.import} | 6 +- demo/demo/scenes/demo.tscn | 12 +- 11 files changed, 235 insertions(+), 40 deletions(-) delete mode 100644 demo/demo/agents/agent_fencer.tscn create mode 100644 demo/demo/agents/agent_melee_combo.tscn create mode 100644 demo/demo/ai/tasks/leap_forward.gd create mode 100644 demo/demo/ai/trees/enemy_melee_combo.tres rename demo/demo/assets/{agent_fencer.png => agent_combo.png} (100%) rename demo/demo/assets/{agent_fencer.png.import => agent_combo.png.import} (71%) diff --git a/demo/demo/agents/agent_base.tscn b/demo/demo/agents/agent_base.tscn index 872569c..70681a1 100644 --- a/demo/demo/agents/agent_base.tscn +++ b/demo/demo/agents/agent_base.tscn @@ -3509,7 +3509,8 @@ fixed_fps = 60 [node name="WeaponNinjaStar" type="Sprite2D" parent="Root"] visible = false -position = Vector2(-55, -76) +position = Vector2(-71.6739, -72.9077) +rotation = 0.204683 scale = Vector2(0.999983, 0.999976) texture = ExtResource("4_1c5xq") diff --git a/demo/demo/agents/agent_fencer.tscn b/demo/demo/agents/agent_fencer.tscn deleted file mode 100644 index 0b630a4..0000000 --- a/demo/demo/agents/agent_fencer.tscn +++ /dev/null @@ -1,24 +0,0 @@ -[gd_scene load_steps=3 format=3 uid="uid://blxmw2w1h2s0s"] - -[ext_resource type="PackedScene" uid="uid://ooigbfhfy4wa" path="res://demo/agents/agent_base.tscn" id="1_fkh4o"] -[ext_resource type="Texture2D" uid="uid://bo0ibp7tvjbba" path="res://demo/assets/agent_fencer.png" id="2_r3phr"] - -[node name="Bobby" instance=ExtResource("1_fkh4o")] - -[node name="LegL" parent="Root/Rig" index="0"] -texture = ExtResource("2_r3phr") - -[node name="LegR" parent="Root/Rig" index="1"] -texture = ExtResource("2_r3phr") - -[node name="Body" parent="Root/Rig" index="2"] -texture = ExtResource("2_r3phr") - -[node name="Hat" parent="Root/Rig/Body" index="0"] -texture = ExtResource("2_r3phr") - -[node name="HandL" parent="Root/Rig/Body" index="1"] -texture = ExtResource("2_r3phr") - -[node name="HandR" parent="Root/Rig/Body" index="2"] -texture = ExtResource("2_r3phr") diff --git a/demo/demo/agents/agent_melee_combo.tscn b/demo/demo/agents/agent_melee_combo.tscn new file mode 100644 index 0000000..36ddd90 --- /dev/null +++ b/demo/demo/agents/agent_melee_combo.tscn @@ -0,0 +1,41 @@ +[gd_scene load_steps=5 format=3 uid="uid://blxmw2w1h2s0s"] + +[ext_resource type="PackedScene" uid="uid://ooigbfhfy4wa" path="res://demo/agents/agent_base.tscn" id="1_dny3b"] +[ext_resource type="Texture2D" uid="uid://bo0ibp7tvjbba" path="res://demo/assets/agent_combo.png" id="2_4rqld"] +[ext_resource type="BehaviorTree" uid="uid://cpncl1db8j12f" path="res://demo/ai/trees/enemy_melee_combo.tres" id="3_l805q"] + +[sub_resource type="BlackboardPlan" id="BlackboardPlan_78rmu"] +var/speed/name = "speed" +var/speed/type = 3 +var/speed/value = 400.0 +var/speed/hint = 1 +var/speed/hint_string = "10,1000,10" + +[node name="AgentMeleeCombo" instance=ExtResource("1_dny3b")] + +[node name="LegL" parent="Root/Rig" index="1"] +texture = ExtResource("2_4rqld") + +[node name="LegR" parent="Root/Rig" index="2"] +texture = ExtResource("2_4rqld") + +[node name="Body" parent="Root/Rig" index="3"] +texture = ExtResource("2_4rqld") + +[node name="Hat" parent="Root/Rig/Body" index="0"] +texture = ExtResource("2_4rqld") + +[node name="HandL" parent="Root/Rig/Body" index="1"] +texture = ExtResource("2_4rqld") + +[node name="HandR" parent="Root/Rig/Body" index="2"] +texture = ExtResource("2_4rqld") + +[node name="WeaponNinjaStar" parent="Root" index="2"] +position = Vector2(-55, -76) +rotation = 0.0 +scale = Vector2(0.999983, 0.999976) + +[node name="BTPlayer" type="BTPlayer" parent="." index="4"] +behavior_tree = ExtResource("3_l805q") +blackboard_plan = SubResource("BlackboardPlan_78rmu") diff --git a/demo/demo/ai/tasks/get_first_in_group.gd b/demo/demo/ai/tasks/get_first_in_group.gd index 93646f2..bbd3755 100644 --- a/demo/demo/ai/tasks/get_first_in_group.gd +++ b/demo/demo/ai/tasks/get_first_in_group.gd @@ -18,7 +18,7 @@ extends BTAction func _generate_name() -> String: - return "GetFirstNodeInGroup \"%s\" -> %s" % [ + return "GetFirstNodeInGroup \"%s\" ➜%s" % [ group, LimboUtility.decorate_var(output_var) ] diff --git a/demo/demo/ai/tasks/leap_forward.gd b/demo/demo/ai/tasks/leap_forward.gd new file mode 100644 index 0000000..1f86ca3 --- /dev/null +++ b/demo/demo/ai/tasks/leap_forward.gd @@ -0,0 +1,32 @@ +#* +#* leap_forward.gd +#* ============================================================================= +#* Copyright 2021-2024 Serhii Snitsaruk +#* +#* Use of this source code is governed by an MIT-style +#* license that can be found in the LICENSE file or at +#* https://opensource.org/licenses/MIT. +#* ============================================================================= +#* +@tool +extends BTAction +## LeapForward: Applies force each tick until duration is exceeded. +## Returns SUCCESS if elapsed time exceeded duration. +## Returns RUNNING if elapsed time didn't exceed duration. + + +@export var force: float = 1000.0 +@export var duration: float = 0.1 + +# Display a customized name (requires @tool). +func _generate_name() -> String: + return "LeapForward force: " + str(force) + +# Called each time this task is ticked (aka executed). +func _tick(_delta: float) -> Status: + var facing: float = agent.get_facing() + agent.velocity = Vector2.RIGHT * facing * force + agent.move_and_slide() + if elapsed_time > duration: + return SUCCESS + return RUNNING diff --git a/demo/demo/ai/tasks/select_flanking_pos.gd b/demo/demo/ai/tasks/select_flanking_pos.gd index f0ed70e..021086b 100644 --- a/demo/demo/ai/tasks/select_flanking_pos.gd +++ b/demo/demo/ai/tasks/select_flanking_pos.gd @@ -14,7 +14,7 @@ extends BTAction ## Returns FAILURE, if the target is not valid. ## Blackboard variable that holds current target (should be a Node2D instance). -@export var target_var: String = "_target" +@export var target_var: String = "target" ## Should closest side be selected? @export var closest_side: bool = false diff --git a/demo/demo/ai/trees/enemy_melee_combo.tres b/demo/demo/ai/trees/enemy_melee_combo.tres new file mode 100644 index 0000000..f6a2694 --- /dev/null +++ b/demo/demo/ai/trees/enemy_melee_combo.tres @@ -0,0 +1,147 @@ +[gd_resource type="BehaviorTree" load_steps=37 format=3 uid="uid://cpncl1db8j12f"] + +[ext_resource type="Script" path="res://demo/ai/tasks/get_first_in_group.gd" id="1_e6233"] +[ext_resource type="Script" path="res://demo/ai/tasks/select_flanking_pos.gd" id="2_ifiw3"] +[ext_resource type="Script" path="res://demo/ai/tasks/arrive_pos.gd" id="3_4bv56"] +[ext_resource type="Script" path="res://demo/ai/tasks/face_target.gd" id="3_ylc6a"] +[ext_resource type="Script" path="res://demo/ai/tasks/leap_forward.gd" id="5_1wo58"] + +[sub_resource type="BlackboardPlan" id="BlackboardPlan_46tbn"] +var/speed/name = "speed" +var/speed/type = 3 +var/speed/value = 400.0 +var/speed/hint = 1 +var/speed/hint_string = "10,1000,10" + +[sub_resource type="BBNode" id="BBNode_nrd4b"] +saved_value = NodePath("AnimationPlayer") +resource_name = "AnimationPlayer" + +[sub_resource type="BTPlayAnimation" id="BTPlayAnimation_qiw21"] +animation_player = SubResource("BBNode_nrd4b") +animation_name = &"idle" +blend = 0.1 + +[sub_resource type="BTRandomWait" id="BTRandomWait_xlud8"] +min_duration = 0.7 +max_duration = 1.5 + +[sub_resource type="BTAction" id="BTAction_ulbrf"] +script = ExtResource("1_e6233") +group = &"player" +output_var = "target" + +[sub_resource type="BTSequence" id="BTSequence_yhjh1"] +custom_name = "Pause before action" +children = [SubResource("BTPlayAnimation_qiw21"), SubResource("BTRandomWait_xlud8"), SubResource("BTAction_ulbrf")] + +[sub_resource type="BTAction" id="BTAction_85keo"] +script = ExtResource("2_ifiw3") +target_var = "target" +closest_side = true +range_min = 300 +range_max = 400 +position_var = "pos" + +[sub_resource type="BBNode" id="BBNode_wpj6d"] +saved_value = NodePath("AnimationPlayer") +resource_name = "AnimationPlayer" + +[sub_resource type="BTPlayAnimation" id="BTPlayAnimation_olf37"] +animation_player = SubResource("BBNode_wpj6d") +animation_name = &"walk" +blend = 0.1 + +[sub_resource type="BTAction" id="BTAction_ygjnc"] +script = ExtResource("3_4bv56") +target_position_var = "pos" +speed_var = "speed" +tolerance = 50.0 + +[sub_resource type="BTTimeLimit" id="BTTimeLimit_xek5v"] +children = [SubResource("BTAction_ygjnc")] + +[sub_resource type="BTSequence" id="BTSequence_1xfnq"] +custom_name = "Align with player" +children = [SubResource("BTAction_85keo"), SubResource("BTPlayAnimation_olf37"), SubResource("BTTimeLimit_xek5v")] + +[sub_resource type="BTAction" id="BTAction_kidxn"] +script = ExtResource("3_ylc6a") +target_var = "target" + +[sub_resource type="BTWait" id="BTWait_tadkc"] +duration = 0.2 + +[sub_resource type="BBNode" id="BBNode_s8evu"] +saved_value = NodePath("AnimationPlayer") +resource_name = "AnimationPlayer" + +[sub_resource type="BTPlayAnimation" id="BTPlayAnimation_ppmxd"] +await_completion = 2.0 +animation_player = SubResource("BBNode_s8evu") +animation_name = &"attack_1" + +[sub_resource type="BTAction" id="BTAction_u22bc"] +script = ExtResource("5_1wo58") +force = 1000.0 +duration = 0.1 + +[sub_resource type="BTParallel" id="BTParallel_ec2e3"] +num_successes_required = 2 +children = [SubResource("BTPlayAnimation_ppmxd"), SubResource("BTAction_u22bc")] + +[sub_resource type="BTWait" id="BTWait_c0lo0"] +duration = 0.2 + +[sub_resource type="BBNode" id="BBNode_3oljv"] +saved_value = NodePath("AnimationPlayer") +resource_name = "AnimationPlayer" + +[sub_resource type="BTPlayAnimation" id="BTPlayAnimation_q4uis"] +await_completion = 2.0 +animation_player = SubResource("BBNode_3oljv") +animation_name = &"attack_2" + +[sub_resource type="BTAction" id="BTAction_yuxl3"] +script = ExtResource("5_1wo58") +force = 1000.0 +duration = 0.1 + +[sub_resource type="BTParallel" id="BTParallel_thojy"] +num_successes_required = 2 +children = [SubResource("BTPlayAnimation_q4uis"), SubResource("BTAction_yuxl3")] + +[sub_resource type="BTWait" id="BTWait_y3pmv"] +duration = 0.2 + +[sub_resource type="BBNode" id="BBNode_ot40l"] +saved_value = NodePath("AnimationPlayer") +resource_name = "AnimationPlayer" + +[sub_resource type="BTPlayAnimation" id="BTPlayAnimation_ye5pt"] +await_completion = 2.0 +animation_player = SubResource("BBNode_ot40l") +animation_name = &"attack_3" + +[sub_resource type="BTAction" id="BTAction_rwp18"] +script = ExtResource("5_1wo58") +force = 1000.0 +duration = 0.1 + +[sub_resource type="BTParallel" id="BTParallel_qmdfb"] +num_successes_required = 2 +children = [SubResource("BTPlayAnimation_ye5pt"), SubResource("BTAction_rwp18")] + +[sub_resource type="BTWait" id="BTWait_nxyko"] +duration = 0.5 + +[sub_resource type="BTSequence" id="BTSequence_ww5v2"] +custom_name = "Combo Melee attack" +children = [SubResource("BTAction_kidxn"), SubResource("BTWait_tadkc"), SubResource("BTParallel_ec2e3"), SubResource("BTWait_c0lo0"), SubResource("BTParallel_thojy"), SubResource("BTWait_y3pmv"), SubResource("BTParallel_qmdfb"), SubResource("BTWait_nxyko")] + +[sub_resource type="BTSequence" id="BTSequence_pxl2k"] +children = [SubResource("BTSequence_yhjh1"), SubResource("BTSequence_1xfnq"), SubResource("BTSequence_ww5v2")] + +[resource] +blackboard_plan = SubResource("BlackboardPlan_46tbn") +root_task = SubResource("BTSequence_pxl2k") diff --git a/demo/demo/ai/trees/enemy_melee_simple.tres b/demo/demo/ai/trees/enemy_melee_simple.tres index 3465280..26330b1 100644 --- a/demo/demo/ai/trees/enemy_melee_simple.tres +++ b/demo/demo/ai/trees/enemy_melee_simple.tres @@ -40,11 +40,11 @@ blend = 0.1 [sub_resource type="BTAction" id="BTAction_ulbrf"] script = ExtResource("1_2jpsu") group = &"player" -output_var = "_target" +output_var = "target" [sub_resource type="BTAction" id="BTAction_a4jqi"] script = ExtResource("2_h5db5") -target_var = "_target" +target_var = "target" speed_var = "speed" approach_distance = 100.0 @@ -58,7 +58,7 @@ children = [SubResource("BTPlayAnimation_olf37"), SubResource("BTAction_ulbrf"), [sub_resource type="BTAction" id="BTAction_kidxn"] script = ExtResource("3_bpmfp") -target_var = "_target" +target_var = "target" [sub_resource type="BTWait" id="BTWait_tadkc"] duration = 0.1 diff --git a/demo/demo/assets/agent_fencer.png b/demo/demo/assets/agent_combo.png similarity index 100% rename from demo/demo/assets/agent_fencer.png rename to demo/demo/assets/agent_combo.png diff --git a/demo/demo/assets/agent_fencer.png.import b/demo/demo/assets/agent_combo.png.import similarity index 71% rename from demo/demo/assets/agent_fencer.png.import rename to demo/demo/assets/agent_combo.png.import index 44c4b87..8128a64 100644 --- a/demo/demo/assets/agent_fencer.png.import +++ b/demo/demo/assets/agent_combo.png.import @@ -3,15 +3,15 @@ importer="texture" type="CompressedTexture2D" uid="uid://bo0ibp7tvjbba" -path="res://.godot/imported/agent_fencer.png-289d0f406fd6917ada1d9dd1003e1532.ctex" +path="res://.godot/imported/agent_combo.png-1aabf55a6f7673e34d40eadba34cb086.ctex" metadata={ "vram_texture": false } [deps] -source_file="res://demo/assets/agent_fencer.png" -dest_files=["res://.godot/imported/agent_fencer.png-289d0f406fd6917ada1d9dd1003e1532.ctex"] +source_file="res://demo/assets/agent_combo.png" +dest_files=["res://.godot/imported/agent_combo.png-1aabf55a6f7673e34d40eadba34cb086.ctex"] [params] diff --git a/demo/demo/scenes/demo.tscn b/demo/demo/scenes/demo.tscn index a125238..3d424fb 100644 --- a/demo/demo/scenes/demo.tscn +++ b/demo/demo/scenes/demo.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=12 format=3 uid="uid://bsig1usigbbuy"] +[gd_scene load_steps=13 format=3 uid="uid://bsig1usigbbuy"] [ext_resource type="Texture2D" uid="uid://b3g14elmg0m36" path="res://demo/assets/env_rocks.png" id="1_145kx"] [ext_resource type="PackedScene" uid="uid://dt2jlrqffpyw" path="res://demo/scenes/clouds.tscn" id="1_gsxmp"] @@ -7,6 +7,7 @@ [ext_resource type="PackedScene" uid="uid://bpd1wmw2f7bvg" path="res://demo/props/gong.tscn" id="3_nbto3"] [ext_resource type="PackedScene" uid="uid://d07ag5dcje13i" path="res://demo/agents/player/player.tscn" id="5_cmgoj"] [ext_resource type="PackedScene" uid="uid://ci3x5esepbu5v" path="res://demo/agents/agent_ranged.tscn" id="6_76icr"] +[ext_resource type="PackedScene" uid="uid://blxmw2w1h2s0s" path="res://demo/agents/agent_melee_combo.tscn" id="7_jlgir"] [ext_resource type="PackedScene" uid="uid://2e4ohaqjaawb" path="res://demo/agents/agent_melee_nuanced.tscn" id="8_ovfbp"] [sub_resource type="Animation" id="Animation_gwtgs"] @@ -2885,12 +2886,6 @@ position = Vector2(227, 231) [node name="AgentMeleeNuanced5" parent="YSort/Agents" instance=ExtResource("8_ovfbp")] position = Vector2(592, -163) -[node name="AgentMeleeNuanced6" parent="YSort/Agents" instance=ExtResource("8_ovfbp")] -position = Vector2(1589, -87) - -[node name="AgentMeleeNuanced7" parent="YSort/Agents" instance=ExtResource("8_ovfbp")] -position = Vector2(-154, 726) - [node name="Player" parent="YSort/Agents" instance=ExtResource("5_cmgoj")] position = Vector2(633, 256) @@ -2907,6 +2902,9 @@ drag_bottom_margin = 0.1 [node name="AgentRanged" parent="YSort/Agents" instance=ExtResource("6_76icr")] position = Vector2(2067, 543) +[node name="AgentMeleeCombo" parent="YSort/Agents" instance=ExtResource("7_jlgir")] +position = Vector2(1778, 22) + [node name="Props" type="Node2D" parent="YSort"] y_sort_enabled = true metadata/_edit_lock_ = true