Unity-SpaceShooter/Assets/Scripts/GameController.cs

78 lines
1.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameController : MonoBehaviour {
public GameObject[] hazards;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait, startWait, waveWait;
public Text scoreText, gameOverText; //restartText
public GameObject restartButton;
private int score;
private bool gameOver;
private bool restart;
void Start()
{
score = 0;
gameOver = false;
restart = false;
//restartText.text = "";
gameOverText.text = "";
restartButton.SetActive(false);
UpdateScore();
StartCoroutine (SpawnWaves());
}
// void Update()
// {
// if (restart)
// {
// if (Input.GetKeyDown(KeyCode.R))
// {
// Application.LoadLevel(Application.loadedLevel);
// }
// }
// }
IEnumerator SpawnWaves()
{
yield return new WaitForSeconds(startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
GameObject hazard = hazards[Random.Range(0, hazards.Length)];
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
yield return new WaitForSeconds(waveWait);
if (gameOver)
{
// restartText.text = "Press 'R' for Restart";
restartButton.SetActive(true);
restart = true;
break;
}
}
}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore();
}
void UpdateScore()
{
scoreText.text = "Score: " + score;
}
public void GameOver()
{
gameOverText.text = "Game Over!";
gameOver = true;
}
public void RestartGame()
{
Application.LoadLevel(Application.loadedLevel);
}
}