Guest User

GameManagment

a guest
Jan 15th, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.11 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6.  
  7. public class GameManagment : MonoBehaviour
  8. {
  9. //UI
  10. public Canvas ability;
  11. public TextMeshProUGUI timer;
  12.  
  13. //main things
  14. Transform player;
  15. InventoryManager inventory;
  16. EnemyMovment em;
  17. EnemySpawner es;
  18. Rigidbody2D playerRB;
  19. PlayerShooting ps;
  20. WeaponController wc;
  21. UIManager uiManager;
  22.  
  23. //controllers
  24. [SerializeField] GameObject axeControllerObject; //1
  25. AxeController axeController;
  26. [SerializeField] GameObject bookControllerObject; //2
  27. BookController bookController;
  28. [SerializeField] GameObject fireballControllerObject; //3
  29. FireballController fireballController;
  30. [SerializeField] GameObject scytheControllerObject; //4
  31. ScytheController scytheController;
  32.  
  33. //data
  34. bool stopTimer = false;
  35. float currentLvl;
  36. float playerLvl;
  37. float minutes, seconds;
  38. float time;
  39. int slotIndex;
  40. int rand1, rand2, rand3;
  41. GameObject[] weaponsControllers;
  42.  
  43. void Start()
  44. {
  45. player = GameObject.FindGameObjectWithTag("Player").transform;
  46. es = FindObjectOfType<EnemySpawner>();
  47. playerRB = GameObject.FindGameObjectWithTag("Player").GetComponent<Rigidbody2D>();
  48. ps = FindObjectOfType<PlayerShooting>();
  49. playerLvl = FindObjectOfType<PlayerStats>().lvl;
  50. inventory = FindObjectOfType<InventoryManager>();
  51. wc = FindObjectOfType<WeaponController>();
  52. uiManager = FindObjectOfType<UIManager>();
  53.  
  54. ability.enabled = false;
  55. currentLvl = playerLvl;
  56. minutes = 0f;
  57. seconds = 0f;
  58. time = 0f;
  59. slotIndex = 1;
  60.  
  61. }
  62. void Update()
  63. {
  64. playerLvl = FindObjectOfType<PlayerStats>().lvl;
  65.  
  66. GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
  67.  
  68. if (currentLvl != playerLvl) //freeze all
  69. {
  70. currentLvl = playerLvl;
  71.  
  72. stopTimer = true;
  73. playerRB.constraints = RigidbodyConstraints2D.FreezeAll;
  74. es.enabled = false;
  75. ps.enabled = false;
  76. wc.enabled = false;
  77. //weapons RigidbodyConstrains2D.FreezeAll
  78.  
  79. foreach (GameObject enemy in enemies)
  80. {
  81. em = enemy.GetComponent<EnemyMovment>();
  82. em.enabled = false;
  83. }
  84.  
  85. if (FindObjectOfType<AxeController>() != null)
  86. {
  87. axeController = FindObjectOfType<AxeController>();
  88. axeController = axeController.GetComponent<AxeController>();
  89. Debug.Log(axeController);
  90. }
  91. else axeController = null;
  92.  
  93. //przejśc przez listę Weapon slots i off
  94. weaponsControllers = GameObject.FindGameObjectsWithTag("WeaponController");
  95. foreach (GameObject weaponControllerObject in weaponsControllers)
  96. {
  97. weaponControllerObject.SetActive(false);
  98. }
  99.  
  100. //losowość - wybierz z puli 1 do 4 włącznie, po wybraniu nie bierz ponownie tej liczby od uwagę, o ile nie została dodana do invenory - wtedy ulepsze broń
  101. rand1 = Random.Range(1, 5);
  102. rand2 = Random.Range(1, 5);
  103. rand3 = Random.Range(1, 5);
  104. uiManager.SetBbutton1(rand1);
  105. uiManager.SetBbutton2(rand2);
  106. uiManager.SetBbutton3(rand3);
  107.  
  108. ability.enabled = true;
  109. }
  110.  
  111. if (!stopTimer)
  112. {
  113. time += 1 * Time.deltaTime;
  114. minutes = Mathf.FloorToInt(time / 60);
  115. seconds = Mathf.FloorToInt(time % 60);
  116. timer.text = string.Format("{0:00}:{1:00}", minutes, seconds);
  117. }
  118. }
  119.  
  120. /*
  121. BŁEDY
  122. -dodać sprawdzenie czy lista jest pełna jak tak to już nie losuj nowcyh broni.
  123. -jak juz broć istnieje to nie dodawać jej ponownie do inventory tylko zwiększyć jej poziom
  124. -poprawić generowanie bookweapon
  125. -przystosować bronie do zwiększania ich poziomów - prznieść pola danych do controllera i weapon dziedziczy po nim
  126. */
  127. public void Click1() //unfreeze all
  128. {
  129. stopTimer = false;
  130. playerRB.constraints = RigidbodyConstraints2D.FreezeRotation;
  131. es.enabled = true;
  132. ps.enabled = true;
  133. wc.enabled = true;
  134. //weapons RigidbodyConstraints2D.FreezeRotation
  135.  
  136. GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
  137.  
  138. foreach (GameObject enemy in enemies)
  139. {
  140. em = enemy.GetComponent<EnemyMovment>();
  141. em.enabled = true;
  142. }
  143.  
  144. //zamiast if do każdego, przejść przez listę inventoryManager i off. dostać się do gameObject poprzez Weaponslots gameObjectu Inventory Manager.
  145. foreach (GameObject weaponControllerObject in weaponsControllers)
  146. {
  147. weaponControllerObject.SetActive(true);
  148. }
  149. RollWeapon(rand1);
  150.  
  151. ability.enabled = false;
  152. }
  153. public void Click2() //unfreeze all
  154. {
  155. stopTimer = false;
  156. playerRB.constraints = RigidbodyConstraints2D.FreezeRotation;
  157. es.enabled = true;
  158. ps.enabled = true;
  159. wc.enabled = true;
  160. //weapons RigidbodyConstraints2D.FreezeRotation
  161.  
  162. GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
  163.  
  164. foreach (GameObject enemy in enemies)
  165. {
  166. em = enemy.GetComponent<EnemyMovment>();
  167. em.enabled = true;
  168. }
  169.  
  170. //zamiast if do każdego, przejść przez listę inventoryManager i off. dostać się do gameObject poprzez Weaponslots gameObjectu Inventory Manager.
  171. foreach (GameObject weaponControllerObject in weaponsControllers)
  172. {
  173. weaponControllerObject.SetActive(true);
  174. }
  175. RollWeapon(rand2);
  176.  
  177. ability.enabled = false;
  178. }
  179. public void Click3() //unfreeze all
  180. {
  181. stopTimer = false;
  182. playerRB.constraints = RigidbodyConstraints2D.FreezeRotation;
  183. es.enabled = true;
  184. ps.enabled = true;
  185. wc.enabled = true;
  186. //weapons RigidbodyConstraints2D.FreezeRotation
  187.  
  188. GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
  189.  
  190. foreach (GameObject enemy in enemies)
  191. {
  192. em = enemy.GetComponent<EnemyMovment>();
  193. em.enabled = true;
  194. }
  195.  
  196. //zamiast if do każdego, przejść przez listę inventoryManager i off. dostać się do gameObject poprzez Weaponslots gameObjectu Inventory Manager.
  197. foreach (GameObject weaponControllerObject in weaponsControllers)
  198. {
  199. weaponControllerObject.SetActive(true);
  200. }
  201. RollWeapon(rand3);
  202.  
  203. ability.enabled = false;
  204. }
  205. public bool CheckController(WeaponController weapon)
  206. {
  207. Debug.Log(axeController);
  208. if(weapon == null) return false;
  209. for (int i = 0; i < inventory.weaponSlots.Count; i++)
  210. {
  211. if (inventory.weaponSlots[i] == weapon) return true;
  212. }
  213. return false;
  214. }
  215. public void RollWeapon(int rand)
  216. {
  217. switch (rand)
  218. {
  219. case 1:
  220. //if (!CheckController(axeController.GetComponent<AxeWeapon>()))
  221. if (!CheckController(axeController))
  222. {
  223. GameObject axeController = (GameObject)Instantiate(axeControllerObject, player.position, player.rotation);
  224. axeController.transform.SetParent(player);
  225. AxeController _axeController = axeController.GetComponent<AxeController>();
  226.  
  227. inventory.AddWeapon(slotIndex, _axeController);
  228. slotIndex++;
  229. break;
  230. }
  231. else
  232. {
  233. //zwiększ poziom broni
  234. break;
  235. }
  236. case 2:
  237. if (!CheckController(bookController = FindObjectOfType<BookController>()))
  238. {
  239. GameObject bookController = (GameObject)Instantiate(bookControllerObject, player.position, player.rotation);
  240. bookController.transform.SetParent(player);
  241. BookController _bookController = bookController.GetComponent<BookController>();
  242.  
  243. inventory.AddWeapon(slotIndex, _bookController);
  244. slotIndex++;
  245. break;
  246. }
  247. else
  248. {
  249. //zwiększ poziom broni
  250. break;
  251. }
  252. case 3:
  253. if (!CheckController(fireballController = FindObjectOfType<FireballController>()))
  254. {
  255. GameObject fireballController = (GameObject)Instantiate(fireballControllerObject, player.position, player.rotation);
  256. fireballController.transform.SetParent(player);
  257. FireballController _fireballController = fireballController.GetComponent<FireballController>();
  258.  
  259. inventory.AddWeapon(slotIndex, _fireballController);
  260. slotIndex++;
  261. break;
  262. }
  263. else
  264. {
  265. //zwiększ poziom broni
  266. break;
  267. }
  268. case 4:
  269. if (!CheckController(scytheController = FindObjectOfType<ScytheController>()))
  270. {
  271. GameObject scytheController = (GameObject)Instantiate(scytheControllerObject, player.position, player.rotation);
  272. scytheController.transform.SetParent(player);
  273. ScytheController _scytheController = scytheController.GetComponent<ScytheController>();
  274.  
  275. inventory.AddWeapon(slotIndex, _scytheController);
  276. slotIndex++;
  277. break;
  278. }
  279. else
  280. {
  281. //zwiększ poziom broni
  282. break;
  283. }
  284. }
  285. }
  286. }
Add Comment
Please, Sign In to add comment