From 1e1af4344d79bec612dd41c15e4c7a9de3dda48c Mon Sep 17 00:00:00 2001 From: Sara Date: Tue, 17 Dec 2024 14:13:15 +0100 Subject: [PATCH] feat: improved enemy accuracy, simplified code and removed miss-before-hit --- godot/objects/enemy.tscn | 10 ++++++++++ src/enemy.cpp | 17 +++++++---------- src/enemy.hpp | 11 ++++++----- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/godot/objects/enemy.tscn b/godot/objects/enemy.tscn index 18eef11..9c16079 100644 --- a/godot/objects/enemy.tscn +++ b/godot/objects/enemy.tscn @@ -113,4 +113,14 @@ autoplay = true max_distance = 13.36 panning_strength = 0.17 +[node name="DroneSound2" type="AudioStreamPlayer3D" parent="."] +unique_name_in_owner = true +stream = ExtResource("2_8cpm2") +attenuation_model = 2 +volume_db = 1.0 +unit_size = 27.02 +autoplay = true +max_distance = 13.36 +panning_strength = 0.17 + [editable path="CharacterModel"] diff --git a/src/enemy.cpp b/src/enemy.cpp index 0c58ee5..3596290 100644 --- a/src/enemy.cpp +++ b/src/enemy.cpp @@ -43,7 +43,6 @@ void Enemy::update() { } Enemy::ActionFn Enemy::wait_line_of_sight() { - if(this->can_see_player) return (ActionFn)&Enemy::take_aim; else @@ -51,9 +50,7 @@ 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->get_global_position(), {0.f, 1.f, 0.f}); - this->target_rotation -= gd::Math::sign(this->target_rotation) * this->MISS_ANGLE; - this->target_rotation += this->get_global_rotation().y; + 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->anim_tree->set_aim_weapon(true); if(this->anim_tree->get_current_state().begins_with("Aim") && this->at_target_angle) return (ActionFn)&Enemy::fire; @@ -63,17 +60,12 @@ Enemy::ActionFn Enemy::take_aim() { Enemy::ActionFn Enemy::fire() { this->anim_tree->set_fire_weapon(); - this->missed_shots = (this->missed_shots + 1) % (this->SHOTS_BEFORE_HIT + 1); 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->get_global_position(), {0.f, 1.f, 0.f})}; + 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->missed_shots < this->SHOTS_BEFORE_HIT) - this->target_rotation -= gd::Math::sign(target_diff) * this->MISS_ANGLE; - else if(this->missed_shots >= this->SHOTS_BEFORE_HIT) - this->target_rotation -= gd::Math::sign(target_diff) * this->HIT_ANGLE; if(this->at_target_angle && !this->anim_tree->get_current_state().begins_with("Fire") && !this->anim_tree->get_fire_weapon()) return (ActionFn)&Enemy::fire; @@ -130,3 +122,8 @@ void Enemy::set_update_interval(float time) { float Enemy::get_update_interval() const { return this->update_interval; } + +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 5933b36..a33133c 100644 --- a/src/enemy.hpp +++ b/src/enemy.hpp @@ -32,15 +32,16 @@ public: void set_update_interval(float time); float get_update_interval() const; private: - int const SHOTS_BEFORE_HIT{0}; - float const TURN_SPEED{3.f}; - float const HIT_ANGLE{.05f}; - float const MISS_ANGLE{.2f}; + gd::Vector3 aim_offset_position() const; +private: + int const SHOTS_BEFORE_MOVE{}; + float const TURN_SPEED{8.f}; + float const AIM_OFFSET{-0.18f}; float target_rotation{0.f}; bool at_target_angle{false}; gd::Vector3 last_known_player_position{0.f, 0.f, 0.f}; - int missed_shots{0}; + int shots_fired{0}; double update_interval{0.4}; ActionFn current_action_fn{nullptr}; bool can_see_player{false};