155 lines
3.8 KiB
Plaintext
155 lines
3.8 KiB
Plaintext
/*
|
|
文件名:AudioClass.nut
|
|
路径:System/BaseTool/AudioClass.nut
|
|
创建日期:2024-05-30 20:48
|
|
文件用途:音频类
|
|
*/
|
|
if (!(getroottable().rawin("SoundEffectArr"))) SoundEffectArr <- [];
|
|
class Sound extends CL_BaseObject {
|
|
//名称
|
|
Name = null;
|
|
//路径
|
|
Path = null;
|
|
//使用时间
|
|
UseTime = null;
|
|
//创建时间
|
|
CreateTime = null;
|
|
|
|
constructor(gName) {
|
|
Name = gName;
|
|
Path = getconsttable().SOUND[Name];
|
|
local C_Object = Sound_CreateSound(Path);
|
|
base.constructor(C_Object);
|
|
|
|
CreateTime = clock();
|
|
}
|
|
|
|
//播放
|
|
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 IsPlaying() {
|
|
return Sound_IsPlaying(C_Object);
|
|
}
|
|
}
|
|
|
|
class SoundEffect extends Sound {
|
|
|
|
constructor(Name) {
|
|
base.constructor(Name);
|
|
|
|
//加入全局临时音效组
|
|
JoinSoundEffect();
|
|
//默认调用播放
|
|
Play();
|
|
}
|
|
|
|
//加入全局临时音效组
|
|
function JoinSoundEffect() {
|
|
SoundEffectArr.append(this);
|
|
}
|
|
|
|
}
|
|
|
|
class AudioControlClass {
|
|
|
|
//当前播放音乐集合
|
|
CurrentMusicList = null;
|
|
//音源库
|
|
MusicList = null;
|
|
|
|
//销毁临时音效的时间Flag
|
|
CloseSoundEffectTimeFlag = 0;
|
|
//销毁临时音效的间隔时间 //5秒一次
|
|
CloseSoundEffectTime = 5000;
|
|
//销毁音乐的时间Flag
|
|
CloseMusicTimeFlag = 0;
|
|
//销毁音乐的间隔时间 //5秒一次
|
|
CloseMusicTime = 5000;
|
|
|
|
constructor() {
|
|
CurrentMusicList = {};
|
|
MusicList = {};
|
|
|
|
|
|
}
|
|
|
|
//预加载音乐
|
|
function PreLoad(Name) {
|
|
local Ret = Sound(Name);
|
|
MusicList[Name] <- Ret;
|
|
}
|
|
//加载音乐 加载完成会自动播放
|
|
function Load(Name) {
|
|
local Ret = Sound(Name);
|
|
Ret.Play();
|
|
CurrentMusicList[Name] <- Ret;
|
|
MusicList[Name] <- Ret;
|
|
}
|
|
|
|
|
|
function OnUpdate(dt) {
|
|
//销毁临时音效
|
|
CloseSoundEffect(dt);
|
|
//销毁音乐
|
|
CloseMusic(dt);
|
|
}
|
|
|
|
function CloseSoundEffect(dt) {
|
|
CloseSoundEffectTimeFlag += dt;
|
|
if (CloseSoundEffectTimeFlag >= CloseSoundEffectTime) {
|
|
for (local i = 0; i< SoundEffectArr.len(); i++) {
|
|
local SoundEffectObj = SoundEffectArr[i];
|
|
//播放完成销毁资源
|
|
if (!SoundEffectObj.IsPlaying()) {
|
|
SoundEffectArr.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"))) AudioControl <- AudioControlClass();
|
|
|
|
|
|
//播放临时音效
|
|
function PlaySoundEffect(Name) {
|
|
return SoundEffect(Name);
|
|
} |