-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameController.cs
111 lines (95 loc) · 2.39 KB
/
GameController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameControl : MonoBehaviour
{
public static GameControl instance;
// Game states
public enum GameState { MainMenu, Playing, Paused, GameOver }
public GameState currentState;
// UI panels go here
private void Awake()
{
// Ensure only one instance of GameControl exists :)
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
private void Start()
{
// Initialize the game
ChangeState(GameState.Playing);
}
private void Update()
{
//input based on the current game state
switch (currentState)
{
case GameState.Playing:
if (Input.GetKeyDown(KeyCode.Escape))
{
PauseGame();
}
break;
case GameState.Paused:
if (Input.GetKeyDown(KeyCode.Escape))
{
ResumeGame();
}
break;
}
}
public void ChangeState(GameState newState)
{
currentState = newState;
// Perform additional actions based on the state
switch (currentState)
{
case GameState.MainMenu:
Time.timeScale = 1;
break;
case GameState.Playing:
Time.timeScale = 1;
break;
case GameState.Paused:
Time.timeScale = 0;
break;
case GameState.GameOver:
Time.timeScale = 1;
break;
}
}
public void StartGame()
{
ChangeState(GameState.Playing);
SceneManager.LoadScene("GameScene");
}
public void PauseGame()
{
ChangeState(GameState.Paused);
}
public void ResumeGame()
{
ChangeState(GameState.Playing);
}
public void GameOver()
{
ChangeState(GameState.GameOver);
}
public void ReturnToMainMenu()
{
ChangeState(GameState.MainMenu);
SceneManager.LoadScene("MainMenu"); // Ensure you have a scene named "MainMenu"
}
public void QuitGame()
{
Application.Quit();
}
}