-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameManager.cs
228 lines (212 loc) · 6.74 KB
/
GameManager.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Graffiti Softwerks 2022
* Game Manager.cs
* Author: Nash Ali
* Creation Date: 04-10-2022
* Last Update: 04-30-2022
*
* Copyright (c) Graffiti Softwerks 2022
*
* Defines how the program flows from the various scenes.
* gameplay mechanics can be created and adjusted from here.
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.IO;
using UnityEngine.Networking;
using UnityEngine.AdaptivePerformance;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class GameManager : MonoBehaviour
{
private static GameManager _instance;
public static GameManager Instance
{
get
{
if (_instance == null)
{
Debug.LogError("Game Manager is null.");
}
return _instance;
}
}
public SoundManager soundManager;
public GameObject player, bullet, bomb;
public List<GameObject> enemies;
public float difficulty = 1;
private int _scoreValue;
public int Score
{
get
{
return _scoreValue;
}
}
private int cowsNumber = 0 , bullsNumber = 0;
[SerializeField]
public bool isGameActive, isPlayerAlive = false;
[SerializeField]
private GameObject InGameMenu;
[SerializeField]
public TextMeshProUGUI scoreText, levelText, myTime;
[SerializeField]
private Button startButton, restartButton, stopButton, continueButton;
[SerializeField]
private RawImage gameOverImage;
string stringToSave;
// Frequency of spawning, this will get smaller as difficulty increases.
private float spawnRate = 10.0f;
//200 meters range to start and it decrements as difficulty increases ie they get closer.
private float spawnRange = 50.0f;
private IAdaptivePerformance ap;
#region ******************* Monobehaviour ********************
private void Awake()
{
Input.backButtonLeavesApp = true;
}
void Start()
{
Debug.Log("GAME MANAGER: game started - menu buttons ready!");
CheckAP();
player.GetComponent<GameObject>();
startButton.GetComponent<Button>();
restartButton.GetComponent<Button>();
stopButton.GetComponent<Button>();
continueButton.GetComponent<Button>();
myTime.GetComponent<Text>();
gameOverImage.GetComponent<RawImage>();
//add listeners for game menu.
startButton.onClick.AddListener(StartGame);
stopButton.onClick.AddListener(StopGame);
continueButton.onClick.AddListener(ContinueGame);
restartButton.onClick.AddListener(RestartGame);
}
/// <summary>
/// Update is called once per frame
/// </summary>
void Update()
{
ShowTime();
}
#endregion *********************
#region ****************** User Code *******************************
void CheckAP()
{
ap = Holder.Instance;
if (ap == null || !ap.Active)
{
Debug.Log("[AP ClusterInfo] Adaptive Performance not active.");
return;
}
if (!ap.SupportedFeature(UnityEngine.AdaptivePerformance.Provider.Feature.ClusterInfo))
{
Debug.Log("[AP ClusterInfo] Feature not supported.");
}
_ = ap.PerformanceStatus.PerformanceMetrics.ClusterInfo;
}
/// <summary>
/// When the game starts, these are the items that need to be initialized correctly.
/// This is done when the game start has been pressed or a restart is called.
/// </summary>
public void StartGame()
{
Debug.Log("GAME MANAGER: game running - player pressed start!");
isPlayerAlive = true;
isGameActive = true;
_scoreValue = 0;
UpdateScore(0);
spawnRate /= difficulty;
StartCoroutine(SpawnTarget());
}
// Handles the clean exit from app.
public void StopGame()
{
SaveGame();
#if UNITY_EDITOR
EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
public void ShowTime()
{
if (myTime.isActiveAndEnabled)
{
myTime.text = System.DateTime.Now.ToString("HH:mm dd MMMM, yyyy");
}
}
public void ContinueGame()
{
Debug.Log("GAME MANAGER: game running - player continue!");
InGameMenu.SetActive(false);
isGameActive = true;
}
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void PauseGame()
{
Debug.Log("GAME MANAGER: game running - player pressed pause!");
isGameActive = false;
InGameMenu.SetActive(true);
}
public void UpdateScore(int scoreToAdd)
{
Debug.Log("GAME MANAGER: game running - score updated!");
_scoreValue += scoreToAdd;
scoreText.text = "Score: " + _scoreValue;
}
void SaveGame()
{
PlayerPrefs.SetInt("HighScore", _scoreValue);
PlayerPrefs.SetString("PlayerName", stringToSave);
PlayerPrefs.Save();
Debug.Log("Game data saved!");
}
Vector3 RandomPosition()
{
return new Vector3(Random.Range(-spawnRange, spawnRange), 0, Random.Range(-spawnRange, spawnRange));
}
/*****************************************************************
*
* This will spawn the cows in progressive waves.
*****************************************************************/
IEnumerator SpawnTarget()
{
while (isGameActive && isPlayerAlive)
{
yield return new WaitForSeconds(spawnRate);
Vector3 randomPosition = new(RandomPosition().x, 0, RandomPosition().z);
//set gender - random.
int sex = Random.Range(0, targets.Count);
Instantiate(targets[sex], randomPosition, Quaternion.Euler(0, Random.Range(0f, 360f), 0f));
//call up the Sound Manager to play the sound
soundManager.PlaySpawnSound();
if (sex == 0)
{
cowsNumber++;
//cowsText.text = "Cows : " + cowsNumber;
}
else
{
bullsNumber++;
//bullsText.text = "Bulls : " + bullsNumber;
}
}
}
public void GameOver()
{
isGameActive = false;
isPlayerAlive = false;
gameOverImage.gameObject.SetActive(true);
InGameMenu.SetActive(true);
}
#endregion ********************************************
}