Advertisement
SizilStank

Untitled

Oct 3rd, 2022
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.09 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.VisualScripting;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using TMPro;
  7. using UnityEngine.SocialPlatforms;
  8.  
  9. public class Player : MonoBehaviour
  10. {
  11.  
  12.     //_______float int_______//
  13.     [SerializeField] private float _defaultSpeed = 1f;
  14.     [SerializeField] private float _speedBoostPowerUpSpeed = 1f;
  15.     [SerializeField] private float _speedBoostTimer = 3.5f;
  16.     [SerializeField] private float _fireRate = 0.5f;
  17.     [SerializeField] private float _tripleShotTimer = 3.5f;
  18.     [SerializeField] private int _lives = 4;
  19.     [SerializeField] private int _score;
  20.     [SerializeField] private float _playerTimeInBumper;
  21.     [SerializeField] private float _playerTimeOutBumper;
  22.  
  23.  
  24.     //_______Audio Components_______//
  25.     [SerializeField] private AudioClip _fireDefaultLaser;
  26.     [SerializeField] private AudioClip _tripleShotShooting;
  27.     [SerializeField] private AudioClip _tripleShotOver;
  28.     [SerializeField] private AudioClip _powerUpCollectedAudioClip;
  29.     [SerializeField] private AudioClip _tripleShotLoad;
  30.     [SerializeField] private AudioClip _speedBoostActive;
  31.     [SerializeField] private AudioClip _speedBoostOver;
  32.     [SerializeField] private AudioClip _shieldOnPlayerActive;
  33.     [SerializeField] private AudioClip _shieldIsOver;
  34.  
  35.  
  36.     //_______Ainmations_______//
  37.     [SerializeField] private Animator _anim;
  38.  
  39.  
  40.     //_______Game Prefabs_______//
  41.     [SerializeField] private GameObject _laserPrefab;
  42.     [SerializeField] private GameObject _tripleShotPrefab;
  43.     [SerializeField] private GameObject _shieldPrefab;
  44.     [SerializeField] private GameObject _playerHit1Prefab;
  45.     [SerializeField] private GameObject _playerHit2Prefab;
  46.     [SerializeField] private GameObject _playerHit3Prefab;
  47.     [SerializeField] private AudioManager _audioManager;
  48.  
  49.  
  50.     //_______Booleans_______//
  51.     private bool _canFire = true;
  52.     private bool _isTripleShotActive;
  53.     private bool _isSpeedBoostActive;
  54.    [SerializeField] private bool _isShieldActive;
  55.     [SerializeField] private bool _startTimer;
  56.     private bool _isPaused;
  57.  
  58.  
  59.     //_______GetComponents_______//
  60.     [SerializeField] private AudioSource _audioSource;
  61.     [SerializeField] private SpawnManager _spawnManager;
  62.     [SerializeField] private UIManager _uiManager;
  63.  
  64.  
  65.     // Start is called before the first frame update
  66.     void Start()
  67.     {
  68.        
  69.         transform.position = new Vector3(0, 0, 0);
  70.        
  71.        
  72.         _shieldPrefab.SetActive(false);
  73.         _playerHit1Prefab.SetActive(false);
  74.         _playerHit2Prefab.SetActive(false);
  75.         _playerHit3Prefab.SetActive(false);
  76.  
  77.         AudioSource audio = GetComponent<AudioSource>();
  78.         Animator animation = GetComponent<Animator>();
  79.  
  80.         if (_laserPrefab == null || _spawnManager == null)
  81.         {
  82.             Debug.Log("Get Component NULL!");
  83.         }
  84.  
  85.         _spawnManager = GameObject.Find("SpawnManager").GetComponent<SpawnManager>();
  86.         _uiManager = GameObject.Find("Canvas").GetComponent<UIManager>();
  87.         _audioManager = GameObject.Find("AudioManager").GetComponent<AudioManager>();
  88.     }
  89.  
  90.     // Update is called once per frame
  91.     void Update()
  92.     {
  93.        
  94.         StartTimer();
  95.         StopTimer();
  96.         CalculateMovement();
  97.         LaserInstantiate();
  98.         MouseAmins();
  99.         MouseLockandPause();
  100.         // print(Input.GetAxisRaw("Mouse X"));
  101.     }
  102.  
  103.     private void StartTimer()
  104.     {
  105.         if (_startTimer == true)
  106.         {
  107.             _playerTimeInBumper = Time.time;
  108.         }
  109.     }
  110.  
  111.     private void StopTimer()
  112.     {
  113.         if (_startTimer == false)
  114.         {
  115.             _playerTimeOutBumper = Time.time;
  116.         }
  117.     }
  118.  
  119.     void CalculateMovement()
  120.  
  121.     {
  122.         float horizontalInput = Input.GetAxis("Mouse X");
  123.         float verticalInput = Input.GetAxis("Mouse Y");
  124.  
  125.        Cursor.lockState = CursorLockMode.Locked;
  126.  
  127.         Vector3 direction = new Vector3(horizontalInput, verticalInput, 0);
  128.         transform.Translate(direction * _defaultSpeed * Time.deltaTime);
  129.  
  130.         if (_isSpeedBoostActive == true)
  131.         {
  132.             transform.Translate(direction * _speedBoostPowerUpSpeed * Time.deltaTime);
  133.         }
  134.  
  135.  
  136.         if (transform.position.x >= 11.28f)
  137.         {
  138.             transform.position = new Vector3(-11.2f, transform.position.y, 0);
  139.         }
  140.         else if (transform.position.x <= -11.28f)
  141.         {
  142.             transform.position = new Vector3(11.19f, transform.position.y, 0);
  143.         }
  144.  
  145.         if (transform.position.y >= 0.9f)
  146.         {
  147.             transform.position = new Vector3(transform.position.x, 0.9f, 0);
  148.         }
  149.         else if (transform.position.y <= -5.0)
  150.         {
  151.             transform.position = new Vector3(transform.position.x, -5.0f, 0);
  152.         }
  153.     }
  154.  
  155.     void MouseLockandPause()
  156.     {
  157.         if (Input.GetKeyDown(KeyCode.Escape))
  158.         {
  159.             Cursor.lockState = CursorLockMode.None;
  160.         }
  161.  
  162.         if (Input.GetKeyDown(KeyCode.P) && _isPaused == false)
  163.         {
  164.             Time.timeScale = 0;
  165.             _isPaused = true;          
  166.         }
  167.         else if (Input.GetKeyDown(KeyCode.P) && _isPaused == true)
  168.         {
  169.             Time.timeScale = 1;
  170.             _isPaused = false;
  171.         }
  172.  
  173.         if (_isPaused == true)
  174.         {
  175.             Cursor.lockState = CursorLockMode.None;
  176.         }
  177.  
  178.         if (_isPaused == false)
  179.         {
  180.             Cursor.lockState = CursorLockMode.Locked;
  181.         }
  182.     }
  183.    
  184.     void MouseAmins()
  185.     {
  186.         if (Input.GetAxisRaw("Mouse X") == 0)
  187.         {
  188.             _anim.SetBool("PlayerCenter", true);
  189.             _anim.SetBool("PlayerRight", false);
  190.             _anim.SetBool("PlayerLeft", false);
  191.         }
  192.         else if (Input.GetAxisRaw("Mouse X") >= 0.1)
  193.         {
  194.             _anim.SetBool("PlayerRight", true);
  195.             _anim.SetBool("PlayerCenter", false);
  196.             _anim.SetBool("PlayerLeft", false);
  197.         }
  198.         else if (Input.GetAxisRaw("Mouse X") <= -0.1)
  199.         {
  200.             _anim.SetBool("PlayerLeft", true);
  201.             _anim.SetBool("PlayerRight", false);
  202.             _anim.SetBool("PlayerCenter", false);
  203.         }
  204.     }
  205.  
  206.     void LaserInstantiate()
  207.     {
  208.         if (Input.GetMouseButtonDown(0) && _canFire == true && _isPaused == false && _isTripleShotActive == false)
  209.         {
  210.             _audioSource.PlayOneShot(_fireDefaultLaser);
  211.             Vector3 laserOffset = _laserPrefab.transform.position = new Vector3(transform.position.x, transform.position.y + 1.16f, 0);
  212.             Instantiate(_laserPrefab, laserOffset, Quaternion.identity);
  213.             _canFire = false;
  214.             StartCoroutine(LaserCooldown());
  215.         }
  216.         else if (Input.GetMouseButtonDown(0) && _canFire == true && _isPaused == false && _isTripleShotActive == true)
  217.         {
  218.             _audioSource.PlayOneShot(_tripleShotShooting);
  219.             Vector3 laserOffset = _tripleShotPrefab.transform.position = new Vector3(transform.position.x, transform.position.y + 1.16f, 0);
  220.             Instantiate(_tripleShotPrefab, laserOffset, Quaternion.identity);
  221.             _canFire = false;
  222.             StartCoroutine(LaserCooldown());
  223.         }
  224.     }
  225.  
  226.     public void TripleShotActive()
  227.     {
  228.         _isTripleShotActive = true;
  229.         _audioSource.PlayOneShot(_powerUpCollectedAudioClip);
  230.         _audioSource.PlayOneShot(_tripleShotLoad);
  231.         StartCoroutine(TripleShotActiveWithTimer());
  232.     }
  233.  
  234.  
  235.     IEnumerator TripleShotActiveWithTimer()
  236.     {
  237.         if (_isTripleShotActive == true)
  238.         {
  239.             yield return new WaitForSeconds(_tripleShotTimer);
  240.             _audioSource.PlayOneShot(_tripleShotOver, 2);
  241.             _isTripleShotActive = false;
  242.  
  243.         }
  244.     }
  245.  
  246.     public void SpeedBoostActive()
  247.     {
  248.         _isSpeedBoostActive = true;
  249.         _audioSource.PlayOneShot(_speedBoostActive, 2);
  250.         _audioSource.PlayOneShot(_powerUpCollectedAudioClip);
  251.         _audioSource.PlayOneShot(_tripleShotLoad);
  252.         StartCoroutine(SpeedBoostActiveWithTimer());
  253.     }
  254.  
  255.     IEnumerator SpeedBoostActiveWithTimer()
  256.     {
  257.         if (_isSpeedBoostActive == true)
  258.         {
  259.             yield return new WaitForSeconds(_speedBoostTimer);
  260.             _audioSource.PlayOneShot(_speedBoostOver, 2);
  261.             _isSpeedBoostActive = false;
  262.         }
  263.     }
  264.  
  265.     public void ShieldIsActive()
  266.     {
  267.         _isShieldActive = true;
  268.         _shieldPrefab.SetActive(true);  
  269.         _audioSource.PlayOneShot(_powerUpCollectedAudioClip);
  270.         _audioSource.loop = true;
  271.         _audioSource.clip = _shieldOnPlayerActive;
  272.         _audioSource.Play();
  273.     }
  274.  
  275.  
  276.     IEnumerator LaserCooldown()
  277.     {
  278.         if (_canFire == false)
  279.         {
  280.             yield return new WaitForSeconds(_fireRate);
  281.             _canFire = true;
  282.         }
  283.     }
  284.  
  285.     public void AddPointToScore(int points)
  286.     {
  287.         _score += points;
  288.         _uiManager.UpdateScore(_score);
  289.     }
  290.  
  291.     private void OnTriggerEnter2D(Collider2D collision)
  292.     {
  293.        
  294.         if (collision.CompareTag("EnemyA") || collision.CompareTag("EnemyLaser"))
  295.         {          
  296.             Damage();
  297.             _audioManager.PlayerHitByEnemyLaser();
  298.             Destroy(collision.gameObject);
  299.  
  300.             /*if (_lives == 3)
  301.             {
  302.                 _playerHit1Prefab.SetActive(true);
  303.             }
  304.             else if (_lives == 2)
  305.             {
  306.                 _playerHit2Prefab.SetActive(true);
  307.             }
  308.             else if (_lives == 1)
  309.             {
  310.                 _playerHit3Prefab.SetActive(true);
  311.             }*/
  312.  
  313.             switch (_lives)
  314.             {
  315.                 case 1: _playerHit1Prefab.SetActive(true);
  316.                     break;
  317.                 case 2: _playerHit2Prefab.SetActive(true);
  318.                     break;
  319.                 case 3: _playerHit3Prefab.SetActive(true);
  320.                     break;
  321.             }
  322.         }
  323.         if (collision.CompareTag("Bumper"))
  324.         {
  325.             _startTimer = true;
  326.             if (_playerTimeInBumper > _playerTimeOutBumper)
  327.             {
  328.                 Debug.Log("In the Bumper");
  329.             }          
  330.         }
  331.     }
  332.     private void OnTriggerExit2D(Collider2D collision)
  333.     {
  334.         if (collision.CompareTag("Bumper"))
  335.         {
  336.             _startTimer = false;
  337.             Debug.Log("Out of the Bumper");
  338.         }  
  339.     }
  340.  
  341.     public void Damage()
  342.     {
  343.         if(_isShieldActive == true)
  344.         {
  345.             _shieldPrefab.SetActive(false);
  346.             _isShieldActive = false;
  347.             _audioSource.Stop();
  348.             _audioSource.loop = false;
  349.             _audioSource.clip = _shieldOnPlayerActive;
  350.             _audioSource.PlayOneShot(_shieldIsOver, 1);
  351.             return;
  352.         }
  353.            
  354.             _lives--;
  355.  
  356.         _uiManager.UpdateLives(_lives);
  357.  
  358.             if (_lives < 1)
  359.             {
  360.                 Cursor.lockState = CursorLockMode.None;
  361.                 _spawnManager.StopEnemySpawner();
  362.                 _spawnManager.StopPowerUpSpawner();
  363.                 Destroy(this.gameObject);
  364.             }      
  365.     }
  366.  
  367. }
  368.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement