Advertisement
TheLetterM

Warp: Game Manager

May 13th, 2023
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.09 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6.  
  7. public class GameManager : MonoBehaviour
  8. {
  9.     //public GameObject[] alivePlayers;
  10.    
  11.  
  12.     [Header("Player-Related")]
  13.     public GameObject player1, player2; // Stores references to the individual players
  14.     public List<GameObject> deadPlayers = new List<GameObject>();
  15.     public GameObject playerOneSpawn, playerTwoSpawn;
  16.     public GameObject player1Mark, player2Mark;
  17.     public GameObject winner, loser; // Used to refer to which player was the winner and loser for UI elements
  18.  
  19.     public int playerOneWins = 0, playerTwoWins = 0; // Both win counts start at 0
  20.     CameraController theCamera;
  21.  
  22.     [Header("Level Prefabs")]
  23.     public List<GameObject> listOfLevels = new List<GameObject>();
  24.  
  25.     [Header("Level-Related")]
  26.     public GameObject lastLevel, currentLevel; // Stores reference of the previous loaded level so it can be destroyed when new level is created
  27.     public GameObject killPlane;
  28.     GameObject levelToLoad;
  29.     int levelID;
  30.     public static GameManager inst;
  31.  
  32.     float levelsLoaded = 1; // this is 1 and not 0 as it includes the starting level which will always be the same
  33.     Vector3 offset;
  34.  
  35.     [Header("UI")]
  36.     public TMP_Text playerOneCount, playerTwoCount; // Store reference to the UI Text for the individual players' wins
  37.     public GameObject endScreen;
  38.     public GameObject menuButton, continueButton;
  39.     public GameObject winnerText, loserText, winnerName, loserName, winnerIcon, loserIcon; // Disable all of these at the start of the game and reenable them later
  40.     public Image endScreenBackground;
  41.     public Sprite playerOneIcon, playerTwoIcon; // Store references to the images that will be changed to reflect the winner and loser's icons
  42.  
  43.    
  44.  
  45.     Color endScreenColour;
  46.     private void Awake()
  47.     {
  48.         inst = this;
  49.     }
  50.     void Start()
  51.     {
  52.         theCamera = FindObjectOfType<CameraController>();
  53.         playerOneSpawn = StartPointP1.spawnpoint1;
  54.         playerTwoSpawn = StartPointP2.spawnpoint2;
  55.  
  56.         endScreenColour = endScreenBackground.GetComponent<Image>().color; // Set this color to be the original colour of the endScreen.
  57.         Color transparent = new Color(0, 0, 0, 0);
  58.         endScreenBackground.GetComponent<Image>().color = transparent; // Set the endScreen to be transparent so we can fade it in later
  59.  
  60.         endScreen.SetActive(false); winnerName.SetActive(false); // Disable the endScreen and its children which are all related to end of game's UI
  61.         loserName.SetActive(false); winnerIcon.SetActive(false); loserIcon.SetActive(false); winnerText.SetActive(false); loserText.SetActive(false);
  62.         menuButton.SetActive(false); continueButton.SetActive(false);
  63.  
  64.     }
  65.  
  66.     // Update is called once per frame
  67.     void Update()
  68.     {
  69.         playerOneSpawn = StartPointP1.spawnpoint1;
  70.         playerTwoSpawn = StartPointP2.spawnpoint2;
  71.         if (deadPlayers.Count == 0) // If there are no dead players, dont do anything
  72.         {
  73.             return;
  74.         }
  75.         else
  76.         {
  77.             theCamera.StartCoroutine(theCamera.DeathCam(deadPlayers[0])); // Focuses the death cam on the first player that died in the round
  78.         }
  79.  
  80.     }
  81.  
  82.     public void AddPlayerWin(GameObject player)
  83.     {
  84.         if (player == player1) // If the player that called this method is the same as player1 reference, give player1 1 win
  85.         {
  86.             playerOneWins += 1;
  87.  
  88.             if (playerOneWins == 5) // If this player has reached 5 wins, they won
  89.             {
  90.                 winner = player1;
  91.                 loser = player2;
  92.             }
  93.         }
  94.         else if (player == player2) // Else if player that called this method is the same as player2 reference, give player2 1 win
  95.         {
  96.             playerTwoWins += 1;
  97.  
  98.             if (playerTwoWins == 5) // If this player has reached 5 wins, they won
  99.             {
  100.                 winner = player2;
  101.                 loser = player1;
  102.             }
  103.         }
  104.  
  105.         StartCoroutine(UpdateWinUI());
  106.     }
  107.  
  108.     public IEnumerator MovePlayer(GameObject player, GameObject spawnpoint)
  109.     {
  110.         yield return new WaitForSeconds(0.1f);
  111.         Vector3 offset = new Vector3(0, 1, 0); // Add this to transform.position of spawnpoint so player is moved on top of it
  112.  
  113.         for (float t = 0; t < 2f; t += Time.deltaTime)
  114.         {
  115.             player.transform.position = Vector3.Lerp(player.transform.position, spawnpoint.transform.position + offset, t / 6);
  116.  
  117.             yield return null;
  118.         }
  119.  
  120.         // Reenable character controller of player if there is a character controller attached
  121.         if (player.GetComponent<CharacterController>())
  122.         {
  123.             player.GetComponent<CharacterController>().enabled = true;
  124.         }
  125.  
  126.         //if (player.GetComponent<PlayerCube>())
  127.         //{
  128.         //    player.GetComponent<PlayerCube>().isMoving = true; // Allow the player to die again
  129.         //}
  130.  
  131.         // Reenable the kill plane
  132.         killPlane.SetActive(true);
  133.         yield break;
  134.     }
  135.  
  136.     public IEnumerator SpawnLevel()
  137.     {
  138.         Vector3 modifier = new Vector3(levelsLoaded, 0, 0); // Multiplies the offset so new levels keep getting created further in the x-axis
  139.  
  140.        
  141.         while (winner == null)
  142.         {
  143.             // Gets a random level from the level list to load
  144.             levelID = Random.Range(0, listOfLevels.Count - 1); // Substract 1 from Count because final level is the win level; We don't want to load it
  145.                                                                // if no one won. Max is excluded, therefore the highest number correspond to level before final
  146.                                                                // level in list
  147.             break;
  148.         }
  149.         while (winner != null) // If there is a winner, do not load a random level and always load end level instead
  150.         {
  151.             levelID = listOfLevels.Count - 1; // End Level should always be final element in list for this to work
  152.             break;
  153.         }
  154.  
  155.         Debug.Log("Getting new Level");
  156.         levelToLoad = listOfLevels[levelID];
  157.  
  158.         // Multiply these two vectors component by component, EX: 100 * modifier.x, 0 * modifier.y, 100 * modifier.z
  159.         offset = Vector3.Scale(new Vector3(60, 0, 0), modifier);
  160.         print("" + offset);
  161.  
  162.         // Load the new level offset from the previous level in the x-axis
  163.         currentLevel = Instantiate(levelToLoad, offset, Quaternion.identity);
  164.         Debug.Log("New Level Spawned");
  165.         // Increase the number of levels loaded by one
  166.         levelsLoaded += 1;
  167.  
  168.         yield return null;
  169.  
  170.         // Disable the kill plane to allow fallen players to move to the next level without dying in the process
  171.         killPlane.SetActive(false);
  172.  
  173.         // Move the players and their marks to the next level
  174.         StartCoroutine(MovePlayer(player1, playerOneSpawn));
  175.         StartCoroutine(MovePlayer(player1Mark, playerOneSpawn));
  176.         StartCoroutine(MovePlayer(player2, playerTwoSpawn));
  177.         StartCoroutine(MovePlayer(player2Mark, playerTwoSpawn));
  178.  
  179.         player1.GetComponent<PlayerCube>().won = false;
  180.         player2.GetComponent<PlayerCube>().won = false;
  181.  
  182.         yield return new WaitForSeconds(1.5f); // Add a delay so theres enough for the players to move away so camera doesnt see lastLevel get destroyed
  183.         Destroy(lastLevel); // Destroys the previous level off-screen
  184.         lastLevel = currentLevel; // Changes last level to the new current level
  185.  
  186.         while (winner != null) // If there is a winner, switch from regular level music to victory music
  187.         {
  188.             SoundManager.instance.StopPlaying("LevelMusic");
  189.             SoundManager.instance.Play("VictoryMusic");
  190.             break;
  191.         }
  192.  
  193.         yield break;
  194.     }
  195.  
  196.     public IEnumerator UpdateWinUI()
  197.     {
  198.         yield return new WaitForSeconds(3.5f); // Wait for first camera transition to zoom in and out
  199.  
  200.         Debug.Log("Shatter");
  201.         SoundManager.instance.Play("Shatter"); // Play Shatter SFX, followed shortly by ding SFX
  202.         yield return new WaitForSeconds(0.1f);
  203.  
  204.         Debug.Log("Ding");
  205.         SoundManager.instance.Play("Ding");
  206.         yield return new WaitForSeconds(0.45f);
  207.  
  208.         // Update the number of wins in the canvas
  209.         playerOneCount.text = "" + playerOneWins;
  210.         playerTwoCount.text = "" + playerTwoWins;
  211.  
  212.         while (winner != null)
  213.         {
  214.             StartCoroutine(WinScreenReveal(winner)); // If there is winner, start the winScreenReveal
  215.  
  216.             yield break;
  217.         }
  218.     }
  219.  
  220.     public IEnumerator WinScreenReveal(GameObject nameOfWinner)
  221.     {
  222.         // Re-enable the endScreen
  223.         endScreen.SetActive(true);
  224.  
  225.         // Set scales of the images to 0,0,0 so we can scale them up later
  226.         winnerIcon.transform.localScale = Vector3.zero;
  227.         loserIcon.transform.localScale = Vector3.zero;
  228.  
  229.         yield return new WaitForSeconds(2.9f); // Wait a few seconds to give players time to move to the final scene
  230.         // Set the strings and images for the winner and loser's UI
  231.         while (nameOfWinner == player1)
  232.         {
  233.             // If player 1 won, change winner and loser's name to P1 and P2
  234.             winnerName.GetComponent<TMP_Text>().text = "Player 1";
  235.             loserName.GetComponent<TMP_Text>().text = "Player 2";
  236.  
  237.             // Change the icons as well
  238.             winnerIcon.GetComponent<Image>().sprite = playerOneIcon;
  239.             loserIcon.GetComponent<Image>().sprite = playerTwoIcon;
  240.  
  241.             break;
  242.         }
  243.  
  244.         while (nameOfWinner == player2)
  245.         {
  246.             // If player 2 won, change winner and loser's name to P2 and P1
  247.             winnerName.GetComponent<TMP_Text>().text = "Player 2";
  248.             loserName.GetComponent<TMP_Text>().text = "Player 1";
  249.  
  250.             // Change the icons as well
  251.             winnerIcon.GetComponent<Image>().sprite = playerTwoIcon;
  252.             loserIcon.GetComponent<Image>().sprite = playerOneIcon;
  253.  
  254.             break;
  255.         }
  256.  
  257.         // Get the current color of endScreen to use for the transition
  258.         Color transitionColor = endScreen.GetComponent<Image>().color;
  259.  
  260.         for (float t = 0.0f; t < 1.0f; t += Time.deltaTime) // Over one second, fade in the endScreen
  261.         {
  262.             transitionColor = Color.Lerp(transitionColor, endScreenColour, t); // Lerp the transitionColor torwards the originalColour over time
  263.             endScreen.GetComponent<Image>().color = transitionColor; // Set the new colour of endScreen
  264.  
  265.             yield return null;
  266.         }
  267.  
  268.         yield return new WaitForSeconds(1f); // Wait for 1 second
  269.  
  270.         // Re-enable the winner and loser text, these are not the names of the players who won/lost
  271.         winnerText.SetActive(true); loserText.SetActive(true);
  272.  
  273.         winnerIcon.SetActive(true);
  274.         for (float t = 0.0f; t < 0.5f; t += Time.deltaTime)
  275.         {
  276.             // Lerp the scale of the winnerIcon first up to 1,1,1
  277.             winnerIcon.transform.localScale = Vector3.Lerp(winnerIcon.transform.localScale, Vector3.one,  2 * t); // Multiply t by 2 since t < 0.5
  278.  
  279.             yield return null;
  280.         }
  281.        
  282.         // Re-enable the winner's name after the icon of the winner has popped up
  283.         winnerName.SetActive(true);
  284.  
  285.         yield return new WaitForSeconds(0.4f);
  286.         loserIcon.SetActive(true);
  287.         for (float t = 0.0f; t < 0.5f; t += Time.deltaTime)
  288.         {
  289.             // Lerp the scale of the loserIcon next, up to 1,1,1
  290.             loserIcon.transform.localScale = Vector3.Lerp(loserIcon.transform.localScale, Vector3.one, 2 * t);
  291.  
  292.             yield return null;
  293.         }
  294.  
  295.         // Re-enable the loser's name after the icon of the loser has popped up
  296.         loserName.SetActive(true);
  297.  
  298.         SoundManager.instance.Play("Applause");
  299.  
  300.         yield return new WaitForSeconds(3f); // Show completed winScreen for at least 3s before the player can close it
  301.  
  302.         menuButton.SetActive(true); continueButton.SetActive(true);
  303.     }
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement