From aa734ef7ae91ad1d48fd4069c3a12508fc0ee35a Mon Sep 17 00:00:00 2001 From: David Markowitz Date: Mon, 7 Aug 2023 00:44:57 -0700 Subject: [PATCH] Working Pausing and Resuming --- dGame/dComponents/MovementAIComponent.cpp | 25 +++++++++++++++++++++-- dGame/dComponents/MovementAIComponent.h | 11 ++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/dGame/dComponents/MovementAIComponent.cpp b/dGame/dComponents/MovementAIComponent.cpp index 7f17fb18..f0487ad5 100644 --- a/dGame/dComponents/MovementAIComponent.cpp +++ b/dGame/dComponents/MovementAIComponent.cpp @@ -25,6 +25,7 @@ namespace { MovementAIComponent::MovementAIComponent(Entity* parent, MovementAIInfo info) : Component(parent) { m_Info = info; + m_IsPaused = true; m_AtFinalWaypoint = true; m_BaseCombatAI = nullptr; @@ -70,8 +71,8 @@ void MovementAIComponent::Update(const float deltaTime) { return; } - // Are we done? - if (AtFinalWaypoint()) return; + // Are we done or paused? + if (AtFinalWaypoint() || IsPaused()) return; if (m_HaltDistance > 0) { // Prevent us from hugging the target @@ -207,6 +208,25 @@ bool MovementAIComponent::Warp(const NiPoint3& point) { return true; } +void MovementAIComponent::Pause() { + if (AtFinalWaypoint() || IsPaused()) return; + SetPosition(ApproximateLocation()); + SetVelocity(NiPoint3::ZERO); + + // Clear this as we may be somewhere else when we resume movement. + m_InterpolatedWaypoints.clear(); + m_IsPaused = true; + m_PathIndex = 0; + m_TimeToTravel = 0; + m_TimeTravelled = 0; +} + +void MovementAIComponent::Resume() { + if (AtFinalWaypoint() || !IsPaused()) return; + m_IsPaused = false; + SetDestination(GetCurrentPathWaypoint()); +} + void MovementAIComponent::Stop() { if (AtFinalWaypoint()) return; @@ -363,6 +383,7 @@ void MovementAIComponent::SetDestination(const NiPoint3& destination) { m_TimeToTravel = 0; m_AtFinalWaypoint = false; + m_IsPaused = false; } NiPoint3 MovementAIComponent::GetDestination() const { diff --git a/dGame/dComponents/MovementAIComponent.h b/dGame/dComponents/MovementAIComponent.h index 5148dfdf..76d12b32 100644 --- a/dGame/dComponents/MovementAIComponent.h +++ b/dGame/dComponents/MovementAIComponent.h @@ -173,6 +173,12 @@ public: */ bool AtFinalWaypoint() const { return m_AtFinalWaypoint; } + bool IsPaused() const { return m_IsPaused; } + + void Pause(); + + void Resume(); + /** * Renders the entity stationary */ @@ -322,6 +328,11 @@ private: * Whether or not the path is being read in reverse */ bool m_IsInReverse; + + /** + * Whether or not the current movement via pathing is paused. + */ + bool m_IsPaused; }; #endif // MOVEMENTAICOMPONENT_H