DOF/sqr/Core/BaseClass/AudioClass.nut

221 lines
5.7 KiB
Plaintext

/*
文件名:AudioClass.nut
路径:Core/BaseClass/AudioClass.nut
创建日期:2024-05-30 20:48
文件用途:音频类
*/
if (!(getroottable().rawin("_Globa_Audio_Volume_"))) _Globa_Audio_Volume_ <- 1.0;
if (!(getroottable().rawin("_SoundEffect_List_"))) _SoundEffect_List_ <- [];
class Sound extends CL_BaseObject {
//名称
Name = null;
//路径
Path = null;
//使用时间
UseTime = null;
//创建时间
CreateTime = null;
constructor(gName) {
Path = gName;
local C_Object = Sound_CreateSound(Path);
base.constructor(C_Object);
CreateTime = clock();
SetVolume(1.0);
}
//播放
function Play() {
Sound_Play(C_Object);
UseTime = clock();
}
//暂停
function Pause() {
Sound_Pause(C_Object);
}
//继续
function Resume() {
Sound_Resume(C_Object);
}
//关闭并销毁
function Close() {
Sound_Close(C_Object);
}
//获取音量
function GetVolume() {
return Sound_GetVolume(C_Object);
}
//设置音量
function SetVolume(value) {
Sound_SetVolume(C_Object, value * _Globa_Audio_Volume_);
}
function IsPlaying() {
return Sound_IsPlaying(C_Object);
}
}
class SoundEffect extends Sound {
constructor(Name) {
base.constructor(Name);
//加入全局临时音效组
JoinSoundEffect();
//默认调用播放
Play();
}
//加入全局临时音效组
function JoinSoundEffect() {
_SoundEffect_List_.append(this);
}
}
class AudioControlClass {
//当前播放音乐集合
CurrentMusicList = null;
//音源库
MusicList = null;
//销毁临时音效的时间Flag
CloseSoundEffectTimeFlag = 0;
//销毁临时音效的间隔时间 //5秒一次
CloseSoundEffectTime = 5000;
//销毁音乐的时间Flag
CloseMusicTimeFlag = 0;
//销毁音乐的间隔时间 //5秒一次
CloseMusicTime = 5000;
//待移除音乐
RemoveMusicQueue = null;
constructor() {
CurrentMusicList = {};
MusicList = {};
RemoveMusicQueue = [];
}
//预加载音乐
function PreLoad(Name) {
local Ret = Sound(Name);
MusicList[Name] <- Ret;
}
//加载音乐 加载完成会自动播放
function Load(Name) {
if (MusicList.rawin(Name)) {
CurrentMusicList[Name] <- MusicList[Name];
MusicList[Name].Play();
} else {
local Ret = Sound(Name);
Ret.Play();
CurrentMusicList[Name] <- Ret;
MusicList[Name] <- Ret;
}
return CurrentMusicList[Name];
}
//移除音乐
function Remove(Name) {
CurrentMusicList[Name].Pause();
delete CurrentMusicList[Name];
}
//淡出移除音乐 不传值默认倍率 传值设定倍率
function RemoveByFadeout(Name, ...) {
local realrate = 1.0;
if (vargv.len() > 0) realrate = vargv[0];
RemoveMusicQueue.append({
sound = CurrentMusicList[Name],
rate = realrate
});
}
function RemoveMusic(dt) {
for (local i = 0; i< RemoveMusicQueue.len(); i++) {
local Object = RemoveMusicQueue[i];
local SoundObj = Object.sound;
local Rate = Object.rate;
local SoCurV = SoundObj.GetVolume();
//音量降到最低了 停止播放
if (SoCurV == 0) {
SoundObj.Pause();
delete CurrentMusicList[SoundObj.Name];
RemoveMusicQueue.remove(i);
}
//渐变设置音量
else {
local SetValue = SoCurV - (dt * Rate * 0.0005);
if (SetValue< 0) SetValue = 0;
SoundObj.SetVolume(SetValue);
}
}
}
function OnUpdate(dt, Stage) {
//移除音乐(淡出)
RemoveMusic(dt);
//销毁临时音效
CloseSoundEffect(dt);
//销毁音乐
CloseMusic(dt);
}
function CloseSoundEffect(dt) {
CloseSoundEffectTimeFlag += dt;
if (CloseSoundEffectTimeFlag >= CloseSoundEffectTime) {
for (local i = 0; i< _SoundEffect_List_.len(); i++) {
local SoundEffectObj = _SoundEffect_List_[i];
//播放完成销毁资源
if (!SoundEffectObj.IsPlaying()) {
_SoundEffect_List_.remove(i);
}
}
CloseSoundEffectTimeFlag = 0;
}
}
function CloseMusic(dt) {
CloseMusicTimeFlag += dt;
if (CloseMusicTimeFlag >= CloseMusicTime) {
local CurrT = clock();
//遍历 如果正在播放刷新时间
foreach(SoundObj in MusicList) {
if (SoundObj.IsPlaying()) SoundObj.UseTime = CurrT;
}
//遍历判断使用时间距离现在已经超过了500秒就销毁
local Arr = [];
foreach(SoundObj in MusicList) {
if (SoundObj.UseTime && (clock() - SoundObj.UseTime) > 500000) {
Arr.append(SoundObj.Name);
}
}
foreach(Name in Arr) {
if (CurrentMusicList.rawin(Name)) CurrentMusicList.rawdelete(Name);
if (MusicList.rawin(Name)) MusicList.rawdelete(Name);
}
CloseMusicTimeFlag = 0;
}
}
}
if (!(getroottable().rawin("AudioControl"))) _Global_AudioControl_ <- AudioControlClass();
//播放临时音效
function Sq_PlaySoundEffect(Name) {
return SoundEffect(Name);
}
//全局音效逻辑
function _Yosin_Sound_Logic_(Dt, Stage) {
_Global_AudioControl_.OnUpdate(Dt, Stage);
}