1
0
mirror of https://projects.caleb-brown.dev/UDRI-XRT/UDRIGEEKCup2024.git synced 2025-01-22 15:18:25 -05:00
UDRIGEEKCup2024/Assets/Obstacles/Bomb.cs

98 lines
2.9 KiB
C#
Raw Normal View History

2024-04-18 12:52:07 -04:00
using System;
using System.Collections;
using UnityEngine;
namespace MAVRIC.GEEKCup.Obstacles
{
public class Bomb : MonoBehaviour
{
public float explosionTimeSeconds = 0.1f;
public float force = 10.0f;
public GameObject modelObject;
public SphereCollider explosionCollider;
public ParticleSystem ParticleSystem;
public float maxExplosionRadius = 10.0f;
public float maxDonutRadius = 10.0f;
public ParticleSystem.ShapeModule editableShape;
public bool primed;
2024-04-23 11:45:53 -04:00
private bool isBlowingUp;
private bool primeTimedOut;
private void Start()
{
StartCoroutine(Timeout());
IEnumerator Timeout()
{
yield return new WaitForSeconds(2.5f);
BombBlowUp();
}
}
2024-04-18 12:52:07 -04:00
private void OnCollisionEnter(Collision collision)
{
2024-04-23 11:45:53 -04:00
if(primeTimedOut) return;
2024-04-18 12:52:07 -04:00
if (!primed)
{
primed = true;
2024-04-23 11:45:53 -04:00
primeTimedOut = true;
StartCoroutine(TimeOut());
IEnumerator TimeOut()
{
yield return new WaitForSeconds(0.1f);
primeTimedOut = false;
}
2024-04-18 12:52:07 -04:00
return;
}
2024-04-23 11:45:53 -04:00
BombBlowUp();
}
private void BombBlowUp()
{
if (isBlowingUp) return;
isBlowingUp = true;
2024-04-18 12:52:07 -04:00
ParticleSystem.gameObject.SetActive(true);
editableShape = ParticleSystem.shape;
StartCoroutine(Disappear());
IEnumerator Disappear()
{
yield return new WaitForSeconds(explosionTimeSeconds / 3.0f);
modelObject.SetActive(false);
ParticleSystem.gameObject.transform.SetParent(null);
ParticleSystem.gameObject.transform.rotation = Quaternion.identity;
ParticleSystem.Play();
explosionCollider.gameObject.SetActive(true);
for (float timer = 0; timer < explosionTimeSeconds; timer += Time.deltaTime)
{
float percent = timer / explosionTimeSeconds;
float radius = Mathf.Lerp(1, maxExplosionRadius, percent);
float donut = Mathf.Lerp(1, maxDonutRadius, percent);
editableShape.radius = radius;
editableShape.donutRadius = donut;
yield return null;
}
while (ParticleSystem.IsAlive()) yield return null;
Destroy(gameObject);
Destroy(ParticleSystem.gameObject);
}
}
public void OnTriggerEnter(Collider other)
{
Rigidbody body = other.attachedRigidbody;
if (body == null) return;
body.AddExplosionForce(force,transform.position,explosionCollider.radius);
}
}
}