zimethsuki

GameManager.cs

Jun 14th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. public class GameManager : MonoBehaviour
  5. {
  6.  
  7.     //bikin array untuk sprite2 mobil nya/
  8.  
  9.     public Sprite[] carSprites;
  10.  
  11.     //Reference to our game objects
  12.     public GameObject playButton;
  13.  
  14.     public GameObject playerCar;
  15.     public GameObject enemySpawner;
  16.     public GameObject GameOverGO;
  17.     public GameObject TimeCountdown;
  18.     public GameObject CongratulationMessage;
  19.     public GameObject FailedMessage;
  20.     private GameObject GameMaster;
  21.     public Button ReturnButton;
  22.  
  23.     public static GameManager GM;
  24.     public int carSelected;
  25.  
  26.     private GameScore ScoreManager;
  27.  
  28.     // Use this for initialization
  29.  
  30.     private void Awake()
  31.     {
  32.     }
  33.     private void Start()
  34.     {
  35.         GM = this;
  36.         GameMaster = GameObject.Find("GameMaster");
  37.         StartGamePlay();
  38.         ScoreManager = FindObjectOfType<GameScore>();
  39.         Debug.Log(carSelected);
  40.  
  41.     }
  42.  
  43.     // Function to update the game manager state
  44.     public void GamePlay()
  45.     {
  46.          //Count the timer
  47.         TimeCountdown.GetComponent<TimeCounter>().StartTimeCounter();
  48.  
  49.         //show the player while game start
  50.         playerCar.GetComponent<PlayerController>().Init();
  51.  
  52.         //Start Enemy Spawner
  53.         enemySpawner.GetComponent<EnemySpawner>().ScheduleEnemySpawner();
  54.     }
  55.  
  56.     public void GameOver()  
  57.     {
  58.         //Stop score when player die
  59.         ScoreManager.scoreIncrease = false;
  60.  
  61.         //Stop enemy Spawn
  62.         enemySpawner.GetComponent<EnemySpawner>().UnscheduleEnemySpawner();
  63.  
  64.         //Display Game Over
  65.         GameOverGO.SetActive(true);
  66.  
  67.         // ***** tombol return muncul kalo mati (cuma buat debug) *****
  68.         ReturnButton.gameObject.SetActive(true);
  69.        
  70.  
  71.         // ***** Kalau dapet highscore, munculin kata2 selamat. *****
  72.         if (ScoreManager.scoreCount >= ScoreManager.highScoreCount)
  73.         {
  74.             CongratulationMessage.SetActive(true);
  75.  
  76.             // ***** sekalian write highsscore nya ke playerpref *****
  77.             PlayerPrefs.SetFloat("HighScore", ScoreManager.highScoreCount);
  78.  
  79.             FailedMessage.SetActive(false);
  80.         }
  81.         else
  82.         {
  83.             FailedMessage.SetActive(true);
  84.             CongratulationMessage.SetActive(false);
  85.         }
  86.  
  87.         //Change game manager state to opening state
  88.         Invoke("ChangeToMainMenu", 8f);
  89.  
  90.         TimeCountdown.GetComponent<TimeCounter>().StopTimeCounter();
  91.     }
  92.  
  93.     public void StartGamePlay()
  94.     {
  95.         carSelected = GameMaster.GetComponent<GameMaster>().carSelected;
  96.         playerCar.GetComponent<SpriteRenderer>().sprite = carSprites[carSelected];
  97.         GamePlay();
  98.     }
  99.  
  100.     //Function to change game manager state to opening state
  101.     public void ChangeToMainMenu()
  102.     {
  103.         Application.LoadLevel("mainmenu");
  104.     }
  105. }
Add Comment
Please, Sign In to add comment