Advertisement
Smudla

da

Mar 7th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. public class GameController : MonoBehaviour
  6. {
  7.     float timeLeft = 3.0f;
  8.  
  9.     bool gameDone = false;
  10.  
  11.     // text který se zobrazí při ukončení hry
  12.     public Text winText;
  13.  
  14.     public Text changeLevelTextT;
  15.     public Text changeLevelTextC;
  16.    
  17.     //reference na gameobject hráč
  18.     public GameObject playerOne;
  19.     public GameObject playerTwo;
  20.    
  21.     //Maximální počet bodů ve scéně
  22.     public float maxScore;
  23.  
  24.     //reference na instanci scriptu "PlayerController"
  25.     private PlayerController pOne;
  26.     private PlayerController pTwo;
  27.  
  28.     void Start () {
  29.         //Inicializace referencí
  30.         pOne = playerOne.GetComponent<PlayerController>();
  31.         pTwo = playerTwo.GetComponent<PlayerController>();
  32.  
  33.         //Inicializace winTextu na prázdný řetězec
  34.         winText.text = "";
  35.  
  36.     }
  37.    
  38.     void Update () {
  39.             //Pokud jsou všechny body sesbírány true
  40.             if (pOne.GetCount() + pTwo.GetCount() >= maxScore)
  41.             {
  42.                 //
  43.                 gameOver();
  44.                 gameDone = true;
  45.                
  46.                 //odečítání času, který byl potřeba pro dokončení posledního framu každý frame
  47.                 timeLeft -= Time.deltaTime;
  48.                              
  49.                 //pokud vypršel čas, přepne scénu na "Level02"
  50.                 if (timeLeft <= 0.0)
  51.                 {
  52.                     Application.LoadLevel("Level02");
  53.                 }
  54.                
  55.                 //je počet bodů hráče jedna větší než hráče 2 ?
  56.                 if (pOne.GetCount() > pTwo.GetCount())
  57.                 {
  58.                    changeLevelTextT.text = "Level will be changed in: " + timeLeft + " seconds!";
  59.                    winText.text = "Player ONE win!";
  60.                 }
  61.  
  62.                 //je počet bodů hráče 2 větší než hráče 1 ?
  63.                 else if (pOne.GetCount() < pTwo.GetCount())
  64.                 {
  65.                    changeLevelTextT.text = "Level will be changed in: " + timeLeft + " seconds!";
  66.                    winText.text = "Player TWO win!";
  67.                 }
  68.  
  69.                 //je to remíza
  70.                 else
  71.                 {
  72.                    changeLevelTextT.text = "Level will be changed in: " + timeLeft + " seconds!";
  73.                    winText.text = "Same score.";
  74.             }
  75.         }
  76.     }
  77.  
  78.  
  79.     void gameOver()
  80.     {
  81.         if (gameDone == false)
  82.         {
  83.             StartCoroutine(Fade(3));
  84.         }
  85.  
  86.     }
  87.  
  88.  
  89.     private IEnumerator Fade(int coroutineTimeLeft)
  90.     {
  91.         for (int i = coroutineTimeLeft; i > 0; i--)
  92.         {
  93.             changeLevelTextC.text = "Level will be changed in: " + i + " seconds!";
  94.             yield return new WaitForSeconds(1);
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement