diff --git a/dGame/dComponents/MovementAIComponent.cpp b/dGame/dComponents/MovementAIComponent.cpp index 15c83f886..0fd9569f1 100644 --- a/dGame/dComponents/MovementAIComponent.cpp +++ b/dGame/dComponents/MovementAIComponent.cpp @@ -259,7 +259,12 @@ void MovementAIComponent::Run(const float deltaTime) { m_IsBounced = !m_IsBounced; std::vector waypoints = m_Path->pathWaypoints; if (m_IsBounced) std::ranges::reverse(waypoints); + // It turns round at the end: on to the waypoint before it, not to the end again + // (PathFollowWaypointsHelper: waypoint 1, or count - 2) + const bool turns = waypoints.size() > 1; + if (turns) waypoints.erase(waypoints.begin()); SetPath(waypoints); + if (turns && !m_IsBounced) m_CurrentPathStart = 1; } else if (m_Path->pathBehavior == PathBehavior::Once) { // In this case we intended to follow a path and once we've followed it we camp there, otherwise we'd just wander home again. Stop(); diff --git a/tests/dGameTests/dComponentsTests/MovementAIPathTests.cpp b/tests/dGameTests/dComponentsTests/MovementAIPathTests.cpp index 8d2073b5d..bcb244cac 100644 --- a/tests/dGameTests/dComponentsTests/MovementAIPathTests.cpp +++ b/tests/dGameTests/dComponentsTests/MovementAIPathTests.cpp @@ -121,3 +121,18 @@ TEST_F(MovementAIPathTest, ChangeWaypointCommandWithoutAWaypointStartsAtTheFirst EXPECT_EQ(reached, (std::vector{ 0, 0, 1, 2 })); } + +// PathFollowWaypointsHelper turns round at an end: the end waypoint is reached once, then the one before it +TEST_F(MovementAIPathTest, BouncePathReachesEachEndOnce) { + AddPath(MakePath("BouncePath", 3, PathBehavior::Bounce)); + Entity entity(304, info); + auto* const movement = Walker(entity); + movement->SetPath("BouncePath"); + Walk(*movement, 120.0f); + + ASSERT_GE(reached.size(), 9u); + reached.resize(9); + EXPECT_EQ(reached, (std::vector{ 0, 1, 2, 1, 0, 1, 2, 1, 0 })); + const std::vector expectedX{ 10.0f, 20.0f, 30.0f, 20.0f, 10.0f, 20.0f, 30.0f, 20.0f, 10.0f }; + for (size_t i = 0; i < expectedX.size(); i++) EXPECT_NEAR(reachedX[i], expectedX[i], 0.01f) << i; +}