SizilStank

Untitled

May 3rd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.42 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class Ball : MonoBehaviour
  7. {
  8.     // config params
  9.     public int currentHealth = 3;//currentHealth of the player it is redused by 1 on the losecollider hit
  10.     [SerializeField]
  11.     PaddleMove _paddle1;
  12.     [SerializeField ]
  13.     float xPush = 2;
  14.     [SerializeField]
  15.     float yPush = 15;
  16.     [SerializeField]
  17.     AudioClip[] _ballSounds;// aray
  18.     [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!
  19.  
  20.     private Spawn_Manager _spawnManager;
  21.     private GameStatus _gameStatus;
  22.  
  23.     [SerializeField] private bool slowGameSpeed = false;
  24.     [SerializeField] private bool fastGameSpeed = false;
  25.     [SerializeField] private float newGameSpeed = .22f;
  26.     [SerializeField] private float newFastGameSpeed = 1f;
  27.     [SerializeField] private float normalGameSpeed = .77f;
  28.    
  29.  
  30.  
  31.     public GameObject ball;
  32.    
  33.  
  34.  
  35.     //state
  36.     private bool hasStarted = false;
  37.     Vector2 paddleToBallVector;
  38.  
  39.     //cached componet ref
  40.     AudioSource myAudioSource;
  41.     Rigidbody2D myRigidbody2D;// if you use somthing more then once cache it as a var
  42.  
  43.     // Start is called before the first frame update
  44.     void Start()
  45.     {
  46.         paddleToBallVector = transform.position - _paddle1.transform.position;
  47.         myAudioSource = GetComponent<AudioSource>();
  48.         myRigidbody2D = GetComponent<Rigidbody2D>();
  49.  
  50.         _gameStatus = GameObject.Find("GameStatus").GetComponent<GameStatus>();
  51.  
  52.         _spawnManager = GameObject.Find("Spawn_Manager").GetComponent<Spawn_Manager>();
  53.  
  54.        
  55.             _spawnManager.StartSpawnRoutines();
  56.        
  57.  
  58.     }
  59.  
  60.     // Update is called once per frame
  61.     void Update()
  62.     {
  63.         //BothPowerUps();
  64.  
  65.         SlowGamePowerUp();
  66.         FastGamePowerUp();
  67.  
  68.  
  69.         if (!hasStarted)
  70.         {          
  71.             LockBallToPaddle();
  72.             LaunchOnMouseClick();
  73.         }
  74.     }
  75.  
  76.     private void LaunchOnMouseClick()
  77.     {
  78.         if (Input.GetMouseButtonDown(0))
  79.         {
  80.             hasStarted = true;
  81.             myRigidbody2D.velocity = new Vector2(xPush, yPush);
  82.  
  83.            
  84.         }
  85.     }
  86.  
  87.     private void LockBallToPaddle()
  88.     {
  89.         Vector2 paddlePos = new Vector2(_paddle1.transform.position.x, _paddle1.transform.position.y);
  90.         transform.position = paddlePos + paddleToBallVector;
  91.     }
  92.  
  93.     private void OnCollisionEnter2D(Collision2D collision)
  94.     {
  95.         Vector2 velocityTweak = new Vector2(UnityEngine.Random.Range(0f, randomFactor), UnityEngine.Random.Range(0f, randomFactor));
  96.  
  97.         if (hasStarted)          
  98.         {
  99.             AudioClip clip = _ballSounds[UnityEngine.Random.Range(0, _ballSounds.Length)];
  100.             //PlayOneShot will let the audio clip play uninterupted where Play can be interupted by another udio clip
  101.             myAudioSource.PlayOneShot(clip);
  102.             myRigidbody2D.velocity += velocityTweak;// we need this for it to work
  103.         }
  104.     }
  105.  
  106.     public void Damage(int damage)//this will reduse the health cuz of the damage
  107.     {
  108.         currentHealth -= damage;
  109.     }
  110.  
  111.    
  112.  
  113.  
  114.     public void SlowGamePowerUp()
  115.     {
  116.         if (slowGameSpeed == true)
  117.         {
  118.             _gameStatus.gameSpeed = newGameSpeed;
  119.         }
  120.         else
  121.         {
  122.             _gameStatus.gameSpeed = normalGameSpeed;
  123.         }
  124.     }
  125.  
  126.     public void CanGameSlowOn()
  127.     {
  128.         slowGameSpeed = true;
  129.         StartCoroutine(canSpeedSlowPowerDownRoutine());
  130.     }
  131.  
  132.     public IEnumerator canSpeedSlowPowerDownRoutine()
  133.     {
  134.         yield return new WaitForSeconds(8f);
  135.         slowGameSpeed = false;
  136.     }
  137.  
  138.    
  139.  
  140.     public void FastGamePowerUp()
  141.     {
  142.         if (fastGameSpeed == true)
  143.         {
  144.             _gameStatus.gameSpeed = newFastGameSpeed;
  145.         }
  146.         else
  147.         {
  148.             _gameStatus.gameSpeed = normalGameSpeed;
  149.         }
  150.     }
  151.  
  152.     public void CanGameFastOn()
  153.     {
  154.         fastGameSpeed = true;
  155.         StartCoroutine(canSpeedFastPowerDownRoutine());
  156.     }
  157.  
  158.     public IEnumerator canSpeedFastPowerDownRoutine()
  159.     {
  160.         yield return new WaitForSeconds(8f);
  161.         fastGameSpeed = false;
  162.     }
  163.  
  164.  
  165.     //public void BothPowerUps()
  166.     //{
  167.     //    SlowGamePowerUp();
  168.     //    FastGamePowerUp();
  169.     //}
  170. }
Add Comment
Please, Sign In to add comment