206 lines
6.8 KiB
C#
206 lines
6.8 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace GazeLab.UI
|
|
{
|
|
public enum SciFiBarType
|
|
{
|
|
StationHull,
|
|
StationShield,
|
|
BossHealth,
|
|
Generic
|
|
}
|
|
|
|
/// <summary>
|
|
/// Procedural sci-fi energy bar controller that animates dynamic traveling plasma waves,
|
|
/// a damage ghost lag bar, neon crest highlights, and low-health danger strobes via SciFiEnergyBar.shader.
|
|
/// </summary>
|
|
[RequireComponent(typeof(Image))]
|
|
public sealed class SciFiEnergyBar : MonoBehaviour
|
|
{
|
|
private static readonly int propFill = Shader.PropertyToID("_FillAmount");
|
|
private static readonly int propGhost = Shader.PropertyToID("_GhostFillAmount");
|
|
private static readonly int propDanger = Shader.PropertyToID("_DangerPulse");
|
|
private static readonly int propColor = Shader.PropertyToID("_Color");
|
|
private static readonly int propSecondary = Shader.PropertyToID("_SecondaryColor");
|
|
private static readonly int propGlow = Shader.PropertyToID("_GlowColor");
|
|
private static readonly int propWaveSpeed = Shader.PropertyToID("_WaveSpeed");
|
|
|
|
public SciFiBarType barType = SciFiBarType.StationHull;
|
|
|
|
[Tooltip("Optional custom shader. If null, automatically finds 'GazeLab/UI/SciFiEnergyBar'.")]
|
|
public Shader energyBarShader;
|
|
[Tooltip("Optional custom material preset.")]
|
|
public Material customMaterial;
|
|
|
|
[Range(0f, 1f)]
|
|
public float targetFillRatio = 1f;
|
|
|
|
private Image targetImage;
|
|
private Material barMaterial;
|
|
private float currentFill = 1f;
|
|
private float ghostFill = 1f;
|
|
private float ghostDelayTimer = 0f;
|
|
private bool isMaterialInitialized = false;
|
|
|
|
private void Awake()
|
|
{
|
|
if (targetImage == null)
|
|
{
|
|
targetImage = GetComponent<Image>();
|
|
}
|
|
EnsureMaterial();
|
|
}
|
|
|
|
private void EnsureMaterial()
|
|
{
|
|
if (targetImage == null)
|
|
{
|
|
targetImage = GetComponent<Image>();
|
|
}
|
|
if (targetImage == null) return;
|
|
if (isMaterialInitialized && barMaterial != null) return;
|
|
|
|
if (customMaterial != null)
|
|
{
|
|
barMaterial = new Material(customMaterial);
|
|
barMaterial.name = $"{gameObject.name}_EnergyBar_Mat";
|
|
targetImage.material = barMaterial;
|
|
targetImage.type = Image.Type.Simple;
|
|
isMaterialInitialized = true;
|
|
return;
|
|
}
|
|
|
|
Shader shader = energyBarShader != null ? energyBarShader : Shader.Find("GazeLab/UI/SciFiEnergyBar");
|
|
if (shader == null)
|
|
{
|
|
shader = Shader.Find("UI/Default");
|
|
}
|
|
|
|
if (shader != null)
|
|
{
|
|
barMaterial = new Material(shader);
|
|
barMaterial.name = $"{gameObject.name}_EnergyBar_Mat";
|
|
targetImage.material = barMaterial;
|
|
targetImage.type = Image.Type.Simple; // Full quad allows shader to draw complete bar, ghost & track
|
|
isMaterialInitialized = true;
|
|
}
|
|
}
|
|
|
|
public void ConfigureAsHull()
|
|
{
|
|
barType = SciFiBarType.StationHull;
|
|
EnsureMaterial();
|
|
if (barMaterial == null) return;
|
|
|
|
barMaterial.SetColor(propColor, new Color(0.18f, 0.88f, 0.42f, 1f));
|
|
barMaterial.SetColor(propSecondary, new Color(0.48f, 1.0f, 0.78f, 1f));
|
|
barMaterial.SetColor(propGlow, new Color(0.85f, 1.0f, 0.95f, 1f));
|
|
barMaterial.SetFloat(propWaveSpeed, 3.0f);
|
|
}
|
|
|
|
public void ConfigureAsShield()
|
|
{
|
|
barType = SciFiBarType.StationShield;
|
|
EnsureMaterial();
|
|
if (barMaterial == null) return;
|
|
|
|
barMaterial.SetColor(propColor, new Color(0.16f, 0.62f, 1.0f, 1f));
|
|
barMaterial.SetColor(propSecondary, new Color(0.45f, 0.92f, 1.0f, 1f));
|
|
barMaterial.SetColor(propGlow, new Color(0.90f, 0.98f, 1.0f, 1f));
|
|
barMaterial.SetFloat(propWaveSpeed, 4.8f);
|
|
}
|
|
|
|
public void ConfigureAsBoss()
|
|
{
|
|
barType = SciFiBarType.BossHealth;
|
|
EnsureMaterial();
|
|
if (barMaterial == null) return;
|
|
|
|
barMaterial.SetColor(propColor, new Color(0.95f, 0.18f, 0.22f, 1f));
|
|
barMaterial.SetColor(propSecondary, new Color(1.0f, 0.58f, 0.16f, 1f));
|
|
barMaterial.SetColor(propGlow, new Color(1.0f, 0.92f, 0.75f, 1f));
|
|
barMaterial.SetFloat(propWaveSpeed, 3.2f);
|
|
}
|
|
|
|
public void SetFill(float ratio, bool instant = false)
|
|
{
|
|
ratio = Mathf.Clamp01(ratio);
|
|
|
|
if (instant)
|
|
{
|
|
targetFillRatio = ratio;
|
|
currentFill = ratio;
|
|
ghostFill = ratio;
|
|
ghostDelayTimer = 0f;
|
|
}
|
|
else
|
|
{
|
|
if (ratio < targetFillRatio)
|
|
{
|
|
// Damage taken: hold ghost bar for catch-up effect
|
|
ghostDelayTimer = 0.35f;
|
|
}
|
|
else if (ratio > targetFillRatio)
|
|
{
|
|
// Healed / recharged: move ghost up immediately
|
|
ghostFill = ratio;
|
|
}
|
|
targetFillRatio = ratio;
|
|
}
|
|
|
|
if (barMaterial == null && targetImage != null)
|
|
{
|
|
targetImage.fillAmount = ratio;
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (targetImage == null) return;
|
|
|
|
// Smooth fill transition
|
|
float dt = Time.unscaledDeltaTime;
|
|
currentFill = Mathf.MoveTowards(currentFill, targetFillRatio, dt * 8f);
|
|
|
|
// Ghost damage lag catch-up
|
|
if (ghostDelayTimer > 0f)
|
|
{
|
|
ghostDelayTimer -= dt;
|
|
}
|
|
else
|
|
{
|
|
ghostFill = Mathf.MoveTowards(ghostFill, currentFill, dt * 2.2f);
|
|
}
|
|
|
|
if (barMaterial != null)
|
|
{
|
|
barMaterial.SetFloat(propFill, currentFill);
|
|
barMaterial.SetFloat(propGhost, ghostFill);
|
|
|
|
// Low health alert pulse
|
|
float danger = 0f;
|
|
if (barType == SciFiBarType.StationHull && currentFill < 0.28f)
|
|
{
|
|
danger = Mathf.Clamp01((0.28f - currentFill) / 0.28f);
|
|
}
|
|
barMaterial.SetFloat(propDanger, danger);
|
|
}
|
|
else
|
|
{
|
|
targetImage.fillAmount = currentFill;
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (barMaterial != null)
|
|
{
|
|
Destroy(barMaterial);
|
|
barMaterial = null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|