using System.Collections.Generic; using GazeLab.Data; using UnityEngine; namespace GazeLab.Combat { public sealed class ShockwaveExplosionController : MonoBehaviour { private static readonly Queue explosionPool = new Queue(); private static readonly List activeExplosions = new List(); private SpriteRenderer primaryRenderer; private SpriteRenderer secondaryCoreRenderer; private CentroidOpacityProfile profile; private MaterialPropertyBlock propBlock; private float elapsed; private float duration = 0.45f; private float baseScale = 0.85f; private float rotationSpeedDeg = 95f; private bool isVoidGravityVortex; private float vortexPullRadius = 2.4f; private float vortexPullForce = 3.5f; public static ShockwaveExplosionController Spawn( Vector3 worldPos, Sprite explosionSprite, CentroidOpacityProfile centroidProfile, float scale = 0.85f, Color? tintOverride = null, bool voidGravityPull = false, float pullRadius = 2.5f) { ShockwaveExplosionController ctrl = null; while (explosionPool.Count > 0 && ctrl == null) { ctrl = explosionPool.Dequeue(); } if (ctrl == null) { GameObject go = new GameObject("ShockwaveExplosion"); ctrl = go.AddComponent(); } ctrl.transform.position = worldPos; ctrl.gameObject.SetActive(true); ctrl.Initialize(explosionSprite, centroidProfile, scale, tintOverride ?? Color.white, voidGravityPull, pullRadius); activeExplosions.Add(ctrl); return ctrl; } public static void RecycleAllActive() { var arr = activeExplosions.ToArray(); for (int i = 0; i < arr.Length; i++) { if (arr[i] != null) { arr[i].Recycle(); } } activeExplosions.Clear(); } public void Recycle() { if (!gameObject.activeSelf) return; activeExplosions.Remove(this); gameObject.SetActive(false); explosionPool.Enqueue(this); } private void Initialize( Sprite sprite, CentroidOpacityProfile centroidProfile, float scale, Color tint, bool voidVortex, float pullRad) { elapsed = 0f; if (propBlock == null) propBlock = new MaterialPropertyBlock(); profile = centroidProfile; baseScale = Mathf.Max(0.1f, scale); duration = centroidProfile != null ? Mathf.Max(0.15f, centroidProfile.effectDuration) : (voidVortex ? 1.8f : 0.42f); isVoidGravityVortex = voidVortex; vortexPullRadius = pullRad; rotationSpeedDeg = voidVortex ? -220f : Random.Range(-110f, 110f); if (primaryRenderer == null) { primaryRenderer = gameObject.AddComponent(); primaryRenderer.sharedMaterial = CentroidShieldRenderer.GetSharedCentroidMaterial(); } primaryRenderer.sprite = sprite != null ? sprite : GetFallbackGlowSprite(); primaryRenderer.sortingOrder = 22; primaryRenderer.color = tint; // Secondary counter-rotating core layer for extra depth if (secondaryCoreRenderer == null) { GameObject coreGo = new GameObject("InnerCoreFlash"); coreGo.transform.SetParent(transform, false); coreGo.transform.localScale = Vector3.one * 0.62f; secondaryCoreRenderer = coreGo.AddComponent(); secondaryCoreRenderer.sharedMaterial = CentroidShieldRenderer.GetSharedCentroidMaterial(); } secondaryCoreRenderer.sprite = primaryRenderer.sprite; secondaryCoreRenderer.sortingOrder = 23; secondaryCoreRenderer.color = new Color(1f, 0.95f, 0.85f, 0.85f); transform.localRotation = Quaternion.Euler(0f, 0f, Random.Range(0f, 360f)); UpdateVisuals(0f); } private void Update() { elapsed += Time.deltaTime; float t = Mathf.Clamp01(elapsed / duration); transform.Rotate(0f, 0f, rotationSpeedDeg * Time.deltaTime); if (secondaryCoreRenderer != null) { secondaryCoreRenderer.transform.Rotate(0f, 0f, -rotationSpeedDeg * 1.6f * Time.deltaTime); } if (isVoidGravityVortex && t < 0.9f) { ApplyVoidVortexPull(); } UpdateVisuals(t); if (elapsed >= duration) { Recycle(); } } private void ApplyVoidVortexPull() { var targets = DamageableEntity.ActiveEntities; Vector3 center = transform.position; float radSq = vortexPullRadius * vortexPullRadius; for (int i = 0; i < targets.Count; i++) { DamageableEntity ent = targets[i]; if (ent == null || ent.IsPlayerStation || !ent.IsAlive) continue; Vector3 diff = center - ent.transform.position; diff.z = 0f; float dSq = diff.sqrMagnitude; if (dSq > 0.02f && dSq <= radSq) { ent.transform.position += diff.normalized * (vortexPullForce * Time.deltaTime); } } } private void UpdateVisuals(float t) { float scaleMult = profile != null ? profile.scaleOverLifetime.Evaluate(t) : (isVoidGravityVortex ? Mathf.Sin(t * Mathf.PI) * 1.15f : Mathf.Lerp(0.25f, 1.15f, Mathf.Sqrt(t))); float alpha = profile != null ? profile.alphaOverLifetime.Evaluate(t) : (1f - t * t); transform.localScale = Vector3.one * (baseScale * Mathf.Max(0.05f, scaleMult)); if (primaryRenderer != null) { Color c = profile != null ? profile.tintColor : primaryRenderer.color; c.a = Mathf.Clamp01(alpha); primaryRenderer.color = c; primaryRenderer.GetPropertyBlock(propBlock); if (profile != null) { profile.ApplyToPropertyBlock(propBlock, primaryRenderer.sprite, new Vector2(0.5f, 0.5f), 0f, t); } else { propBlock.SetVector("_SpriteUVRect", CentroidOpacityProfile.CalculateSpriteUVRect(primaryRenderer.sprite)); propBlock.SetVector("_CentroidUV", new Vector4(0.5f, 0.5f, 0f, 0f)); propBlock.SetFloat("_InnerRadius", Mathf.Lerp(0.0f, 0.35f, t)); propBlock.SetFloat("_OuterRadius", 0.48f); propBlock.SetFloat("_OuterFadeWidth", 0.06f); propBlock.SetFloat("_CenterAlpha", isVoidGravityVortex ? 0.1f : Mathf.Lerp(0.85f, 0.0f, t)); propBlock.SetFloat("_EdgeAlpha", 1.0f); propBlock.SetFloat("_FalloffPower", 1.5f); propBlock.SetFloat("_DarkKeyThreshold", 0.14f); propBlock.SetFloat("_DarkKeySoftness", 0.16f); propBlock.SetFloat("_EmissionBoost", 1.45f); } primaryRenderer.SetPropertyBlock(propBlock); } if (secondaryCoreRenderer != null) { Color cc = secondaryCoreRenderer.color; cc.a = Mathf.Clamp01((1f - t) * 0.85f); secondaryCoreRenderer.color = cc; secondaryCoreRenderer.GetPropertyBlock(propBlock); propBlock.SetVector("_SpriteUVRect", CentroidOpacityProfile.CalculateSpriteUVRect(secondaryCoreRenderer.sprite)); propBlock.SetFloat("_DarkKeyThreshold", 0.14f); propBlock.SetFloat("_DarkKeySoftness", 0.16f); propBlock.SetFloat("_CenterAlpha", 0.5f * (1f - t)); propBlock.SetFloat("_EdgeAlpha", 0.9f * (1f - t)); secondaryCoreRenderer.SetPropertyBlock(propBlock); } } private static Sprite fallbackGlowSprite; public static Sprite GetFallbackGlowSprite() { if (fallbackGlowSprite != null) return fallbackGlowSprite; const int size = 64; Texture2D tex = new Texture2D(size, size, TextureFormat.RGBA32, false) { wrapMode = TextureWrapMode.Clamp, filterMode = FilterMode.Bilinear }; Vector2 center = new Vector2((size - 1) * 0.5f, (size - 1) * 0.5f); float maxR = size * 0.5f; for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { float d = Vector2.Distance(new Vector2(x, y), center) / maxR; float ring = Mathf.Clamp01(1f - Mathf.Abs(d - 0.6f) * 3.2f); float core = Mathf.Clamp01(1f - d); float a = Mathf.Clamp01(ring * 0.85f + core * core * 0.5f); tex.SetPixel(x, y, new Color(1f, 1f, 1f, a)); } } tex.Apply(); fallbackGlowSprite = Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(0.5f, 0.5f), 64f); return fallbackGlowSprite; } } }