diff --git a/Assets/Scripts/GameMode/ColorGameMode/RacetrackGameMode.cs b/Assets/Scripts/GameMode/ColorGameMode/RacetrackGameMode.cs index 2bdda89..8e2208d 100644 --- a/Assets/Scripts/GameMode/ColorGameMode/RacetrackGameMode.cs +++ b/Assets/Scripts/GameMode/ColorGameMode/RacetrackGameMode.cs @@ -35,7 +35,7 @@ public class RacetrackGameMode : GameMode /// protected override void OnRoundEnd(PlayerData[] allPlayers) { - cameraCheck(); + cameraCheck(allPlayers); //At the end of each round, any stuck players are freed to resume moving next round foreach (PlayerData player in allPlayers) @@ -51,10 +51,67 @@ public class RacetrackGameMode : GameMode RoundCount++; } - void cameraCheck() + void cameraCheck(PlayerData[] allPlayers) { + //Get the average x-position of all players + float xAvg = 0.0f; + int livePlayerCount = 0; + + foreach (PlayerData player in allPlayers) + { + if (player.client.Lives > 0) + { + xAvg += player.character.transform.position.x; + livePlayerCount++; + } + } + + xAvg = xAvg / livePlayerCount; + + //Turn that position into a vector in viewport space + Vector3 xAvgPos = new Vector3(xAvg, 0.0f, 0.0f); + Vector3 xAvgVP = Camera.main.WorldToViewportPoint(xAvgPos); + + Debug.Log("xAvgPos = " + xAvgPos + ", xAvgVP = " + xAvgVP); + + //We move the camera forward by at least one increment + //Keep doing it until the average x-position is roughly centred + do + { + Camera.main.transform.Translate(scrollSpeed, 0, 0, Space.World); + xAvgVP = Camera.main.WorldToViewportPoint(xAvgPos); + + Debug.Log("Camera = " + Camera.main.transform.position + ", xAvgVP = " + xAvgVP); + } while (xAvgVP.x > 0.5f || xAvgVP.y > 0.5f); + + //If the foremost player is off the screen, scroll forward to catch up with them + + //Find who's furthest ahead + PlayerData playerAhead = allPlayers[0]; + foreach (PlayerData player in allPlayers) + { + if (player.client.Lives > 0 && player.character.transform.position.x > playerAhead.character.transform.position.x) + { + playerAhead = player; + } + } + + //Turn their position to a viewport vector + Vector3 playerVP = Camera.main.WorldToViewportPoint(playerAhead.character.transform.position); + + //If that position is not in view, advance until it is + while (playerVP.x > 1 || playerVP.y > 1) + { + Camera.main.transform.Translate(scrollSpeed, 0, 0, Space.World); + playerVP = Camera.main.WorldToViewportPoint(playerAhead.character.transform.position); + }/**/ + + + //We check for track sections we need to add/remove + mapManager.checkTrack(); + //Move the camera forward at a steady rate each round - if (scrollSpeed > 0.0f) + /*if (scrollSpeed > 0.0f) { Camera.main.transform.Translate(scrollSpeed, 0, 0, Space.World); mapManager.checkTrack(); @@ -68,7 +125,7 @@ public class RacetrackGameMode : GameMode else { //Debug.Log("Not scrolling"); - } + }*/ }