Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Ball : MonoBehaviour
- {
- // config params
- public int currentHealth = 3;//currentHealth of the player it is redused by 1 on the losecollider hit
- [SerializeField]
- PaddleMove _paddle1;
- [SerializeField ]
- float xPush = 2;
- [SerializeField]
- float yPush = 15;
- [SerializeField]
- AudioClip[] _ballSounds;// aray
- [SerializeField] float randomFactor = .5f;//this sets the ball to shoot randomly in the x anf y axis, its an offset so we dont get in a boring side to side or up and down loop!
- private Spawn_Manager _spawnManager;
- private GameStatus _gameStatus;
- [SerializeField] private bool slowGameSpeed = false;
- [SerializeField] private bool fastGameSpeed = false;
- [SerializeField] private float newGameSpeed = .22f;
- [SerializeField] private float newFastGameSpeed = 1f;
- [SerializeField] private float normalGameSpeed = .77f;
- public GameObject ball;
- //state
- private bool hasStarted = false;
- Vector2 paddleToBallVector;
- //cached componet ref
- AudioSource myAudioSource;
- Rigidbody2D myRigidbody2D;// if you use somthing more then once cache it as a var
- // Start is called before the first frame update
- void Start()
- {
- paddleToBallVector = transform.position - _paddle1.transform.position;
- myAudioSource = GetComponent<AudioSource>();
- myRigidbody2D = GetComponent<Rigidbody2D>();
- _gameStatus = GameObject.Find("GameStatus").GetComponent<GameStatus>();
- _spawnManager = GameObject.Find("Spawn_Manager").GetComponent<Spawn_Manager>();
- _spawnManager.StartSpawnRoutines();
- }
- // Update is called once per frame
- void Update()
- {
- //BothPowerUps();
- SlowGamePowerUp();
- FastGamePowerUp();
- if (!hasStarted)
- {
- LockBallToPaddle();
- LaunchOnMouseClick();
- }
- }
- private void LaunchOnMouseClick()
- {
- if (Input.GetMouseButtonDown(0))
- {
- hasStarted = true;
- myRigidbody2D.velocity = new Vector2(xPush, yPush);
- }
- }
- private void LockBallToPaddle()
- {
- Vector2 paddlePos = new Vector2(_paddle1.transform.position.x, _paddle1.transform.position.y);
- transform.position = paddlePos + paddleToBallVector;
- }
- private void OnCollisionEnter2D(Collision2D collision)
- {
- Vector2 velocityTweak = new Vector2(UnityEngine.Random.Range(0f, randomFactor), UnityEngine.Random.Range(0f, randomFactor));
- if (hasStarted)
- {
- AudioClip clip = _ballSounds[UnityEngine.Random.Range(0, _ballSounds.Length)];
- //PlayOneShot will let the audio clip play uninterupted where Play can be interupted by another udio clip
- myAudioSource.PlayOneShot(clip);
- myRigidbody2D.velocity += velocityTweak;// we need this for it to work
- }
- }
- public void Damage(int damage)//this will reduse the health cuz of the damage
- {
- currentHealth -= damage;
- }
- public void SlowGamePowerUp()
- {
- if (slowGameSpeed == true)
- {
- _gameStatus.gameSpeed = newGameSpeed;
- }
- else
- {
- _gameStatus.gameSpeed = normalGameSpeed;
- }
- }
- public void CanGameSlowOn()
- {
- slowGameSpeed = true;
- StartCoroutine(canSpeedSlowPowerDownRoutine());
- }
- public IEnumerator canSpeedSlowPowerDownRoutine()
- {
- yield return new WaitForSeconds(8f);
- slowGameSpeed = false;
- }
- public void FastGamePowerUp()
- {
- if (fastGameSpeed == true)
- {
- _gameStatus.gameSpeed = newFastGameSpeed;
- }
- else
- {
- _gameStatus.gameSpeed = normalGameSpeed;
- }
- }
- public void CanGameFastOn()
- {
- fastGameSpeed = true;
- StartCoroutine(canSpeedFastPowerDownRoutine());
- }
- public IEnumerator canSpeedFastPowerDownRoutine()
- {
- yield return new WaitForSeconds(8f);
- fastGameSpeed = false;
- }
- //public void BothPowerUps()
- //{
- // SlowGamePowerUp();
- // FastGamePowerUp();
- //}
- }
Add Comment
Please, Sign In to add comment