TO SQUASH: Nuke ball explosion.

This commit is contained in:
2026-08-28 18:23:35 -04:00
parent 716e35c615
commit c91a8a4011
7 changed files with 456 additions and 17 deletions
+62 -2
View File
@@ -1,4 +1,6 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NukeBall : BallBase
{
@@ -6,6 +8,8 @@ public class NukeBall : BallBase
public AudioClip explosionSound;
public GameObject explosionEffectPrefab;
public WorldFiller worldFiller;
// Tracks escalating danger/critical phases for audio cues.
@@ -30,6 +34,8 @@ public class NukeBall : BallBase
private float bounceDuration = 1.00f;
private bool exploding = false;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
@@ -55,6 +61,11 @@ public class NukeBall : BallBase
// Update is called once per frame
void FixedUpdate()
{
if (exploding)
{
return;
}
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
if (stateInfo.IsName("NuclearBallBounce")) {
@@ -100,14 +111,20 @@ public class NukeBall : BallBase
{
// Set critical state.
var area = worldFiller != null ? worldFiller.GetAreaPercentOfFreeRegion(transform.position) : -1.0f;
criticalState = (area < 0.2f) ? 2 : (area < 0.4f) ? 1 : 0;
if (area == -1.0f || exploding)
{
return;
}
criticalState = (area < 0.12f) ? 3 : (area < 0.2f) ? 2 : (area < 0.4f) ? 1 : 0;
// State 1 beeps every 0.5s, state 2 beeps every 0.25s.
criticalTime = (criticalState == 2) ? 0.5f : 1.0f;
// If in a critical state, increment the critical timer and play the
// critical sound if the timer exceeds the critical time.
if (criticalState >= 1)
if (criticalState == 1 || criticalState == 2)
{
criticalTimer += Time.deltaTime;
@@ -119,6 +136,28 @@ public class NukeBall : BallBase
criticalTimer -= criticalTime;
}
}
else if (criticalState == 3)
{
audioSource.PlayOneShot(explosionSound);
var balls = worldFiller != null ?
worldFiller.GetBallsInSameFreeRegion(transform.position) :
new Queue<GameObject>();
foreach (var ball in balls)
{
ball.GetComponent<BallBase>()?.StopBall();
if (explosionEffectPrefab != null)
{
Instantiate(explosionEffectPrefab, ball.transform.position, Quaternion.identity);
}
}
exploding = true;
StartCoroutine(DestroyAndFillRegion(balls));
}
else
{
criticalTimer = 0.0f;
@@ -198,4 +237,25 @@ public class NukeBall : BallBase
}
}
}
private IEnumerator DestroyAndFillRegion(Queue<GameObject> balls)
{
yield return new WaitForSeconds(1.4f);
foreach (var ball in balls)
{
Destroy(ball);
}
var freeRegion = worldFiller != null ?
worldFiller.GetFreeRegionOfPosition(transform.position) :
null;
if (freeRegion != null)
{
worldFiller.FillFreeRegion(freeRegion.Value);
}
Destroy(gameObject);
}
}