Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.82 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5.  
  6. public class Player : MonoBehaviour
  7. {
  8. [SerializeField]
  9. private float _speed = 3.5f;
  10. private float _speedMultiplier = 2f;
  11. [SerializeField]
  12. private GameObject _laserPrefab;
  13. [SerializeField]
  14. private float _fireRate = 0.5f;
  15. private float _canFire = -1f;
  16. [SerializeField]
  17. private int _lives = 3;
  18. private SpawnManager _spawnManager;
  19. [SerializeField]
  20. private bool _isTripleShotActive = false;
  21. [SerializeField]
  22. private GameObject _tripleShotPrefab;
  23. private bool _stopSpawning = false;
  24. private bool _isSpeedBoostActive = false;
  25. private bool _isShieldActive = false;
  26. [SerializeField]
  27. private GameObject _shieldVisualizer;
  28. [SerializeField]
  29. private int _score;
  30. private UIManager _uiManager;
  31. [SerializeField]
  32. private GameObject _Left_Hurt;
  33. [SerializeField]
  34. private GameObject _Right_Hurt;
  35. [SerializeField]
  36. private AudioClip _laserShot;
  37. private AudioSource _audioSource;
  38. [SerializeField]
  39. private AudioClip _explosionSound;
  40. private GameManager _gameManager;
  41. public bool IsPlayerOne = false;
  42. public bool IsPlayerTwo = false;
  43.  
  44. // Start is called before the first frame update
  45. void Start()
  46. {
  47. _audioSource = GetComponent<AudioSource>();
  48.  
  49.  
  50. _spawnManager = GameObject.Find("Spawn_Manager").GetComponent<SpawnManager>();
  51. if (_spawnManager == null)
  52. {
  53. Debug.LogError("Spawn manager is NULL!");
  54. }
  55. _uiManager = GameObject.Find("Canvas").GetComponent<UIManager>();
  56. if (_uiManager == null)
  57. {
  58. Debug.LogError("UIMANAGER IS NULL!");
  59. }
  60. _gameManager = GameObject.Find("Game_Manager").GetComponent<GameManager>();
  61. if (_gameManager == null)
  62. {
  63. Debug.LogError("Game Manager Is null!");
  64. }
  65. if (_gameManager._isCoOpMode == false)
  66. {
  67. transform.position = new Vector3(0, 0, 0);
  68. }
  69. if (_audioSource == null)
  70. {
  71. Debug.LogError("Audio Source is null");
  72. }
  73. else
  74. {
  75. _audioSource.clip = _laserShot;
  76. }
  77. }
  78.  
  79. // Update is called once per frame
  80. void Update()
  81. {
  82. if (IsPlayerOne == true)
  83. {
  84. CalculateMovement();
  85. if ((Input.GetKeyDown(KeyCode.Space) && Time.time > _canFire) && IsPlayerOne == true)
  86. {
  87. Shoot();
  88. }
  89. }
  90. else
  91. {
  92. CalculateMovementP2();
  93. if ((Input.GetKeyDown(KeyCode.RightShift) && Time.time > _canFire))
  94. {
  95. ShootP2();
  96.  
  97. }
  98. }
  99.  
  100. void CalculateMovement()
  101. {
  102. if (IsPlayerOne == true)
  103. {
  104. //variables
  105. float horizontalInput = Input.GetAxis("Player1 Horizontal");
  106. float verticalInput = Input.GetAxis("Player1 Vertical");
  107.  
  108. //float xpos = transform.position.x ;
  109. /*
  110. if (horizontalInput != 0)
  111. {
  112. transform.Translate(Vector3.right * horizontalInput * _speed * Time.deltaTime);
  113. }
  114. if (horizontalInput != 0)
  115. {
  116. transform.Translate(Vector3.up * verticalInput * _speed * Time.deltaTime);
  117. }
  118. */
  119.  
  120. Vector3 direction = new Vector3(horizontalInput, verticalInput, 0);
  121.  
  122. transform.Translate(direction * _speed * Time.deltaTime);
  123.  
  124. transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, -3.8f, 0), 0);
  125.  
  126. if (transform.position.x > 11.16f)
  127. {
  128. transform.position = new Vector3(-11.16f, transform.position.y, 0);
  129. }
  130. else if (transform.position.x <= -11.16f)
  131. transform.position = new Vector3(11.16f, transform.position.y, 0);
  132. }
  133. }
  134.  
  135. void CalculateMovementP2()
  136. {
  137. if (IsPlayerTwo == true)
  138. {
  139. float horizontalInput2 = Input.GetAxis("Player2 Horizontal");
  140. float verticalInput2 = Input.GetAxis("Player2 Vertical");
  141.  
  142. /* if (horizontalInput2 != 0)
  143. {
  144. transform.Translate(Vector3.right * horizontalInput2 * _speed * Time.deltaTime);
  145. }
  146. else
  147. {
  148. Debug.LogError("HORIZONTAL INPUT IS NULL!");
  149. }
  150. if (verticalInput2 != 0)
  151. {
  152. transform.Translate(Vector3.up * verticalInput2 * _speed * Time.deltaTime);
  153. }
  154. else
  155. {
  156. Debug.LogError("VERTICAL INOUT IS NULL!");
  157. }
  158.  
  159. if (Input.GetButtonDown("Player2 Horizontal"))
  160. {
  161. transform.Translate(Vector3.right * horizontalInput2 * _speed * Time.deltaTime);
  162. }
  163. if (Input.GetButtonDown("Player2 Vertical"))
  164. {
  165. transform.Translate(Vector3.up * verticalInput2 * _speed * Time.deltaTime);
  166. }
  167. */
  168. Vector3 direction = new Vector3(horizontalInput2, verticalInput2, 0);
  169.  
  170. transform.Translate(direction * _speed * Time.deltaTime);
  171.  
  172. transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, -3.8f, 0), 0);
  173.  
  174. //warping
  175. if (transform.position.x > 11.16f)
  176. {
  177. transform.position = new Vector3(-11.16f, transform.position.y, 0);
  178. }
  179. else if (transform.position.x <= -11.16f)
  180. transform.position = new Vector3(11.16f, transform.position.y, 0);
  181. }
  182.  
  183. }
  184.  
  185. void Shoot()
  186. {
  187.  
  188. _canFire = Time.time + _fireRate;
  189. if (Input.GetKeyDown(KeyCode.Space) && _isTripleShotActive == true)
  190. {
  191. Instantiate(_tripleShotPrefab, transform.position, Quaternion.identity);
  192. _audioSource.Play();
  193. }
  194. else if (Input.GetKeyDown(KeyCode.Space) && _isTripleShotActive == false)
  195. {
  196. Instantiate(_laserPrefab, this.transform.position + new Vector3(0, 1.05f, 0), Quaternion.identity);
  197. _audioSource.Play();
  198. }
  199. }
  200.  
  201. void ShootP2()
  202. {
  203. if (Input.GetKeyDown(KeyCode.RightShift) && _isTripleShotActive == true)
  204. {
  205. Instantiate(_tripleShotPrefab, transform.position, Quaternion.identity);
  206. _audioSource.Play();
  207. }
  208. else if (Input.GetKeyDown(KeyCode.RightShift) && _isTripleShotActive == false)
  209. {
  210. Instantiate(_laserPrefab, this.transform.position + new Vector3(0, 1.05f, 0), Quaternion.identity);
  211. _audioSource.Play();
  212. }
  213.  
  214. }
  215.  
  216.  
  217.  
  218. }
  219. public void Damage()
  220. {
  221. if (_isShieldActive == true)
  222. {
  223. _shieldVisualizer.SetActive(false);
  224. _isShieldActive = false;
  225. return;
  226. }
  227. if (_lives >= 1)
  228. {
  229. _lives--;
  230. }
  231. if (_lives == 2)
  232. {
  233. _Left_Hurt.SetActive(true);
  234. }
  235. else if (_lives == 1)
  236. {
  237. _Right_Hurt.SetActive(true);
  238. }
  239. _uiManager.UpdateLives(_lives);
  240.  
  241. if (_lives <= 0)
  242. {
  243.  
  244. _spawnManager.OnPlayerDeath();
  245. _audioSource.clip = _explosionSound;
  246. Destroy(this.gameObject, 0.5f);
  247. _audioSource.Play();
  248. }
  249.  
  250. }
  251. public void TripleShotCollected()
  252. {
  253. _isTripleShotActive = true;
  254. StartCoroutine(TripleShotPowerDown());
  255.  
  256. IEnumerator TripleShotPowerDown()
  257. {
  258.  
  259. yield return new WaitForSeconds(5.0f);
  260. _isTripleShotActive = false;
  261. }
  262.  
  263.  
  264. }
  265. public void SpeedBoostCollected()
  266. {
  267. _isSpeedBoostActive = true;
  268. _speed *= _speedMultiplier;
  269. StartCoroutine(SpeedBoostCoolDown());
  270.  
  271. IEnumerator SpeedBoostCoolDown()
  272. {
  273. yield return new WaitForSeconds(5.0f);
  274. _isSpeedBoostActive = false;
  275. _speed /= _speedMultiplier;
  276. }
  277. }
  278. public void isShieldActive()
  279. {
  280. _shieldVisualizer.SetActive(true);
  281. _isShieldActive = true;
  282. }
  283.  
  284. public void score(int points)
  285. {
  286. _score += points;
  287. _uiManager.UpdateScore(_score);
  288. }
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement