Advertisement
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
- {
- [Header("Physics")]
- [SerializeField] float ylaunchMin;
- [SerializeField] float ylaunchMax;
- [SerializeField] float bounceMin;
- [SerializeField] float bounceMax;
- [SerializeField] float randomFactor;
- [SerializeField] float speedMultiplier;
- [SerializeField] float[] xLaunch = { -3, 3 };
- [SerializeField] float maxSpeedNegative;
- [SerializeField] float maxSpeedPositive;
- [SerializeField] float minSpeedNegative;
- [SerializeField] float minSpeedPositive;
- [Header("Other")]
- [SerializeField] float startDelay;
- [SerializeField] float beaconFrequency;
- [SerializeField] GameObject beaconPrefab;
- [SerializeField] float beaconSpeed;
- [Header("Player References")]
- [SerializeField] Paddle player1;
- [SerializeField] Paddle player2;
- [Header("Sound")]
- [SerializeField] AudioClip[] ballSounds;
- Rigidbody2D myRigidbody2D;
- //bool ballLaunched = false;
- public int owner = 0;
- AI ai;
- public bool isActive = true;
- AudioSource myAudioSource;
- // Start is called before the first frame update
- void Start()
- {
- myRigidbody2D = GetComponent<Rigidbody2D>();
- StartCoroutine(Countdown());
- StartCoroutine(ShootBeacon());
- ai = FindObjectOfType<AI>();
- myAudioSource = GetComponent<AudioSource>();
- }
- private void Update()
- {
- SendLocation();
- if (!isActive)
- {
- myRigidbody2D.velocity = new Vector2(0, 0);
- }
- ManageSpeed();
- }
- public IEnumerator Countdown()
- {
- transform.position = new Vector2(0, 0);
- myRigidbody2D.velocity = new Vector2(0, 0);
- GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 1);
- owner = 0;
- float time = 0;
- if (isActive)
- {
- while (time < startDelay)
- {
- time += Time.deltaTime;
- float normalizedTime = time / startDelay;
- if (normalizedTime >= 1)
- {
- GetComponent<Rigidbody2D>().velocity = new Vector2(xLaunch[UnityEngine.Random.Range(0, xLaunch.Length)], UnityEngine.Random.Range(ylaunchMin, ylaunchMax));
- }
- yield return null;
- }
- }
- }
- private void OnCollisionEnter2D(Collision2D collision)
- {
- if (collision.gameObject.name == "Player01")
- {
- GetComponent<SpriteRenderer>().color = new Color(0.075f, 0.6f, 1, 1);
- owner = 1;
- player1.freeze = true;
- myRigidbody2D.velocity = myRigidbody2D.velocity + (collision.gameObject.GetComponent<Rigidbody2D>().velocity * 1.1f);
- myAudioSource.PlayOneShot(ballSounds[0]);
- }
- else if (collision.gameObject.name == "Player02")
- {
- GetComponent<SpriteRenderer>().color = new Color(1, 0.62f, 0.075f, 255);
- owner = 2;
- player2.freeze = true;
- myRigidbody2D.velocity = myRigidbody2D.velocity + (collision.gameObject.GetComponent<Rigidbody2D>().velocity * 1.1f);
- myAudioSource.PlayOneShot(ballSounds[0]);
- }
- else
- {
- myAudioSource.PlayOneShot(ballSounds[1]);
- }
- }
- private void OnCollisionExit2D(Collision2D collision)
- {
- if (collision.gameObject.name == "Player01")
- {
- player1.freeze = false;
- }
- else if (collision.gameObject.name == "Player02")
- {
- player2.freeze = false;
- }
- }
- IEnumerator ShootBeacon()
- {
- GameObject beacon = Instantiate(beaconPrefab, transform.position, Quaternion.identity) as GameObject;
- beacon.GetComponent<Rigidbody2D>().velocity = myRigidbody2D.velocity * beaconSpeed;
- yield return new WaitForSeconds(beaconFrequency);
- StartCoroutine(ShootBeacon());
- }
- void ManageSpeed()
- {
- float velocityX = myRigidbody2D.velocity.x;
- float velocityY = myRigidbody2D.velocity.y;
- if ((myRigidbody2D.velocity.x < minSpeedPositive) && (myRigidbody2D.velocity.x > 0))
- {
- velocityX = minSpeedPositive;
- }
- if ((myRigidbody2D.velocity.y < minSpeedPositive) && (myRigidbody2D.velocity.y > 0))
- {
- velocityY = minSpeedPositive;
- }
- if ((myRigidbody2D.velocity.y > minSpeedNegative) && (myRigidbody2D.velocity.y < 0))
- {
- velocityY = minSpeedNegative;
- }
- if ((myRigidbody2D.velocity.x > minSpeedNegative) && (myRigidbody2D.velocity.x < 0))
- {
- velocityX = minSpeedNegative;
- }
- if (myRigidbody2D.velocity.x > maxSpeedPositive)
- {
- velocityX = maxSpeedPositive;
- }
- if (myRigidbody2D.velocity.y > maxSpeedPositive)
- {
- velocityY = maxSpeedPositive;
- }
- if (myRigidbody2D.velocity.x < maxSpeedNegative)
- {
- velocityX = maxSpeedNegative;
- }
- if (myRigidbody2D.velocity.y < maxSpeedNegative)
- {
- velocityY = maxSpeedNegative;
- }
- myRigidbody2D.velocity = new Vector2(velocityX, velocityY);
- }
- public void SendLocation()
- {
- ai.ballLocation = transform.position;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement