From 2b81e808f5575416a30c23a53760575f2067adad Mon Sep 17 00:00:00 2001 From: Sara Date: Thu, 19 Dec 2024 23:09:58 +0100 Subject: [PATCH] fix: enemies will now follow their paths as intended --- godot/models/animations/character_tree.tres | 11 +-- godot/objects/enemy.tscn | 92 ++++--------------- godot/objects/player.tscn | 74 +--------------- src/enemy.cpp | 97 ++++++++++++++++++--- src/enemy.hpp | 20 ++++- src/player_anim_tree.cpp | 10 ++- 6 files changed, 131 insertions(+), 173 deletions(-) diff --git a/godot/models/animations/character_tree.tres b/godot/models/animations/character_tree.tres index 43d2d8c..8cbd16d 100644 --- a/godot/models/animations/character_tree.tres +++ b/godot/models/animations/character_tree.tres @@ -177,7 +177,7 @@ advance_mode = 2 xfade_time = 0.4 priority = 2 advance_mode = 2 -advance_expression = "get_is_running() && is_walking()" +advance_expression = "get_is_running()" [sub_resource type="AnimationNodeStateMachineTransition" id="AnimationNodeStateMachineTransition_ocffv"] xfade_time = 0.3 @@ -187,13 +187,14 @@ advance_expression = "get_is_running() [sub_resource type="AnimationNodeStateMachineTransition" id="AnimationNodeStateMachineTransition_h0my5"] xfade_time = 0.4 +priority = 2 advance_mode = 2 -advance_expression = "!get_is_running() || !is_walking()" +advance_expression = "!get_is_running()" [sub_resource type="AnimationNodeStateMachineTransition" id="AnimationNodeStateMachineTransition_nymcr"] xfade_time = 0.3 advance_mode = 2 -advance_expression = "!get_is_running()" +advance_expression = "!get_is_running() && get_is_walking()" [sub_resource type="AnimationNodeStateMachineTransition" id="AnimationNodeStateMachineTransition_g5qf0"] xfade_time = 0.5 @@ -234,7 +235,7 @@ advance_mode = 2 [sub_resource type="AnimationNodeStateMachine" id="AnimationNodeStateMachine_7smdg"] "states/Aim Down [aim]/node" = SubResource("AnimationNodeAnimation_ukhqm") -"states/Aim Down [aim]/position" = Vector2(831.525, 251.767) +"states/Aim Down [aim]/position" = Vector2(919.525, 258.767) "states/Aim [aim] [turn]/node" = SubResource("AnimationNodeAnimation_nhlru") "states/Aim [aim] [turn]/position" = Vector2(919.525, 94.3447) states/End/position = Vector2(195.182, 94.3447) @@ -254,7 +255,7 @@ states/Start/position = Vector2(572.26, 14.954) "states/Walk [turn]/node" = SubResource("AnimationNodeBlendTree_2mbyh") "states/Walk [turn]/position" = Vector2(572.26, 258.767) transitions = ["Aim [aim] [turn]", "Aim Down [aim]", SubResource("AnimationNodeStateMachineTransition_4lybd"), "Aim Down [aim]", "Walk [turn]", SubResource("AnimationNodeStateMachineTransition_5aafd"), "Aim [aim] [turn]", "Fire [aim]", SubResource("AnimationNodeStateMachineTransition_8f4gl"), "Fire [aim]", "Aim [aim] [turn]", SubResource("AnimationNodeStateMachineTransition_xnat3"), "Walk [turn]", "Aim [aim] [turn]", SubResource("AnimationNodeStateMachineTransition_12bos"), "Aim Down [aim]", "Stationary [turn]", SubResource("AnimationNodeStateMachineTransition_n0ndr"), "Walk [turn]", "Stationary [turn]", SubResource("AnimationNodeStateMachineTransition_0wc5e"), "Stationary [turn]", "Walk [turn]", SubResource("AnimationNodeStateMachineTransition_4hisb"), "Start", "Stationary [turn]", SubResource("AnimationNodeStateMachineTransition_bmty6"), "Stationary [turn]", "RESET To Aim", SubResource("AnimationNodeStateMachineTransition_kyd6p"), "RESET To Aim", "Aim [aim] [turn]", SubResource("AnimationNodeStateMachineTransition_w5kob"), "Stationary [turn]", "Run", SubResource("AnimationNodeStateMachineTransition_g0i3x"), "Walk [turn]", "Run", SubResource("AnimationNodeStateMachineTransition_ocffv"), "Run", "Stationary [turn]", SubResource("AnimationNodeStateMachineTransition_h0my5"), "Run", "Walk [turn]", SubResource("AnimationNodeStateMachineTransition_nymcr"), "Run", "Stab", SubResource("AnimationNodeStateMachineTransition_g5qf0"), "Stab", "Run", SubResource("AnimationNodeStateMachineTransition_27kmb"), "Stationary [turn]", "Stab", SubResource("AnimationNodeStateMachineTransition_4q0ot"), "Stab", "Stationary [turn]", SubResource("AnimationNodeStateMachineTransition_h6ujc"), "Walk [turn]", "Stab", SubResource("AnimationNodeStateMachineTransition_3bu8l"), "Stab", "Walk [turn]", SubResource("AnimationNodeStateMachineTransition_cpibo"), "Hit", "Stationary [turn]", SubResource("AnimationNodeStateMachineTransition_0ppx7")] -graph_offset = Vector2(-87.4971, -116.108) +graph_offset = Vector2(25.0029, -61.608) [sub_resource type="AnimationNodeAnimation" id="AnimationNodeAnimation_au62i"] animation = &"Fall_die" diff --git a/godot/objects/enemy.tscn b/godot/objects/enemy.tscn index 18eef11..420df04 100644 --- a/godot/objects/enemy.tscn +++ b/godot/objects/enemy.tscn @@ -4,7 +4,7 @@ [ext_resource type="AudioStream" uid="uid://b5evvhyrhybkf" path="res://sounds/drone.ogg" id="2_8cpm2"] [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_3tduq"] -radius = 0.370388 +radius = 0.211247 height = 2.38725 [node name="Enemy" type="Enemy"] @@ -15,86 +15,15 @@ collision_mask = 3 [node name="CharacterModel" parent="." instance=ExtResource("1_i7aop")] unique_name_in_owner = true -[node name="Skeleton3D" parent="CharacterModel/skeleton_character" index="0"] -bones/0/rotation = Quaternion(1.12928e-22, 1, 2.98023e-08, -1.94707e-07) -bones/1/position = Vector3(3.1225e-17, 0.826102, -2.26306e-09) -bones/1/rotation = Quaternion(-0.0189874, -6.30661e-09, -1.08732e-09, 0.99982) -bones/2/rotation = Quaternion(0.0262842, 8.66579e-09, 1.56723e-09, 0.999655) -bones/3/rotation = Quaternion(-0.013812, -0.124595, 8.42949e-05, 0.992112) -bones/4/rotation = Quaternion(0.0100466, -3.89644e-10, 6.78268e-10, 0.99995) -bones/5/rotation = Quaternion(-0.0233085, 0.0669743, -0.00156502, 0.997481) -bones/6/rotation = Quaternion(0.623077, 0.672557, 0.307599, -0.254607) -bones/7/rotation = Quaternion(-0.311931, -0.528262, 0.360764, 0.702487) -bones/8/rotation = Quaternion(0.385362, 0.19105, 0.552644, 0.713849) -bones/9/rotation = Quaternion(0.204715, 0.0914037, 0.275611, 0.93476) -bones/10/rotation = Quaternion(-0.160687, -0.0459478, -0.0475321, 0.984789) -bones/11/rotation = Quaternion(-0.030518, -0.0130498, -0.0216154, 0.999215) -bones/12/rotation = Quaternion(0.0087248, 0.00565774, 0.0013103, 0.999945) -bones/13/rotation = Quaternion(-0.617934, -0.0462445, 0.180663, 0.763793) -bones/14/rotation = Quaternion(-0.23045, 0.0743406, 0.160693, 0.956841) -bones/15/rotation = Quaternion(-0.541263, -0.00752765, 0.238029, 0.806424) -bones/16/rotation = Quaternion(-0.611024, -0.262913, 0.402215, 0.629087) -bones/17/rotation = Quaternion(0.0158022, 0.0368156, 0.177499, 0.983305) -bones/18/rotation = Quaternion(-0.115784, 0.0786986, 0.503797, 0.852402) -bones/19/rotation = Quaternion(-0.77808, 0.139756, 0.0140334, 0.612261) -bones/20/rotation = Quaternion(-0.0923591, 0.0238578, 0.0736642, 0.992711) -bones/21/rotation = Quaternion(-0.193467, 0.101022, 0.0447897, 0.974864) -bones/22/rotation = Quaternion(-0.293695, -0.10649, 0.259809, 0.91373) -bones/23/rotation = Quaternion(-0.0310475, -0.0162325, -0.00151601, 0.999385) -bones/24/rotation = Quaternion(0.140362, -0.0289769, -0.195164, 0.970242) -bones/25/rotation = Quaternion(0.585184, -0.476496, -0.473244, -0.454479) -bones/26/rotation = Quaternion(-0.449756, 0.301541, -0.311322, 0.780943) -bones/27/rotation = Quaternion(-0.122233, -0.135947, -0.565028, 0.804562) -bones/28/rotation = Quaternion(0.155388, -0.432411, -0.521703, 0.71882) -bones/29/rotation = Quaternion(-0.0486368, 0.0590466, -0.0759536, 0.994173) -bones/30/rotation = Quaternion(-0.0305179, 0.0130499, 0.0216153, 0.999215) -bones/31/rotation = Quaternion(-0.518356, -0.00412336, -0.00408935, 0.855145) -bones/32/rotation = Quaternion(-0.290993, 0.0788459, 0.0248015, 0.953148) -bones/33/rotation = Quaternion(-0.419789, -0.067382, -0.039477, 0.904256) -bones/34/rotation = Quaternion(-0.266884, 0.00530696, 0.000658852, 0.963714) -bones/35/rotation = Quaternion(-0.422483, 0.214372, -0.00100308, 0.880654) -bones/36/rotation = Quaternion(-0.345783, -0.0439312, -0.261553, 0.900052) -bones/37/rotation = Quaternion(-0.0107009, 0.0076969, 0.00365465, 0.999906) -bones/38/rotation = Quaternion(-0.389883, 0.108139, -0.120461, 0.906525) -bones/39/rotation = Quaternion(-0.399909, -0.0921328, -0.130163, 0.902575) -bones/40/rotation = Quaternion(-0.0125688, 0.0114118, 0.00134583, 0.999855) -bones/41/rotation = Quaternion(-0.0497163, 0.0839878, -0.166226, 0.981246) -bones/42/rotation = Quaternion(-0.0310475, 0.0162325, 0.00151601, 0.999385) -bones/43/rotation = Quaternion(-0.00109124, -0.0104143, 0.0283538, 0.999543) -bones/44/position = Vector3(0.16719, 0.195796, -0.0454406) -bones/44/rotation = Quaternion(-0.610992, 0.0971646, 0.14821, 0.771545) -bones/45/rotation = Quaternion(-0.82326, 4.1804e-09, -1.03579e-07, 0.567665) -bones/46/position = Vector3(8.07956e-09, 0.0363644, 0.0392254) -bones/46/rotation = Quaternion(-0.00112372, -6.23921e-08, 6.44731e-09, 0.999999) -bones/48/rotation = Quaternion(0.706312, -2.55434e-08, 6.24918e-08, 0.707901) -bones/49/position = Vector3(0.0228929, -0.0282939, -0.00824324) -bones/49/rotation = Quaternion(-0.112129, 0.585203, -0.155744, 0.78785) -bones/50/position = Vector3(-0.0246244, 0.246628, -0.0197923) -bones/50/rotation = Quaternion(0.31578, 0.920316, 0.115311, -0.200014) -bones/51/position = Vector3(0.0902225, 0.042579, 0.00142519) -bones/51/rotation = Quaternion(0.997309, -0.00549226, 0.0715033, -0.0152251) -bones/52/rotation = Quaternion(-0.00225666, 0.00306653, -0.000453776, 0.999993) -bones/53/rotation = Quaternion(0.665461, 0.000165138, -0.00238783, 0.746429) -bones/54/position = Vector3(-0.0902225, 0.042579, 0.00142518) -bones/54/rotation = Quaternion(0.997309, 0.00549227, -0.0715033, -0.0152251) -bones/55/rotation = Quaternion(-0.00225666, -0.0030665, 0.000453778, 0.999993) -bones/56/rotation = Quaternion(0.665461, -0.000165001, 0.00238774, 0.746429) -bones/57/position = Vector3(3.64125e-07, 0.0462301, -0.935059) -bones/58/rotation = Quaternion(-2.99921e-13, 0.707107, 0.707107, -2.99921e-13) -bones/59/rotation = Quaternion(2.01446e-13, 0.707107, 0.707107, 2.01446e-13) -bones/60/rotation = Quaternion(-0.707107, 1.25097e-07, 1.25097e-07, 0.707107) -bones/61/rotation = Quaternion(-0.707107, 1.25097e-07, 1.25097e-07, 0.707107) -bones/62/rotation = Quaternion(-0.707107, 1.5189e-07, 1.5189e-07, 0.707107) -bones/63/rotation = Quaternion(-0.707107, 1.5189e-07, 1.5189e-07, 0.707107) - [node name="mesh_helmet_a" parent="CharacterModel/skeleton_character/Skeleton3D" index="1"] visible = false [node name="BoneAttachment3D" parent="CharacterModel/skeleton_character/Skeleton3D" index="3"] -transform = Transform3D(-0.999748, -0.00611106, 0.0216074, 0.0216358, -0.00461901, 0.999755, -0.00600976, 0.999971, 0.00475004, -0.156654, 1.50959, 0.292832) +transform = Transform3D(-0.937186, 0.347435, 0.0311762, 0.0973187, 0.17459, 0.97982, 0.33498, 0.921308, -0.197435, -0.16719, 1.02003, 0.0528419) [node name="AnimationTree" parent="CharacterModel" index="2"] -aim_weapon = true +parameters/Actions/Run/Speed/blend_amount = 0.0 +"parameters/Actions/Walk [turn]/Speed/blend_amount" = 0.0 [node name="CollisionShape3D" type="CollisionShape3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.19104, 0) @@ -102,6 +31,9 @@ shape = SubResource("CapsuleShape3D_3tduq") [node name="NavigationAgent3D" type="NavigationAgent3D" parent="."] unique_name_in_owner = true +path_desired_distance = 0.5 +simplify_path = true +debug_enabled = true [node name="DroneSound" type="AudioStreamPlayer3D" parent="."] unique_name_in_owner = true @@ -113,4 +45,14 @@ autoplay = true max_distance = 13.36 panning_strength = 0.17 +[node name="DebugLabel" type="Label3D" parent="."] +unique_name_in_owner = true +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.09348, 0) +ignore_occlusion_culling = true +pixel_size = 0.003 +billboard = 1 +no_depth_test = true +texture_filter = 0 +text = "State: None" + [editable path="CharacterModel"] diff --git a/godot/objects/player.tscn b/godot/objects/player.tscn index 17afb2a..b24ed2d 100644 --- a/godot/objects/player.tscn +++ b/godot/objects/player.tscn @@ -3,7 +3,7 @@ [ext_resource type="PackedScene" uid="uid://bnr6lab7heo8e" path="res://objects/character_model_base.tscn" id="1_cwt7u"] [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_ewsvd"] -radius = 0.370388 +radius = 0.22 [sub_resource type="SphereShape3D" id="SphereShape3D_v7ajo"] radius = 0.1 @@ -16,78 +16,6 @@ floor_snap_length = 1.0 [node name="CharacterModel" parent="." instance=ExtResource("1_cwt7u")] unique_name_in_owner = true -[node name="Skeleton3D" parent="CharacterModel/skeleton_character" index="0"] -bones/0/rotation = Quaternion(1.12928e-22, 1, 2.98023e-08, -1.94707e-07) -bones/1/position = Vector3(3.1225e-17, 0.826102, -2.26306e-09) -bones/1/rotation = Quaternion(-0.0189874, -6.30661e-09, -1.08732e-09, 0.99982) -bones/2/rotation = Quaternion(0.0262842, 8.66579e-09, 1.56723e-09, 0.999655) -bones/3/rotation = Quaternion(-0.013812, -0.124595, 8.42949e-05, 0.992112) -bones/4/rotation = Quaternion(0.0100466, -3.89644e-10, 6.78268e-10, 0.99995) -bones/5/rotation = Quaternion(-0.0233085, 0.0669743, -0.00156502, 0.997481) -bones/6/rotation = Quaternion(0.623077, 0.672557, 0.307599, -0.254607) -bones/7/rotation = Quaternion(-0.311931, -0.528262, 0.360764, 0.702487) -bones/8/rotation = Quaternion(0.385362, 0.19105, 0.552644, 0.713849) -bones/9/rotation = Quaternion(0.204715, 0.0914037, 0.275611, 0.93476) -bones/10/rotation = Quaternion(-0.160687, -0.0459478, -0.0475321, 0.984789) -bones/11/rotation = Quaternion(-0.030518, -0.0130498, -0.0216154, 0.999215) -bones/12/rotation = Quaternion(0.0087248, 0.00565774, 0.0013103, 0.999945) -bones/13/rotation = Quaternion(-0.617934, -0.0462445, 0.180663, 0.763793) -bones/14/rotation = Quaternion(-0.23045, 0.0743406, 0.160693, 0.956841) -bones/15/rotation = Quaternion(-0.541263, -0.00752765, 0.238029, 0.806424) -bones/16/rotation = Quaternion(-0.611024, -0.262913, 0.402215, 0.629087) -bones/17/rotation = Quaternion(0.0158022, 0.0368156, 0.177499, 0.983305) -bones/18/rotation = Quaternion(-0.115784, 0.0786986, 0.503797, 0.852402) -bones/19/rotation = Quaternion(-0.77808, 0.139756, 0.0140334, 0.612261) -bones/20/rotation = Quaternion(-0.0923591, 0.0238578, 0.0736642, 0.992711) -bones/21/rotation = Quaternion(-0.193467, 0.101022, 0.0447897, 0.974864) -bones/22/rotation = Quaternion(-0.293695, -0.10649, 0.259809, 0.91373) -bones/23/rotation = Quaternion(-0.0310475, -0.0162325, -0.00151601, 0.999385) -bones/24/rotation = Quaternion(0.140362, -0.0289769, -0.195164, 0.970242) -bones/25/rotation = Quaternion(0.585184, -0.476496, -0.473244, -0.454479) -bones/26/rotation = Quaternion(-0.449756, 0.301541, -0.311322, 0.780943) -bones/27/rotation = Quaternion(-0.122233, -0.135947, -0.565028, 0.804562) -bones/28/rotation = Quaternion(0.155388, -0.432411, -0.521703, 0.71882) -bones/29/rotation = Quaternion(-0.0486368, 0.0590466, -0.0759536, 0.994173) -bones/30/rotation = Quaternion(-0.0305179, 0.0130499, 0.0216153, 0.999215) -bones/31/rotation = Quaternion(-0.518356, -0.00412336, -0.00408935, 0.855145) -bones/32/rotation = Quaternion(-0.290993, 0.0788459, 0.0248015, 0.953148) -bones/33/rotation = Quaternion(-0.419789, -0.067382, -0.039477, 0.904256) -bones/34/rotation = Quaternion(-0.266884, 0.00530696, 0.000658852, 0.963714) -bones/35/rotation = Quaternion(-0.422483, 0.214372, -0.00100308, 0.880654) -bones/36/rotation = Quaternion(-0.345783, -0.0439312, -0.261553, 0.900052) -bones/37/rotation = Quaternion(-0.0107009, 0.0076969, 0.00365465, 0.999906) -bones/38/rotation = Quaternion(-0.389883, 0.108139, -0.120461, 0.906525) -bones/39/rotation = Quaternion(-0.399909, -0.0921328, -0.130163, 0.902575) -bones/40/rotation = Quaternion(-0.0125688, 0.0114118, 0.00134583, 0.999855) -bones/41/rotation = Quaternion(-0.0497163, 0.0839878, -0.166226, 0.981246) -bones/42/rotation = Quaternion(-0.0310475, 0.0162325, 0.00151601, 0.999385) -bones/43/rotation = Quaternion(-0.00109124, -0.0104143, 0.0283538, 0.999543) -bones/44/position = Vector3(0.16719, 0.195796, -0.0454406) -bones/44/rotation = Quaternion(-0.610992, 0.0971646, 0.14821, 0.771545) -bones/45/rotation = Quaternion(-0.82326, 4.1804e-09, -1.03579e-07, 0.567665) -bones/46/position = Vector3(8.07956e-09, 0.0363644, 0.0392254) -bones/46/rotation = Quaternion(-0.00112372, -6.23921e-08, 6.44731e-09, 0.999999) -bones/48/rotation = Quaternion(0.706312, -2.55434e-08, 6.24918e-08, 0.707901) -bones/49/position = Vector3(0.0228929, -0.0282939, -0.00824324) -bones/49/rotation = Quaternion(-0.112129, 0.585203, -0.155744, 0.78785) -bones/50/position = Vector3(-0.0246244, 0.246628, -0.0197923) -bones/50/rotation = Quaternion(0.31578, 0.920316, 0.115311, -0.200014) -bones/51/position = Vector3(0.0902225, 0.042579, 0.00142519) -bones/51/rotation = Quaternion(0.997309, -0.00549226, 0.0715033, -0.0152251) -bones/52/rotation = Quaternion(-0.00225666, 0.00306653, -0.000453776, 0.999993) -bones/53/rotation = Quaternion(0.665461, 0.000165138, -0.00238783, 0.746429) -bones/54/position = Vector3(-0.0902225, 0.042579, 0.00142518) -bones/54/rotation = Quaternion(0.997309, 0.00549227, -0.0715033, -0.0152251) -bones/55/rotation = Quaternion(-0.00225666, -0.0030665, 0.000453778, 0.999993) -bones/56/rotation = Quaternion(0.665461, -0.000165001, 0.00238774, 0.746429) -bones/57/position = Vector3(3.64125e-07, 0.0462301, -0.935059) -bones/58/rotation = Quaternion(-2.99921e-13, 0.707107, 0.707107, -2.99921e-13) -bones/59/rotation = Quaternion(2.01446e-13, 0.707107, 0.707107, 2.01446e-13) -bones/60/rotation = Quaternion(-0.707107, 1.25097e-07, 1.25097e-07, 0.707107) -bones/61/rotation = Quaternion(-0.707107, 1.25097e-07, 1.25097e-07, 0.707107) -bones/62/rotation = Quaternion(-0.707107, 1.5189e-07, 1.5189e-07, 0.707107) -bones/63/rotation = Quaternion(-0.707107, 1.5189e-07, 1.5189e-07, 0.707107) - [node name="mesh_helmet_a_001" parent="CharacterModel/skeleton_character/Skeleton3D" index="2"] visible = false diff --git a/src/enemy.cpp b/src/enemy.cpp index 3596290..9b1e618 100644 --- a/src/enemy.cpp +++ b/src/enemy.cpp @@ -21,11 +21,15 @@ void Enemy::_ready() { timer->connect("timeout", callable_mp(this, &Enemy::update)); this->target_rotation = this->get_rotation().y; this->drone_sound = this->get_node("%DroneSound"); + if(this->has_node("%DebugLabel")) { + this->debug_label = this->get_node("%DebugLabel"); + } } void Enemy::_process(double delta) { + this->set_current_state_name(this->current_state_name); float const angle_left{this->target_rotation - this->get_rotation().y}; - float const step(gd::Math::sign(angle_left) * delta * this->TURN_SPEED); + float const step(gd::Math::sign(angle_left) * delta * (this->anim_tree->get_current_state().begins_with("Run") ? this->TURN_SPEED : this->AIM_SPEED)); if(gd::Math::abs(angle_left) <= gd::Math::abs(step)) { this->rotate_y(angle_left); this->at_target_angle = true; @@ -33,16 +37,22 @@ void Enemy::_process(double delta) { this->rotate_y(step); this->at_target_angle = false; } + if(this->current_action_fn == (ActionFn)&Enemy::chase_player) + this->target_rotation = gd::Vector3{0.f, 0.f, 1.f}.signed_angle_to(this->agent->get_next_path_position() - this->get_global_position(), {0.f, 1.f, 0.f}); } void Enemy::update() { - if(this->can_see_player) + if(this->can_see_player) { this->last_known_player_position = this->player->get_global_position(); + this->last_known_player_rotation = -this->player->get_global_rotation().y; + this->shots_fired = 0; + } if(this->current_action_fn != nullptr) this->current_action_fn = (ActionFn)(this->*current_action_fn)(); } Enemy::ActionFn Enemy::wait_line_of_sight() { + this->set_current_state_name("Guard"); if(this->can_see_player) return (ActionFn)&Enemy::take_aim; else @@ -50,8 +60,10 @@ Enemy::ActionFn Enemy::wait_line_of_sight() { } Enemy::ActionFn Enemy::take_aim() { - this->target_rotation = this->get_global_basis().get_column(2).signed_angle_to(this->last_known_player_position - this->aim_offset_position(), {0.f, 1.f, 0.f}) + this->get_global_rotation().y; + this->set_current_state_name("Aim"); + this->target_rotation = gd::Vector3{0.f, 0.f, 1.f}.signed_angle_to(this->last_known_player_position - this->aim_offset_position(), {0.f, 1.f, 0.f}); this->anim_tree->set_aim_weapon(true); + if(this->anim_tree->get_current_state().begins_with("Aim") && this->at_target_angle) return (ActionFn)&Enemy::fire; else @@ -59,30 +71,80 @@ Enemy::ActionFn Enemy::take_aim() { } Enemy::ActionFn Enemy::fire() { + this->set_current_state_name("Shoot (fire)"); + ++this->shots_fired; this->anim_tree->set_fire_weapon(); return (ActionFn)&Enemy::wait_end_of_shot; } Enemy::ActionFn Enemy::wait_end_of_shot() { - float const target_diff{this->get_global_basis().get_column(2).signed_angle_to(this->last_known_player_position - this->aim_offset_position(), {0.f, 1.f, 0.f})}; - this->target_rotation = this->get_global_rotation().y + target_diff; + this->set_current_state_name("Shoot (wait)"); + this->target_rotation = gd::Vector3{0.f, 0.f, 1.f}.signed_angle_to(this->last_known_player_position - this->aim_offset_position(), {0.f, 1.f, 0.f}); if(this->at_target_angle && !this->anim_tree->get_current_state().begins_with("Fire") && !this->anim_tree->get_fire_weapon()) - return (ActionFn)&Enemy::fire; - else - return (ActionFn)&Enemy::wait_end_of_shot; + return this->shots_fired < this->SHOTS_BEFORE_MOVE ? (ActionFn)&Enemy::fire : (ActionFn)&Enemy::chase_enter; + return (ActionFn)&Enemy::wait_end_of_shot; } -void Enemy::_physics_process(double delta [[maybe_unused]]) { - this->update_can_see_player(); +Enemy::ActionFn Enemy::stab() { + this->set_current_state_name("Stab (start)"); + this->anim_tree->set_aim_weapon(false); + if(this->anim_tree->get_current_state().begins_with("Aim")) + return (ActionFn)&Enemy::stab; + this->anim_tree->set_stab(); + float const target_diff{this->get_global_basis().get_column(2).signed_angle_to(this->last_known_player_position - this->aim_offset_position(), {0.f, 1.f, 0.f})}; + this->target_rotation = this->get_global_rotation().y + target_diff; + return (ActionFn)&Enemy::wait_end_of_stab; +} + +Enemy::ActionFn Enemy::wait_end_of_stab() { + this->set_current_state_name("Stab (wait)"); + float const target_diff{this->get_global_basis().get_column(2).signed_angle_to(this->last_known_player_position - this->aim_offset_position(), {0.f, 1.f, 0.f})}; + this->target_rotation = this->get_global_rotation().y + target_diff; + if(this->at_target_angle && !this->anim_tree->get_current_state().begins_with("Stab") && !this->anim_tree->get_fire_weapon()) + return this->is_in_stab_range() ? (ActionFn)&Enemy::chase_enter : (ActionFn)&Enemy::stab; + return (ActionFn)&Enemy::wait_end_of_stab; +} + +Enemy::ActionFn Enemy::chase_enter() { + this->set_current_state_name("Chase (plot)"); + this->anim_tree->set_aim_weapon(false); + this->agent->set_target_position(this->last_known_player_position); + return (ActionFn)&Enemy::chase_player; +} + +Enemy::ActionFn Enemy::chase_player() { + this->set_current_state_name("Chase (run)"); + if(this->can_see_player && this->is_in_stab_range()) { + this->anim_tree->set_lock_running(false); + return (ActionFn)&Enemy::stab; + } else if(this->agent->is_navigation_finished() || this->is_on_wall()) { + this->target_rotation = this->last_known_player_rotation; + this->anim_tree->set_lock_running(false); + return (ActionFn)&Enemy::stop_running; + } else { + this->anim_tree->set_lock_running(true); + return (ActionFn)&Enemy::chase_player; + } +} + +Enemy::ActionFn Enemy::stop_running() { + this->set_current_state_name("Chase (stop)"); + this->agent->set_target_position(this->get_global_position()); + return this->anim_tree->get_current_state().begins_with("Run") ? (ActionFn)&Enemy::stop_running : (ActionFn)&Enemy::wait_line_of_sight; +} + +void Enemy::_physics_process(double delta) { gd::Basis const basis{this->get_global_basis()}; gd::Vector3 const motion{this->anim_tree->get_root_motion_position()}; - this->set_velocity({ + this->set_velocity(gd::Vector3{ basis.get_column(0) * motion.x + basis.get_column(1) * motion.y + basis.get_column(2) * motion.z - }); + } / delta); this->move_and_slide(); + + this->update_can_see_player(); } void Enemy::damage() { @@ -92,6 +154,8 @@ void Enemy::damage() { this->set_process(false); this->set_physics_process(false); this->drone_sound->stop(); + this->current_action_fn = nullptr; + this->set_current_state_name("None"); } void Enemy::notice_player(Player *player) { @@ -123,6 +187,15 @@ float Enemy::get_update_interval() const { return this->update_interval; } +bool Enemy::is_in_stab_range() const { + return this->player->get_global_position().distance_squared_to(this->get_global_position()) < this->STAB_RANGE*this->STAB_RANGE; +} + +void Enemy::set_current_state_name(gd::String name) { + this->debug_label->set_text(gd::vformat("Action: %s\nState: %s", name, this->anim_tree->get_current_state())); + this->current_state_name = name; +} + gd::Vector3 Enemy::aim_offset_position() const { gd::Basis const basis{this->get_global_basis()}; return this->get_global_position() + basis.get_column(0) * this->AIM_OFFSET; diff --git a/src/enemy.hpp b/src/enemy.hpp index a33133c..80e71b6 100644 --- a/src/enemy.hpp +++ b/src/enemy.hpp @@ -7,6 +7,7 @@ #include "utils/godot_macros.hpp" #include #include +#include #include namespace gd = godot; @@ -22,25 +23,33 @@ public: ActionFn wait_line_of_sight(); ActionFn take_aim(); ActionFn fire(); - ActionFn stab_enter(); - ActionFn stab(); ActionFn wait_end_of_shot(); + ActionFn stab(); + ActionFn wait_end_of_stab(); + ActionFn chase_enter(); + ActionFn chase_player(); + ActionFn stop_running(); virtual void _physics_process(double delta) override; virtual void damage() override; void notice_player(Player *player); void update_can_see_player(); void set_update_interval(float time); float get_update_interval() const; + bool is_in_stab_range() const; private: + void set_current_state_name(gd::String name); gd::Vector3 aim_offset_position() const; private: - int const SHOTS_BEFORE_MOVE{}; - float const TURN_SPEED{8.f}; + int const SHOTS_BEFORE_MOVE{5}; + float const AIM_SPEED{8.f}; + float const TURN_SPEED{10.f}; float const AIM_OFFSET{-0.18f}; + float const STAB_RANGE{2.f}; float target_rotation{0.f}; bool at_target_angle{false}; gd::Vector3 last_known_player_position{0.f, 0.f, 0.f}; + float last_known_player_rotation{0.f}; int shots_fired{0}; double update_interval{0.4}; ActionFn current_action_fn{nullptr}; @@ -49,6 +58,9 @@ private: gd::NavigationAgent3D *agent{nullptr}; PlayerAnimTree *anim_tree{nullptr}; gd::AudioStreamPlayer3D *drone_sound{nullptr}; + gd::String current_state_name{}; + + gd::Label3D *debug_label{nullptr}; }; #endif // !ENEMY_HPP diff --git a/src/player_anim_tree.cpp b/src/player_anim_tree.cpp index 119181c..dcc8b50 100644 --- a/src/player_anim_tree.cpp +++ b/src/player_anim_tree.cpp @@ -5,7 +5,7 @@ void PlayerAnimTree::_bind_methods() { #define CLASSNAME PlayerAnimTree GDPROPERTY(target_turn_speed, gd::Variant::FLOAT); - GDPROPERTY(is_walking, gd::Variant::FLOAT); + GDPROPERTY(is_walking, gd::Variant::BOOL); GDPROPERTY(walk_speed, gd::Variant::FLOAT); GDPROPERTY(lock_running, gd::Variant::BOOL); GDFUNCTION(get_is_running); @@ -69,6 +69,7 @@ float PlayerAnimTree::get_walk_speed() const { void PlayerAnimTree::set_lock_running(bool value) { this->lock_running = value; + this->commit_walk_speed(); } bool PlayerAnimTree::get_lock_running() const { @@ -80,7 +81,7 @@ void PlayerAnimTree::set_is_running() { } bool PlayerAnimTree::get_is_running() const { - return this->lock_running || this->running_time > 0.0; + return this->lock_running || (this->running_time > 0.0 && this->get_is_walking()); } void PlayerAnimTree::set_aim_weapon(bool value) { @@ -146,6 +147,7 @@ void PlayerAnimTree::commit_turn_speed() { } void PlayerAnimTree::commit_walk_speed() { - this->set("parameters/Actions/Walk [turn]/Speed/blend_amount", this->walk_speed); - this->set("parameters/Actions/Run/Speed/blend_amount", this->walk_speed); + float const true_speed{this->lock_running ? 1.f : this->walk_speed}; + this->set("parameters/Actions/Walk [turn]/Speed/blend_amount", true_speed); + this->set("parameters/Actions/Run/Speed/blend_amount", true_speed); }