468 lines
12 KiB
C#
468 lines
12 KiB
C#
using cfg.ActorCfg;
|
|
using cfg.AudioCfg;
|
|
using Cysharp.Threading.Tasks;
|
|
using Framework;
|
|
using PhxhSDK;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
public class AlbumManager : Singlenton<AlbumManager>
|
|
{
|
|
public enum PlayMode
|
|
{
|
|
Order,
|
|
Random,
|
|
Single
|
|
}
|
|
PlayMode curPlayMode = PlayMode.Order;
|
|
public PlayMode CurPlayMode => curPlayMode;
|
|
|
|
List<int> playOrderList = new();
|
|
private int curOrderIndex = 0;
|
|
|
|
bool initialized = false;
|
|
|
|
public string curMusicKey;
|
|
|
|
Dictionary<int, DataAlbum> albumDic = new Dictionary<int, DataAlbum>();
|
|
List<DataAlbum> albumList = new List<DataAlbum>();
|
|
|
|
public readonly bool loadFromLocal = true;
|
|
|
|
AlbumLocalData _saveData;
|
|
|
|
|
|
public Timer playTimer = new();
|
|
|
|
void loadOrInit()
|
|
{
|
|
if(!initialized)
|
|
{
|
|
Init();
|
|
initialized = true;
|
|
playTimer.onPlayEnd += NextAudio;
|
|
}
|
|
}
|
|
public void Init()
|
|
{
|
|
albumDic.Clear();
|
|
var albumCfg = TableManager.Instance.Tables.AlbumConfig;
|
|
foreach (var item in albumCfg.DataList)
|
|
{
|
|
albumDic.Add(item.Id, item);
|
|
albumList.Add(item);
|
|
}
|
|
|
|
if (loadFromLocal)
|
|
{
|
|
_saveData = StorageMgr.Instance.GetStorage<AlbumLocalData>("ALBUM_LOCAL_DATA");
|
|
if (_saveData == null)
|
|
{
|
|
_saveData = new AlbumLocalData();
|
|
}
|
|
curPlayMode = (PlayMode)_saveData.curOrderMode;
|
|
SetPlayMode(curPlayMode);
|
|
SetPlayList();
|
|
var curAudio = CurPlayAudio();
|
|
if (curAudio != null)
|
|
{
|
|
curMusicKey = curAudio.Key;
|
|
}
|
|
else
|
|
{
|
|
DebugUtil.LogError("CurAudio is null");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//TODO ·þÎñÆ÷Âß¼
|
|
}
|
|
}
|
|
|
|
public void Release()
|
|
{
|
|
albumDic.Clear();
|
|
albumList.Clear();
|
|
}
|
|
|
|
public Dictionary<int, DataAlbum> AlbumsDic => albumDic;
|
|
public List<DataAlbum> AlbumsLst => albumList;
|
|
|
|
public bool AlbumUnlocked(int albumId)
|
|
{
|
|
loadOrInit();
|
|
if(albumDic.ContainsKey(albumId))
|
|
{
|
|
if (albumDic[albumId].DefaultUnlock)//ĬÈϽâËø×¨¼
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
int itemID = albumDic[albumId].ItemID;
|
|
if (CategoryManager.Instance.GetItemCount(itemID) > 0)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//int ItemID=
|
|
DebugUtil.LogError(albumId + " not found in album config");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public List<cfg.item.ItemCounts> AlbumUnlockCost(int albumId)
|
|
{
|
|
if (albumDic.ContainsKey(albumId))
|
|
{
|
|
return albumDic[albumId].CostItems;
|
|
}
|
|
else
|
|
{
|
|
DebugUtil.LogError(albumId + " not found in album config");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public DataAlbum GetCurAlbum()
|
|
{
|
|
loadOrInit();
|
|
if (loadFromLocal)
|
|
{
|
|
if (_saveData == null)
|
|
{
|
|
_saveData = StorageMgr.Instance.GetStorage<AlbumLocalData>("ALBUM_LOCAL_DATA");
|
|
if (_saveData != null && _saveData.curAlbumId > 0)
|
|
{
|
|
return albumDic[_saveData.curAlbumId];
|
|
}
|
|
else
|
|
{
|
|
return albumList[0];
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return albumDic[_saveData.curAlbumId];
|
|
}
|
|
}
|
|
else
|
|
{
|
|
DebugUtil.Log("·þÎñÆ÷¶ÁÈ¡");
|
|
return null;
|
|
//TODO ·þÎñÆ÷Âß¼
|
|
}
|
|
}
|
|
public int GetCurAudioIndex()
|
|
{
|
|
loadOrInit();
|
|
if (_saveData == null)
|
|
{
|
|
_saveData = StorageMgr.Instance.GetStorage<AlbumLocalData>("ALBUM_LOCAL_DATA");
|
|
}
|
|
return _saveData.curPlayAudioIndex;
|
|
}
|
|
|
|
public void SetCurAlbum(int albumId, int audioIndex)
|
|
{
|
|
loadOrInit();
|
|
if (albumDic.ContainsKey(albumId))
|
|
{
|
|
if (_saveData == null)
|
|
{
|
|
_saveData = StorageMgr.Instance.GetStorage<AlbumLocalData>("ALBUM_LOCAL_DATA");
|
|
}
|
|
_saveData.curAlbumId = albumId;
|
|
_saveData.curPlayAudioIndex = audioIndex;
|
|
StorageMgr.Instance.SyncForce = true;
|
|
}
|
|
else
|
|
{
|
|
DebugUtil.LogError(albumId + " not found in album config");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// ר¼ÄÚÈÝÁбí
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<int> AlbumPlayList(int albumID)
|
|
{
|
|
if(albumDic.ContainsKey(albumID))
|
|
{
|
|
return albumDic[albumID].PlayList.ToList();
|
|
}
|
|
else
|
|
{
|
|
DebugUtil.LogError(albumID + " not found in album config");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public List<int> CurAlbumPlayList()
|
|
{
|
|
if(_saveData == null)
|
|
{
|
|
_saveData = StorageMgr.Instance.GetStorage<AlbumLocalData>("ALBUM_LOCAL_DATA");
|
|
}
|
|
return AlbumPlayList(_saveData.curAlbumId);
|
|
}
|
|
|
|
public DataAudio CurPlayAudio()
|
|
{
|
|
if(_saveData == null)
|
|
{
|
|
_saveData = StorageMgr.Instance.GetStorage<AlbumLocalData>("ALBUM_LOCAL_DATA");
|
|
}
|
|
var playList = CurAlbumPlayList();
|
|
if (playList != null && playList.Count > 0)
|
|
{
|
|
return TableManager.Instance.Tables.Audios.GetByID(playList[_saveData.curPlayAudioIndex]);
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
public int CurPlayAudioIndex()
|
|
{
|
|
if(_saveData == null)
|
|
{
|
|
_saveData = StorageMgr.Instance.GetStorage<AlbumLocalData>("ALBUM_LOCAL_DATA");
|
|
}
|
|
return _saveData.curPlayAudioIndex;
|
|
}
|
|
|
|
public void SetPlayMode(PlayMode mode)
|
|
{
|
|
curPlayMode = mode;
|
|
SetPlayList();
|
|
}
|
|
|
|
public void SetPlayList()
|
|
{
|
|
playOrderList.Clear();
|
|
if (_saveData == null)
|
|
{
|
|
_saveData = StorageMgr.Instance.GetStorage<AlbumLocalData>("ALBUM_LOCAL_DATA");
|
|
}
|
|
for (int i = 0; i < AlbumPlayList(_saveData.curAlbumId).Count; i++)
|
|
{
|
|
playOrderList.Add(i);
|
|
}
|
|
|
|
switch (curPlayMode)
|
|
{
|
|
case PlayMode.Order:
|
|
break;
|
|
case PlayMode.Random:
|
|
Shuffle(playOrderList);
|
|
break;
|
|
case PlayMode.Single:
|
|
int curIndex = _saveData.curPlayAudioIndex;
|
|
for (int i = 0; i < playOrderList.Count; i++)
|
|
{
|
|
playOrderList[i] = curIndex;
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
for (int i = 0; i < playOrderList.Count; i++)
|
|
{
|
|
if (playOrderList[i] == _saveData.curPlayAudioIndex)
|
|
{
|
|
curOrderIndex = i;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
void Shuffle(List<int> ts)
|
|
{
|
|
for (int i = 0; i < ts.Count; i++)
|
|
{
|
|
int randomIndex = UnityEngine.Random.Range(i, ts.Count);
|
|
(ts[randomIndex], ts[i]) = (ts[i], ts[randomIndex]);
|
|
}
|
|
}
|
|
|
|
public async void PlayAlbumAudio(int albumID, int audioIndex)
|
|
{
|
|
if (albumDic.ContainsKey(albumID))
|
|
{
|
|
var playList = AlbumPlayList(albumID);
|
|
if (playList != null && playList.Count > 0)
|
|
{
|
|
if (audioIndex >= playList.Count)
|
|
{
|
|
audioIndex = 0;
|
|
}
|
|
await AudioManager.Instance.PlayAlbum(playList[audioIndex]);
|
|
curMusicKey = TableManager.Instance.Tables.Audios.GetByID(playList[audioIndex]).Key;
|
|
|
|
SetCurAlbum(albumID, audioIndex);
|
|
|
|
var totalSeconds = AudioManager.Instance.GetAlbumLength(playList[audioIndex]);
|
|
playTimer.Start(totalSeconds);
|
|
}
|
|
else
|
|
{
|
|
DebugUtil.LogError("PlayList is empty");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
DebugUtil.LogError(albumID + " not found in album config");
|
|
}
|
|
}
|
|
public void PlayPreAudio()
|
|
{
|
|
var playList = CurAlbumPlayList();
|
|
if (playList != null && playList.Count > 0)
|
|
{
|
|
if (curPlayMode == PlayMode.Single)
|
|
{
|
|
curOrderIndex = _saveData.curPlayAudioIndex;
|
|
}
|
|
curOrderIndex--;
|
|
if (curOrderIndex < 0)
|
|
{
|
|
curOrderIndex = playList.Count - 1;
|
|
}
|
|
if (curPlayMode == PlayMode.Single)
|
|
{
|
|
for (int i = 0; i < playOrderList.Count; i++)
|
|
{
|
|
playOrderList[i] = curOrderIndex;
|
|
}
|
|
curOrderIndex = 0;
|
|
}
|
|
|
|
PlayAlbumAudio(GetCurAlbum().Id, playOrderList[curOrderIndex]);
|
|
}
|
|
}
|
|
public void PlayNextAudio()
|
|
{
|
|
var playList = CurAlbumPlayList();
|
|
if (playList != null && playList.Count > 0)
|
|
{
|
|
if (curPlayMode == PlayMode.Single)
|
|
{
|
|
curOrderIndex = _saveData.curPlayAudioIndex;
|
|
}
|
|
curOrderIndex++;
|
|
if (curOrderIndex >= playList.Count)
|
|
{
|
|
curOrderIndex = 0;
|
|
}
|
|
if (curPlayMode == PlayMode.Single)
|
|
{
|
|
for (int i = 0; i < playOrderList.Count; i++)
|
|
{
|
|
playOrderList[i] = curOrderIndex;
|
|
}
|
|
curOrderIndex = 0;
|
|
}
|
|
|
|
PlayAlbumAudio(GetCurAlbum().Id, playOrderList[curOrderIndex]);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// ²¥·ÅÍê³É»Øµ÷
|
|
/// </summary>
|
|
public void NextAudio()
|
|
{
|
|
var playList = CurAlbumPlayList();
|
|
if (playList != null && playList.Count > 0)
|
|
{
|
|
curOrderIndex++;
|
|
if (curOrderIndex >= playList.Count)
|
|
{
|
|
curOrderIndex = 0;
|
|
}
|
|
|
|
PlayAlbumAudio(GetCurAlbum().Id, playOrderList[curOrderIndex]);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// ²¥·Å½ø¶È¼ÆÊ±Æ÷
|
|
/// </summary>
|
|
public class Timer
|
|
{
|
|
bool isPlaying;
|
|
float audioLength = -1;
|
|
|
|
public delegate void OnSecondChange();
|
|
public event OnSecondChange onSecondChange;
|
|
public delegate void OnPlayEnd();
|
|
public event OnPlayEnd onPlayEnd;
|
|
|
|
async UniTask AudioTimer()
|
|
{
|
|
while (isPlaying)
|
|
{
|
|
float totalSeconds = AudioManager.Instance.GetAlbumProgress();
|
|
if (totalSeconds < 0)
|
|
{
|
|
isPlaying = false;
|
|
break;
|
|
}
|
|
|
|
if (totalSeconds >= audioLength && audioLength > 0)
|
|
{
|
|
isPlaying = false;
|
|
//AudioManager.Instance.StopMusicById(CurPlayAudio().ID);
|
|
onPlayEnd?.Invoke();
|
|
break;
|
|
}
|
|
onSecondChange?.Invoke();
|
|
await UniTask.Delay(100);//0.1s
|
|
}
|
|
}
|
|
|
|
public void Start(float audiolen)
|
|
{
|
|
audioLength = audiolen;
|
|
if (isPlaying)
|
|
{
|
|
return;
|
|
}
|
|
isPlaying = true;
|
|
AudioTimer().Forget();
|
|
}
|
|
public void Start()
|
|
{
|
|
if (isPlaying)
|
|
{
|
|
return;
|
|
}
|
|
isPlaying = true;
|
|
AudioTimer().Forget();
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
isPlaying = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public class AlbumLocalData
|
|
{
|
|
public int curAlbumId = 1;
|
|
//public List<int> unlockedAlbumLst = new List<int>();
|
|
public int curPlayAudioIndex = 0;
|
|
|
|
public int curOrderMode = 0;
|
|
}
|