// orient for collidables which have a platform as a parent public virtual void Orient(BasicObject parent, Vector3 position, Quaternion rotation) { thisTransform.parent = parent.GetTransform(); Vector3 pos = Vector3.zero; float yAngle = rotation.eulerAngles.y; pos.Set(startPosition.x * Mathf.Cos(yAngle * Mathf.Deg2Rad) + startPosition.z * Mathf.Sin(yAngle * Mathf.Deg2Rad), startPosition.y, -startPosition.x * Mathf.Sin(yAngle * Mathf.Deg2Rad) + startPosition.z * Mathf.Cos(yAngle * Mathf.Deg2Rad)); pos += position; thisTransform.localPosition = parent.GetTransform().InverseTransformPoint(pos); thisTransform.rotation = startRotation; thisTransform.Rotate(0, rotation.eulerAngles.y, 0, Space.World); }
/* Set parent object */ public void SetObjectParent(BasicObject parentObject) { objectParent = parentObject; thisTransform.parent = parentObject.GetTransform(); }
// Move all of the active objects public void MoveObjects(float moveDistance) { if (moveDistance == 0) { return; } // The distance vector to move the objects Vector3 delta = moveDirection * 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 BasicObject currentObject = null; Transform objectTransform = null; for (int i = 0; i < 2; ++i) // loop through the platform and scenes { bool isScene = i == 0; for (int j = 0; j < (int)Direction.Count; ++j) { // Move objects currentObject = ObjectHistory.instance.GetTopObject((Direction)j, isScene); if (currentObject != null) { objectTransform = currentObject.GetTransform(); Vector3 pos = objectTransform.position; pos -= delta; objectTransform.position = pos; // Check for removing bottom object if it is out of range currentObject = ObjectHistory.instance.GetBottomObject((Direction)j, isScene); if (playerTransform.InverseTransformPoint(currentObject.GetTransform().position).z < removeHorizon) { if (turnPlatform[j] == currentObject) { turnPlatform[j] = null; } ObjectHistory.instance.ObjectRemoved((Direction)j, isScene); currentObject.Deactivate(); } } } // Move turn objects currentObject = ObjectHistory.instance.GetTopTurnObject(isScene); if (currentObject != null) { objectTransform = currentObject.GetTransform(); Vector3 pos = objectTransform.position; pos -= delta; objectTransform.position = pos; currentObject = ObjectHistory.instance.GetBottomTurnObject(isScene); if (playerTransform.InverseTransformPoint(currentObject.GetTransform().position).z < removeHorizon) { ObjectHistory.instance.TurnObjectRemoved(isScene); currentObject.Deactivate(); } } } if (!stopSpawning) { SpawnObjects(true); } }
// Spawn objects public void SpawnObjects(bool activateImmediately) { // Spawn objects in center direction BasicObject prevPlatform = ObjectHistory.instance.GetTopObject(Direction.Center, IS_PLATFORM); bool isWithinSpawnDistance = prevPlatform == null || Vector3.Scale(prevPlatform.GetTransform().position, spawnDirection).sqrMagnitude < sqrHorizon; bool hasStraightPath = turnPlatform[(int)Direction.Center] == null || (turnPlatform[(int)Direction.Center].isStraight); while (isWithinSpawnDistance && hasStraightPath) { Vector3 position = Vector3.zero; if (prevPlatform != null) { int prevPlatformIndex = ObjectHistory.instance.GetLastLocalIndex(Direction.Center, ObjectType.Platform); position = prevPlatform.GetTransform().position - GetPrevPlatformStartPosition(prevPlatform, prevPlatformIndex, spawnDirection) + platformSizes[prevPlatformIndex].z / 2 * spawnDirection + platformSizes[prevPlatformIndex].y * Vector3.up; } PlatformObject platform = SpawnPlatformAndCollidables(Direction.Center, position, spawnDirection, activateImmediately); if (platform == null) { return; } SpawnSceneForPlatform(platform, Direction.Center, spawnDirection, activateImmediately); prevPlatform = ObjectHistory.instance.GetTopObject(Direction.Center, IS_PLATFORM); // Refresh conditions isWithinSpawnDistance = prevPlatform == null || Vector3.Scale(prevPlatform.GetTransform().position, spawnDirection).sqrMagnitude < sqrHorizon; hasStraightPath = turnPlatform[(int)Direction.Center] == null || turnPlatform[(int)Direction.Center].isStraight; } // Spawn objects in the left and right direction if (turnPlatform[(int)Direction.Center] != null) { Vector3 turnDirection = turnPlatform[(int)Direction.Center].GetTransform().right; // spawn the platform and scene objects for the left and right turns for (int i = 0; i < 2; ++i) { Direction location = (i == 0 ? Direction.Right : Direction.Left); bool canSpawn = (location == Direction.Right && turnPlatform[(int)Direction.Center].isRightTurn) || (location == Direction.Left && turnPlatform[(int)Direction.Center].isLeftTurn); if (canSpawn && turnPlatform[(int)location] == null) { prevPlatform = ObjectHistory.instance.GetTopObject(location, IS_PLATFORM); isWithinSpawnDistance = prevPlatform == null || Vector3.Scale(prevPlatform.GetTransform().position, turnDirection).sqrMagnitude < sqrHorizon; if (isWithinSpawnDistance) { ObjectHistory.instance.SetActiveDirection(location); Vector3 position = Vector3.zero; if (prevPlatform != null) { int prevPlatformIndex = ObjectHistory.instance.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)Direction.Center]; int centerTurnIndex = platformTurnIndex[(int)Direction.Center]; position = centerTurn.GetTransform().position - platformStartPositions[centerTurnIndex].x * turnDirection - Vector3.up * platformStartPositions[centerTurnIndex].y - platformStartPositions[centerTurnIndex].z * spawnDirection + centerTurn.centerOffset.x * turnDirection + centerTurn.centerOffset.z * spawnDirection + platformSizes[centerTurnIndex].y * Vector3.up; } PlatformObject platform = SpawnPlatformAndCollidables(location, position, turnDirection, activateImmediately); if (platform == null) { return; } SpawnSceneForPlatform(platform, location, turnDirection, activateImmediately); } } turnDirection *= -1; } // Reset ObjectHistory.instance.SetActiveDirection(Direction.Center); } }
// it is a lot of work to adjust for the previous platform start position private Vector3 GetPrevPlatformStartPosition(BasicObject platform, int platformIndex, Vector3 direction) { return(platformStartPositions[platformIndex].x * platform.GetTransform().right + platformStartPositions[platformIndex].y * Vector3.up + platformStartPositions[platformIndex].z * direction); }