Files
2026-07-30 23:15:58 +08:00

102 lines
3.1 KiB
C#

using UnityEngine;
/// <summary>
/// 练习模式配置:支持从任意位置起播 + A-B 段落循环。
/// 供后续快速接入(UI/GameManager 逻辑需另行连接)。
/// 默认关闭,不影响现有正常游玩流程。
/// </summary>
public static class PracticeMode
{
private const string PREF_ENABLED = "PracticeMode_Enabled";
private const string PREF_START_TIME = "PracticeMode_StartTime";
private const string PREF_END_TIME = "PracticeMode_EndTime";
private const string PREF_LOOP = "PracticeMode_Loop";
/// <summary>
/// 练习模式开关(默认 false)。开启后从 StartTimeSeconds 起播,到达 EndTimeSeconds 时结束或循环。
/// </summary>
public static bool Enabled
{
get => PlayerPrefs.GetInt(PREF_ENABLED, 0) == 1;
set
{
PlayerPrefs.SetInt(PREF_ENABLED, value ? 1 : 0);
PlayerPrefs.Save();
}
}
/// <summary>
/// 起始时间(秒,谱面 songTime)。默认 0(从头开始)。
/// </summary>
public static float StartTimeSeconds
{
get => PlayerPrefs.GetFloat(PREF_START_TIME, 0f);
set
{
PlayerPrefs.SetFloat(PREF_START_TIME, Mathf.Max(0f, value));
PlayerPrefs.Save();
}
}
/// <summary>
/// 结束时间(秒,谱面 songTime)。默认 -1(无限制,播到谱面结束)。
/// 若 >= 0 且 < StartTimeSeconds,视为无效,忽略。
/// </summary>
public static float EndTimeSeconds
{
get => PlayerPrefs.GetFloat(PREF_END_TIME, -1f);
set
{
PlayerPrefs.SetFloat(PREF_END_TIME, value);
PlayerPrefs.Save();
}
}
/// <summary>
/// A-B 循环开关(默认 false)。若开启且 EndTimeSeconds 有效,到达终点时自动重新加载并从 StartTimeSeconds 继续。
/// </summary>
public static bool LoopEnabled
{
get => PlayerPrefs.GetInt(PREF_LOOP, 0) == 1;
set
{
PlayerPrefs.SetInt(PREF_LOOP, value ? 1 : 0);
PlayerPrefs.Save();
}
}
/// <summary>
/// 检查当前 songTime 是否已到达练习终点(需要结束或循环)。
/// </summary>
public static bool ShouldEndOrLoop(float currentSongTime)
{
if (!Enabled) return false;
float end = EndTimeSeconds;
if (end < 0f) return false; // 无限制
if (end <= StartTimeSeconds) return false; // 无效配置
return currentSongTime >= end;
}
/// <summary>
/// 重置为默认值(关闭、从头播放、无循环)。
/// </summary>
public static void Reset()
{
Enabled = false;
StartTimeSeconds = 0f;
EndTimeSeconds = -1f;
LoopEnabled = false;
}
/// <summary>
/// 快捷方法:设置 A-B 段落循环(自动开启练习模式和循环开关)。
/// </summary>
public static void SetABLoop(float startSeconds, float endSeconds)
{
Enabled = true;
StartTimeSeconds = startSeconds;
EndTimeSeconds = endSeconds;
LoopEnabled = true;
}
}