_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/Battle_Royale/LightBlink.cs

96 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using UnityEngine.UI;
public class LightBlink : MonoBehaviour
{
public Image lightImage; // 灯光图片
private float minBlinkInterval = 0.2f; // 每次闪烁的最小间隔
private float maxBlinkInterval = 0.6f; // 每次闪烁的最大间隔
public float minCooldown = 5f; // 最小冷却时间
public float maxCooldown = 10f; // 最大冷却时间
private float nextBlinkTime; // 下一次闪烁时间点
private int remainingBlinks; // 当前剩余的闪烁次数
private bool isInCooldown; // 是否处于冷却状态
private void Start()
{
// 确保灯光默认是亮着的
if (lightImage != null)
{
lightImage.enabled = true;
}
EnterCooldown();
}
private void Update()
{
if (lightImage == null) return;
if (isInCooldown && Time.time >= nextBlinkTime)
{
StartBlinkSequence();
}
else if (!isInCooldown && Time.time >= nextBlinkTime)
{
PerformBlink();
}
}
private void StartBlinkSequence()
{
// 随机生成闪烁次数2到3次
remainingBlinks = Random.Range(2, 4);
isInCooldown = false;
// 开始第一次闪烁
PerformBlink();
}
private void PerformBlink()
{
// 短暂关闭灯光,再打开
lightImage.enabled = false;
// 减少剩余的闪烁次数
remainingBlinks--;
// 间隔后重新点亮灯光
Invoke("TurnOnLight", Random.Range(minBlinkInterval, maxBlinkInterval));
// 如果闪烁次数用完,进入冷却
if (remainingBlinks <= 0)
{
EnterCooldown();
}
else
{
// 继续闪烁,安排下一次闪烁时间
ScheduleNextBlink(minBlinkInterval, maxBlinkInterval);
}
}
private void TurnOnLight()
{
if (lightImage != null)
{
lightImage.enabled = true;
}
}
private void EnterCooldown()
{
// 进入冷却状态,随机冷却时间
isInCooldown = true;
lightImage.enabled = true; // 确保灯光保持亮着
ScheduleNextBlink(minCooldown, maxCooldown);
}
private void ScheduleNextBlink(float minTime, float maxTime)
{
// 随机生成下一次事件的时间间隔
float interval = Random.Range(minTime, maxTime);
nextBlinkTime = Time.time + interval;
}
}