public GUIResult ShowEndOfGameReport(EndOfGameInfo info, float displayTimeSpan) { if (guiState != GUIState.EndOfGameReport) { endOfGameInfo = info; endOfGameStartTime = Time.time; endOfGameDisplayTimeSpan = displayTimeSpan; guiState = GUIState.EndOfGameReport; guiResult = GUIResult.NotStarted; } return guiResult; }
// Start is called before the first frame update void Start() { //Test whether the match ended or if the player left early. gameInfo = GameObject.Find("Network Manager").GetComponent <EndOfGameInfo>(); if (gameInfo.gameEnded) { //Match just ended OkButton.onClick.AddListener(okButtonClicked); //Show screen gameObject.SetActive(true); mainMenuCanvas.SetActive(false); leaderboard.text = gameInfo.leaderboardText; gameInfo.gameEnded = false; } else { //Main menu opened but not after a match. //Show main menu, hide this screen. mainMenuCanvas.SetActive(true); gameObject.SetActive(false); } }
// Update is called once per frame void Update() { //Handle 'esc' button for quit if (Input.GetKey("escape")) { gameState = GameState.Closing; } // Ensure sound keeps looping - Sometimes it stops randomly if (!audio.isPlaying) { audio.Play(); Debug.Log("Sound was restarted!!"); } if (gameState == GameState.Starting) { // Show Title if (!showTitleScreen || guiController.ShowTitleScreen(3.0f) == GUIResult.Finished) { gameState = GameState.MainMenu; } } if (gameState == GameState.MainMenu) { var result = guiController.ShowMainMenu(); if (result.isFinished) { switch (result.mainMenuAction) { case MainMenuAction.NewGame: gameState = GameState.LoadingNewGame; newGameOptions = result.newGameOptions; break; case MainMenuAction.Close: default: gameState = GameState.Closing; break; } } } if (gameState == GameState.LoadingNewGame) { // TEMP: Refactor this guiController.ShowNone(); var options = newGameOptions; var humans = new List<int>(); for (int i = 0; i < options.humanPlayerCount; i++) { humans.Add(i); } PerformanceController.PausePerformance(); NewGame(options.humanPlayerCount + options.computerPlayerCount, humans); PerformanceController.ResetPerformance(0.5f); gameState = GameState.Editor; } if (gameState == GameState.Editor) { HandleCameraInputs(); // Handle when editor is finished if (editorController.editorState == EditorState.Finished || !newGameOptions.shouldShowEditor) { editorController.FinishedIsHandled(); PerformanceController.PausePerformance(); // Disable editor for performance Destroy(editorController); CreateBasesIfEmpty(); RemoveAllAligners(); EnableBases(); PerformanceController.ResetPerformance(1.0f); // Start Game Mode gameState = GameState.Playing; } } if (gameState == GameState.Playing) { SetRadarCameraVisible(true); var hasMoved = HandleCameraInputs(); if (hasMoved) { hasCameraMovedForTurnState = true; } if (turnState == TurnState.Starting) { // Move camera var pos = players[playerTurnIndex].baseObject.transform.position; mouseHelper.MoveCameraToLookAtGroundPoint(pos.x, pos.z); // Move the ActiveBaseMarker activeBaseMarker.transform.position = pos; // TODO: Display Player Info Debug.Log("Turn = PlayerIndex " + playerTurnIndex); turnState = TurnState.Aiming; hitBaseIndex = -1; } if (turnState == TurnState.Aiming) { GameAction gameAction; if (players[playerTurnIndex].isHuman) { // TODO: Allow player to shoot blocks only near base gameAction = HandleGameInputs(); } else { // Computer Aiming gameAction = ComputerAim(); } if (gameAction == GameAction.PlayerShotBlock) { turnState = TurnState.Shooting; } else if (gameAction == GameAction.None) { // Do Nothing } else { throw new System.Exception("Unknown GameAction: " + gameAction); } } if (turnState == TurnState.Shooting) { // Detect shooting block stop movement + Timeout var sController = shootingBlock.GetComponent<ShootingBlockController>(); var isTurnFinished = sController.shootingBlockState == ShootingBlockState.Finished && Time.time - sController.finishedTime > endOfTurnTimeSpan; // Detect if has hit enemy base var pos = shootingBlock.transform.position; for (var iPlayer = 0; iPlayer < players.Count; iPlayer++) { if (iPlayer == playerTurnIndex) { continue; } var player = players[iPlayer]; var pBase = player.baseObject; if (!player.isAlive) { continue; } var bPos = pBase.transform.position; var distance = Vector3.Distance(bPos, pos); if (distance < blockHitBaseDistance) { hitBaseIndex = iPlayer; } } if (sController.shootingBlockState != ShootingBlockState.Finished && !hasCameraMovedForTurnState) { if (hitBaseIndex < 0) { // Follow the shooting block with the camera (while it is still moving) mouseHelper.MoveCameraToLookAtGroundPoint(pos.x, pos.z); } else { // Move camera to base var pBasePos = players[hitBaseIndex].baseObject.transform.position; mouseHelper.MoveCameraToLookAtGroundPoint(pBasePos.x, pBasePos.z); } } if (isTurnFinished) { sController.FinishedIsHandled(); turnState = TurnState.Ended; } } if (turnState == TurnState.Ended) { // Detect End of Game (if only one human player is still active or all human players are inactive) // Or if no human players - then only one computer left var humanPlayerCount = 0; var humansAliveCount = 0; var computersAliveCount = 0; foreach (var p in players) { if (p.isHuman) { humanPlayerCount++; if (p.isAlive) { humansAliveCount++; } } else { if (p.isAlive) { computersAliveCount++; } } } var playersAliveCount = humansAliveCount + computersAliveCount; if ((humansAliveCount == 0 && humanPlayerCount > 0) || playersAliveCount <= 1) { Debug.Log("Players -> GameOver"); gameState = GameState.EndOfGameReport; } // Go to next alive player var origPlayer = playerTurnIndex; do { playerTurnIndex++; if (playerTurnIndex >= players.Count) { playerTurnIndex = 0; } if (playerTurnIndex == origPlayer) { // Game Over (only one player alive (or maybe none alive)) - This should be handled by above check break; } } while (!players[playerTurnIndex].isAlive); turnState = TurnState.Starting; } } if (gameState == GameState.EndOfGameReport) { SetRadarCameraVisible(false); var winnerName = "Nobody"; var isWinnerHuman = false; var loserHumans = new List<string>(); foreach (var p in players) { if (p.isAlive) { winnerName = p.name; isWinnerHuman = p.isHuman; } else { if (p.isHuman) { loserHumans.Add(p.name); } } } var reportInfo = new EndOfGameInfo(winnerName, isWinnerHuman, loserHumans.ToArray()); if (guiController.ShowEndOfGameReport(reportInfo, 5.0f) == GUIResult.Finished) { gameState = GameState.MainMenu; } } if (gameState == GameState.Closing) { Application.Quit(); } }