// it is a lot of work to adjust for the previous platform start position private Vector3 GetPrevPlatformStartPosition(InfiniteObject platform, int platformIndex, Vector3 direction) { return(platformStartPosition[platformIndex].x * platform.GetTransform().right + platformStartPosition[platformIndex].y * Vector3.up + platformStartPosition[platformIndex].z * direction); }
// move all of the active objects public void MoveObjects(float moveDistance) { if (moveDistance == 0) { return; } // the distance to move the objects Vector3 delta = moveDirection * moveDistance; if (moveDirection != spawnDirection) { wrongMoveDistance += moveDistance; } // only move the top most platform/scene of each ObjectLocation because all of the other objects are children of these two // objects. Only have to check the bottom-most platform/scene as well to determine if it should be removed InfiniteObject infiniteObject = null; Transform objectTransform = null; PlatformObject platformObject = null; for (int i = 0; i < 2; ++i) // loop through the platform and scenes { for (int j = 0; j < (int)ObjectLocation.Last; ++j) { // move infiniteObject = infiniteObjectHistory.GetTopInfiniteObject((ObjectLocation)j, i == 0); if (infiniteObject != null) { objectTransform = infiniteObject.GetTransform(); Vector3 pos = objectTransform.position; pos -= delta; objectTransform.position = pos; // check for removal.. there will always be a bottom object if there is a top object infiniteObject = infiniteObjectHistory.GetBottomInfiniteObject((ObjectLocation)j, i == 0); if (playerTransform.InverseTransformPoint(infiniteObject.GetTransform().position).z < removeHorizon) { // if the infinite object is a platform and it has changes height, move everything down by that height if (heightReposition && i == 1) // 1 are platforms { platformObject = infiniteObject as PlatformObject; if (platformObject.slope != PlatformSlope.None) { TransitionHeight(platformSizes[platformObject.GetLocalIndex()].y); } } if (turnPlatform[j] == infiniteObject) { turnPlatform[j] = null; } infiniteObjectHistory.ObjectRemoved((ObjectLocation)j, i == 0); infiniteObject.Deactivate(); } } } // loop through all of the turn objects infiniteObject = infiniteObjectHistory.GetTopTurnInfiniteObject(i == 0); if (infiniteObject != null) { objectTransform = infiniteObject.GetTransform(); Vector3 pos = objectTransform.position; pos -= delta; objectTransform.position = pos; infiniteObject = infiniteObjectHistory.GetBottomTurnInfiniteObject(i == 0); if (playerTransform.InverseTransformPoint(infiniteObject.GetTransform().position).z < removeHorizon) { infiniteObjectHistory.TurnObjectRemoved(i == 0); infiniteObject.Deactivate(); } } } if (!stopObjectSpawns) { dataManager.AddToScore(moveDistance); SpawnObjectRun(true); } }
// An object run contains many platforms strung together with collidables: obstacles, power ups, and coins. If spawnObjectRun encounters a turn, // it will spawn the objects in the correct direction public void SpawnObjectRun(bool activateImmediately) { // spawn the center objects InfiniteObject prevPlatform = infiniteObjectHistory.GetTopInfiniteObject(ObjectLocation.Center, false); while ((prevPlatform == null || (Vector3.Scale(prevPlatform.GetTransform().position, spawnDirection)).sqrMagnitude < sqrHorizon) && (turnPlatform[(int)ObjectLocation.Center] == null || turnPlatform[(int)ObjectLocation.Center].straight)) { Vector3 position = Vector3.zero; if (prevPlatform != null) { int prevPlatformIndex = infiniteObjectHistory.GetLastLocalIndex(ObjectLocation.Center, ObjectType.Platform); position = prevPlatform.GetTransform().position - GetPrevPlatformStartPosition(prevPlatform, prevPlatformIndex, spawnDirection) + platformSizes[prevPlatformIndex].z / 2 * spawnDirection + platformSizes[prevPlatformIndex].y * Vector3.up; } PlatformObject platform = SpawnObjects(ObjectLocation.Center, position, spawnDirection, activateImmediately); if (platform == null) { return; } PlatformSpawned(platform, ObjectLocation.Center, spawnDirection, activateImmediately); prevPlatform = infiniteObjectHistory.GetTopInfiniteObject(ObjectLocation.Center, false); if (spawnFullLength) { SpawnObjectRun(activateImmediately); } } // spawn the left and right objects if (turnPlatform[(int)ObjectLocation.Center] != null) { Vector3 turnDirection = turnPlatform[(int)ObjectLocation.Center].GetTransform().right; // spawn the platform and scene objects for the left and right turns for (int i = 0; i < 2; ++i) { ObjectLocation location = (i == 0 ? ObjectLocation.Right : ObjectLocation.Left); bool canSpawn = (location == ObjectLocation.Right && turnPlatform[(int)ObjectLocation.Center].rightTurn) || (location == ObjectLocation.Left && turnPlatform[(int)ObjectLocation.Center].leftTurn); if (canSpawn && turnPlatform[(int)location] == null) { prevPlatform = infiniteObjectHistory.GetTopInfiniteObject(location, false); if (prevPlatform == null || (Vector3.Scale(prevPlatform.GetTransform().position, turnDirection)).sqrMagnitude < sqrHorizon) { infiniteObjectHistory.SetActiveLocation(location); Vector3 position = Vector3.zero; if (prevPlatform != null) { int prevPlatformIndex = infiniteObjectHistory.GetLastLocalIndex(location, ObjectType.Platform); position = prevPlatform.GetTransform().position - GetPrevPlatformStartPosition(prevPlatform, prevPlatformIndex, turnDirection) + platformSizes[prevPlatformIndex].z / 2 * turnDirection + platformSizes[prevPlatformIndex].y * Vector3.up; } else { PlatformObject centerTurn = turnPlatform[(int)ObjectLocation.Center]; int centerTurnIndex = turnIndex[(int)ObjectLocation.Center]; position = centerTurn.GetTransform().position - platformStartPosition[centerTurnIndex].x * turnDirection - Vector3.up * platformStartPosition[centerTurnIndex].y - platformStartPosition[centerTurnIndex].z * spawnDirection + centerTurn.centerOffset.x * turnDirection + centerTurn.centerOffset.z * spawnDirection + platformSizes[centerTurnIndex].y * Vector3.up; } PlatformObject platform = SpawnObjects(location, position, turnDirection, activateImmediately); if (platform == null) { return; } PlatformSpawned(platform, location, turnDirection, activateImmediately); } } turnDirection *= -1; } // reset infiniteObjectHistory.SetActiveLocation(ObjectLocation.Center); } }