Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class Player : MonoBehaviour
- {
- [SerializeField]
- private float _speed = 3.5f;
- private float _speedMultiplier = 2f;
- [SerializeField]
- private GameObject _laserPrefab;
- [SerializeField]
- private float _fireRate = 0.5f;
- private float _canFire = -1f;
- [SerializeField]
- private int _lives = 3;
- private SpawnManager _spawnManager;
- [SerializeField]
- private bool _isTripleShotActive = false;
- [SerializeField]
- private GameObject _tripleShotPrefab;
- private bool _stopSpawning = false;
- private bool _isSpeedBoostActive = false;
- private bool _isShieldActive = false;
- [SerializeField]
- private GameObject _shieldVisualizer;
- [SerializeField]
- private int _score;
- private UIManager _uiManager;
- [SerializeField]
- private GameObject _Left_Hurt;
- [SerializeField]
- private GameObject _Right_Hurt;
- [SerializeField]
- private AudioClip _laserShot;
- private AudioSource _audioSource;
- [SerializeField]
- private AudioClip _explosionSound;
- private GameManager _gameManager;
- public bool IsPlayerOne = false;
- public bool IsPlayerTwo = false;
- // Start is called before the first frame update
- void Start()
- {
- _audioSource = GetComponent<AudioSource>();
- _spawnManager = GameObject.Find("Spawn_Manager").GetComponent<SpawnManager>();
- if (_spawnManager == null)
- {
- Debug.LogError("Spawn manager is NULL!");
- }
- _uiManager = GameObject.Find("Canvas").GetComponent<UIManager>();
- if (_uiManager == null)
- {
- Debug.LogError("UIMANAGER IS NULL!");
- }
- _gameManager = GameObject.Find("Game_Manager").GetComponent<GameManager>();
- if (_gameManager == null)
- {
- Debug.LogError("Game Manager Is null!");
- }
- if (_gameManager._isCoOpMode == false)
- {
- transform.position = new Vector3(0, 0, 0);
- }
- if (_audioSource == null)
- {
- Debug.LogError("Audio Source is null");
- }
- else
- {
- _audioSource.clip = _laserShot;
- }
- }
- // Update is called once per frame
- void Update()
- {
- if (IsPlayerOne == true)
- {
- CalculateMovement();
- if ((Input.GetKeyDown(KeyCode.Space) && Time.time > _canFire) && IsPlayerOne == true)
- {
- Shoot();
- }
- }
- else
- {
- CalculateMovementP2();
- if ((Input.GetKeyDown(KeyCode.RightShift) && Time.time > _canFire))
- {
- ShootP2();
- }
- }
- void CalculateMovement()
- {
- if (IsPlayerOne == true)
- {
- //variables
- float horizontalInput = Input.GetAxis("Player1 Horizontal");
- float verticalInput = Input.GetAxis("Player1 Vertical");
- //float xpos = transform.position.x ;
- /*
- if (horizontalInput != 0)
- {
- transform.Translate(Vector3.right * horizontalInput * _speed * Time.deltaTime);
- }
- if (horizontalInput != 0)
- {
- transform.Translate(Vector3.up * verticalInput * _speed * Time.deltaTime);
- }
- */
- Vector3 direction = new Vector3(horizontalInput, verticalInput, 0);
- transform.Translate(direction * _speed * Time.deltaTime);
- transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, -3.8f, 0), 0);
- if (transform.position.x > 11.16f)
- {
- transform.position = new Vector3(-11.16f, transform.position.y, 0);
- }
- else if (transform.position.x <= -11.16f)
- transform.position = new Vector3(11.16f, transform.position.y, 0);
- }
- }
- void CalculateMovementP2()
- {
- if (IsPlayerTwo == true)
- {
- float horizontalInput2 = Input.GetAxis("Player2 Horizontal");
- float verticalInput2 = Input.GetAxis("Player2 Vertical");
- /* if (horizontalInput2 != 0)
- {
- transform.Translate(Vector3.right * horizontalInput2 * _speed * Time.deltaTime);
- }
- else
- {
- Debug.LogError("HORIZONTAL INPUT IS NULL!");
- }
- if (verticalInput2 != 0)
- {
- transform.Translate(Vector3.up * verticalInput2 * _speed * Time.deltaTime);
- }
- else
- {
- Debug.LogError("VERTICAL INOUT IS NULL!");
- }
- if (Input.GetButtonDown("Player2 Horizontal"))
- {
- transform.Translate(Vector3.right * horizontalInput2 * _speed * Time.deltaTime);
- }
- if (Input.GetButtonDown("Player2 Vertical"))
- {
- transform.Translate(Vector3.up * verticalInput2 * _speed * Time.deltaTime);
- }
- */
- Vector3 direction = new Vector3(horizontalInput2, verticalInput2, 0);
- transform.Translate(direction * _speed * Time.deltaTime);
- transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, -3.8f, 0), 0);
- //warping
- if (transform.position.x > 11.16f)
- {
- transform.position = new Vector3(-11.16f, transform.position.y, 0);
- }
- else if (transform.position.x <= -11.16f)
- transform.position = new Vector3(11.16f, transform.position.y, 0);
- }
- }
- void Shoot()
- {
- _canFire = Time.time + _fireRate;
- if (Input.GetKeyDown(KeyCode.Space) && _isTripleShotActive == true)
- {
- Instantiate(_tripleShotPrefab, transform.position, Quaternion.identity);
- _audioSource.Play();
- }
- else if (Input.GetKeyDown(KeyCode.Space) && _isTripleShotActive == false)
- {
- Instantiate(_laserPrefab, this.transform.position + new Vector3(0, 1.05f, 0), Quaternion.identity);
- _audioSource.Play();
- }
- }
- void ShootP2()
- {
- if (Input.GetKeyDown(KeyCode.RightShift) && _isTripleShotActive == true)
- {
- Instantiate(_tripleShotPrefab, transform.position, Quaternion.identity);
- _audioSource.Play();
- }
- else if (Input.GetKeyDown(KeyCode.RightShift) && _isTripleShotActive == false)
- {
- Instantiate(_laserPrefab, this.transform.position + new Vector3(0, 1.05f, 0), Quaternion.identity);
- _audioSource.Play();
- }
- }
- }
- public void Damage()
- {
- if (_isShieldActive == true)
- {
- _shieldVisualizer.SetActive(false);
- _isShieldActive = false;
- return;
- }
- if (_lives >= 1)
- {
- _lives--;
- }
- if (_lives == 2)
- {
- _Left_Hurt.SetActive(true);
- }
- else if (_lives == 1)
- {
- _Right_Hurt.SetActive(true);
- }
- _uiManager.UpdateLives(_lives);
- if (_lives <= 0)
- {
- _spawnManager.OnPlayerDeath();
- _audioSource.clip = _explosionSound;
- Destroy(this.gameObject, 0.5f);
- _audioSource.Play();
- }
- }
- public void TripleShotCollected()
- {
- _isTripleShotActive = true;
- StartCoroutine(TripleShotPowerDown());
- IEnumerator TripleShotPowerDown()
- {
- yield return new WaitForSeconds(5.0f);
- _isTripleShotActive = false;
- }
- }
- public void SpeedBoostCollected()
- {
- _isSpeedBoostActive = true;
- _speed *= _speedMultiplier;
- StartCoroutine(SpeedBoostCoolDown());
- IEnumerator SpeedBoostCoolDown()
- {
- yield return new WaitForSeconds(5.0f);
- _isSpeedBoostActive = false;
- _speed /= _speedMultiplier;
- }
- }
- public void isShieldActive()
- {
- _shieldVisualizer.SetActive(true);
- _isShieldActive = true;
- }
- public void score(int points)
- {
- _score += points;
- _uiManager.UpdateScore(_score);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement