Advertisement
Guest User

Main Code

a guest
Jul 28th, 2021
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.SceneManagement;
  7.  
  8.  
  9. public class GameLogic : MonoBehaviour
  10. {
  11. [SerializeField] public static int health = 10;
  12. string healthtextstring;
  13. [SerializeField] Text healthcomponent;
  14.  
  15. public static int bullets = 0;
  16. string bullettextstring;
  17. [SerializeField] Text bulletcomponent;
  18.  
  19. [SerializeField] Text textComponent;
  20. [SerializeField] State startingState;
  21. [SerializeField] State gameoverState;
  22. public State currentstate;
  23.  
  24. AudioSource audiosource;
  25.  
  26.  
  27. // Start is called before the first frame update
  28. void Start()
  29. {
  30. currentstate = startingState;
  31. textComponent.text = currentstate.GetStateStory();
  32. audiosource = GetComponent<AudioSource>();
  33. }
  34.  
  35. // Update is called once per frame
  36. void Update()
  37. {
  38. Debug.Log(currentstate.optionsounds);
  39. ManageHealth();
  40.  
  41. ManageState();
  42.  
  43. ManageBullets();
  44.  
  45. if(Input.GetKeyDown(KeyCode.Q))
  46. {
  47. SceneManager.LoadScene(0);
  48. }
  49. }
  50.  
  51. private void ManageHealth()
  52. {
  53. //Always sets the health string to the health integer(in case it is changed elseware), and the health component
  54. //sets the UI element to the string
  55. healthtextstring = health.ToString();
  56. healthcomponent.text = healthtextstring;
  57.  
  58. if (health <= 0)
  59. {
  60. health = 0;
  61. currentstate = gameoverState;
  62. }
  63. }
  64.  
  65. private void ManageBullets()
  66. {
  67. bullettextstring = bullets.ToString();
  68. bulletcomponent.text = bullettextstring;
  69. if (bullets < 0)
  70. {
  71. bullets = 0;
  72. }
  73. }
  74.  
  75. private void ManageState()
  76. {
  77. var nextStates = currentstate.GetNextStates();
  78.  
  79.  
  80. for(int index = 0; index < nextStates.Length; index++)
  81. {
  82. if (Input.GetKeyDown(KeyCode.Alpha1 + index))
  83. {
  84. if (currentstate.optionsammocost.Length>0)
  85. {
  86. if (bullets >= currentstate.optionsammocost[index])
  87. {
  88. indextvariablechanges(index);
  89. currentstate = nextStates[index];
  90. }
  91. }
  92. else
  93. {
  94. indextvariablechanges(index);
  95. currentstate = nextStates[index];
  96. }
  97.  
  98. }
  99. }
  100.  
  101.  
  102. textComponent.text = currentstate.GetStateStory();
  103. }
  104.  
  105. private void indextvariablechanges(int index)
  106. {
  107. if (index == 0)
  108. {
  109. RemovehealthOnOption(0);
  110. RemovebulletsOnOption(0);
  111. PlaysoundOnOption(0);
  112.  
  113. }
  114. if (index == 1)
  115. {
  116. RemovehealthOnOption(1);
  117. RemovebulletsOnOption(1);
  118. PlaysoundOnOption(1);
  119.  
  120.  
  121. }
  122. if (index == 2)
  123. {
  124. RemovehealthOnOption(2);
  125. RemovebulletsOnOption(2);
  126. PlaysoundOnOption(2);
  127.  
  128.  
  129.  
  130. }
  131. }
  132.  
  133. private void RemovehealthOnOption(int option)
  134. {
  135. if (currentstate.optionsdamage.Length > option)
  136. {
  137. health = health - currentstate.optionsdamage[option];
  138. }
  139. }
  140.  
  141. private void RemovebulletsOnOption(int option)
  142. {
  143. if (currentstate.optionsammocost.Length > option)
  144. {
  145. bullets = bullets - currentstate.optionsammocost[option];
  146. }
  147. }
  148.  
  149. //this is the thing you want
  150. private void PlaysoundOnOption(int option)
  151. {
  152. if(currentstate.optionsounds.Length > option)
  153. {
  154. if (currentstate.optionsounds[option])
  155. {
  156. audiosource.PlayOneShot(currentstate.optionsounds[option]);
  157. }
  158. }
  159.  
  160. }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement