MiniGames/Assets/Scripts/CubeClash/CubeClash_TimeBomb.cs

155 lines
5.2 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
public class CubeClash_TimeBomb : MonoBehaviour
{
[Header("Timing")]
public float countdown = 3f;
[Header("Explosion")]
public float radius = 3f;
public float explosionForce = 20f;
[Tooltip("Upward bias for the explosion force. 0 = flat; 12 = nice pop.")]
public float upwardModifier = 1.0f;
public ForceMode forceMode = ForceMode.Impulse;
[Header("Death")]
[Tooltip("How long to let physics play after the blast before destroying Zibus.")]
public float killDelay = 0.7f;
[Header("Filters (optional)")]
[Tooltip("Only Rigidbodies on these layers will be pushed. Leave as ~0 (Everything) to affect all.")]
public LayerMask pushMask = ~0;
[Tooltip("Layers considered 'Zibu bodies' for killing. Leave as ~0 (Everything) if you use tags/components instead.")]
public LayerMask zibuMask = ~0;
public GameObject bombMesh;
public GameObject bombFX;
void Start()
{
StartCoroutine(ExplodeRoutine());
}
IEnumerator ExplodeRoutine()
{
//transform.DOJump(Vector3.zero + new Vector3(Random.Range(-1.5f, 1.5f),1, Random.Range(-1.5f, 1.5f)),2,1,1);
transform.DOMove(Vector3.zero + new Vector3(Random.Range(-1.5f, 1.5f), 1, Random.Range(-1.5f, 1.5f)), 1).SetEase(Ease.Linear);
yield return new WaitForSeconds(countdown+1);
bombMesh.SetActive(false);
bombFX.SetActive(true);
// 1) Gather colliders in radius
Collider[] cols = Physics.OverlapSphere(transform.position, radius, pushMask, QueryTriggerInteraction.Ignore);
// 2) Unique sets of rigidbodies to push and zibus to kill
HashSet<Rigidbody> bodies = new HashSet<Rigidbody>();
HashSet<GameObject> zibusToKill = new HashSet<GameObject>();
foreach (var col in cols)
{
if (!col) continue;
// Collect rigidbodies to push
if (col.attachedRigidbody != null)
bodies.Add(col.attachedRigidbody);
// Collect Zibus to kill (by tag OR by component on self/parent)
if (col.CompareTag("Zibu"))
{
zibusToKill.Add(col.transform.root.gameObject);
}
else
{
var ctrl = col.GetComponentInParent<CubeClash_ZibuController>();
if (ctrl) zibusToKill.Add(ctrl.gameObject);
}
}
// 3) Apply explosion force to all rigidbodies
foreach (var rb in bodies)
{
// If you froze X/Z rotation on your character RBs, clear it so they can tumble from the blast
if ((rb.constraints & (RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ)) != 0)
rb.constraints &= ~(RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ);
rb.AddExplosionForce(explosionForce, transform.position, radius, upwardModifier, forceMode);
}
// 4) Put Zibus into a brief "ragdoll" / inert state so the shove reads clearly
foreach (var go in zibusToKill)
{
var ctrl = go.GetComponent<CubeClash_ZibuController>();
if (ctrl)
{
// Stop their control for the duration
ctrl.ApplyPushStun(Mathf.Max(killDelay, 0.25f));
// If they have an AI, disable it so it doesn't cancel the shove
var ai = go.GetComponent<CubeClash_ZibuAI>();
if (ai) ai.enabled = false;
// Optional: close any open parachute
ctrl.ForceCloseParachute();
}
var rb = go.GetComponent<Rigidbody>();
if (rb)
{
// Ensure physics is active and allowed to spin
rb.isKinematic = false;
rb.useGravity = true;
rb.constraints &= ~(RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ);
}
}
// 5) Wait, then delete the Zibus
yield return new WaitForSeconds(killDelay);
foreach (var go in zibusToKill)
{
if (go) Destroy(go);
}
// 6) Finally destroy the bomb object
Destroy(gameObject);
}
// Optional: visualize radius in Scene view
void OnDrawGizmosSelected()
{
Gizmos.color = new Color(1f, 0.5f, 0f, 0.35f);
Gizmos.DrawSphere(transform.position, radius);
Gizmos.color = new Color(1f, 0.5f, 0f, 1f);
Gizmos.DrawWireSphere(transform.position, radius);
}
}
//using UnityEngine;
//using System.Collections;
//public class CubeClash_TimeBomb : MonoBehaviour
//{
// public float countdown = 3f;
// public float radius = 3f;
// void Start()
// {
// StartCoroutine(ExplodeRoutine());
// }
// IEnumerator ExplodeRoutine()
// {
// yield return new WaitForSeconds(countdown);
// Collider[] cols = Physics.OverlapSphere(transform.position, radius);
// foreach (Collider col in cols)
// {
// if (col.CompareTag("Zibu"))
// {
// Destroy(col.gameObject);
// }
// }
// Destroy(gameObject);
// }
//}