mirror of
https://projects.caleb-brown.dev/UDRI-XRT/UDRIGEEKCup2024.git
synced 2025-01-22 07:08:51 -05:00
98 lines
2.9 KiB
C#
98 lines
2.9 KiB
C#
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;
|
|
private bool isBlowingUp;
|
|
private bool primeTimedOut;
|
|
|
|
private void Start()
|
|
{
|
|
StartCoroutine(Timeout());
|
|
IEnumerator Timeout()
|
|
{
|
|
yield return new WaitForSeconds(2.5f);
|
|
BombBlowUp();
|
|
}
|
|
}
|
|
|
|
private void OnCollisionEnter(Collision collision)
|
|
{
|
|
if(primeTimedOut) return;
|
|
if (!primed)
|
|
{
|
|
primed = true;
|
|
primeTimedOut = true;
|
|
|
|
StartCoroutine(TimeOut());
|
|
|
|
IEnumerator TimeOut()
|
|
{
|
|
yield return new WaitForSeconds(0.1f);
|
|
primeTimedOut = false;
|
|
}
|
|
|
|
return;
|
|
}
|
|
BombBlowUp();
|
|
}
|
|
|
|
private void BombBlowUp()
|
|
{
|
|
if (isBlowingUp) return;
|
|
isBlowingUp = true;
|
|
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);
|
|
}
|
|
}
|
|
}
|