/* GameManager.cs v1.1 by AABattery
* http://rpgmaker.net/users/AABattery/
* Unity 3.5
* 04/07/12
*
* DESCRIPTION:
* This is a basic script whose purpose is to help organize your game's important
* variables, for example, player name, where the player is, and their progression.
* It will also allow for multiple save and load slots. You can add new functions
* and variables in this script, just make sure that you follow the format that I
* used. The code is easy to follow, so beginners shouldn't find it hard to change.
*
* HOW TO USE:
* - Make a new C# script in your project and name it "GameManager".
* - Open the file and paste this script.
* - Add variables and functions you need. Best way to do this is to copy/paste
* functions I have already made.
* - Make a variable that will contain the script like shown below
* - Drag the GameManger script onto the variable in the editor
* - Use the lines of code shown below to access the GameManager
*
* ACCESSING VIA OTHER SCRIPTS:
* To access the functions in this script, you need to make a variable that will
* contain the script followed by the function you want to use. These are the
* lines you will be using:
*
* public GameManager gameManager; - set variable for use (C#)
* var gameManger : GameManager; - set variable for use (JS)
* gameManager.LoadGame - requires a string
* gameManager.SaveGame - doesn't require a filed
* gameManager.GetInt - requires a string
* gameManager.GetString - requires a string
* gameManager.SetInt - requires a string and an integer
* gameManager.SetString - requires 2 strings
* gameManager.Add - requires a string and an integer
* gameManager.Sub - requires a string and an integer
*
* Here are a few examples on how to use the lines. When dealing with save files,
* the number you place in the parenthesis "()" must be surrounded by quotation
* marks to work.
*
* ===== GameManager.LoadGame =====
* void OnGUI () {
* if (GUI.Button(Rect(10,10,50,50),"File 2"))
* gameManager.LoadGame("2"); // loads vars on file 2 and takes player to game
* }
*
* ===== GameManager.SaveGame =====
* void OnGUI () {
* if (GUI.Button(Rect(10,10,50,50),"Save Game"))
* gameManager.SaveGame(); // saves game in the file selected in the main menu
* }
*
* ===== GameManager.GetInt =====
* void Start () {
* if(gameManager.GetInt("score") > gameManager.GetInt("hiscore"))
* Debug.Log("New HiScore!!!"); // shows if score is higher than hiscore
* }
* ===== GameManger.GetString =====
* void Start () {
* Debug.Log(gameManager.GetName("name")); // prints name in console
* }
*
* ===== GameManager.SetInt =====
* void Start () {
* gameManager.SetInt("life", 3); // Sets the lives to 3
* }
*
* ===== GameManager.SetString =====
* void Start () {
* gameManager.SetString("name", "Bob"); // Sets name to "Bob"
* }
*
* ===== GameManager.Add =====
* void Update () {
* gameManager.Add("score", 60); // Adds 60 points to player score
* }
*
* ===== GameManager.Sub =====
* void OnTriggerEnter () {
* Debug.Log("You got hit!");
* if(gameManager.Sub("life", 1)){ // player loses life, if no lives left...
* GameOver.Run(); // ...run a Game Over script
* }
* }
*
* CONFUSED?
* Scroll down to each function to see what each do. This way, it will be easier
* to make your own.
*/

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {

// Variables used in the script.
public string saveFile;
public string name;
public int lives;
public int score;
public string level;

/* ========================== FILE MANAGEMENT ==========================
* The functions below control the saving and loading of information. The
* variables are saved in the player preferences.
*/

// Loads all the variables. Requires an integer (wrapped in quotes) so that
// your game can support multiple save files. After the load, the function
// will send the player to the scene they saved in.
public void LoadGame (string file) {
saveFile = file;
name = PlayerPrefs.GetString("Name" + file);
lives = PlayerPrefs.GetInt("Lives" + file);
level = PlayerPrefs.GetString("Level" + file);
Application.LoadLevel(level);
}

// Saves all variables onto the player prefs. Requires that you define
// the save slot when player creates a new game.
public void SaveGame () {
PlayerPrefs.SetString("Name" + saveFile, name);
PlayerPrefs.SetInt("Lives" + saveFile, lives);
PlayerPrefs.SetString("Level" + saveFile, level);
}

/* =========================== GET VARIABLES ===========================
* The functions below will allow you to get the variables that you need
* for use outside this script, for example, displaying the player's score.
* They return a value, so I had to separate the string and int variables
* into different functions.
* The string in each of the if statements determine the name you must
* use outside this script. Change them to whichever word suits you.
*/

// Gets the integer from the variable specified. Returns an int value.
// Variables in this function must be integers to work.
public int GetInt (string variable) {
if(variable == "lives") {
return lives;
} else if(variable == "score") {
return score;
} else {
return 0;
}
}

// Gets the string from the variable specified. Returns an int value.
// Variables in this function must be strings to work.

public string GetString (string variable) {
if(variable == "save") {
return saveFile;
} else if(variable == "name") {
return name;
} else {
return "";
}
}

/* =========================== SET VARIABLES ===========================
* The functions below will allow you to set the variables to whatever
* you want, for example, setting the name.
* They return a value, so I had to separate the string and int variables
* into different functions.
* The string in each of the if statements determine the name you must
* use outside this script. Change them to whichever word suits you.
*/

// Sets an integer to the variable specified. Variables in this function
// must be integers to work.
public void SetInt (string variable, int amount) {
if(variable == "lives") {
lives = amount;
} else if(variable == "score") {
score = amount;
}
}

// Sets a string to the variable specified. Variables in this function
// must be strings to work.
public void SetString (string variable, string line) {
if(variable == "save") {
saveFile = line;
} else if(variable == "name") {
name = line;
}
}

/* ========================= INTEGER CONTROL =========================
* The functions below makes adding and subtracting quick. This section
* doesn't need much explaining since the functions speak for themselves,
* however, you can only add or subtract integers. Strings won't work.
*/

// Adds to an integer
public void Add (string variable, int amount) {
if(variable == "score") {
score += amount;
} else if (variable == "lives") {
lives += amount;
}
}

// Subtracts from an integer
public void Sub (string variable, int amount) {
if(variable == "score") {
score -= amount;
if(score < 0) {
score = 0;
}
} else if (variable == "lives") {
SubLife(amount);
}
}

/* ========================== SUBTRACT LIFE ==========================
* An extra that I have added onto the Manager. This function subtracts
* the lives from the player and checks whether they have lives left. If
* they do not, the function will redirect to the Game Over script.
* This function can also be rewritten for inventory systems and money
* systems.
*/

public void SubLife(int amount) {
if(amount > lives) {
// run Game Over script.
} else {
lives -= amount;
}
}
}