Advertisement
SizilStank

Untitled

Feb 6th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.22 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class Player : MonoBehaviour
  7. {
  8.    
  9.     [SerializeField] private GameObject _laserPrefab;
  10.     [SerializeField] private GameObject _tripleLaserPrefab;
  11.     [SerializeField] private GameObject _shieldAnimationPrefab;
  12.     [SerializeField] private GameObject _ringsAnimationPrefab;
  13.     [SerializeField] private GameObject _enemyPrefab;
  14.     [SerializeField] private GameObject _healthPowerUpPrefab;
  15.     [SerializeField] private GameObject _ringsActiveIcon1, _ringsActiveIcon2, _ringsActiveIcon3;
  16.     [SerializeField] private GameObject[] _setRandomPlayerWingDamage;
  17.     [SerializeField] private GameObject _speedThrusters;
  18.  
  19.     [SerializeField] private AudioClip _laserShoot;
  20.     [SerializeField] private AudioClip _explosion;
  21.     [SerializeField] private AudioClip _powerUpClip;
  22.     [SerializeField] private AudioClip _shieldTakeDamage;
  23.     [SerializeField] private AudioClip _playerHit;
  24.     [SerializeField] private AudioClip _ringsSoundClip;
  25.     [SerializeField] private AudioClip _OutOfLasers;
  26.     [SerializeField] private AudioClip _superThruseterClip;
  27.  
  28.     [SerializeField] private float _speed = 5f;
  29.     [SerializeField] private float _speedPowerUpSpeed = 10f;
  30.     [SerializeField] private float _volumeMax = 1f;
  31.     [SerializeField] private float _volumeMin = 0.25f;
  32.     [SerializeField] private float _volumeNULL = 0f;
  33.     [SerializeField] private float _fireRate = 0.15f;
  34.     [SerializeField] private float _canFire = -1f;
  35.     [SerializeField] private float _activeTripleShotTime = 5f;
  36.     [SerializeField] private float _activeSpeedPowerUpTime = 5f;
  37.     [SerializeField] private float _activeShieldPowerUpTime = 5;
  38.  
  39.     [SerializeField] private float _timeLeft = 4f;
  40.     [SerializeField] private float _coolDownTimer;
  41.  
  42.     [SerializeField] private int _lives = 3;
  43.     [SerializeField] private int _shieldLives = 3;
  44.     [SerializeField] private int _score;
  45.     [SerializeField] private int _laserAmmo = 15;
  46.     [SerializeField] private int _ringsIconCounter;
  47.     [SerializeField] private int _currentIndex = 0;
  48.  
  49.     [SerializeField] private bool _hasAmmo = true;
  50.     [SerializeField] private bool _isTripleShotActive;
  51.     [SerializeField] private bool _isSpeedPowerUpActive;
  52.     [SerializeField] private bool _isShieldActive;
  53.     [SerializeField] private bool _isRingActive;
  54.     [SerializeField] private bool _timerActive;
  55.  
  56.     private SpawnManager _spawnManager;//handle to component... then FIND it
  57.     private UIManager _uiManager;
  58.     private AudioSource _audioSource;
  59.     private ShieldAnim _shieldAnim;
  60.     private Canvas _canvas;
  61.     [SerializeField] private Slider _sliderLaserAmmoCount;
  62.     [SerializeField] private Slider _thusterSlider;
  63.     private Camera _camera;
  64.  
  65.     private void Start()
  66.     {    
  67.         _sliderLaserAmmoCount.value = _laserAmmo;
  68.         _thusterSlider.value = _timeLeft;
  69.         PlayerComponets();
  70.        
  71.     }
  72.  
  73.  
  74.     // Update is called once per frame
  75.     void Update()
  76.     {        
  77.         PlayerMovement();
  78.         PlayerInput();
  79.  
  80.         if (_timeLeft < 0)
  81.         {
  82.             _timeLeft = 0;
  83.         }
  84.         else if (_timeLeft == 0)
  85.         {
  86.             _speedThrusters.SetActive(false);
  87.             StartCoroutine(RestartTimeLeft());
  88.             Debug.Log("Counting " + _timeLeft);
  89.         }
  90.     }
  91.  
  92.     private void PlayerComponets() //Called at Start
  93.     {
  94.         _ringsAnimationPrefab.SetActive(false);
  95.         _ringsActiveIcon1.SetActive(false);
  96.         _ringsActiveIcon2.SetActive(false);
  97.         _ringsActiveIcon3.SetActive(false);
  98.  
  99.         _score = Mathf.Clamp(_score, 0, 9999999);
  100.         _laserAmmo = Mathf.Clamp(_laserAmmo, 0, 100);    
  101.  
  102.         if (_shieldAnim != null)
  103.         {
  104.             _shieldAnim = GameObject.Find("ShieldAnim").GetComponent<ShieldAnim>();
  105.         }
  106.  
  107.         _audioSource = GetComponent<AudioSource>();
  108.         if (_laserShoot == null)
  109.         {
  110.             Debug.LogError("The Audio Source on Player is NULL!");
  111.         }
  112.         else
  113.         {
  114.             _audioSource.clip = _laserShoot;
  115.         }
  116.  
  117.         _spawnManager = GameObject.Find("SpawnManager").GetComponent<SpawnManager>();
  118.         if (_spawnManager == null)
  119.         {
  120.             Debug.LogError("The Spawn Manager is NULL!");
  121.         }
  122.  
  123.         _uiManager = GameObject.Find("Canvas").GetComponent<UIManager>();
  124.         if (_uiManager == null)
  125.         {
  126.             Debug.LogError("UI Manager is NULL!");
  127.         }
  128.  
  129.         _camera = GameObject.Find("Main Camera").GetComponent<Camera>();
  130.         if (_camera == null)
  131.         {
  132.             Debug.LogError("Camera is NULL");
  133.         }
  134.  
  135.         _canvas = GameObject.Find("Canvas").GetComponent<Canvas>();
  136.         if (_canvas == null)
  137.         {
  138.             Debug.LogError("Canvas is NULL");
  139.         }
  140.     }
  141.  
  142.     private void PlayerMovement()
  143.     {
  144.  
  145.  
  146.         float horizontalInput = Input.GetAxis("Horizontal");
  147.         float verticalInput = Input.GetAxis("Vertical");
  148.  
  149.         Vector3 moveDirection = new Vector3(horizontalInput, verticalInput, 0);
  150.  
  151.         if (_isSpeedPowerUpActive == true)
  152.         {
  153.             transform.Translate(moveDirection * _speedPowerUpSpeed * Time.deltaTime);
  154.         }
  155.         else if (Input.GetKey(KeyCode.LeftShift)  && _timeLeft > 0)
  156.         {
  157.             _thusterSlider.value -= 1 * Time.deltaTime;
  158.             _timeLeft -= Time.deltaTime;
  159.             _speedThrusters.SetActive(true);                            
  160.             transform.Translate(moveDirection * _speed * 3 * Time.deltaTime);
  161.         }
  162.         else
  163.         {
  164.             transform.Translate(moveDirection * _speed * Time.deltaTime);
  165.         }
  166.  
  167.         if (Input.GetKeyUp(KeyCode.LeftShift) && _timeLeft > 0)
  168.         {
  169.             _speedThrusters.SetActive(false);
  170.         }
  171.  
  172.         transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, -3.8f, 1.7f), 0);
  173.  
  174.         if (transform.position.x >= 11.27f)
  175.         {
  176.             transform.position = new Vector3(-11.27f, transform.position.y, 0);
  177.         }
  178.         else if (transform.position.x <= -11.27f)
  179.         {
  180.             transform.position = new Vector3(11.27f, transform.position.y, 0);
  181.         }
  182.     }
  183.  
  184.     private IEnumerator RestartTimeLeft()
  185.     {
  186.         yield return new WaitForSeconds(10);
  187.         _timeLeft = 4;
  188.         _thusterSlider.value = 4;
  189.     }
  190.  
  191.     private void PlayerInput()
  192.     {
  193.         if (Input.GetMouseButtonDown(0) && Time.time > _canFire && _hasAmmo == true)
  194.         {
  195.             _sliderLaserAmmoCount.value -= 1;
  196.             _audioSource.PlayOneShot(_laserShoot, _volumeMin);
  197.             FireLaser();
  198.         }
  199.  
  200.         if (Input.GetMouseButtonDown(1) && _isRingActive == true)
  201.         {
  202.             _ringsIconCounter--;
  203.  
  204.             if (_ringsIconCounter == 0)
  205.             {
  206.                 _isRingActive = false;
  207.             }
  208.  
  209.             _audioSource.PlayOneShot(_ringsSoundClip, _volumeMin);
  210.             _ringsAnimationPrefab.SetActive(true);
  211.             StartCoroutine(RingsActiveWaitForSeconds());  // _isRingsActive == flase;
  212.  
  213.             switch (_ringsIconCounter)
  214.             {
  215.                 case 0:
  216.                     _ringsActiveIcon1.SetActive(false);
  217.                     break;
  218.                 case 1:
  219.                     _ringsActiveIcon2.SetActive(false);
  220.                     break;
  221.                 case 2:
  222.                     _ringsActiveIcon3.SetActive(false);
  223.                     break;
  224.                 default:
  225.                     Debug.Log("Its okay");
  226.                     break;
  227.             }
  228.         }
  229.     }
  230.  
  231.     private void FireLaser()
  232.     {
  233.         _laserAmmo--;
  234.  
  235.         if (_laserAmmo > 1)
  236.         {
  237.             _hasAmmo = true;
  238.         }
  239.         else if (_laserAmmo == 0)
  240.         {
  241.             _audioSource.PlayOneShot(_OutOfLasers, _volumeMin);
  242.             _hasAmmo = false;
  243.         }
  244.  
  245.         if (_hasAmmo == false)
  246.         {
  247.             _audioSource.PlayOneShot(_laserShoot, _volumeNULL);
  248.             _laserAmmo = 0;
  249.             return;
  250.         }
  251.  
  252.         _canFire = Time.time + _fireRate;
  253.        
  254.         if (_isTripleShotActive == true)
  255.         {
  256.             Instantiate(_tripleLaserPrefab, transform.position + new Vector3(-0.078f, 0.126f, 0), Quaternion.identity);
  257.         }
  258.         else
  259.         {
  260.             Instantiate(_laserPrefab, transform.position + new Vector3(0, 1.0f, 0), Quaternion.identity);
  261.         }
  262.     }
  263.  
  264.     public void Damage()
  265.     {
  266.         _lives = Mathf.Clamp(_lives, 0, 3);
  267.         if (_isShieldActive == true)// we need this so the player does not take damage while in the shield
  268.         {
  269.             return;
  270.         }
  271.  
  272.         _lives --;
  273.  
  274.         _audioSource.PlayOneShot(_playerHit, _volumeMax);
  275.  
  276.         _uiManager.UpdateLives(_lives);
  277.  
  278.         if (_lives < 1)
  279.         {
  280.             _audioSource.PlayOneShot(_explosion, _volumeMax);
  281.  
  282.             _spawnManager.OnPlayerDeath();
  283.  
  284.             Destroy(this.gameObject);
  285.         }
  286.  
  287.         if (_lives == 2)
  288.         {
  289.             _setRandomPlayerWingDamage[Random.Range(0, _setRandomPlayerWingDamage.Length)].SetActive(true);
  290.         }
  291.         else if (_lives == 1)
  292.         {
  293.             _setRandomPlayerWingDamage[0].SetActive(true);
  294.             _setRandomPlayerWingDamage[1].SetActive(true);
  295.         }
  296.        
  297.     }
  298.  
  299.     IEnumerator RingsActiveWaitForSeconds()
  300.     {      
  301.         yield return new WaitForSeconds(0.3f);
  302.         _ringsAnimationPrefab.SetActive(false);
  303.     }
  304.  
  305.     private void OnTriggerEnter2D(Collider2D other)
  306.     {
  307.  
  308.         if (other.gameObject.tag == "Enemy" && _isShieldActive == true)
  309.         {
  310.  
  311.             _shieldLives--;
  312.  
  313.             if (_shieldLives == 2)
  314.             {
  315.                 _audioSource.PlayOneShot(_shieldTakeDamage, _volumeMax);
  316.                 _shieldAnimationPrefab.GetComponent<Renderer>().material.color = Color.green;
  317.             }
  318.             else if (_shieldLives == 1)
  319.             {
  320.                 _audioSource.PlayOneShot(_shieldTakeDamage, _volumeMax);
  321.                 _shieldAnimationPrefab.GetComponent<Renderer>().material.color = Color.red;
  322.             }
  323.  
  324.             if (_shieldLives < 1)
  325.             {
  326.                 _audioSource.PlayOneShot(_shieldTakeDamage, _volumeMax);
  327.                 _isShieldActive = false;
  328.                 _shieldLives = 3;
  329.                 _shieldAnimationPrefab.GetComponent<Renderer>().material.color = Color.white;
  330.                 gameObject.transform.GetChild(0).gameObject.SetActive(false);
  331.             }
  332.         }
  333.  
  334.         if (other.gameObject.tag == "PowerUp")
  335.         {
  336.             _audioSource.PlayOneShot(_powerUpClip, _volumeMin);
  337.         }
  338.  
  339.         if (other.gameObject.tag == "RingsPowerUpCollected")
  340.         {
  341.             _audioSource.PlayOneShot(_powerUpClip, _volumeMin);
  342.  
  343.             _ringsIconCounter++;
  344.  
  345.             _ringsIconCounter = Mathf.Clamp(_ringsIconCounter, 0, 3);
  346.  
  347.             switch (_ringsIconCounter)
  348.             {
  349.                 case 1:
  350.                     _ringsActiveIcon1.SetActive(true);
  351.                     break;
  352.                 case 2:
  353.                     _ringsActiveIcon2.SetActive(true);
  354.                     break;
  355.                 case 3:
  356.                     _ringsActiveIcon3.SetActive(true);
  357.                     break;
  358.                 default:
  359.                     Debug.Log("Its okay");
  360.                     break;
  361.             }
  362.         }
  363.  
  364.         if (other.gameObject.tag == "LaserAmmo")
  365.         {
  366.             _audioSource.PlayOneShot(_powerUpClip, _volumeMin);
  367.             _sliderLaserAmmoCount.value += 15;
  368.         }
  369.     }
  370.  
  371.     public void AddScore(int points)
  372.     {
  373.         _score += points;
  374.         _uiManager.UpdateScore(_score);
  375.     }
  376.  
  377.     public void TripleShotActive()
  378.     {
  379.         _isTripleShotActive = true;
  380.         StartCoroutine(TripleShotPowerDownRoutine());
  381.     }
  382.  
  383.     IEnumerator TripleShotPowerDownRoutine()
  384.     {
  385.         if (_isTripleShotActive == true)
  386.         {
  387.             yield return new WaitForSeconds(_activeTripleShotTime);
  388.             _isTripleShotActive = false;
  389.         }
  390.     }
  391.  
  392.     public void SpeedPowerUpActive()
  393.     {
  394.         _speedThrusters.SetActive(true);
  395.         _audioSource.PlayOneShot(_superThruseterClip, _volumeMin);
  396.         _isSpeedPowerUpActive = true;
  397.         StartCoroutine(SpeedPowerUpCoolDown());
  398.     }
  399.  
  400.     IEnumerator SpeedPowerUpCoolDown()
  401.     {
  402.         if (_isSpeedPowerUpActive == true)
  403.         {
  404.             yield return new WaitForSeconds(_activeSpeedPowerUpTime);
  405.             _speedThrusters.SetActive(false);
  406.             _isSpeedPowerUpActive = false;
  407.         }
  408.     }
  409.  
  410.     public void ShieldIsActiveRunAnimation()
  411.     {
  412.         _isShieldActive = true;
  413.         _shieldAnimationPrefab.gameObject.SetActive(true);
  414.     }
  415.  
  416.     public void AmmoPowerUpGiveAmmo()
  417.     {
  418.         _hasAmmo = true;
  419.         _laserAmmo += 15;
  420.     }
  421.  
  422.     public void ActiveRingPowerUp()
  423.     {
  424.         _isRingActive = true;
  425.     }
  426.  
  427.     public void HealthPowerUp()
  428.     {
  429.         _lives++;
  430.         _setRandomPlayerWingDamage[0].SetActive(false);
  431.         _setRandomPlayerWingDamage[1].SetActive(false);
  432.         _uiManager.UpdateLives(_lives);
  433.     }
  434. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement