Starter

So one thing that really bothers me about doing jams is that my code sucks.  So as a professional coder I take pride in making good workable code.  So I’ve played around with creating a starter with no time pressure.

So some things with this starter that I have been avoiding that needs to be done is create a DATA object that doesn’t get deleted and has all current settings for the game.  A Singleton per se.  This is than attached to an empty object in the project.

public class Data : MonoBehaviour {
    public static string title = Title;
    public GameStates GameState;
    void Awake() {
        DontDestroyOnLoad(transform.gameObject);
    }
}

So another thing that I like is having the menu and pause screen overlay the game so I’ve created GameStates enum and a Menu script.  In the menu script it handles overlaying of the menu based on the current state.

public enum GameStates {
    MainMenu = 0,Running = 1,Paused = 2
}

public class Menu : MonoBehaviour {

    public Data data;

    // Use this for initialization
    void Start () {
        Time.timeScale = 0;
    }
    
    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown(KeyCode.Escape)) {
            Pause();
        }
    }
    void OnGUI() {
        switch (data.GameState)
        {
        case GameStates.MainMenu:
            MainMenu();
            break;
        case GameStates.Paused:
            PauseMenu();
            break;
        }
        
    }
    void MainMenu() {
        GUI.Label(new Rect((Screen.width / 2) – 2510200200), Data.title);
        if (GUI.Button(new Rect((Screen.width / 2) – 50Screen.height – 100 , 10050), Start)) {
            StartGame();
        }
    }
    void PauseMenu() {
        GUI.Label(new Rect((Screen.width / 2) – 2510200200), Data.title);
        GUI.Label(new Rect((Screen.width / 2) – 25110200200), PAUSED);
        if (GUI.Button(new Rect((Screen.width / 2) – 50Screen.height – 100 , 10050), Quit)) {
            Application.Quit();
        }
        if (GUI.Button(new Rect((Screen.width / 2) – 50Screen.height – 200 , 10050), Continue)) {
            UnPause();
        }
    }
    void Pause() {
        Time.timeScale = 0;
        data.GameState = GameStates.Paused;
    }
    void UnPause() {
        Time.timeScale = 1;
        data.GameState = GameStates.Running;
    }
    void StartGame() {
        Time.timeScale = 1;
        data.GameState = GameStates.Running;
        //TODOSend message
    }
}

So if you see in the menu class there is a super shitty menu layout.  So when 4.6 goes out of beta this will have to be updated.  You can also see that I’m using the timescale variable to turn off the game and turn the game on.
There is a TODO: send message.  So what this is designed for is I want to send an event saying that the game is unpaused or started and if another object wants to know it can listen for it.  Caviate is I don’t really know how to do that.  If you know please leave a comment.

Here is the link to the starter

Advertisements