Files
ichni_Official/Assets/Scripts/Manager/SongPlayer.cs

163 lines
5.1 KiB
C#
Raw Normal View History

2025-06-03 02:42:28 -04:00
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
2026-03-14 03:13:10 -04:00
using SLSUtilities.WwiseAssistance;
2025-06-03 02:42:28 -04:00
using UnityEngine;
using Event = AK.Wwise.Event;
namespace Ichni
{
2025-06-06 10:14:55 -04:00
public class SongPlayer : SerializedMonoBehaviour
2025-06-03 02:42:28 -04:00
{
2026-03-14 03:13:10 -04:00
public bool isLoading = true;
2026-04-03 10:53:11 -04:00
public bool isStarting = false;
2026-03-14 03:13:10 -04:00
public bool isDelaying = false;
public bool isPlaying = false; // 是否正在播放音乐
public bool isPausing = false; // 是否正在暂停音乐
public bool isFinished = false;
2026-04-03 10:53:11 -04:00
public bool isUpdating => isStarting || isDelaying || isPlaying;
2026-03-14 03:13:10 -04:00
2025-06-03 02:42:28 -04:00
public Event PlayMusicEvent; // 播放背景音乐的事件
public Event ResumeMusicEvent; // 恢复播放背景音乐的事件
public Event PauseMusicEvent; // 暂停播放背景音乐的事件
public Event StopMusicEvent; // 停止播放背景音乐的事件
2025-07-08 14:28:40 -04:00
private uint _playingId;
2025-06-03 02:42:28 -04:00
public float songTimeSegment = 0;
public float pauseTimeSegment;
private float duration;
private float recordedSongSeg;
public float judgeOffset = 0;
2026-03-14 03:13:10 -04:00
private void Start()
{
InformationTransistor.instance.chapterSwitch.SetValue(gameObject);
InformationTransistor.instance.songSwitch.SetValue(gameObject);
isLoading = true;
2026-04-03 10:53:11 -04:00
isStarting = false;
2026-03-14 03:13:10 -04:00
}
2025-06-03 02:42:28 -04:00
private void Update()
{
2026-03-14 03:13:10 -04:00
if (isLoading)
2025-07-08 14:28:40 -04:00
{
return;
}
2026-03-14 03:13:10 -04:00
if (isDelaying)
2025-06-03 02:42:28 -04:00
{
songTimeSegment += Time.deltaTime;
2025-08-11 14:04:06 -04:00
//songTimeSegment = Mathf.Max(songTimeSegment, 0); // 确保时间段不为负数
2025-07-08 14:28:40 -04:00
if (songTimeSegment >= 0)
{
2026-03-14 03:13:10 -04:00
isDelaying = false;
2025-08-11 14:04:06 -04:00
songTimeSegment = 0; // 延迟结束后,时间段归零
2026-03-14 03:13:10 -04:00
PlaySong();
2025-07-08 14:28:40 -04:00
}
2025-06-03 02:42:28 -04:00
}
else
{
2026-03-14 03:13:10 -04:00
if (GameManager.Instance.isDebugging)
2025-06-03 02:42:28 -04:00
{
return;
}
2026-03-14 03:13:10 -04:00
if (isFinished)
2025-06-03 02:42:28 -04:00
{
songTimeSegment = recordedSongSeg;
2025-07-08 14:28:40 -04:00
2025-06-03 02:42:28 -04:00
return;
}
2026-03-14 03:13:10 -04:00
if (isPlaying)
2025-06-03 02:42:28 -04:00
{
2025-08-22 14:54:40 -04:00
float currentSongSegment = PlaySegment() / 1000f - (judgeOffset / 1000f);
if (recordedSongSeg < currentSongSegment)
2025-06-03 02:42:28 -04:00
{
2025-08-22 14:54:40 -04:00
songTimeSegment = currentSongSegment;
recordedSongSeg = currentSongSegment;
2025-06-03 02:42:28 -04:00
}
}
2026-03-14 03:13:10 -04:00
else if (isPausing)
2025-06-03 02:42:28 -04:00
{
2025-08-11 14:04:06 -04:00
songTimeSegment = pauseTimeSegment;
2025-06-03 02:42:28 -04:00
}
}
}
[Button]
2025-06-06 10:14:55 -04:00
public void PlaySong()
2025-06-03 02:42:28 -04:00
{
_playingId = PlayMusicEvent.Post(gameObject,
2025-08-11 14:04:06 -04:00
(uint)AkCallbackType.AK_EnableGetMusicPlayPosition |
(uint)AkCallbackType.AK_MusicSyncEntry |
(uint)AkCallbackType.AK_MusicSyncExit,
2025-06-03 02:42:28 -04:00
OnMusicEvent, null);
2026-03-14 03:13:10 -04:00
isPlaying = true;
isPausing = false;
isFinished = false;
2025-06-03 02:42:28 -04:00
}
[Button]
2025-06-06 10:14:55 -04:00
public void PauseSong()
2025-06-03 02:42:28 -04:00
{
2025-08-22 14:54:40 -04:00
pauseTimeSegment = songTimeSegment;
2026-03-14 03:13:10 -04:00
isPlaying = false;
isPausing = true;
2025-06-03 02:42:28 -04:00
PauseMusicEvent.Post(gameObject);
Time.timeScale = 0;
}
[Button]
2025-06-06 10:14:55 -04:00
public void ResumeSong()
2025-06-03 02:42:28 -04:00
{
Time.timeScale = 1;
2026-03-14 03:13:10 -04:00
isPlaying = true;
isPausing = false;
2025-06-03 02:42:28 -04:00
ResumeMusicEvent.Post(gameObject);
}
[Button]
2025-06-06 10:14:55 -04:00
public void StopSong()
2025-06-03 02:42:28 -04:00
{
2026-03-14 03:13:10 -04:00
isPlaying = false;
isPausing = false;
2025-06-03 02:42:28 -04:00
StopMusicEvent.Post(gameObject);
}
2025-08-11 14:04:06 -04:00
2025-06-03 02:42:28 -04:00
private void OnMusicEvent(object in_cookie, AkCallbackType in_type, AkCallbackInfo in_info)
{
2025-08-11 14:04:06 -04:00
Debug.Log(in_type + " " + in_info);
if (in_type == AkCallbackType.AK_MusicSyncEntry)
2025-06-03 02:42:28 -04:00
{
2025-08-11 14:04:06 -04:00
if (in_info is AkMusicSyncCallbackInfo musicInfo)
2025-06-03 02:42:28 -04:00
{
2026-03-14 03:13:10 -04:00
GameManager.Instance.songInformation.songLength = musicInfo.segmentInfo_iActiveDuration / 1000f;
2025-08-11 14:04:06 -04:00
InformationTransistor.instance.songLength = musicInfo.segmentInfo_iActiveDuration / 1000f;
2026-03-14 03:13:10 -04:00
InformationTransistor.instance.bpm = GameManager.Instance.songInformation.bpm;
2025-06-03 02:42:28 -04:00
}
}
2025-08-11 14:04:06 -04:00
if (in_type == AkCallbackType.AK_MusicSyncExit)
{
2026-03-14 03:13:10 -04:00
isFinished = true;
GameManager.Instance.summaryPageCanvas.SetUpSummary();
GameManager.Instance.summaryPageCanvas.FadeIn();
2025-08-11 14:04:06 -04:00
}
2025-06-03 02:42:28 -04:00
}
2025-08-11 14:04:06 -04:00
2025-06-03 02:42:28 -04:00
int PlaySegment()
{
AkSegmentInfo segmentInfo = new AkSegmentInfo();
AkSoundEngine.GetPlayingSegmentInfo(_playingId, segmentInfo,true);
return segmentInfo.iCurrentPosition;
}
}
}