From 2b253a824837396e474eb914fc2ede4bbad3fa04 Mon Sep 17 00:00:00 2001 From: David Markowitz <39972741+EmosewaMC@users.noreply.github.com> Date: Sun, 24 Mar 2024 20:24:38 -0700 Subject: [PATCH] fix: movement ai remove goto, do todo, remove unused call (#1505) * remove goto * Update MovementAIComponent.cpp --- dGame/dComponents/BaseCombatAIComponent.cpp | 1 - dGame/dComponents/MovementAIComponent.cpp | 51 +++++++++------------ 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/dGame/dComponents/BaseCombatAIComponent.cpp b/dGame/dComponents/BaseCombatAIComponent.cpp index d40cf73e..2bd2b6f1 100644 --- a/dGame/dComponents/BaseCombatAIComponent.cpp +++ b/dGame/dComponents/BaseCombatAIComponent.cpp @@ -162,7 +162,6 @@ void BaseCombatAIComponent::Update(const float deltaTime) { // Check if we should stop the tether effect if (m_TetherEffectActive) { m_TetherTime -= deltaTime; - const auto& info = m_MovementAI->GetInfo(); if (m_Target != LWOOBJID_EMPTY || (NiPoint3::DistanceSquared( m_StartPosition, m_Parent->GetPosition()) < 20 * 20 && m_TetherTime <= 0) diff --git a/dGame/dComponents/MovementAIComponent.cpp b/dGame/dComponents/MovementAIComponent.cpp index 8377031a..8aefdb5b 100644 --- a/dGame/dComponents/MovementAIComponent.cpp +++ b/dGame/dComponents/MovementAIComponent.cpp @@ -96,33 +96,29 @@ void MovementAIComponent::Update(const float deltaTime) { if (m_NextWaypoint == source) { m_TimeToTravel = 0.0f; + } else { + if (m_CurrentSpeed < m_MaxSpeed) { + m_CurrentSpeed += m_Acceleration; + } - goto nextAction; + m_CurrentSpeed = std::min(m_CurrentSpeed, m_MaxSpeed); + + const auto speed = m_CurrentSpeed * m_BaseSpeed; // scale speed based on base speed + + const auto delta = m_NextWaypoint - source; + + // Normalize the vector + const auto length = delta.Length(); + if (length > 0.0f) { + velocity = (delta / length) * speed; + } + + // Calclute the time it will take to reach the next waypoint with the current speed + m_TimeTravelled = 0.0f; + m_TimeToTravel = length / speed; + + SetRotation(NiQuaternion::LookAt(source, m_NextWaypoint)); } - - if (m_CurrentSpeed < m_MaxSpeed) { - m_CurrentSpeed += m_Acceleration; - } - - if (m_CurrentSpeed > m_MaxSpeed) { - m_CurrentSpeed = m_MaxSpeed; - } - - const auto speed = m_CurrentSpeed * m_BaseSpeed; // scale speed based on base speed - - const auto delta = m_NextWaypoint - source; - - // Normalize the vector - const auto length = delta.Length(); - if (length > 0) { - velocity = (delta / length) * speed; - } - - // Calclute the time it will take to reach the next waypoint with the current speed - m_TimeTravelled = 0.0f; - m_TimeToTravel = length / speed; - - SetRotation(NiQuaternion::LookAt(source, m_NextWaypoint)); } else { // Check if there are more waypoints in the queue, if so set our next destination to the next waypoint if (m_CurrentPath.empty()) { @@ -135,8 +131,6 @@ void MovementAIComponent::Update(const float deltaTime) { m_CurrentPath.pop(); } -nextAction: - SetVelocity(velocity); Game::entityManager->SerializeEntity(m_Parent); @@ -319,8 +313,7 @@ void MovementAIComponent::SetDestination(const NiPoint3& destination) { auto step = delta / 10.0f; for (int i = 0; i < 10; i++) { - // TODO: Replace this with += when the NiPoint3::operator+= is fixed - start = start + step; + start += step; computedPath.push_back(start); }