From 2eeacbf7461317c28e52d95466fab5be952870a0 Mon Sep 17 00:00:00 2001 From: Aronwk Date: Fri, 13 Jun 2025 01:05:12 -0500 Subject: [PATCH] Add mandlers for all the trigger related waypoint commands And add a call to the cppscript for reaching a waypoint --- dGame/dComponents/MovementAIComponent.cpp | 63 ++++++++++++++++++++++- dGame/dComponents/MovementAIComponent.h | 4 ++ dGame/dComponents/TriggerComponent.cpp | 21 ++++---- 3 files changed, 78 insertions(+), 10 deletions(-) diff --git a/dGame/dComponents/MovementAIComponent.cpp b/dGame/dComponents/MovementAIComponent.cpp index 9463e7d6..c3ea09e2 100644 --- a/dGame/dComponents/MovementAIComponent.cpp +++ b/dGame/dComponents/MovementAIComponent.cpp @@ -66,6 +66,67 @@ void MovementAIComponent::SetPath(const std::string pathName) { SetMaxSpeed(1); SetCurrentSpeed(m_BaseSpeed); SetPath(m_Path->pathWaypoints); + + auto start_index = m_Parent->GetVarAs(u"attached_path_start"); + if (start_index >= m_Path->waypointCount) start_index = 0; + + SetDestination(m_Path->pathWaypoints[m_PathIndex].position); + m_PathIndex = start_index; + +} + +void MovementAIComponent::SetPath(const std::string pathName, uint32_t startIndex, bool reverse) { + m_Path = Game::zoneManager->GetZone()->GetPath(pathName); + if (!pathName.empty()) LOG("WARNING: %s path %s", m_Path ? "Found" : "Failed to find", pathName.c_str()); + if (!m_Path) return; + SetMaxSpeed(1); + SetCurrentSpeed(m_BaseSpeed); + + if (reverse) { + m_IsBounced = !m_IsBounced; + auto pathWaypoints = m_Path->pathWaypoints; + std::reverse(pathWaypoints.begin(), pathWaypoints.end()); + SetPath(pathWaypoints); + } else { + SetPath(m_Path->pathWaypoints); + } + + SetDestination(m_Path->pathWaypoints[startIndex].position); + m_PathIndex = startIndex; +} + +void MovementAIComponent::TurnAroundOnPath() { + if (m_Path) { + auto currentIndex = m_PathIndex; + m_IsBounced = !m_IsBounced; + std::vector waypoints = m_Path->pathWaypoints; + if (m_IsBounced) std::reverse(waypoints.begin(), waypoints.end()); + SetPath(waypoints); + SetDestination(waypoints[currentIndex].position); + m_PathIndex = currentIndex; + } +} + +void MovementAIComponent::GoForwardOnPath() { + if (m_Path) { + auto currentIndex = m_PathIndex; + m_IsBounced = false; + SetPath(m_Path->pathWaypoints); + SetDestination(m_Path->pathWaypoints[currentIndex].position); + m_PathIndex = currentIndex; + } +} + +void MovementAIComponent::GoBackwardOnPath() { + if (m_Path) { + auto currentIndex = m_PathIndex; + m_IsBounced = true; + std::vector waypoints = m_Path->pathWaypoints; + std::reverse(waypoints.begin(), waypoints.end()); + SetPath(waypoints); + SetDestination(waypoints[currentIndex].position); + m_PathIndex = currentIndex; + } } void MovementAIComponent::Pause() { @@ -188,7 +249,7 @@ bool MovementAIComponent::AdvanceWaypointIndex() { if (m_PathIndex >= m_InterpolatedWaypoints.size()) { return false; } - + m_Parent->GetScript()->OnWaypointReached(m_Parent, m_PathIndex); m_PathIndex++; return true; diff --git a/dGame/dComponents/MovementAIComponent.h b/dGame/dComponents/MovementAIComponent.h index dbb0661c..9de4873a 100644 --- a/dGame/dComponents/MovementAIComponent.h +++ b/dGame/dComponents/MovementAIComponent.h @@ -65,6 +65,10 @@ public: MovementAIComponent(Entity* parentEntity, MovementAIInfo info); void SetPath(const std::string pathName); + void SetPath(const std::string pathName, uint32_t startIndex, bool reverse); + void TurnAroundOnPath(); + void GoForwardOnPath(); + void GoBackwardOnPath(); void Update(float deltaTime) override; diff --git a/dGame/dComponents/TriggerComponent.cpp b/dGame/dComponents/TriggerComponent.cpp index a9d00b15..02805411 100644 --- a/dGame/dComponents/TriggerComponent.cpp +++ b/dGame/dComponents/TriggerComponent.cpp @@ -457,40 +457,43 @@ void TriggerComponent::HandleActivatePhysics(Entity* targetEntity, std::string a void TriggerComponent::HandleSetPath(Entity* targetEntity, std::vector argArray) { auto* movementAIComponent = targetEntity->GetComponent(); if (!movementAIComponent) return; - // movementAIComponent->SetupPath(argArray.at(0)); + uint32_t start_index = 0; + bool reverse = false; if (argArray.size() >= 2) { auto index = GeneralUtils::TryParse(argArray.at(1)); - if (!index) return; - // movementAIComponent->SetPathStartingWaypointIndex(index.value()); + if (index) { + start_index = index.value(); + } } if (argArray.size() >= 3 && argArray.at(2) == "1") { - // movementAIComponent->ReversePath(); + reverse = true; } + movementAIComponent->SetPath(argArray.at(0), start_index, reverse); } void TriggerComponent::HandleTurnAroundOnPath(Entity* targetEntity) { auto* movementAIComponent = targetEntity->GetComponent(); - // if (movementAIComponent) movementAIComponent->ReversePath(); + if (movementAIComponent) movementAIComponent->TurnAroundOnPath(); } void TriggerComponent::HandleGoForwardOnPath(Entity* targetEntity) { auto* movementAIComponent = targetEntity->GetComponent(); if (!movementAIComponent) return; - // if (movementAIComponent->GetIsInReverse()) movementAIComponent->ReversePath(); + movementAIComponent->GoForwardOnPath(); } void TriggerComponent::HandleGoBackwardOnPath(Entity* targetEntity) { auto* movementAIComponent = targetEntity->GetComponent(); if (!movementAIComponent) return; - // if (!movementAIComponent->GetIsInReverse()) movementAIComponent->ReversePath(); + movementAIComponent->GoBackwardOnPath(); } void TriggerComponent::HandleStopPathing(Entity* targetEntity) { auto* movementAIComponent = targetEntity->GetComponent(); - // if (movementAIComponent) movementAIComponent->Pause(); + if (movementAIComponent) movementAIComponent->Pause(); } void TriggerComponent::HandleStartPathing(Entity* targetEntity) { auto* movementAIComponent = targetEntity->GetComponent(); - // if (movementAIComponent) movementAIComponent->Resume(); + if (movementAIComponent) movementAIComponent->Resume(); }