Advertisement
DugganSC

Untitled

Sep 17th, 2023
674
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.61 KB | None | 0 0
  1. // Player Script :
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class Player : MonoBehaviour
  7. {
  8.     [SerializeField]
  9.      private float _speed = 4f;
  10.      private float _speedMultiplier = 2;
  11.     [SerializeField]
  12.      private GameObject _laserPrefab;
  13.      [SerializeField]
  14.      private float _firerate = 0.5f;
  15.     [SerializeField]
  16.     private float _canfire = -1f;
  17.     [SerializeField]
  18.     private int _lives = 3;
  19.     [SerializeField]
  20.     private SpawnManager _spawnManager;
  21.     [SerializeField]
  22.     private bool _isTripleShotActive = false;
  23.     [SerializeField]
  24.     private bool _isSpeedBoostActive = false;
  25.     [SerializeField]
  26.     private GameObject _tripleShotPrefab;
  27.     [SerializeField]          
  28.     private bool _isShieldActive = false;
  29.     [SerializeField]
  30.     private GameObject _shieldVisualizer;
  31.     [SerializeField]
  32.     private GameObject _leftEngine , _rightEngine ;
  33.     [SerializeField]
  34.     private int _score;
  35.      [SerializeField]
  36.      private int _bestScore;
  37.     private UIManager _uiManager;
  38.     [SerializeField]
  39.     private AudioClip _laserSoundClip;
  40.    
  41.     private AudioSource _audioSource;  
  42.      void Start()
  43.     {
  44.        
  45.         transform.position = new Vector3(0,0,0);
  46.         _spawnManager = GameObject.Find("Spawn_Manager").GetComponent<SpawnManager>();
  47.         _uiManager = GameObject.Find("Canvas").GetComponent<UIManager>();
  48.        _audioSource = GetComponent<AudioSource>();
  49.         if(_spawnManager==null) {
  50.           Debug.LogError("The Spawn Manager is Null");
  51.         }  
  52.         if(_uiManager == null)
  53.         {
  54.          Debug.Log("The UI Manager is null");
  55.         }
  56.         if(_audioSource == null)
  57.         {
  58.           Debug.LogError("Audio is not there");
  59.         }  
  60.         else
  61.         {
  62.           _audioSource.clip = _laserSoundClip;
  63.         }
  64.     }        
  65.      
  66.     void Update()
  67.     {
  68.         CalculateMovement();
  69.  
  70.         if(Input.GetKeyDown(KeyCode.Space)&& Time.time > _canfire) {
  71.           FireLaser();
  72.         }
  73.    
  74.     }
  75.          
  76.  
  77.     void CalculateMovement () {
  78.         float horizontalInput = Input.GetAxis("Horizontal");
  79.          float verticalInput = Input.GetAxis("Vertical");
  80.      
  81.         Vector3 direction = new Vector3(horizontalInput,verticalInput,0);
  82.        
  83.       transform.Translate(direction*_speed*Time.deltaTime);
  84.        
  85.        transform.position = new Vector3(transform.position.x,Mathf.Clamp(transform.position.y,-3.8f,0),0);
  86.      
  87.       if(transform.position.x > 11.3f) {
  88.         transform.position = new Vector3(-11.3f,transform.position.y,0);
  89.       }
  90.       else if(transform.position.x < -11.3f) {
  91.         transform.position = new Vector3(11.3f,transform.position.y,0);
  92.       }
  93.     }
  94.  
  95.     void FireLaser() {
  96.           _canfire = Time.time + _firerate;
  97.  
  98.          if(_isTripleShotActive == true) {
  99.             Instantiate(_tripleShotPrefab, transform.position,Quaternion.identity);
  100.          }
  101.          else {
  102.           Instantiate(_laserPrefab,transform.position + new Vector3(0,1.05f,0),Quaternion.identity);
  103.          }
  104.          _audioSource.Play();
  105.            }
  106.                              
  107.    public void Damage() {  
  108.  
  109.     if(_isShieldActive == true) {
  110.       _isShieldActive = false;
  111.       _shieldVisualizer.SetActive(false);
  112.       return;
  113.     }
  114.  
  115.   _lives--;  
  116.      
  117.    if(_lives == 2) {
  118.     _leftEngine.SetActive(true);
  119.    }
  120.    else if(_lives == 1) {
  121.     _rightEngine.SetActive(true);
  122.    }
  123.  
  124.     _uiManager.UpdateLives(_lives);  
  125.       if(_lives <= 0) {
  126.      _spawnManager.OnPlayerDeath();
  127.      _uiManager.CheckForBestScore();
  128.       Destroy(this.gameObject);
  129.      
  130.      }
  131.        
  132.     }      
  133.  
  134.     public void TripleShotActive()
  135.     {
  136.       _isTripleShotActive = true;
  137.       StartCoroutine(TripleShotPowerDownRoutine());
  138.     }
  139.  
  140.    IEnumerator TripleShotPowerDownRoutine()
  141.    {
  142.     yield return new WaitForSeconds(5.0f);
  143.     _isTripleShotActive = false;
  144.    }
  145.  
  146. public void SpeedBoostActive() {
  147.   _isSpeedBoostActive = true;
  148.   _speed*= _speedMultiplier;
  149.   StartCoroutine(SpeedBoostPowerDownRoutine());
  150. }
  151.  
  152. IEnumerator SpeedBoostPowerDownRoutine () {
  153.   yield return new WaitForSeconds(5.0f);
  154.   _isSpeedBoostActive = false;
  155.   _speed/= _speedMultiplier;
  156. }
  157.  
  158. public void ShieldsActive() {
  159.   _isShieldActive = true;
  160.   _shieldVisualizer.SetActive(true);
  161. }
  162.  
  163. public void AddScore(int points)
  164. {
  165.   _score += points;
  166.   _uiManager.UpdatedScore(_score);
  167. }
  168.  
  169. public void bestScore(int _score)
  170. {
  171.    if(_score >_bestScore )
  172.      {
  173.        _bestScore = _score;
  174.        _uiManager.CheckForBestScore(_bestScore);
  175.        
  176.      }
  177. }
  178. }
  179.  
  180.  
  181. // UIManager Script
  182. using System.Collections;
  183. using System.Collections.Generic;
  184. using UnityEngine;
  185. using UnityEngine.UI;
  186. using TMPro;
  187. using UnityEngine.SceneManagement;
  188.  
  189. public class UIManager : MonoBehaviour
  190. {
  191.     [SerializeField]
  192.     private TextMeshProUGUI _scoreText, bestText;
  193.     [SerializeField]
  194.     private Image _LivesImg;
  195.     [SerializeField]
  196.     private Sprite[] _livesSprites;
  197.     [SerializeField]
  198.     private TextMeshProUGUI _gameOverText;
  199.      [SerializeField]
  200.      private TextMeshProUGUI _reStartKey;
  201.      private GameManager _gameManager;
  202.  
  203.      
  204.    
  205.       void Start()
  206.     {
  207.         _scoreText.text = "Score: " + 0;
  208.         _gameOverText.gameObject.SetActive(false);
  209.        _reStartKey.gameObject.SetActive(false);
  210.        _gameManager = GameObject.Find("Game_Manager").GetComponent<GameManager>();
  211.  
  212.      if(_gameManager == null) {
  213.       Debug.LogError("Game Manager is Null");
  214.      }
  215.     }
  216.  
  217.     public void UpdatedScore(int playerScore)
  218.     {
  219.       _scoreText.text = "Score: " + playerScore.ToString();
  220.     }
  221.    
  222.     public void CheckForBestScore(int _bestScore)
  223.     {
  224.     bestText.text = "Best: " + _bestScore.ToString();
  225.      
  226.     }
  227.  
  228. public void UpdateLives(int cuurentLives)
  229. {
  230.   _LivesImg.sprite = _livesSprites[cuurentLives];  
  231.   if(cuurentLives==0) {
  232.     GameOverSequence();
  233.   }
  234.   }  
  235.    
  236. void GameOverSequence()
  237. {
  238.   _gameManager.GameOver();
  239.   _gameOverText.gameObject.SetActive(true);
  240.     StartCoroutine(GameOverFlickerRoutine());
  241.     _reStartKey.gameObject.SetActive(true);
  242. }
  243.  
  244. IEnumerator GameOverFlickerRoutine()
  245. {
  246.   while(true)
  247.   {
  248.     _gameOverText.text = "GAME OVER";
  249.     yield return new WaitForSeconds(0.5f);
  250.     _gameOverText.text = "";
  251.     yield return new WaitForSeconds(0.5f);
  252.   }
  253. }
  254.  
  255. //Resume Play
  256. public void ResumePlay()
  257. {
  258.   GameManager gm = GameObject.Find("Game_Manager").GetComponent<GameManager>();
  259.   gm.ResumeGame();
  260. }
  261.  
  262. // BackToMainMenu
  263. public void BackToMainMenu()
  264.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement