topical media & game development
lib-unity-tutorial-m2h-2-Assets-Game-3-Marble-game-Scripts-MarbleGameManager.cs / cs
using UnityEngine;
using System.Collections;
public enum MarbleGameState {playing, won,lost };
public class MarbleGameManager : MonoBehaviour
{
public static MarbleGameManager SP;
private int totalGems;
private int foundGems;
private MarbleGameState gameState;
void Awake()
{
SP = this;
foundGems = 0;
gameState = MarbleGameState.playing;
totalGems = GameObject.FindGameObjectsWithTag("Pickup").Length;
Time.timeScale = 1.0f;
}
void OnGUI () {
GUILayout.Label(" Found gems: "+foundGems+"/"+totalGems );
if (gameState == MarbleGameState.lost)
{
GUILayout.Label("You Lost!");
if(GUILayout.Button("Try again") ){
Application.LoadLevel(Application.loadedLevel);
}
}
else if (gameState == MarbleGameState.won)
{
GUILayout.Label("You won!");
if(GUILayout.Button("Play again") ){
Application.LoadLevel(Application.loadedLevel);
}
}
}
public void FoundGem()
{
foundGems++;
if (foundGems >= totalGems)
{
WonGame();
}
}
public void WonGame()
{
Time.timeScale = 0.0f; //Pause game
gameState = MarbleGameState.won;
}
public void SetGameOver()
{
Time.timeScale = 0.0f; //Pause game
gameState = MarbleGameState.lost;
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.