From 3e12dd782b86149948aefc80646960d100cc0ab4 Mon Sep 17 00:00:00 2001 From: David Markowitz Date: Sun, 6 Aug 2023 21:15:06 -0700 Subject: [PATCH] working forward --- dGame/dComponents/MovementAIComponent.cpp | 24 ++++++++++++++----- dGame/dComponents/MovementAIComponent.h | 10 +++++++- .../ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp | 6 +---- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/dGame/dComponents/MovementAIComponent.cpp b/dGame/dComponents/MovementAIComponent.cpp index c2873f99..bfd96963 100644 --- a/dGame/dComponents/MovementAIComponent.cpp +++ b/dGame/dComponents/MovementAIComponent.cpp @@ -49,6 +49,8 @@ MovementAIComponent::MovementAIComponent(Entity* parent, MovementAIInfo info) : m_MaxSpeed = 0; m_CurrentPathWaypointIndex = 0; m_LockRotation = false; + m_IsInReverse = false; + m_NextPathWaypointIndex = 0; } void MovementAIComponent::Update(const float deltaTime) { @@ -124,7 +126,7 @@ void MovementAIComponent::Update(const float deltaTime) { 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 (AdvancePathWaypointIndex()) { + if (!AdvancePathWaypointIndex()) { Stop(); return; } @@ -139,8 +141,14 @@ nextAction: } bool MovementAIComponent::AdvancePathWaypointIndex() { - if (m_CurrentPathWaypointIndex < m_CurrentPath.size()) m_CurrentPathWaypointIndex++; - return m_CurrentPathWaypointIndex < m_CurrentPath.size(); + m_CurrentPathWaypointIndex = m_NextPathWaypointIndex; + if (m_IsInReverse) { + if (m_CurrentPathWaypointIndex > 0) m_NextPathWaypointIndex--; + return m_NextPathWaypointIndex >= 0; + } else { + if (m_CurrentPathWaypointIndex < m_CurrentPath.size()) m_NextPathWaypointIndex++; + return m_CurrentPathWaypointIndex < m_CurrentPath.size(); + } } const MovementAIInfo& MovementAIComponent::GetInfo() const { @@ -229,12 +237,16 @@ void MovementAIComponent::PullToPoint(const NiPoint3& point) { m_PullPoint = point; } -void MovementAIComponent::SetPath(std::vector path) { +void MovementAIComponent::SetPath(std::vector path, bool startInReverse) { if (path.empty()) return; m_CurrentPath = path; + m_IsInReverse = startInReverse; - m_CurrentPathWaypointIndex = 0; - + // Start the Entity out at the first waypoint with their next waypoint being the same one. + // This is so AdvancePathWaypointIndex can do the recovery from effectively a paused state. + m_CurrentPathWaypointIndex = m_IsInReverse ? m_CurrentPath.size() - 1 : 0; + m_NextPathWaypointIndex = m_IsInReverse ? m_CurrentPath.size() - 1 : 0; + AdvancePathWaypointIndex(); SetDestination(m_CurrentPath.front()); } diff --git a/dGame/dComponents/MovementAIComponent.h b/dGame/dComponents/MovementAIComponent.h index cc63a9ea..7d7257a6 100644 --- a/dGame/dComponents/MovementAIComponent.h +++ b/dGame/dComponents/MovementAIComponent.h @@ -189,8 +189,9 @@ public: * Sets a path to follow for the AI * @param path the path to follow */ - void SetPath(std::vector path); + void SetPath(std::vector path, bool startsInReverse = false); + // Advance the path waypoint index and return if there is a next waypoint bool AdvancePathWaypointIndex(); const NiPoint3& GetCurrentPathWaypoint() const { return m_CurrentPathWaypointIndex < m_CurrentPath.size() ? m_CurrentPath.at(m_CurrentPathWaypointIndex) : m_Parent->GetPosition(); } @@ -311,6 +312,13 @@ private: * The index of the current waypoint in the path */ uint32_t m_CurrentPathWaypointIndex; + + /** + * The index of the current waypoint in the path + */ + uint32_t m_NextPathWaypointIndex; + + bool m_IsInReverse; }; #endif // MOVEMENTAICOMPONENT_H diff --git a/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp b/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp index c18ebb9f..3a901f82 100644 --- a/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp +++ b/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp @@ -301,11 +301,7 @@ void SGCannon::OnActivityTimerDone(Entity* self, const std::string& name) { pathWaypoints.push_back(waypoint.position); } - // if (GeneralUtils::GenerateRandomNumber(0, 1) < 0.5f) { - // std::reverse(pathWaypoints.begin(), pathWaypoints.end()); - // } - - movementAI->SetPath(pathWaypoints); + movementAI->SetPath(pathWaypoints, true); enemy->AddDieCallback([this, self, enemy, name]() { RegisterHit(self, enemy, name);