diff --git a/dGame/dComponents/MovingPlatformComponent.cpp b/dGame/dComponents/MovingPlatformComponent.cpp index e2ebb47c2..e1b59d959 100644 --- a/dGame/dComponents/MovingPlatformComponent.cpp +++ b/dGame/dComponents/MovingPlatformComponent.cpp @@ -460,8 +460,15 @@ void MovingPlatformComponent::PlatformGotoWaypoint(PlatformSubComponent& platfor platform.mPercentBetweenPoints = 1.0f - std::min(percent, 1.0f); platform.mSegmentLength = Vector3::Distance(waypoints[platform.mCurrentWaypointIndex].position, waypoints[platform.mNextWaypointIndex].position); platform.mWaitTime = waypoints[platform.mCurrentWaypointIndex].movingPlatform.wait; - if (platform.mTimeBased) platform.mMoveTimeElapsed = platform.mCurrentSpeed * platform.mPercentBetweenPoints; - else std::swap(platform.mCurrentSpeed, platform.mNextSpeed); + if (platform.mTimeBased) { + // The client's turn round (ProcessStateChange of the waypoint it was heading to, not moving there) starts + // the stretch's clock again: from where it is, it takes the whole stretch's time at the whole stretch's + // speed, so it goes past the waypoint by as far as it had come + platform.mMoveTimeElapsed = 0.0f; + platform.mPercentBetweenPoints = 0.0f; + } else { + std::swap(platform.mCurrentSpeed, platform.mNextSpeed); + } } } PlatformResumeTravelling(platform, false); diff --git a/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp b/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp index 96831593b..e2a2c87e5 100644 --- a/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp +++ b/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp @@ -1462,3 +1462,44 @@ TEST_F(MovingPlatformComponentTest, FireTempleMillstoneTurnsTheShortWay) { EXPECT_NEAR(lowest, -179.0f, 0.1f); EXPECT_NEAR(highest, -1.0f, 0.1f); } + +// A mover on a time based path turned round between two waypoints starts the stretch's clock again, as the client's +// (ProcessStateChange zeroes the time travelled): from where it is it takes the whole stretch's time at the whole +// stretch's speed, so it goes past the waypoint it turned back to by as far as it had come +TEST_F(MovingPlatformComponentTest, TimeBasedMoverTurnedRoundRestartsTheStretch) { + Path path; + path.pathType = PathType::MovingPlatform; + path.pathName = "TestTimeTurnPath"; + path.pathBehavior = PathBehavior::Once; + path.movingPlatform.timeBasedMovement = 1; + PathWaypoint first; + first.position = NiPoint3(0.0f, 0.0f, 0.0f); + first.speed = 5.0f; // seconds to the next waypoint + PathWaypoint second = first; + second.position = NiPoint3(10.0f, 0.0f, 0.0f); + path.pathWaypoints = { first, second }; + const_cast(Game::zoneManager->GetZone())->AddPath(path); + + info.pos = NiPoint3(0.0f, 0.0f, 0.0f); + Entity entity(288300744895900150, info); + entity.AddComponent(-1); + auto* const platform = entity.AddComponent(-1, "TestTimeTurnPath"); + auto* const mover = platform->GetMoverSubComponent(); + platform->GotoWaypoint(1); + for (int32_t i = 0; i < 15 * 3; i++) platform->Run(1.0f / 15.0f); + ASSERT_EQ(mover->mState & 2u, 2u); + const float turnedAt = entity.GetPosition().x; + EXPECT_GT(turnedAt, 3.0f); + EXPECT_LT(turnedAt, 7.0f); + + platform->GotoWaypoint(0); + EXPECT_EQ(mover->mCurrentWaypointIndex, 1u); + EXPECT_EQ(mover->mNextWaypointIndex, 0u); + EXPECT_EQ(mover->mMoveTimeElapsed, 0.0f); + EXPECT_EQ(mover->mPercentBetweenPoints, 0.0f); + // Just before the whole stretch's time is up it has gone nearly the whole stretch back, at the whole stretch's + // speed: past waypoint 0 by nearly as far as it had come (it arrives when the time is up) + for (int32_t i = 0; i < 15 * 5 - 3; i++) platform->Run(1.0f / 15.0f); + EXPECT_NEAR(entity.GetPosition().x, turnedAt - 10.0f * 72.0f / 75.0f, 0.75f); + EXPECT_LT(entity.GetPosition().x, -2.0f); +}