Advertisement
SizilStank

Untitled

Jan 25th, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.61 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7.  
  8.  
  9.     [SerializeField] private float _speed = 5f;
  10.     [SerializeField] private float _speedPowerUpSpeed = 10f;
  11.  
  12.  
  13.     [SerializeField] private GameObject _laserPrefab;
  14.     [SerializeField] private GameObject _tripleLaserPrefab;
  15.     [SerializeField] private GameObject _shieldAnimationPrefab;
  16.  
  17.     [SerializeField] private GameObject _playerDamage_L;
  18.     [SerializeField] private GameObject _playerDamage_R;
  19.  
  20.  
  21.     [SerializeField] private float _fireRate = 0.15f;
  22.     [SerializeField] private float _canFire = -1f;
  23.     [SerializeField] private int _lives = 3;
  24.     [SerializeField] private int _shieldLives = 3;
  25.     [SerializeField] private int _score;
  26.  
  27.  
  28.     [SerializeField] private bool _isTripleShotActive;
  29.     [SerializeField] private bool _isSpeedPowerUpActive;
  30.     [SerializeField] private bool _isShieldActive;
  31.  
  32.  
  33.     [SerializeField] private float _activeTripleShotTime = 5f;
  34.     [SerializeField] private float _activeSpeedPowerUpTime = 5f;
  35.     [SerializeField] private float _activeShieldPowerUpTime = 5;
  36.  
  37.     [SerializeField] private GameObject[] _setRandomActive;
  38.     [SerializeField] private int _currentIndex = 0;
  39.  
  40.     private SpawnManager _spawnManager;//handle to component... then FIND it
  41.     private UIManager _uiManager;
  42.  
  43.  
  44.     private void Start()
  45.     {
  46.         _playerDamage_L.SetActive(false);
  47.         _playerDamage_R.SetActive(false);
  48.  
  49.         _spawnManager = GameObject.Find("SpawnManager").GetComponent<SpawnManager>();
  50.         _uiManager = GameObject.Find("Canvas").GetComponent<UIManager>();
  51.  
  52.         if (_spawnManager == null)
  53.         {
  54.             Debug.LogError("The Spawn Manager is NULL");
  55.         }
  56.         if (_uiManager == null)
  57.         {
  58.             Debug.Log("UI Manager is NULL!");
  59.         }
  60.     }
  61.  
  62.     // Update is called once per frame
  63.     void Update()
  64.     {
  65.         PlayerMovement();
  66.        
  67.         if (Input.GetMouseButtonDown(0) && Time.time > _canFire)
  68.         {
  69.             FireLaser();
  70.         }
  71.     }
  72.  
  73.     void PlayerMovement()
  74.     {
  75.         float horizontalInput = Input.GetAxis("Horizontal");
  76.         float verticalInput = Input.GetAxis("Vertical");
  77.  
  78.         Vector3 moveDirection = new Vector3(horizontalInput, verticalInput, 0);
  79.  
  80.         if (_isSpeedPowerUpActive == true)
  81.         {        
  82.             transform.Translate(moveDirection * _speedPowerUpSpeed * Time.deltaTime);
  83.         }
  84.         else
  85.         {
  86.             transform.Translate(moveDirection * _speed * Time.deltaTime);
  87.         }
  88.  
  89.         transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, -3.8f, 1.7f), 0);
  90.  
  91.         if (transform.position.x >= 11.27f)
  92.         {
  93.             transform.position = new Vector3(-11.27f, transform.position.y, 0);
  94.         }
  95.         else if (transform.position.x <= -11.27f)
  96.         {
  97.             transform.position = new Vector3(11.27f, transform.position.y, 0);
  98.         }
  99.     }
  100.  
  101.  
  102.     void FireLaser()
  103.     {
  104.         _canFire = Time.time + _fireRate;
  105.  
  106.         if (_isTripleShotActive == true)
  107.         {
  108.             Instantiate(_tripleLaserPrefab, transform.position + new Vector3(-0.078f, 0.126f, 0), Quaternion.identity);
  109.         }
  110.         else
  111.         {
  112.             Instantiate(_laserPrefab, transform.position + new Vector3(0, 1.0f, 0), Quaternion.identity);
  113.         }
  114.     }
  115.  
  116.     public void Damage()
  117.     {
  118.         if (_isShieldActive == true)// we need this so the player does not take damage while in the shield
  119.         {
  120.             return;
  121.         }
  122.  
  123.         _lives --;
  124.  
  125.         _uiManager.UpdateLives(_lives);
  126.  
  127.         if (_lives < 1)
  128.         {
  129.             _spawnManager.OnPlayerDeath();
  130.             Destroy(this.gameObject);
  131.         }
  132.  
  133.  
  134.         if (_lives == 2)
  135.         {
  136.             _setRandomActive[Random.Range(0, _setRandomActive.Length)].SetActive(true);
  137.         }
  138.         else if (_lives == 1)
  139.         {
  140.             _setRandomActive[0].SetActive(true);
  141.             _setRandomActive[1].SetActive(true);
  142.         }    
  143.     }
  144.  
  145.     private void OnTriggerEnter2D(Collider2D other)
  146.     {
  147.  
  148.         if (other.gameObject.tag == "Enemy")
  149.         {
  150.  
  151.             _shieldLives--;
  152.  
  153.             if (_shieldLives < 1)
  154.             {
  155.                 _isShieldActive = false;
  156.                 _shieldLives = 3;
  157.                 gameObject.transform.GetChild(0).gameObject.SetActive(false);
  158.             }
  159.         }
  160.     }
  161.  
  162.     public void AddScore(int points)
  163.     {
  164.         _score += points;
  165.         _uiManager.UpdateScore(_score);
  166.     }
  167.  
  168.     public void TripleShotActive()
  169.     {
  170.         _isTripleShotActive = true;
  171.         StartCoroutine(TripleShotPowerDownRoutine());
  172.     }
  173.  
  174.     IEnumerator TripleShotPowerDownRoutine()
  175.     {
  176.         if (_isTripleShotActive == true)
  177.         {
  178.             yield return new WaitForSeconds(_activeTripleShotTime);
  179.             _isTripleShotActive = false;
  180.         }
  181.     }
  182.  
  183.     public void SpeedPowerUpActive()
  184.     {
  185.         _isSpeedPowerUpActive = true;
  186.         StartCoroutine(SpeedPowerUpCoolDown());
  187.     }
  188.  
  189.     IEnumerator SpeedPowerUpCoolDown()
  190.     {
  191.         if (_isSpeedPowerUpActive == true)
  192.         {
  193.             yield return new WaitForSeconds(_activeSpeedPowerUpTime);
  194.             _isSpeedPowerUpActive = false;
  195.         }
  196.     }
  197.  
  198.     public void ShieldIsActiveRunAnimation()
  199.     {
  200.         _isShieldActive = true;
  201.         _shieldAnimationPrefab.gameObject.SetActive(true);
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement