204 lines
6.8 KiB
C#
204 lines
6.8 KiB
C#
using UnityEngine;
|
||
using System.Collections.Generic;
|
||
|
||
namespace AibisDream.MiniGame.Language
|
||
{
|
||
/// <summary>
|
||
/// 候选粒子(可交互的主要粒子)
|
||
/// </summary>
|
||
public class CandidateParticle : TextParticle
|
||
{
|
||
[Header("粒子状态")]
|
||
public bool isRed = false; // 是否是红色目标粒子
|
||
public bool isOrange = false; // 是否受到效果影响(橙色)
|
||
public bool originalIsRed = false; // 原始是否为红色
|
||
public bool hasEffect = false; // 是否有效果
|
||
public bool isTemporaryRed = false; // 临时红色状态
|
||
|
||
[Header("连接")]
|
||
public List<CandidateParticle> connections = new List<CandidateParticle>();
|
||
|
||
[Header("效果")]
|
||
public float changeSpeedMultiplier = 1f;
|
||
public Vector2 vibrationOffset;
|
||
public float vibrationPhase;
|
||
public float effectTimer;
|
||
public float effectAlpha;
|
||
public float effectScale = 1f;
|
||
public float effectGlow;
|
||
|
||
[Header("焦虑效果")]
|
||
public float anxietyShakePhase;
|
||
public float anxietyShakeIntensity;
|
||
public float anxietyFloatPhase;
|
||
public float anxietyFloatSpeed;
|
||
public Vector2 anxietyShakeOffset;
|
||
public float anxietyFloatY;
|
||
|
||
[Header("波形关联")]
|
||
public float calmProgress = 0f; // 平静进度(0-1)
|
||
public float dissolveProgress = 0f; // 消散进度(0-1)
|
||
public WaveformPoint waveformPoint; // 关联的波形点
|
||
|
||
protected override void Awake()
|
||
{
|
||
base.Awake();
|
||
|
||
baseAlpha = 0.6f; // 0-1 范围的透明度
|
||
probability = Random.Range(0.1f, 1f);
|
||
alpha = baseAlpha + probability * 0.4f; // 最大 1.0
|
||
size = 3f;
|
||
velocity = new Vector2(Random.Range(-0.5f, 0.5f), Random.Range(-0.5f, 0.5f));
|
||
changeInterval = Random.Range(1f, 2.5f);
|
||
|
||
if (textMesh != null)
|
||
{
|
||
textMesh.fontSize = size;
|
||
}
|
||
}
|
||
|
||
protected override void Update()
|
||
{
|
||
base.Update();
|
||
|
||
// 更新效果动画
|
||
if (hasEffect)
|
||
{
|
||
UpdateEffectAnimation();
|
||
}
|
||
|
||
// 更新焦虑效果
|
||
if (isRed && (anxietyShakeIntensity > 0))
|
||
{
|
||
UpdateAnxietyEffect();
|
||
}
|
||
}
|
||
|
||
private void UpdateEffectAnimation()
|
||
{
|
||
if (effectTimer > 0)
|
||
{
|
||
effectTimer -= Time.deltaTime;
|
||
|
||
// 振动效果
|
||
vibrationPhase += Time.deltaTime * 15f;
|
||
float vibrationIntensity = 0.02f;
|
||
vibrationOffset = new Vector2(
|
||
Mathf.Cos(vibrationPhase) * vibrationIntensity,
|
||
Mathf.Sin(vibrationPhase * 1.3f) * vibrationIntensity
|
||
);
|
||
|
||
// 透明度动画(0-1 范围)
|
||
effectAlpha = Mathf.Lerp(0.6f, 1f, (Mathf.Sin(Time.time * 3f) + 1f) / 2f);
|
||
|
||
// 缩放动画
|
||
effectScale = Mathf.Lerp(0.95f, 1.05f, (Mathf.Sin(Time.time * 4.5f) + 1f) / 2f);
|
||
|
||
// 发光强度
|
||
effectGlow = Mathf.Lerp(0.06f, 0.1f, (effectAlpha - 0.6f) / 0.4f);
|
||
}
|
||
}
|
||
|
||
private void UpdateAnxietyEffect()
|
||
{
|
||
// 根据平静进度减少抖动强度
|
||
float shakeIntensity = anxietyShakeIntensity * (1 - calmProgress) * 0.01f;
|
||
float floatAmplitude = 0.03f * (1 - calmProgress);
|
||
|
||
if (calmProgress < 0.9f)
|
||
{
|
||
anxietyShakePhase += Time.deltaTime * 10f;
|
||
anxietyFloatPhase += anxietyFloatSpeed;
|
||
}
|
||
|
||
// 计算抖动偏移
|
||
anxietyShakeOffset = new Vector2(
|
||
Mathf.Cos(anxietyShakePhase) * shakeIntensity + Mathf.Sin(anxietyShakePhase * 1.7f) * shakeIntensity * 0.5f,
|
||
Mathf.Sin(anxietyShakePhase * 1.3f) * shakeIntensity + Mathf.Cos(anxietyShakePhase * 0.9f) * shakeIntensity * 0.5f
|
||
);
|
||
|
||
// 计算漂浮偏移
|
||
anxietyFloatY = Mathf.Sin(anxietyFloatPhase) * floatAmplitude;
|
||
}
|
||
|
||
protected override void UpdateVisuals()
|
||
{
|
||
if (textMesh == null) return;
|
||
|
||
// 计算显示位置
|
||
Vector3 displayOffset = (Vector3)(vibrationOffset + anxietyShakeOffset) + Vector3.up * anxietyFloatY;
|
||
textMesh.transform.localPosition = displayOffset;
|
||
|
||
// 计算颜色和透明度
|
||
Color color = Color.white;
|
||
float displayAlpha = alpha;
|
||
|
||
if (hasEffect && effectTimer > 0)
|
||
{
|
||
displayAlpha = effectAlpha;
|
||
textMesh.transform.localScale = Vector3.one * effectScale;
|
||
}
|
||
|
||
// 根据消散进度减少透明度
|
||
displayAlpha *= (1 - dissolveProgress);
|
||
|
||
if (isRed)
|
||
{
|
||
// 红色粒子:根据平静进度调整颜色
|
||
float r = Mathf.Lerp(1f, 0.78f, calmProgress);
|
||
float g = Mathf.Lerp(0.39f, 0.59f, calmProgress);
|
||
float b = Mathf.Lerp(0.39f, 0.59f, calmProgress);
|
||
color = new Color(r, g, b, displayAlpha); // displayAlpha 已经是 0-1 范围
|
||
}
|
||
else if (isOrange)
|
||
{
|
||
color = new Color(1f, 0.59f, 0.2f, displayAlpha);
|
||
}
|
||
else
|
||
{
|
||
color = new Color(0.2f, 0.78f, 1f, displayAlpha);
|
||
}
|
||
|
||
textMesh.color = color;
|
||
|
||
// 更新光晕
|
||
if (glowSprite != null)
|
||
{
|
||
glowSprite.color = new Color(color.r, color.g, color.b, color.a * 0.5f);
|
||
float glowScale = probability * 0.5f;
|
||
if (hasEffect)
|
||
glowScale += effectGlow;
|
||
glowSprite.transform.localScale = Vector3.one * glowScale;
|
||
}
|
||
}
|
||
|
||
public void InitializeAnxietyEffect()
|
||
{
|
||
anxietyShakePhase = Random.Range(0f, Mathf.PI * 2f);
|
||
anxietyShakeIntensity = Random.Range(2f, 4f);
|
||
anxietyFloatPhase = Random.Range(0f, Mathf.PI * 2f);
|
||
anxietyFloatSpeed = Random.Range(0.02f, 0.04f);
|
||
}
|
||
|
||
public void ApplyEffect(float duration = 1f)
|
||
{
|
||
hasEffect = true;
|
||
effectTimer = duration;
|
||
isOrange = true;
|
||
isRed = false;
|
||
changeSpeedMultiplier = 3f;
|
||
vibrationPhase = Random.Range(0f, Mathf.PI * 2f);
|
||
}
|
||
|
||
public void RemoveEffect()
|
||
{
|
||
hasEffect = false;
|
||
isOrange = false;
|
||
isRed = false;
|
||
changeSpeedMultiplier = 1f;
|
||
vibrationOffset = Vector2.zero;
|
||
effectTimer = 0;
|
||
}
|
||
}
|
||
}
|