use indexes for pathing

This commit is contained in:
David Markowitz 2023-08-06 20:25:22 -07:00
parent 20b2a62932
commit bce87aaa06
3 changed files with 27 additions and 14 deletions

View File

@ -47,6 +47,7 @@ MovementAIComponent::MovementAIComponent(Entity* parent, MovementAIInfo info) :
m_TimeTravelled = 0; m_TimeTravelled = 0;
m_CurrentSpeed = 0; m_CurrentSpeed = 0;
m_MaxSpeed = 0; m_MaxSpeed = 0;
m_CurrentPathWaypointIndex = 0;
m_LockRotation = false; m_LockRotation = false;
} }
@ -123,14 +124,11 @@ void MovementAIComponent::Update(const float deltaTime) {
SetRotation(NiQuaternion::LookAt(source, m_NextWaypoint)); SetRotation(NiQuaternion::LookAt(source, m_NextWaypoint));
} else { } else {
// Check if there are more waypoints in the queue, if so set our next destination to the next waypoint // Check if there are more waypoints in the queue, if so set our next destination to the next waypoint
if (m_CurrentPath.empty()) { if (AdvancePathWaypointIndex()) {
Stop(); Stop();
return; return;
} }
SetDestination(m_CurrentPath.top()); SetDestination(GetCurrentPathWaypoint());
m_CurrentPath.pop();
} }
nextAction: nextAction:
@ -140,6 +138,11 @@ nextAction:
Game::entityManager->SerializeEntity(m_Parent); Game::entityManager->SerializeEntity(m_Parent);
} }
bool MovementAIComponent::AdvancePathWaypointIndex() {
if (m_CurrentPathWaypointIndex < m_CurrentPath.size()) m_CurrentPathWaypointIndex++;
return m_CurrentPathWaypointIndex < m_CurrentPath.size();
}
const MovementAIInfo& MovementAIComponent::GetInfo() const { const MovementAIInfo& MovementAIComponent::GetInfo() const {
return m_Info; return m_Info;
} }
@ -209,11 +212,12 @@ void MovementAIComponent::Stop() {
m_AtFinalWaypoint = true; m_AtFinalWaypoint = true;
m_InterpolatedWaypoints.clear(); m_InterpolatedWaypoints.clear();
while (!m_CurrentPath.empty()) m_CurrentPath.pop(); m_CurrentPath.clear();
m_PathIndex = 0; m_PathIndex = 0;
m_CurrentSpeed = 0; m_CurrentSpeed = 0;
m_CurrentPathWaypointIndex = 0;
Game::entityManager->SerializeEntity(m_Parent); Game::entityManager->SerializeEntity(m_Parent);
} }
@ -227,11 +231,11 @@ void MovementAIComponent::PullToPoint(const NiPoint3& point) {
void MovementAIComponent::SetPath(std::vector<NiPoint3> path) { void MovementAIComponent::SetPath(std::vector<NiPoint3> path) {
if (path.empty()) return; if (path.empty()) return;
std::for_each(path.rbegin(), path.rend() - 1, [this](const NiPoint3& point) { m_CurrentPath = path;
this->m_CurrentPath.push(point);
});
SetDestination(path.front()); m_CurrentPathWaypointIndex = 0;
SetDestination(m_CurrentPath.front());
} }
float MovementAIComponent::GetBaseSpeed(LOT lot) { float MovementAIComponent::GetBaseSpeed(LOT lot) {

View File

@ -191,6 +191,10 @@ public:
*/ */
void SetPath(std::vector<NiPoint3> path); void SetPath(std::vector<NiPoint3> path);
bool AdvancePathWaypointIndex();
const NiPoint3& GetCurrentPathWaypoint() const { return m_CurrentPathWaypointIndex < m_CurrentPath.size() ? m_CurrentPath.at(m_CurrentPathWaypointIndex) : m_Parent->GetPosition(); }
/** /**
* Returns the base speed from the DB for a given LOT * Returns the base speed from the DB for a given LOT
* @param lot the lot to check for * @param lot the lot to check for
@ -301,7 +305,12 @@ private:
/** /**
* The path from the current position to the destination. * The path from the current position to the destination.
*/ */
std::stack<NiPoint3> m_CurrentPath; std::vector<NiPoint3> m_CurrentPath;
/**
* The index of the current waypoint in the path
*/
uint32_t m_CurrentPathWaypointIndex;
}; };
#endif // MOVEMENTAICOMPONENT_H #endif // MOVEMENTAICOMPONENT_H

View File

@ -301,9 +301,9 @@ void SGCannon::OnActivityTimerDone(Entity* self, const std::string& name) {
pathWaypoints.push_back(waypoint.position); pathWaypoints.push_back(waypoint.position);
} }
if (GeneralUtils::GenerateRandomNumber<float_t>(0, 1) < 0.5f) { // if (GeneralUtils::GenerateRandomNumber<float_t>(0, 1) < 0.5f) {
std::reverse(pathWaypoints.begin(), pathWaypoints.end()); // std::reverse(pathWaypoints.begin(), pathWaypoints.end());
} // }
movementAI->SetPath(pathWaypoints); movementAI->SetPath(pathWaypoints);