using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour {
public GameObject hazard;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
//游戏计分结束重来的GUI
public GUIText scoreText;
public GUIText restartText;
public GUIText gameOverText;
private int score;
private bool restart;
private bool gameOver;
void Start(){
score = 0;
gameOver = false;
restart = false;
gameOverText.text = "";
restartText.text = "";
UpdateScore ();
StartCoroutine(SpawnWaves ());
}
void Update(){
if (restart) {
if (Input.GetKeyDown(KeyCode.R)){
Application.LoadLevel(Application.loadedLevel);//会加载括号内的指定场景
}
}
}
IEnumerator SpawnWaves()//IEnumerator用来保证后面的WaitForSeconds(spawnWait);协同调用
{
yield return new WaitForSeconds (startWait);
while (true) {
for (int i=0; i<hazardCount; i++) {
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 0.5f, 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";
restart=true;
break;
}
}
}
void UpdateScore()
{
scoreText.text = "Score:" + score;
}
public void GameOver(){
gameOverText.text = "GAMEOVER!";
gameOver = true;
}
public void AddScore(int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
}
U3D<<SPACESHOOT>>个人重制版。代码分享(四)游戏控制部分
最新推荐文章于 2022-12-13 16:09:25 发布