Advertisement
Schicklerdev

Ball.cs

Jan 16th, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.43 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.     [Header("Physics")]
  9.     [SerializeField] float ylaunchMin;
  10.     [SerializeField] float ylaunchMax;
  11.     [SerializeField] float bounceMin;
  12.     [SerializeField] float bounceMax;
  13.     [SerializeField] float randomFactor;
  14.     [SerializeField] float speedMultiplier;
  15.     [SerializeField] float[] xLaunch = { -3, 3 };
  16.     [SerializeField] float maxSpeedNegative;
  17.     [SerializeField] float maxSpeedPositive;
  18.     [SerializeField] float minSpeedNegative;
  19.     [SerializeField] float minSpeedPositive;
  20.  
  21.     [Header("Other")]
  22.     [SerializeField] float startDelay;
  23.     [SerializeField] float beaconFrequency;
  24.     [SerializeField] GameObject beaconPrefab;
  25.     [SerializeField] float beaconSpeed;
  26.  
  27.     [Header("Player References")]
  28.     [SerializeField] Paddle player1;
  29.     [SerializeField] Paddle player2;
  30.  
  31.     [Header("Sound")]
  32.     [SerializeField] AudioClip[] ballSounds;
  33.  
  34.     Rigidbody2D myRigidbody2D;
  35.     //bool ballLaunched = false;
  36.     public int owner = 0;
  37.     AI ai;
  38.     public bool isActive = true;
  39.     AudioSource myAudioSource;
  40.  
  41.  
  42.     // Start is called before the first frame update
  43.     void Start()
  44.     {
  45.         myRigidbody2D = GetComponent<Rigidbody2D>();
  46.         StartCoroutine(Countdown());
  47.         StartCoroutine(ShootBeacon());
  48.         ai = FindObjectOfType<AI>();
  49.         myAudioSource = GetComponent<AudioSource>();
  50.     }
  51.  
  52.     private void Update()
  53.     {
  54.         SendLocation();
  55.         if (!isActive)
  56.         {
  57.             myRigidbody2D.velocity = new Vector2(0, 0);
  58.         }
  59.         ManageSpeed();
  60.     }
  61.  
  62.     public IEnumerator Countdown()
  63.     {
  64.         transform.position = new Vector2(0, 0);
  65.         myRigidbody2D.velocity = new Vector2(0, 0);
  66.         GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 1);
  67.         owner = 0;
  68.         float time = 0;
  69.         if (isActive)
  70.         {
  71.             while (time < startDelay)
  72.             {
  73.                 time += Time.deltaTime;
  74.                 float normalizedTime = time / startDelay;
  75.                 if (normalizedTime >= 1)
  76.                 {
  77.                     GetComponent<Rigidbody2D>().velocity = new Vector2(xLaunch[UnityEngine.Random.Range(0, xLaunch.Length)], UnityEngine.Random.Range(ylaunchMin, ylaunchMax));
  78.                 }
  79.                 yield return null;
  80.             }
  81.         }
  82.     }
  83.  
  84.     private void OnCollisionEnter2D(Collision2D collision)
  85.     {
  86.         if (collision.gameObject.name == "Player01")
  87.         {
  88.             GetComponent<SpriteRenderer>().color = new Color(0.075f, 0.6f, 1, 1);
  89.             owner = 1;
  90.             player1.freeze = true;
  91.             myRigidbody2D.velocity = myRigidbody2D.velocity + (collision.gameObject.GetComponent<Rigidbody2D>().velocity * 1.1f);
  92.             myAudioSource.PlayOneShot(ballSounds[0]);
  93.         }
  94.         else if (collision.gameObject.name == "Player02")
  95.         {
  96.             GetComponent<SpriteRenderer>().color = new Color(1, 0.62f, 0.075f, 255);
  97.             owner = 2;
  98.             player2.freeze = true;
  99.             myRigidbody2D.velocity = myRigidbody2D.velocity + (collision.gameObject.GetComponent<Rigidbody2D>().velocity * 1.1f);
  100.             myAudioSource.PlayOneShot(ballSounds[0]);
  101.         }
  102.         else
  103.         {
  104.             myAudioSource.PlayOneShot(ballSounds[1]);
  105.         }
  106.     }
  107.     private void OnCollisionExit2D(Collision2D collision)
  108.     {
  109.         if (collision.gameObject.name == "Player01")
  110.         {
  111.             player1.freeze = false;
  112.         }
  113.         else if (collision.gameObject.name == "Player02")
  114.         {
  115.             player2.freeze = false;
  116.         }
  117.     }
  118.  
  119.     IEnumerator ShootBeacon()
  120.     {
  121.         GameObject beacon = Instantiate(beaconPrefab, transform.position, Quaternion.identity) as GameObject;
  122.         beacon.GetComponent<Rigidbody2D>().velocity = myRigidbody2D.velocity * beaconSpeed;
  123.         yield return new WaitForSeconds(beaconFrequency);
  124.         StartCoroutine(ShootBeacon());
  125.     }
  126.  
  127.     void ManageSpeed()
  128.     {
  129.         float velocityX = myRigidbody2D.velocity.x;
  130.         float velocityY = myRigidbody2D.velocity.y;
  131.         if ((myRigidbody2D.velocity.x < minSpeedPositive) && (myRigidbody2D.velocity.x > 0))
  132.         {
  133.             velocityX = minSpeedPositive;
  134.         }
  135.         if ((myRigidbody2D.velocity.y < minSpeedPositive) && (myRigidbody2D.velocity.y > 0))
  136.         {
  137.             velocityY = minSpeedPositive;
  138.         }
  139.         if ((myRigidbody2D.velocity.y > minSpeedNegative) && (myRigidbody2D.velocity.y < 0))
  140.         {
  141.             velocityY = minSpeedNegative;
  142.         }
  143.         if ((myRigidbody2D.velocity.x > minSpeedNegative) && (myRigidbody2D.velocity.x < 0))
  144.         {
  145.             velocityX = minSpeedNegative;
  146.         }
  147.         if (myRigidbody2D.velocity.x > maxSpeedPositive)
  148.         {
  149.             velocityX = maxSpeedPositive;
  150.         }
  151.         if (myRigidbody2D.velocity.y > maxSpeedPositive)
  152.         {
  153.             velocityY = maxSpeedPositive;
  154.         }
  155.         if (myRigidbody2D.velocity.x < maxSpeedNegative)
  156.         {
  157.             velocityX = maxSpeedNegative;
  158.         }
  159.         if (myRigidbody2D.velocity.y < maxSpeedNegative)
  160.         {
  161.             velocityY = maxSpeedNegative;
  162.         }
  163.         myRigidbody2D.velocity = new Vector2(velocityX, velocityY);
  164.     }
  165.  
  166.     public void SendLocation()
  167.     {
  168.         ai.ballLocation = transform.position;
  169.     }
  170.  
  171. }
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement