Advertisement
BeneFager

Untitled

Jan 12th, 2022
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.53 KB | None | 0 0
  1. public class DestructableBlock : BlockBase
  2.     {
  3.         //PowerUpStuff
  4.         private GameObject playerBall;
  5.         private BoxCollider boxColliderTrigger;
  6.         private BallMovement ballMovementScript;
  7.  
  8.         //Combo stuff
  9.         //private int cachedPoints;
  10.  
  11.         //ScreenShake stuff
  12.         private FlashWhenStruck screenShakeScript;
  13.         private float deathShake = 0.02f; // Magic number because it need to be very low
  14.  
  15.         //VFX stuff
  16.         public GameObject redExplosionEffect;
  17.         public GameObject redExplosionLight;
  18.         public GameObject yellowExplosionEffect;
  19.         public GameObject yellowExplosionLight;
  20.  
  21.         //SFX stuff
  22.         private BlockAudioClips blockAudioClips;
  23.  
  24.         public override void Start()
  25.         {
  26.             //PowerUpStuff
  27.             playerBall = GameObject.FindWithTag("PlayerBall");
  28.             boxColliderTrigger = gameObject.GetComponent<BoxCollider>();
  29.             ballMovementScript = playerBall.GetComponent<BallMovement>();
  30.  
  31.             //Combo stuff
  32.             //cachedPoints = points;
  33.  
  34.             //ScreenShake stuff
  35.             screenShakeScript = GetComponent<FlashWhenStruck>();
  36.  
  37.             Rigidbody body = GetComponent<Rigidbody>();
  38.             body.constraints = RigidbodyConstraints.FreezeAll;
  39.  
  40.             blockAudioClips = GetComponent<BlockAudioClips>();
  41.         }
  42.  
  43.         private void FixedUpdate()
  44.         {
  45.             if (ballMovementScript.isPoweredUp)
  46.             {
  47.                 boxColliderTrigger.isTrigger = true;
  48.             }
  49.             else
  50.             {
  51.                 if (boxColliderTrigger.isTrigger) boxColliderTrigger.isTrigger = false;
  52.             }
  53.         }
  54.  
  55.         private void OnTriggerEnter(Collider other)
  56.         {
  57.             if (other.gameObject.tag == "PlayerBall")
  58.             {
  59.                 //Bounce VFX
  60.                 Instantiate(yellowExplosionEffect, transform);
  61.  
  62.                 int returnedValue = other.gameObject.GetComponent<Combo>().ApplyComboMultiplier(points);
  63.                 PlayerHighScore.pointsSystem.AddScore(returnedValue);
  64.                 screenShakeScript.DissolveOnDeath();
  65.                 StartCoroutine(deathDelay());
  66.             }
  67.         }
  68.  
  69.         public override void OnCollisionEnter(Collision other)
  70.         {
  71.             if (!other.rigidbody) return;
  72.             ContactPoint[] contactPoints = other.contacts;
  73.             ContactPoint contactpoint = contactPoints[0];
  74.             Bounce(other.rigidbody, contactpoint);
  75.             TakeDamage(5); //Todo fix sumthing
  76.  
  77.             //Combo stuff
  78.             if (other.gameObject.tag == "PlayerBall")
  79.             {
  80.                 int returnedValue = other.gameObject.GetComponent<Combo>().ApplyComboMultiplier(points);
  81.                 //Debug.Log("POINTS ARE NOW = " + returnedValue);
  82.                 PlayerHighScore.pointsSystem.AddScore(returnedValue);
  83.             }
  84.         }
  85.  
  86.         public override void TakeDamage(int damage)
  87.         {
  88.             health -= damage;
  89.             if (health < 1)
  90.             {
  91.                 screenShakeScript.DissolveOnDeath();
  92.                 StartCoroutine(deathDelay());
  93.             }
  94.         }
  95.  
  96.         public override void Die()
  97.         {
  98.             LevelPass.goldenBlockNumber.AddPoints();
  99.             gameObject.SetActive(false);
  100.             HoleManager.HoleManagerSystem.ShouldHolesBeGreenOrRed();
  101.         }
  102.  
  103.         public override void Bounce(Rigidbody body, ContactPoint point)
  104.         {
  105.             var lightEffect = Instantiate(yellowExplosionLight,transform);
  106.             StartCoroutine(VFXStuff(lightEffect));
  107.             blockAudioClips.PlayBounceAudio();
  108.             Vector3 velocity = Vector3.Reflect(body.velocity, point.normal);
  109.             velocity.y = -0.5f; // to not bounce upwards
  110.             body.velocity = velocity.normalized * bouncySpeed;
  111.         }
  112.  
  113.         private IEnumerator deathDelay()
  114.         {
  115.             //ScreenShake stuff
  116.             screenShakeScript.ScreenShake(screenShakeScript.shakeLength, screenShakeScript.shakePower + deathShake);
  117.             screenShakeScript.LateUpdate();
  118.             blockAudioClips.PlayDestuctionAudio();
  119.             yield return new WaitForSeconds(0.2f); // 0.2
  120.             Die();
  121.             yield return null;
  122.         }
  123.  
  124.         private IEnumerator VFXStuff(GameObject lightEffect)
  125.         {
  126.             Instantiate(yellowExplosionEffect, transform);
  127.             yield return new WaitForSeconds(0.1f);
  128.             Destroy(lightEffect);
  129.             yield return null;
  130.         }
  131.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement