Unity – Spaceship Shooting Game – JS – Game Over
MAIN TOP MENU> GameObject> Create Empty, rename it ‘Display Text’, Inspector> Transform> gear icon> Reset
MAIN TOP MENU> GameObject> Create Other> GUI Text, rename it ‘Restart Text’> Text ‘Restart’
MAIN TOP MENU> GameObject> Create Other> GUI Text, rename it ‘Game Over Text’> Text ‘Game Over’
Hierarchy: Display Text (father)
– Score Text (child)
– Restart Text (child)
– Game Over Text (child)
Game Controller empty object has GameController.js:
#pragma strict
var hazard : GameObject;
var spawnValues : Vector3;
var hazardCount : int;
var spawnWait : float;
var startWait : float;
var waveWait : float;
// GUI Text da assegnare da Inspector
var scoreText : GUIText;
var restartText : GUIText;
var gameOverText : GUIText;
// variabili per il game over, restart, sono booleane VERO O FALSO
private var gameOver : boolean;
private var restart : boolean;
private var score : int;
function Start () {
// gameover falso all'inizio
gameOver = false;
// restart falso all'inizio
restart = false;
// GUIText Restart è vuoto all'inizio
restartText.text = "";
// GUI Text Game Over è vuoto all'inizio
gameOverText.text = "";
score = 0;
UpdateScore ();
StartCoroutine (SpawnWaves ());
}
function Update () {
// se restart è vero, vedi sopra che è una variabile booleana
// questo dato è inviato dalla riga - if (gameOver)
if (restart)
{
// resta in ascolto del tasto R da tastiera
// se viene attivato
if (Input.GetKeyDown (KeyCode.R))
{
// ricarica il livello
Application.LoadLevel (Application.loadedLevel);
}
}
}
function SpawnWaves () {
yield WaitForSeconds (startWait);
while (true)
{
for ( var i : int= 0; i < hazardCount; i++)
{
var spawnPosition : Vector3= new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
var spawnRotation : Quaternion= Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);
yield WaitForSeconds (spawnWait);
}
yield WaitForSeconds (waveWait);
// se gameOver è vero, vedi sopra che è una variabile booleana
if (gameOver)
{
// scrivi a video
restartText.text = "Press 'R' for Restart";
// setta restart a vero
restart = true;
// interrompi il gioco
break;
}
}
}
function AddScore (newScoreValue : int) {
score += newScoreValue;
UpdateScore ();
}
function UpdateScore () {
scoreText.text = "Score: " + score;
}
// la funzione GameOver() viene richiamata da DestroyByContact.js
function GameOver () {
// scrivi a video Game Over!
gameOverText.text = "Game Over!";
// setta la variabile booleana a vero
gameOver = true;
}
Inspector, you have to assign:
– Score Text (GUIText)
– Restart Text (GUIText)
– Game over Text (GUIText)
Asteroid prefab has DestroyByContact.js:
#pragma strict
var explosion : GameObject;
var playerExplosion : GameObject;
var scoreValue : int;
private var gameController : GameController;
function Start ()
{
// Richiama l'oggetto Game Controller e il suo script GameController.js INIZIO
var gameControllerObject : GameObject = GameObject.FindWithTag ("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent (GameController);
}
if (gameController == null)
{
Debug.Log ("Cannot find 'GameController' script");
}
// Richiama l'oggetto Game Controller e il suo script GameController.js FINE
}
function OnTriggerEnter(other : Collider)
{
if (other.tag == "Boundary")
{
return;
}
Instantiate(explosion, transform.position, transform.rotation);
if (other.tag == "Player")
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
// dopo che è istanziato il VFX per l'esplosione del Player
// richiama Game Controller oggetto> GameObject.js> funzione Gameover()
gameController.GameOver ();
}
gameController.AddScore (scoreValue);
Destroy(other.gameObject);
Destroy(gameObject);
}
In conclusione:
1. Asteroide è un Prefab con associato DestroyByContact.js
a) istanzia l’esplosione del Player
b) richiamala funzione di Gameover() di GameController.js
2. Game Controller è un empty object con associato GameController.js
a) riceve da DestroyByContact.js l’attivazione della funzione Gameover()
b) Gameover() scrive ‘Game Over!’ sul display e setta la variabile booleana gameOver a VERO
c) Se gameOver è VERO scrive a video ‘Press R for Restart’ e setta la variabile booleana restart a VERO, interrompe il ciclo while con break
d) Se restart è VERO resta in attesa che venga premuto ‘R’
e) Se viene premuto R viene ricaricato il livello – Application.LoadLevel (Application.loadedLevel) –