Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7. [SerializeField]
  8. private float _speed = 3.5f;
  9. private float _speedMultiplier = 2f;
  10. [SerializeField]
  11. private GameObject _laserPrefab;
  12. [SerializeField]
  13. private float _fireRate = 0.5f;
  14. private float _canFire = -1f;
  15. [SerializeField]
  16. private int _lives = 3;
  17. private SpawnManager _spawnManager;
  18. [SerializeField]
  19. private bool _isTripleShotActive = false;
  20. [SerializeField]
  21. private GameObject _tripleShotPrefab;
  22. private bool _stopSpawning = false;
  23. private bool _isSpeedBoostActive = false;
  24. private bool _isShieldActive = false;
  25. [SerializeField]
  26. private GameObject _shieldVisualizer;
  27. [SerializeField]
  28. private int _score;
  29. private UIManager _uiManager;
  30. [SerializeField]
  31. private GameObject _Left_Hurt;
  32. [SerializeField]
  33. private GameObject _Right_Hurt;
  34. [SerializeField]
  35. private AudioClip _laserShot;
  36. private AudioSource _audioSource;
  37. [SerializeField]
  38. private AudioClip _explosionSound;
  39.  
  40.  
  41.  
  42. // Start is called before the first frame update
  43. void Start()
  44. {
  45. _audioSource = GetComponent<AudioSource>();
  46. transform.position = new Vector3(0, 0, 0);
  47. _spawnManager = GameObject.Find("Spawn_Manager").GetComponent<SpawnManager>();
  48. if (_spawnManager == null)
  49. {
  50. Debug.LogError("Spawn manager is NULL!");
  51. }
  52. _uiManager = GameObject.Find("Canvas").GetComponent<UIManager>();
  53. if (_uiManager == null)
  54. {
  55. Debug.LogError("UIMANAGER IS NULL!");
  56. }
  57. if (_audioSource == null)
  58. {
  59. Debug.LogError("Audio Source is null");
  60. }
  61. else
  62. {
  63. _audioSource.clip = _laserShot;
  64. }
  65. }
  66.  
  67. // Update is called once per frame
  68. void Update()
  69. {
  70. CalculateMovement();
  71. if (Input.GetKeyDown(KeyCode.Space) && Time.time > _canFire)
  72. {
  73. Shoot();
  74. }
  75. }
  76. void CalculateMovement()
  77. {
  78. //variables
  79. float horizontalInput = Input.GetAxis("Horizontal");
  80. float verticalInput = Input.GetAxis("Vertical");
  81. //float xpos = transform.position.x ;
  82.  
  83.  
  84.  
  85. Vector3 direction = new Vector3(horizontalInput, verticalInput, 0);
  86.  
  87. transform.Translate(direction * _speed * Time.deltaTime);
  88.  
  89.  
  90. transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, -3.8f, 0), 0);
  91.  
  92. if (transform.position.x > 11.16f)
  93. {
  94. transform.position = new Vector3(-11.16f, transform.position.y, 0);
  95. }
  96. else if (transform.position.x <= -11.16f)
  97. transform.position = new Vector3(11.16f, transform.position.y, 0);
  98. }
  99.  
  100. void Shoot()
  101. {
  102.  
  103. _canFire = Time.time + _fireRate;
  104. if (Input.GetKeyDown(KeyCode.Space) && _isTripleShotActive == true)
  105. {
  106. Instantiate(_tripleShotPrefab, transform.position, Quaternion.identity);
  107.  
  108. }
  109. else if (Input.GetKeyDown(KeyCode.Space) && _isTripleShotActive == false)
  110. {
  111. Instantiate(_laserPrefab, this.transform.position + new Vector3(0, 1.05f, 0), Quaternion.identity);
  112. }
  113. _audioSource.Play();
  114.  
  115.  
  116. }
  117. public void Damage()
  118. {
  119. if (_isShieldActive == true)
  120. {
  121. _shieldVisualizer.SetActive(false);
  122. _isShieldActive = false;
  123. return;
  124. }
  125. _lives--;
  126. if (_lives == 2)
  127. {
  128. _Left_Hurt.SetActive(true);
  129. }
  130. else if (_lives == 1)
  131. {
  132. _Right_Hurt.SetActive(true);
  133. }
  134. _uiManager.UpdateLives(_lives);
  135.  
  136. if (_lives <= 0)
  137. {
  138.  
  139. _spawnManager.OnPlayerDeath();
  140. _audioSource.clip = _explosionSound;
  141. Destroy(this.gameObject, 0.5f);
  142. _audioSource.Play();
  143. }
  144.  
  145. }
  146. public void TripleShotCollected()
  147. {
  148. _isTripleShotActive = true;
  149. StartCoroutine(TripleShotPowerDown());
  150.  
  151. IEnumerator TripleShotPowerDown()
  152. {
  153.  
  154. yield return new WaitForSeconds(5.0f);
  155. _isTripleShotActive = false;
  156. }
  157.  
  158.  
  159. }
  160. public void SpeedBoostCollected()
  161. {
  162. _isSpeedBoostActive = true;
  163. _speed *= _speedMultiplier;
  164. StartCoroutine(SpeedBoostCoolDown());
  165.  
  166. IEnumerator SpeedBoostCoolDown()
  167. {
  168. yield return new WaitForSeconds(5.0f);
  169. _isSpeedBoostActive = false;
  170. _speed /= _speedMultiplier;
  171. }
  172. }
  173. public void isShieldActive()
  174. {
  175. _shieldVisualizer.SetActive(true);
  176. _isShieldActive = true;
  177. }
  178.  
  179. public void score(int points)
  180. {
  181. _score += points;
  182. _uiManager.UpdateScore(_score);
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement