From 2e4591c3dfba33934ccb5d105bb425820ef2c61f Mon Sep 17 00:00:00 2001
From: Sara <sara@saragerretsen.nl>
Date: Sun, 19 May 2024 15:49:48 +0200
Subject: [PATCH] feat: cleanup

---
 src/character_actor.cpp | 41 +++++++++++++++++------------------------
 1 file changed, 17 insertions(+), 24 deletions(-)

diff --git a/src/character_actor.cpp b/src/character_actor.cpp
index fd922a4..dd0585f 100644
--- a/src/character_actor.cpp
+++ b/src/character_actor.cpp
@@ -6,7 +6,6 @@
 #include "tunnels_game_mode.hpp"
 #include "utils/game_root.hpp"
 #include "utils/godot_macros.h"
-#include <cmath>
 #include <godot_cpp/classes/navigation_agent3d.hpp>
 #include <godot_cpp/classes/time.hpp>
 #include <godot_cpp/variant/callable.hpp>
@@ -63,7 +62,7 @@ void CharacterActor::move(Vector3 world_vector) {
 }
 
 void CharacterActor::aim(Vector3 at) {
-    // calculate the forward vector by normalized difference between player character and the target on the XZ plane
+    // the forward vector by normalized difference between player character and the target on the XZ plane
     Vector3 const pos_flat{this->get_global_position().x, 0.f, this->get_global_position().z};
     Vector3 const target_flat{at.x, 0.f, at.z};
     Vector3 const muzzle_flat{this->weapon_muzzle->get_global_position().x, 0.f, this->weapon_muzzle->get_global_position().z};
@@ -105,7 +104,6 @@ void CharacterActor::set_firing(bool firing) {
 void CharacterActor::set_manual_mode(bool value) {
     this->mode_manual = value;
     ProcessMode const mode = value ?  ProcessMode::PROCESS_MODE_DISABLED : ProcessMode::PROCESS_MODE_PAUSABLE;
-    //this->nav_agent->set_process_mode(mode);
     this->nav_agent->set_avoidance_priority(value ? 1.f : 0.9f);
     this->set_state(goap::State::new_invalid());
 }
@@ -259,35 +257,30 @@ void CharacterActor::process_behaviour(double delta_time) {
 }
 
 void CharacterActor::process_navigation(double delta_time) {
-    float const distance_sqr = this->nav_agent->get_target_position().distance_squared_to(this->get_global_position());
-    float const distance_target_sqr = std::pow(this->nav_agent->get_target_desired_distance(), 2.f);
-    if(!this->nav_agent->is_navigation_finished() && distance_sqr >= distance_target_sqr) {
-        Vector3 const target_position = this->nav_agent->get_next_path_position();
-        Vector3 const direction = (target_position - this->get_global_position()).normalized();
-        if(this->nav_agent->get_avoidance_enabled())
-            this->nav_agent->set_velocity(direction * CharacterActor::walk_speed);
-        else
-            this->move(direction);
-    }
+    float const sqr_dist = this->nav_agent->get_target_position().distance_squared_to(this->get_global_position());
+    float const sqr_dist_target = Math::pow(this->nav_agent->get_target_desired_distance(), 2.0);
+    if(this->nav_agent->is_navigation_finished() || sqr_dist < sqr_dist_target)
+        return;
+    Vector3 const target_pos = this->nav_agent->get_next_path_position();
+    Vector3 const direction = (target_pos - this->get_global_position()).normalized();
+    if(this->nav_agent->get_avoidance_enabled())
+        this->nav_agent->set_velocity(direction * CharacterActor::walk_speed);
+    else
+        this->move(direction);
 }
 
 void CharacterActor::process_rotation(double delta_time) {
-    // copy the current transform and basis matrix
-    Transform3D trans{this->get_global_transform()};
-    Basis basis = trans.get_basis();
-    // construct the current rotation ..
+    Basis basis = this->get_global_basis();
     Quaternion const current_quaternion = basis.get_rotation_quaternion();
-    // .. and the target rotation from their respective bases
     Quaternion const target_quaternion = this->target_rotation.get_rotation_quaternion();
-    // calculate the angle that still needs to be traveled
+    // the angle that still needs to be traveled
     float const angle = current_quaternion.angle_to(target_quaternion);
     // calculate the angle amount that can be moved this frame
-    float const angle_step{float(this->rotation_speed_curve->sample(angle) * CharacterActor::rotation_speed * delta_time)};
+    float const step{float(this->rotation_speed_curve->sample(angle) * CharacterActor::rotation_speed * delta_time)};
     // update this object's global transform with the new rotation
-    basis.set_quaternion(angle < angle_step ? target_quaternion // to avoid overshooting, check if the max step is smaller than the angle distance
-                         : current_quaternion.slerp(target_quaternion, angle_step / angle)); // convert the angle step to a lerp t value between current and target rotations
-    trans.set_basis(basis);
-    this->set_global_transform(trans);
+    basis.set_quaternion(angle < step ? target_quaternion // to avoid overshooting, check if the max step is smaller than the angle distance
+                         : current_quaternion.slerp(target_quaternion, step / angle)); // convert the angle step to a lerp t value between current and target rotations
+    this->set_global_basis(basis);
 }
 
 void CharacterActor::try_fire_weapon() {