Advertisement
Guest User

GameManager

a guest
Dec 13th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.79 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using UnityEngine.UI;
  6. using System.Linq;
  7. public class GameManager : MonoBehaviour
  8. {
  9. public List<GameObject> _currentCustomers;
  10. public List<GameObject> currentCustomers
  11. {
  12. get
  13. {
  14. if (_currentCustomers == null || _currentCustomers.Count == 0)
  15. {
  16. _currentCustomers = new List<GameObject>();
  17. }
  18. List<GameObject> temp = new List<GameObject>(_currentCustomers);
  19. foreach (GameObject customer in temp)
  20. {
  21. if (customer == null)
  22. {
  23. _currentCustomers.RemoveAt(temp.IndexOf(customer));
  24. }
  25.  
  26. }
  27. return _currentCustomers;
  28. }
  29. set
  30. {
  31. _currentCustomers = value;
  32. }
  33. }
  34. public Transform customerStartPosition;
  35. public GameObject customerPrefab;
  36.  
  37. [Header("UI references")]
  38. public GameObject startUI;
  39. public GameObject gameOverUI;
  40. public GameObject levelEndUI;
  41. public GameObject buttons;
  42. public TextMeshPro levelHeader;
  43. [Header("Timers")]
  44. // timers
  45. public float spawnTimer;
  46. public float waveTimer;
  47. public int currentSpawned = 0;
  48. public int currentCustomer;
  49. //public LevelData currentDay;
  50. public int currentWaveNum;
  51. public int currentLevelNum;
  52. public LevelData currentLevel;
  53.  
  54. public bool playing = true;
  55.  
  56. bool spawnedWave;
  57. public int spawnedInWave = 0;
  58. public float materialUsage = 0f;
  59.  
  60. public List<LevelData> levelData;
  61. void Start()
  62. {
  63. levelData = UniversalData.levelData;
  64. levelData = levelData.OrderBy(x => float.Parse((float)x.name)).ToList();
  65. foreach (LevelData level in UniversalData.levelData)
  66. {
  67. foreach(Wave wave in level.waves)
  68. {
  69. wave.GenerateCustomers();
  70. }
  71. }
  72. //dayData = UniversalData.dayData;
  73. /*
  74. if (FileBasedPrefs.HasKey("BestScore"))
  75. {
  76. bestScore = FileBasedPrefs.GetFloat("BestScore");
  77. }
  78. gameOverUI.transform.Find("Panel").Find("BestScore").GetComponent<TextMeshPro>().text = "" + bestScore;
  79. */
  80. }
  81. public GameObject robberPrefab;
  82. public Transform robberSpawnPoint;
  83. public void SpawnRobber() {
  84. var robber = Instantiate(robberPrefab);
  85. robber.transform.position = robberSpawnPoint.position;
  86. }
  87.  
  88. public void RegisterMaterialUsage()
  89. {
  90. materialUsage += 1f;
  91. }
  92.  
  93. public void DisplayMenu()
  94. {
  95. DisableUI();
  96. startUI.SetActive(true);
  97. }
  98.  
  99. void DisableUI()
  100. {
  101. levelEndUI.SetActive(false);
  102. gameOverUI.SetActive(false);
  103. startUI.SetActive(false);
  104. }
  105. public void RestartCurrentLevel()
  106. {
  107. DisableUI();
  108. SelectLevelAndStart(currentLevelNum);
  109. }
  110.  
  111. public void NextLevel()
  112.  
  113. {
  114. DisableUI();
  115. currentLevelNum++;
  116. SelectLevelAndStart(currentLevelNum);
  117. }
  118. // toppings and patience
  119. public void GameOver()
  120. {
  121. playing = false;
  122. DisableUI();
  123. gameOverUI.SetActive(true);
  124. buttons.SetActive(true);
  125. // make sure you show the player they died
  126. }
  127.  
  128. void ClearWorkspace()
  129. {
  130. string[] tags =
  131. {
  132. "Dough",
  133. "Topping",
  134. "Pizza",
  135. "RollingPin",
  136. "Receiver",
  137. "PizzaBox",
  138. "Customer",
  139. "Meat",
  140. };
  141. foreach (string tag in tags)
  142. {
  143. GameObject[] gameObjects = GameObject.FindGameObjectsWithTag(tag);
  144. foreach (GameObject gameObj in gameObjects)
  145. {
  146. Destroy(gameObj);
  147. }
  148.  
  149. }
  150. }
  151. public void StartLevel()
  152. {
  153.  
  154. UniversalData.playerManager.ResetStats();
  155. // reset lines
  156. ClearWorkspace();
  157. UniversalData.lineManager.ClearLines();
  158. _currentCustomers = null;
  159.  
  160. spawnTimer = 0f;
  161. //waveTimer = 1.5f;
  162. currentSpawned = 0;
  163. currentWaveNum = 0;
  164. currentCustomer = 0;
  165. spawnedInWave = 0;
  166. currentLevel = levelData[currentLevelNum];
  167. currentLevel.waves[0].GenerateCustomers();
  168. waveTimer = 0f;
  169. playing = true;
  170. }
  171. public void SelectLevelAndStart(int level)
  172. {
  173. cd = 2f;
  174. levelHeader.text = "Level: " + (level + 1);
  175. levelHeader.color = new Color(levelHeader.color.r, levelHeader.color.g, levelHeader.color.b, 1);
  176. currentLevelNum = level;
  177. StartLevel();
  178. }
  179. int CalculateStars(float revenue, float materialUsage)
  180. {
  181. int stars = 0;
  182. float materialCost = materialUsage;
  183. float gross = revenue - materialCost;
  184. if (gross > currentLevel.scoreThreeStar)
  185. {
  186. stars = 3;
  187. }
  188. else if(gross > currentLevel.scoreTwoStar)
  189. {
  190. stars = 2;
  191. }
  192. else if(gross > currentLevel.scoreOneStar)
  193. {
  194. stars = 1;
  195. }
  196.  
  197. //levelData
  198. return stars;
  199. }
  200.  
  201. void EndOfLevel()
  202.  
  203. {
  204.  
  205. DisableUI();
  206. int stars = CalculateStars(UniversalData.playerManager.money, materialUsage);
  207. levelEndUI.SetActive(true);
  208.  
  209. float earnings = UniversalData.playerManager.money;
  210. float materialsCost = -materialUsage;
  211. float bonus = 0f;
  212. float score = earnings + materialsCost + bonus;
  213. float bestScore = 0f;
  214. if (FileBasedPrefs.HasKey("BestScore" + currentLevelNum))
  215. {
  216. bestScore = FileBasedPrefs.GetFloat("BestScore");
  217. if (score > bestScore)
  218. {
  219. FileBasedPrefs.SetFloat("BestScore" + currentLevelNum, score);
  220. bestScore = score;
  221. }
  222. }
  223. else if(score > bestScore)
  224. {
  225. bestScore = score;
  226. }
  227. if (UniversalData.playerManager.reputation == UniversalData.playerManager.maxRep)
  228. {
  229. bonus += earnings * 0.1f;
  230. bonus = Mathf.Round(bonus);
  231. }
  232. Transform panel = levelEndUI.transform.Find("Panel");
  233. panel.Find("Earnings").Find("Value").GetComponent<TextMeshProUGUI>().text = "+$" + earnings;
  234. panel.Find("Materials").Find("Value").GetComponent<TextMeshProUGUI>().text = "-$" + materialsCost;
  235. panel.Find("Bonus").Find("Value").GetComponent<TextMeshProUGUI>().text = "+$" + bonus;
  236. panel.Find("Final").GetComponent<TextMeshProUGUI>().text = "Final: " + score;
  237. panel.Find("Best").GetComponent<TextMeshProUGUI>().text = "Best: " + bestScore;
  238. panel.Find("Stars").Find("Background").GetComponent<Image>().fillAmount = (float)stars / 3f;
  239. buttons.SetActive(true);
  240. // display stars, next if got what is needed, next, and also ssaving best score, if unlocked saving that
  241. //endOfRoundUI.transform.Find("Panel").Find("InfoText").GetComponent<TextMeshPro>().text = "info";
  242. }
  243. public float cd = 0.5f;
  244. private void Update()
  245. {
  246. cd -= Time.deltaTime;
  247. if(cd <= 0f)
  248. {
  249. levelHeader.color = Color.Lerp(levelHeader.color, new Color(levelHeader.color.r, levelHeader.color.g, levelHeader.color.b, 0f), Time.deltaTime * 4f);
  250. }
  251.  
  252. if (Input.GetKeyDown(KeyCode.R))
  253. {
  254. SpawnRobber();
  255. }
  256. if(Input.GetKeyDown(KeyCode.S))
  257. {
  258. StartLevel();
  259. }
  260. if(playing)
  261. {
  262.  
  263. if(currentSpawned < currentLevel.GetCustomerCount())
  264. {
  265. if (spawnTimer > 0f)
  266. {
  267. spawnTimer -= Time.deltaTime;
  268. }
  269.  
  270. if (waveTimer > 0f && spawnedInWave == 0)
  271. {
  272. waveTimer -= Time.deltaTime;
  273. }
  274. // if no wave timer cooldown
  275. if (waveTimer <= 0f)
  276. {
  277. if (spawnTimer <= 0f)
  278. {
  279.  
  280. //print(currentDay.spawnCooldown);
  281. spawnTimer = currentLevel.waves[currentWaveNum].customerSpawnInterval;
  282. SpawnCustomer(currentLevel.waves[currentWaveNum].customers[currentCustomer]);
  283. // basaew
  284.  
  285. currentSpawned++;
  286. spawnedInWave++;
  287. }
  288. // go to next wave, if spawned all the customers, and cooldown has finished
  289. if (spawnedInWave == currentLevel.waves[currentWaveNum].customers.Count && waveTimer <= 0f)
  290. {
  291. spawnedInWave = 0;
  292. spawnTimer = 0f;
  293. waveTimer = currentLevel.waves[currentWaveNum].timeToNextWave;
  294. currentWaveNum++;
  295.  
  296.  
  297.  
  298. }
  299. }
  300. }
  301. // if spawned all the customers..
  302. else if(currentSpawned == currentLevel.GetCustomerCount() && currentCustomers.Count == 0)
  303. {
  304. EndOfLevel();
  305. playing = false;
  306. }
  307.  
  308.  
  309. }
  310. }
  311. void SpawnCustomer(CustomerData customerData)
  312. {
  313. var customer = Instantiate(customerPrefab);
  314. customer.transform.position = customerStartPosition.position;
  315. customer.GetComponent<Customer>().InitCustomerData(customerData);
  316. currentCustomers.Add(customer);
  317. }
  318.  
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement