feat: improved enemy accuracy, simplified code and removed miss-before-hit

main
Sara 2024-12-17 14:13:15 +01:00
parent 5246e47f85
commit 1e1af4344d
3 changed files with 23 additions and 15 deletions

View File

@ -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"]

View File

@ -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;
}

View File

@ -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};