MiniGames/Assets/Scripts/CubeClash/CubeClash_Blueberry.cs

176 lines
5.4 KiB
C#

using UnityEngine;
using System.Collections;
using DG.Tweening;
public class CubeClash_Blueberry : MonoBehaviour
{
[Header("Buff Settings")]
public float buffDuration = 5f;
[Tooltip("Multiply forward dash impulse (e.g., 1.4 = +40%).")]
public float impulseMultiplier = 1.4f;
[Tooltip("Multiply bump cooldown (e.g., 0.7 = 30% faster).")]
public float cooldownMultiplier = 0.7f;
[Tooltip("Optional: increase momentum transfer (e.g., +0.2 adds on top).")]
public float extraMomentumTransfer = 0.2f;
[Header("Glow (last material slot)")]
[Tooltip("Base emissive color (set Emission ON in the last material).")]
public Color emissionBaseColor = Color.white;
[Tooltip("Min/Max intensity multiplier applied to emission color.")]
public float minEmission = 0.0f;
public float maxEmission = 2.5f;
[Tooltip("Seconds for one side of the pulse (full cycle = 2x).")]
public float pulseDuration = 0.8f;
[Tooltip("Use MaterialPropertyBlock (no material instancing).")]
public bool usePropertyBlock = true;
// --- runtime glow refs ---
private Renderer _rend;
private int _submeshIndex = -1;
private Material _instancedMat; // only if not using MPB
private MaterialPropertyBlock _mpb; // only if using MPB
private float _emissionIntensity;
private Tween _glowTween;
private static readonly int EmissionColorID = Shader.PropertyToID("_EmissionColor"); // Standard/URP Lit
private static readonly int EmissiveColorID = Shader.PropertyToID("_EmissiveColor"); // Some HDRP/URP variants
private void Awake()
{
_rend = GetComponentInChildren<Renderer>();
}
private void OnEnable()
{
// Your existing jump (kept)
//transform.DOJump(
// Vector3.zero + new Vector3(Random.Range(-1.5f, 1.5f), 0.55f, Random.Range(-1.5f, 1.5f)),
// 2f, 1, 1f
//).SetTarget(this);
transform.DOMove(Vector3.zero + new Vector3(Random.Range(-1.5f, 1.5f), 0.55f, Random.Range(-1.5f, 1.5f)), 1).SetEase(Ease.Linear);
SetupGlowTarget();
StartGlow();
}
private void OnDisable()
{
StopGlow();
}
private void SetupGlowTarget()
{
if (_rend == null) return;
// last material slot
_submeshIndex = Mathf.Max(0, _rend.sharedMaterials.Length - 1);
if (usePropertyBlock)
{
if (_mpb == null) _mpb = new MaterialPropertyBlock();
_rend.GetPropertyBlock(_mpb, _submeshIndex);
// Emission keyword should already be enabled on the material (as you said).
// We drive the color through MPB.
}
else
{
// Make sure we edit this instance only (not shared material)
var mats = _rend.materials; // instantiates per-renderer materials
if (mats != null && mats.Length > 0)
{
_instancedMat = mats[_submeshIndex];
if (_instancedMat != null)
{
_instancedMat.EnableKeyword("_EMISSION");
}
}
}
}
private void StartGlow()
{
if (_rend == null) return;
_emissionIntensity = minEmission;
ApplyEmission(_emissionIntensity);
_glowTween = DOTween.To(
() => _emissionIntensity,
val => { _emissionIntensity = val; ApplyEmission(_emissionIntensity); },
maxEmission,
pulseDuration
)
.SetEase(Ease.InOutSine)
.SetLoops(-1, LoopType.Yoyo)
.SetTarget(this);
}
private void StopGlow()
{
if (_glowTween != null && _glowTween.IsActive())
_glowTween.Kill();
_glowTween = null;
}
private void ApplyEmission(float intensity)
{
// Multiply base emissive color by intensity
Color emissive = emissionBaseColor * Mathf.Max(0f, intensity);
if (usePropertyBlock)
{
if (_mpb == null || _rend == null) return;
_mpb.SetColor(EmissionColorID, emissive);
_mpb.SetColor(EmissiveColorID, emissive); // fallback for alt shaders
_rend.SetPropertyBlock(_mpb, _submeshIndex);
}
else
{
if (_instancedMat == null) return;
_instancedMat.SetColor(EmissionColorID, emissive);
_instancedMat.SetColor(EmissiveColorID, emissive); // fallback
}
}
private void OnValidate()
{
maxEmission = Mathf.Max(minEmission, maxEmission);
pulseDuration = Mathf.Max(0.05f, pulseDuration);
}
private void OnTriggerEnter(Collider other)
{
var zibu = other.GetComponent<CubeClash_ZibuController>();
if (zibu == null) return;
StartCoroutine(ApplyBuff(zibu));
Destroy(gameObject);
}
private IEnumerator ApplyBuff(CubeClash_ZibuController zibu)
{
// cache old values
float oldImpulse = zibu.dashImpulse;
float oldCooldown = zibu.bumpCooldown;
float oldTransfer = zibu.momentumTransferScale;
// apply buffs
zibu.dashImpulse = oldImpulse * impulseMultiplier;
zibu.bumpCooldown = oldCooldown * cooldownMultiplier;
zibu.momentumTransferScale = oldTransfer + extraMomentumTransfer;
yield return new WaitForSeconds(buffDuration);
// revert
zibu.dashImpulse = oldImpulse;
zibu.bumpCooldown = oldCooldown;
zibu.momentumTransferScale = oldTransfer;
}
}