364 lines
12 KiB
C#
364 lines
12 KiB
C#
using AibisDream.Framework;
|
|
using AibisDream.Kit;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering.Universal;
|
|
using DG.Tweening;
|
|
using System.Collections;
|
|
|
|
namespace AibisDream.FixSystem
|
|
{
|
|
/// <summary>
|
|
/// 灯光状态
|
|
/// </summary>
|
|
public enum LightState
|
|
{
|
|
Normal, // 正常状态
|
|
Warning, // 警告状态
|
|
Emergency // 紧急状态
|
|
}
|
|
|
|
/// <summary>
|
|
/// 维修面板光照系统 - 管理所有光照效果
|
|
/// </summary>
|
|
public class FixPanelLight : MonoBehaviour
|
|
{
|
|
#region 光照系统常量
|
|
|
|
private const float SYSTEM_OFF_ENVIRONMENT_INTENSITY = 0.4f;
|
|
private const float ALARM_LIGHT_MAX_INTENSITY = 4f;
|
|
private const float MIN_FLICKER_BLINK_COUNT = 2;
|
|
private const float MAX_FLICKER_BLINK_COUNT = 4;
|
|
private const float MIN_FLICKER_INTERVAL = 3f;
|
|
private const float MAX_FLICKER_INTERVAL = 8f;
|
|
|
|
#endregion
|
|
|
|
#region 光照系统字段
|
|
|
|
[Header("环境灯设置")]
|
|
[SerializeField] private Light2D environmentLight;
|
|
[SerializeField] private bool isEnvironmentLightFlickering = false;
|
|
[SerializeField] private float normalEnvironmentIntensity = 2f;
|
|
[SerializeField] private float warningEnvironmentIntensity = 2f;
|
|
[SerializeField] private float emergencyEnvironmentIntensity = 2f;
|
|
[SerializeField] private float brokenLightFlickerInterval = 0.1f;
|
|
[SerializeField] private float brokenLightFlickerDuration = 0.05f;
|
|
[SerializeField] private Color normalEnvironmentColor = Color.white;
|
|
[SerializeField] private Color warningEnvironmentColor = Color.yellow;
|
|
[SerializeField] private Color emergencyEnvironmentColor = Color.red;
|
|
|
|
[Header("面板灯设置")]
|
|
[SerializeField] private Light2D panelLight;
|
|
[SerializeField] private float panelLightIntensity = 0.3f;
|
|
[SerializeField] private float panelLightBlinkDuration = 0.12f;
|
|
[SerializeField] private int panelLightBlinkCount = 2;
|
|
[SerializeField] private Color panelLightColor = Color.white;
|
|
|
|
[Header("警报灯设置")]
|
|
[SerializeField] private Light2D alarmLight;
|
|
[SerializeField] private float alarmBlinkSpeed = 0.5f;
|
|
[SerializeField] private Color alarmLightColor = Color.red;
|
|
|
|
private Tween alarmTween;
|
|
private LightState currentState = LightState.Normal;
|
|
|
|
#endregion
|
|
|
|
#region 生命周期
|
|
|
|
private void OnDisable()
|
|
{
|
|
alarmTween?.Kill();
|
|
StopAllCoroutines();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 光照系统 - 公共方法
|
|
|
|
public void Initialize(bool isSystemOn)
|
|
{
|
|
panelLight.intensity = isSystemOn ? panelLightIntensity : 0f;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置灯光状态
|
|
/// </summary>
|
|
public void SetLightState(LightState newState)
|
|
{
|
|
if (currentState != newState)
|
|
{
|
|
currentState = newState;
|
|
UpdateLightsState();
|
|
Debug.Log($"灯光状态已切换为: {newState}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 压暗光照强度
|
|
/// </summary>
|
|
public void DarkenLights()
|
|
{
|
|
panelLight.intensity = 0f;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置环境灯闪烁状态
|
|
/// </summary>
|
|
public void SetEnvironmentLightFlickering(bool isFlickering)
|
|
{
|
|
if (isEnvironmentLightFlickering != isFlickering)
|
|
{
|
|
isEnvironmentLightFlickering = isFlickering;
|
|
if (isFlickering)
|
|
{
|
|
StartCoroutine(EnvironmentLightFlickerRoutine());
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置环境灯损坏状态
|
|
/// </summary>
|
|
public void SetEnvironmentLightBroken()
|
|
{
|
|
if (environmentLight != null)
|
|
{
|
|
environmentLight.color = GetEnvironmentColorByState(currentState);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置警报灯闪烁速度
|
|
/// </summary>
|
|
public void SetAlarmBlinkSpeed(float speed)
|
|
{
|
|
if (speed > 0)
|
|
{
|
|
alarmBlinkSpeed = speed;
|
|
// 如果当前正在闪烁,更新闪烁速度
|
|
if (currentState == LightState.Emergency && alarmLight != null)
|
|
{
|
|
StartAlarmLightBlink();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止警报灯
|
|
/// </summary>
|
|
public void StopAlarmLight()
|
|
{
|
|
if (alarmLight != null)
|
|
{
|
|
alarmTween?.Kill();
|
|
alarmLight.intensity = 0f;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 光照系统 - 状态更新方法
|
|
|
|
/// <summary>
|
|
/// 更新所有灯光状态
|
|
/// </summary>
|
|
private void UpdateLightsState()
|
|
{
|
|
var stateConfig = GetStateConfig(currentState);
|
|
|
|
UpdateAlarmLight(stateConfig);
|
|
UpdateEnvironmentLight(stateConfig);
|
|
UpdatePanelLight();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取状态配置
|
|
/// </summary>
|
|
private (Color alarmColor, Color envColor, float envIntensity) GetStateConfig(LightState state)
|
|
{
|
|
return state switch
|
|
{
|
|
LightState.Normal => (alarmLightColor, normalEnvironmentColor, normalEnvironmentIntensity),
|
|
LightState.Warning => (alarmLightColor, warningEnvironmentColor, warningEnvironmentIntensity),
|
|
LightState.Emergency => (alarmLightColor, emergencyEnvironmentColor, emergencyEnvironmentIntensity),
|
|
_ => (alarmLightColor, normalEnvironmentColor, normalEnvironmentIntensity)
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新警报灯
|
|
/// </summary>
|
|
private void UpdateAlarmLight((Color alarmColor, Color envColor, float envIntensity) config)
|
|
{
|
|
if (alarmLight == null) return;
|
|
|
|
if (currentState == LightState.Emergency)
|
|
{
|
|
alarmLight.color = alarmLightColor;
|
|
StartAlarmLightBlink();
|
|
}
|
|
else
|
|
{
|
|
StopAlarmLight();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 启动警报灯闪烁
|
|
/// </summary>
|
|
private void StartAlarmLightBlink()
|
|
{
|
|
if (alarmLight == null) return;
|
|
|
|
alarmTween?.Kill();
|
|
alarmTween = DOTween.To(
|
|
() => alarmLight.intensity,
|
|
x => alarmLight.intensity = x,
|
|
ALARM_LIGHT_MAX_INTENSITY,
|
|
alarmBlinkSpeed
|
|
)
|
|
.SetLoops(-1, LoopType.Yoyo)
|
|
.SetEase(Ease.InOutSine);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新环境灯
|
|
/// </summary>
|
|
private void UpdateEnvironmentLight((Color alarmColor, Color envColor, float envIntensity) config)
|
|
{
|
|
if (environmentLight == null) return;
|
|
|
|
environmentLight.color = config.envColor;
|
|
environmentLight.intensity = config.envIntensity;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新面板灯
|
|
/// </summary>
|
|
private void UpdatePanelLight()
|
|
{
|
|
if (panelLight == null) return;
|
|
|
|
panelLight.color = panelLightColor;
|
|
panelLight.intensity = panelLightIntensity;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 光照系统 - 环境灯相关方法
|
|
|
|
/// <summary>
|
|
/// 环境灯闪烁协程
|
|
/// </summary>
|
|
private IEnumerator EnvironmentLightFlickerRoutine()
|
|
{
|
|
while (isEnvironmentLightFlickering)
|
|
{
|
|
if (environmentLight == null)
|
|
{
|
|
yield return new WaitForSeconds(0.1f);
|
|
continue;
|
|
}
|
|
|
|
float currentIntensity = GetEnvironmentIntensityByState(currentState);
|
|
environmentLight.intensity = currentIntensity;
|
|
|
|
// 等待随机间隔
|
|
yield return new WaitForSeconds(Random.Range(MIN_FLICKER_INTERVAL, MAX_FLICKER_INTERVAL));
|
|
|
|
// 随机闪烁
|
|
int blinkCount = Random.Range((int)MIN_FLICKER_BLINK_COUNT, (int)MAX_FLICKER_BLINK_COUNT);
|
|
for (int i = 0; i < blinkCount; i++)
|
|
{
|
|
environmentLight.intensity = 0f;
|
|
yield return new WaitForSeconds(brokenLightFlickerDuration);
|
|
|
|
environmentLight.intensity = currentIntensity;
|
|
yield return new WaitForSeconds(brokenLightFlickerInterval);
|
|
}
|
|
}
|
|
|
|
// 确保灯光在停止闪烁时是亮着的
|
|
if (environmentLight != null)
|
|
{
|
|
environmentLight.intensity = GetEnvironmentIntensityByState(currentState);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据状态获取环境灯强度
|
|
/// </summary>
|
|
private float GetEnvironmentIntensityByState(LightState state)
|
|
{
|
|
return state switch
|
|
{
|
|
LightState.Normal => normalEnvironmentIntensity,
|
|
LightState.Warning => warningEnvironmentIntensity,
|
|
LightState.Emergency => emergencyEnvironmentIntensity,
|
|
_ => normalEnvironmentIntensity
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据状态获取环境灯颜色
|
|
/// </summary>
|
|
private Color GetEnvironmentColorByState(LightState state)
|
|
{
|
|
return state switch
|
|
{
|
|
LightState.Normal => normalEnvironmentColor,
|
|
LightState.Warning => warningEnvironmentColor,
|
|
LightState.Emergency => emergencyEnvironmentColor,
|
|
_ => normalEnvironmentColor
|
|
};
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 光照系统 - 面板灯相关方法
|
|
|
|
/// <summary>
|
|
/// 面板灯启动序列
|
|
/// </summary>
|
|
public IEnumerator PanelLightStartSequence()
|
|
{
|
|
if (panelLight == null) yield break;
|
|
|
|
for (int i = 0; i < panelLightBlinkCount; i++)
|
|
{
|
|
// 渐亮
|
|
DOTween.To(() => panelLight.intensity, x => panelLight.intensity = x, panelLightIntensity, panelLightBlinkDuration * 0.5f)
|
|
.SetEase(Ease.InOutQuad);
|
|
if (environmentLight != null)
|
|
{
|
|
DOTween.To(() => environmentLight.intensity, x => environmentLight.intensity = x, normalEnvironmentIntensity, panelLightBlinkDuration * 0.5f)
|
|
.SetEase(Ease.InOutQuad);
|
|
}
|
|
yield return new WaitForSeconds(panelLightBlinkDuration);
|
|
|
|
// 渐暗
|
|
DOTween.To(() => panelLight.intensity, x => panelLight.intensity = x, 0f, panelLightBlinkDuration * 0.5f)
|
|
.SetEase(Ease.InOutQuad);
|
|
if (environmentLight != null)
|
|
{
|
|
DOTween.To(() => environmentLight.intensity, x => environmentLight.intensity = x, 0f, panelLightBlinkDuration * 0.5f)
|
|
.SetEase(Ease.InOutQuad);
|
|
}
|
|
yield return new WaitForSeconds(panelLightBlinkDuration);
|
|
}
|
|
|
|
// 最后渐亮到常亮状态
|
|
DOTween.To(() => panelLight.intensity, x => panelLight.intensity = x, panelLightIntensity, panelLightBlinkDuration * 0.5f)
|
|
.SetEase(Ease.InOutQuad);
|
|
if (environmentLight != null)
|
|
{
|
|
DOTween.To(() => environmentLight.intensity, x => environmentLight.intensity = x, normalEnvironmentIntensity, panelLightBlinkDuration * 0.5f)
|
|
.SetEase(Ease.InOutQuad);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|