using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour
{
public GameObject hazard;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public GUIText sorceText;
public GUIText restartText;
public GUIText gameOverText;
public GUIText testText;
private int score;
private bool gameOver;
private bool restart;
public static float xMin, xMax, yMin, yMax;
void Start ()
{
testText.text = "start";
Debug.Log ("Start ");
Camera cam = Camera.main;
float CamCentX = cam.gameObject.transform.position.x;
float CamCentY = cam.gameObject.transform.position.y;
float Camheight = 2f * cam.orthographicSize;
float Camwidth = Camheight * cam.aspect;
xMin = CamCentX - Camwidth/2.0f;
xMax = CamCentX + Camwidth/2.0f - hazard.transform.localScale.x;
yMin = CamCentY - Camheight/2.0f;
yMax = CamCentY + Camheight/2.0f;
gameOver = false;
restart = false;
restartText.text = "";
gameOverText.text = "";
score = 0;
UpdateScore ();
StartCoroutine (SpawnWaves ());
}
float lastClickTime = 0.0f;
float catchTime = 0.25f;
void Update(){
if (restart) {
if(Input.touchCount > 0 || Input.GetKeyDown(KeyCode.R)){
if(Time.time-lastClickTime<catchTime){
//double click
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex) ;
//print("done:"+(Time.time-lastClickTime).ToString());
}else{
//normal click
print("miss:"+(Time.time-lastClickTime).ToString());
}
}
}
}
IEnumerator SpawnWaves(){
yield return new WaitForSeconds (startWait);
while (true)
{
testText.
text = "In while (true), \nT = " + Time.
time; for (int i = 0; i < hazardCount; i++)
{
//i++;
testText.text = xMin + "," + xMax + "," + yMax+"\n i:"+i+", cnt:"+hazardCount;
testText.
text += "\nwait" + spawnWait
+ "," + waveWait
+ ",T=" + Time.
time + "\n"; testText.text += spawnWait+", "+startWait+", "+ waveWait;
//testText.text = "CreatHazard:" + i;
Vector3 spawnPosition = new Vector3 (Random.Range (xMin, xMax), yMax, 0);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, transform.rotation);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (waveWait);
if (gameOver) {
testText.
text = "In gameOver, \nT = " + Time.
time; //restartText.text = "Press 'R' to Restart";
restartText.text = "Double click to Restart";
restart = true;
break;
}
}
}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
void UpdateScore ()
{
sorceText.text = "Score: " + score;
}
public void GameOver(){
gameOverText.text = "Game Over !!";
gameOver = true;
}
}