281 lines
9.9 KiB
C#
281 lines
9.9 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace GazeLab.Combat
|
|
{
|
|
/// <summary>
|
|
/// Procedurally generates an interlocking Chain-Link texture and renders taut or vibrating
|
|
/// Harpoon Chains and Tesla Lightning Arcs between entities with object pooling.
|
|
/// </summary>
|
|
public sealed class ProceduralChainRenderer : MonoBehaviour
|
|
{
|
|
private static readonly Queue<ProceduralChainRenderer> chainPool = new Queue<ProceduralChainRenderer>();
|
|
private static readonly List<ProceduralChainRenderer> activeChains = new List<ProceduralChainRenderer>();
|
|
|
|
private LineRenderer lineRenderer;
|
|
private Transform originTransform;
|
|
private Transform targetTransform;
|
|
private Vector3 originLocalOffset;
|
|
private float remainingDuration;
|
|
private bool isFullyLocked;
|
|
private bool isLightningArc;
|
|
|
|
private static Material sharedChainMaterial;
|
|
private static Material sharedLightningMaterial;
|
|
|
|
public static Material GetSharedChainMaterial()
|
|
{
|
|
if (sharedChainMaterial != null) return sharedChainMaterial;
|
|
|
|
const int width = 64;
|
|
const int height = 32;
|
|
Texture2D tex = new Texture2D(width, height, TextureFormat.RGBA32, false)
|
|
{
|
|
wrapMode = TextureWrapMode.Repeat,
|
|
filterMode = FilterMode.Bilinear
|
|
};
|
|
|
|
for (int y = 0; y < height; y++)
|
|
{
|
|
float ny = (y / (float)(height - 1)) * 2f - 1f;
|
|
for (int x = 0; x < width; x++)
|
|
{
|
|
float nx = x / (float)width;
|
|
bool firstHalf = nx < 0.55f;
|
|
float localX = firstHalf ? (nx / 0.55f) * 2f - 1f : ((nx - 0.45f) / 0.55f) * 2f - 1f;
|
|
|
|
float ovalDist = Mathf.Sqrt(localX * localX * 0.85f + ny * ny * 1.45f);
|
|
float linkRing = 1f - Mathf.Clamp01(Mathf.Abs(ovalDist - 0.62f) / 0.24f);
|
|
|
|
float centerBar = (Mathf.Abs(ny) < 0.24f && (nx > 0.38f && nx < 0.62f || nx < 0.12f || nx > 0.88f))
|
|
? (1f - Mathf.Abs(ny) / 0.24f)
|
|
: 0f;
|
|
|
|
float alpha = Mathf.Clamp01(Mathf.Max(linkRing, centerBar));
|
|
float highlight = Mathf.Clamp01(1f - Mathf.Abs(ovalDist - 0.58f) / 0.10f) * 0.45f;
|
|
Color c = new Color(
|
|
0.55f + highlight,
|
|
0.78f + highlight * 0.6f,
|
|
0.95f + highlight * 0.4f,
|
|
alpha);
|
|
tex.SetPixel(x, y, c);
|
|
}
|
|
}
|
|
tex.Apply();
|
|
|
|
Shader sh = Shader.Find("Sprites/Default");
|
|
sharedChainMaterial = new Material(sh)
|
|
{
|
|
mainTexture = tex,
|
|
hideFlags = HideFlags.DontSave
|
|
};
|
|
return sharedChainMaterial;
|
|
}
|
|
|
|
public static Material GetSharedLightningMaterial()
|
|
{
|
|
if (sharedLightningMaterial != null) return sharedLightningMaterial;
|
|
Shader sh = Shader.Find("Sprites/Default");
|
|
sharedLightningMaterial = new Material(sh) { hideFlags = HideFlags.DontSave };
|
|
return sharedLightningMaterial;
|
|
}
|
|
|
|
private static ProceduralChainRenderer GetFromPool(string name)
|
|
{
|
|
ProceduralChainRenderer inst = null;
|
|
while (chainPool.Count > 0 && inst == null)
|
|
{
|
|
inst = chainPool.Dequeue();
|
|
}
|
|
if (inst == null)
|
|
{
|
|
GameObject go = new GameObject(name);
|
|
inst = go.AddComponent<ProceduralChainRenderer>();
|
|
}
|
|
else
|
|
{
|
|
inst.gameObject.name = name;
|
|
}
|
|
inst.gameObject.SetActive(true);
|
|
if (!activeChains.Contains(inst))
|
|
activeChains.Add(inst);
|
|
return inst;
|
|
}
|
|
|
|
public static void RecycleAllActive()
|
|
{
|
|
var arr = activeChains.ToArray();
|
|
for (int i = 0; i < arr.Length; i++)
|
|
{
|
|
if (arr[i] != null)
|
|
{
|
|
arr[i].Recycle();
|
|
}
|
|
}
|
|
activeChains.Clear();
|
|
}
|
|
|
|
public void Recycle()
|
|
{
|
|
if (!gameObject.activeSelf) return;
|
|
activeChains.Remove(this);
|
|
originTransform = null;
|
|
targetTransform = null;
|
|
gameObject.SetActive(false);
|
|
chainPool.Enqueue(this);
|
|
}
|
|
|
|
public static ProceduralChainRenderer AttachHarpoonChain(
|
|
Transform stationOrigin,
|
|
Vector3 localOffset,
|
|
Transform target,
|
|
float duration,
|
|
bool fullyLocked)
|
|
{
|
|
var chain = GetFromPool("HarpoonChainLink");
|
|
chain.SetupChain(stationOrigin, localOffset, target, duration, fullyLocked);
|
|
return chain;
|
|
}
|
|
|
|
public static void SpawnElectricArc(Vector3 from, Vector3 to, Color arcColor, float duration = 0.18f)
|
|
{
|
|
var arc = GetFromPool("ElectricChainArc");
|
|
arc.SetupLightning(from, to, arcColor, duration);
|
|
}
|
|
|
|
private void EnsureLineRenderer()
|
|
{
|
|
if (lineRenderer == null)
|
|
{
|
|
lineRenderer = GetComponent<LineRenderer>();
|
|
if (lineRenderer == null)
|
|
{
|
|
lineRenderer = gameObject.AddComponent<LineRenderer>();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SetupChain(Transform origin, Vector3 localOffset, Transform target, float duration, bool locked)
|
|
{
|
|
originTransform = origin;
|
|
originLocalOffset = localOffset;
|
|
targetTransform = target;
|
|
remainingDuration = duration;
|
|
isFullyLocked = locked;
|
|
isLightningArc = false;
|
|
|
|
EnsureLineRenderer();
|
|
lineRenderer.sharedMaterial = GetSharedChainMaterial();
|
|
lineRenderer.textureMode = LineTextureMode.Tile;
|
|
lineRenderer.sortingOrder = 14;
|
|
lineRenderer.positionCount = 8;
|
|
lineRenderer.startWidth = 0.18f;
|
|
lineRenderer.endWidth = 0.18f;
|
|
Color chainCol = locked ? new Color(0.45f, 0.95f, 1f, 0.95f) : new Color(1f, 0.72f, 0.3f, 0.88f);
|
|
lineRenderer.startColor = chainCol;
|
|
lineRenderer.endColor = chainCol;
|
|
UpdateChainPositions();
|
|
}
|
|
|
|
private void SetupLightning(Vector3 from, Vector3 to, Color color, float duration)
|
|
{
|
|
isLightningArc = true;
|
|
remainingDuration = duration;
|
|
EnsureLineRenderer();
|
|
lineRenderer.sharedMaterial = GetSharedLightningMaterial();
|
|
lineRenderer.sortingOrder = 25;
|
|
lineRenderer.startWidth = 0.11f;
|
|
lineRenderer.endWidth = 0.05f;
|
|
lineRenderer.startColor = color;
|
|
lineRenderer.endColor = new Color(color.r, color.g, color.b, 0.4f);
|
|
|
|
const int segs = 7;
|
|
lineRenderer.positionCount = segs;
|
|
Vector3 dir = to - from;
|
|
Vector3 perp = new Vector3(-dir.y, dir.x, 0f).normalized;
|
|
for (int i = 0; i < segs; i++)
|
|
{
|
|
float t = i / (float)(segs - 1);
|
|
Vector3 pt = Vector3.Lerp(from, to, t);
|
|
if (i > 0 && i < segs - 1)
|
|
{
|
|
pt += perp * Random.Range(-0.22f, 0.22f);
|
|
}
|
|
lineRenderer.SetPosition(i, pt);
|
|
}
|
|
}
|
|
|
|
public void SetLockState(bool locked)
|
|
{
|
|
isFullyLocked = locked;
|
|
if (lineRenderer != null)
|
|
{
|
|
Color chainCol = locked ? new Color(0.45f, 0.95f, 1f, 0.95f) : new Color(1f, 0.72f, 0.3f, 0.88f);
|
|
lineRenderer.startColor = chainCol;
|
|
lineRenderer.endColor = chainCol;
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
remainingDuration -= Time.deltaTime;
|
|
if (remainingDuration <= 0f)
|
|
{
|
|
Recycle();
|
|
return;
|
|
}
|
|
|
|
if (isLightningArc)
|
|
{
|
|
if (lineRenderer != null)
|
|
{
|
|
Color c = lineRenderer.startColor;
|
|
c.a = Mathf.Clamp01(remainingDuration * 6f);
|
|
lineRenderer.startColor = c;
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (originTransform == null || targetTransform == null || !targetTransform.gameObject.activeInHierarchy)
|
|
{
|
|
Recycle();
|
|
return;
|
|
}
|
|
|
|
UpdateChainPositions();
|
|
}
|
|
|
|
private void UpdateChainPositions()
|
|
{
|
|
if (lineRenderer == null || originTransform == null || targetTransform == null) return;
|
|
|
|
Vector3 start = originTransform.TransformPoint(originLocalOffset);
|
|
Vector3 end = targetTransform.position;
|
|
start.z = 0f;
|
|
end.z = 0f;
|
|
|
|
Vector3 diff = end - start;
|
|
float dist = diff.magnitude;
|
|
Vector3 perp = dist > 0.01f ? new Vector3(-diff.y, diff.x, 0f) / dist : Vector3.right;
|
|
|
|
int count = lineRenderer.positionCount;
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
float t = i / (float)(count - 1);
|
|
Vector3 pt = Vector3.Lerp(start, end, t);
|
|
if (!isFullyLocked && i > 0 && i < count - 1)
|
|
{
|
|
float wave = Mathf.Sin(Time.time * 38f + i * 1.7f) * 0.07f;
|
|
pt += perp * wave;
|
|
}
|
|
lineRenderer.SetPosition(i, pt);
|
|
}
|
|
|
|
if (lineRenderer.sharedMaterial != null)
|
|
{
|
|
lineRenderer.sharedMaterial.mainTextureScale = new Vector2(Mathf.Max(1f, dist * 3.2f), 1f);
|
|
}
|
|
}
|
|
}
|
|
}
|