Deozaan

Arcade Shooter GameController

Oct 9th, 2013
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.36 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class GameController : MonoBehaviour {
  5.  
  6.     public GameObject enemy;
  7.     public int enemyCount; // 10
  8.    
  9.     private int score;
  10.    
  11.     private bool gameOver;
  12.     private bool restart;
  13.    
  14.     void Awake () {
  15.         score = 0;
  16.         gameOver = false;
  17.         restart = false;
  18.     }
  19.  
  20.     void Start () {
  21.         StartCoroutine(SpawnWaves ());
  22.     }
  23.    
  24.     void OnGUI () {
  25.         GUI.Label(new Rect(10, 10, 100, 20), "Score: " + score);
  26.         if (gameOver) {
  27.             GUI.Label(new Rect(Screen.width / 2 - 40, Screen.height / 3, 80, 20), "Game Over!");
  28.         }
  29.         if (restart) {
  30.             if (GUI.Button(new Rect(Screen.width - 70, 10, 60, 20), "Restart")) {
  31.                 Application.LoadLevel(Application.loadedLevel);
  32.             }
  33.         }
  34.     }
  35.  
  36.     IEnumerator SpawnWaves () {
  37.         yield return new WaitForSeconds (1.0f);
  38.        
  39.         while (true) {
  40.             for (int i = 0; i < enemyCount; i++) {
  41.                 Vector3 spawnPosition = new Vector3(Random.Range(-9.0f, 9.0f), 0.0f, 26.0f);
  42.                 GameObject clone = Instantiate(enemy, spawnPosition, Quaternion.identity) as GameObject;
  43.                 clone.transform.parent = transform;
  44.                 yield return new WaitForSeconds (0.5f);
  45.             }
  46.            
  47.             yield return new WaitForSeconds (3.0f);
  48.            
  49.             if (gameOver) {
  50.                 restart = true;
  51.                 break;
  52.             }
  53.         }
  54.     }
  55.    
  56.     public void AddScore (int newScoreValue) {
  57.         score += newScoreValue;
  58.     }
  59.    
  60.     public void GameOver () {
  61.         gameOver = true;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment