RenderedPlatform GenerateVariableHightPlatform(Rect cameraRect) { //determine width of platform from platform budget GameObject prefab = floatingPlatforms[(int)Mathf.Floor(Random.Range(0, floatingPlatforms.Count - 1))]; //pick random height difference over interval (-inf, 0.5*v0^2/g) float yOffsetBound = 0.5f * Mathf.Pow(jumpVelocity, 2) / gravity; float yOffset = Random.Range(0, yOffsetBound); float xOffsetBound = ( Mathf.Sqrt(jumpVelocity * jumpVelocity * xVelocity * xVelocity - (2 * gravity * xVelocity * xVelocity * yOffset)) + jumpVelocity * xVelocity) / gravity; float budgetWeight = this.budgetWeight(MIN_SPACING, xOffsetBound); float xOffset = Random.Range(MIN_SPACING + budgetWeight, xOffsetBound); if (platforms.Count > 0) { RenderedPlatform previous = platforms[platforms.Count - 1]; float yPosition = Mathf.Clamp(previous.upperRight.y + yOffset, cameraRect.yMin + xBufferDistance, cameraRect.yMax - yBufferDistance); return(RenderedPlatform.FromUpperLeft(prefab, new Vector3(previous.upperRight.x + xOffset, yPosition, 0))); } else { return(RenderedPlatform.FromUpperLeft(prefab, new Vector3(cameraRect.center.x + xOffset, cameraRect.center.y + yOffset, 0))); } }
RenderedPlatform GenerateStaticHeightPlatform(Rect cameraRect) { //determine width of platform from platform budget GameObject prefab = groundedPlatforms[(int)Mathf.Floor(Random.Range(0, groundedPlatforms.Count - 1))]; //determine spacing float xOffsetBound = 2 * jumpVelocity * xVelocity / gravity; float budgetWeight = this.budgetWeight(MIN_SPACING, xOffsetBound); float xOffset = Random.Range(MIN_SPACING + budgetWeight, xOffsetBound); //Debug.Log("Interval: " + (MIN_SPACING + budgetWeight) + " " + xOffsetBound); if (platforms.Count > 0) { RenderedPlatform previous = platforms[platforms.Count - 1]; return(new RenderedPlatform(prefab, new Vector3(previous.upperRight.x + xOffset, waterHeight))); } else { return(new RenderedPlatform(prefab, new Vector3(cameraRect.center.x + xOffset, waterHeight))); } }
// Update is called once per frame void Update() { //get camera pose Rect cameraView = CameraMovement.CameraRect(); //create batch of platforms whenever camera is within given distance of the end of the last run if (cameraView.xMax + LOOKAHEAD_DISTANCE > distanceGenerated) { //generate run of new platforms float targetDistance = distanceGenerated + RUN_LENGTH; while (distanceGenerated < targetDistance) { RenderedPlatform platform = (Random.value > 0.65) ? GenerateStaticHeightPlatform(cameraView) : (ClimateEvents.GetInstance().waterLevel ? GenerateStaticHeightPlatform(cameraView) : GenerateVariableHightPlatform(cameraView)); distanceGenerated = platform.upperRight.x; platforms.Add(platform); } } //discard old platforms for (int i = 0; i < platforms.Count; i++) { if (platforms[i].obj == null) { platforms.RemoveAt(i); i--; continue; } if (platforms[i].obj.transform.position.x + (platforms[i].width / 2) < cameraView.xMin) { Destroy(platforms[i].obj); platforms.RemoveAt(i); i--; } } }