GameManager.cs 1.66 KB
Newer Older
Ashley's avatar
Ashley committed
1 2
using System.Collections;
using System.Collections.Generic;
Ashley's avatar
Ashley committed
3
using UnityEngine.UI;
Ashley's avatar
Ashley committed
4 5
using UnityEngine;

6 7
public class GameManager : SingletonBehaviour<GameManager>
{
Ashley's avatar
Ashley committed
8 9
    //Manages game state.  Pause, death, win, changing levels, and level time.

Ashley's avatar
Ashley committed
10
    private bool _isPaused;
11
    private float _levelTimer;
Ashley's avatar
Ashley committed
12
    public Text timerText;
Ashley's avatar
Ashley committed
13

Ashley's avatar
Ashley committed
14 15 16
    void Awake()
    {
        DontDestroyOnLoad(this);
Ashley's avatar
Ashley committed
17
        isPaused = true;
Ashley's avatar
Ashley committed
18
    }
Ashley's avatar
Ashley committed
19 20

    public bool isPaused
Ashley's avatar
Ashley committed
21
    {
Ashley's avatar
Ashley committed
22
        get
Ashley's avatar
Ashley committed
23
        {
Ashley's avatar
Ashley committed
24
            return _isPaused;
Ashley's avatar
Ashley committed
25
        }
Ashley's avatar
Ashley committed
26
        set
Ashley's avatar
Ashley committed
27
        {
Ashley's avatar
Ashley committed
28 29 30
            if (isPaused)
            {
                //set timescale back to normal
Ashley's avatar
Ashley committed
31
                Time.timeScale = 1;
Ashley's avatar
Ashley committed
32 33 34 35 36
                Debug.Log("Game is unpaused");
            }
            else
            {
                //set timescale to zero
Ashley's avatar
Ashley committed
37
                Time.timeScale = 0;
Ashley's avatar
Ashley committed
38 39
                Debug.Log("Game is paused");
            }
Ashley's avatar
Ashley committed
40
            _isPaused = value;
Ashley's avatar
Ashley committed
41 42
        }
    }
Ashley's avatar
Ashley committed
43
   
44 45 46 47 48
    public float levelTimer
    {
        get; set;
    }

Ashley's avatar
Ashley committed
49
    public void Fail()
Ashley's avatar
Ashley committed
50 51 52 53 54 55 56
    {
        Debug.Log("You have died");
    }
    public void Win()
    {
        Debug.Log("You won!");
    }
Ashley's avatar
Ashley committed
57
    public void ChangeLevel()
Ashley's avatar
Ashley committed
58 59
    {
        Debug.Log("Changing level state");
60
        levelTimer = Time.time;
Ashley's avatar
Ashley committed
61
    }
Ashley's avatar
Ashley committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75
    //formats the time for the text object
    private string formatTime(float time)
    {
        int minutes = Mathf.FloorToInt(time / 60F);
        int seconds = Mathf.FloorToInt(time - minutes * 60);
        return string.Format("{0:0}:{1:00}", minutes, seconds);
    }
    //updates the text object
    void Update()
    {
        string currentTime = formatTime(Time.time - levelTimer);
        timerText.text = "Time: " + currentTime;
    }
   
Ashley's avatar
Ashley committed
76
}