Sun_moves/assets/Script/BallAudioManager.js
2024-05-28 11:38:16 +08:00

315 lines
7.8 KiB
JavaScript

cc.Class({
extends: cc.Component,
properties:{
//背景音乐
audioGameBgm0:{
default:null,
type:cc.AudioClip
},
// 按钮点击
audioButtonClick:{
default:null,
type:cc.AudioClip
},
// 报警音效
audioWarning:{
default:null,
type:cc.AudioClip
},
//游戏开始音效
audioWin:{
default:null,
type:cc.AudioClip
},
reward: false,
finish: false,
rewardCount: 0
},
ctor: function () {
this.mAudioMap = {};
/**
* 默认音量大小
* @type {number}
*/
this.bgMusicVolume = 0.1;
this.effectMusicVolume = 1;
this.mMusicSwitch = 1;
this.mEffectSwitch = 1;
},
play : function(audioSource, loop, callback, isBgMusic) {
if (isBgMusic && !this.mMusicSwitch) return;
if (!isBgMusic && !this.mEffectSwitch) return;
var volume = isBgMusic ? this.bgMusicVolume : this.effectMusicVolume;
if (cc.sys.isBrowser) {
if(audioSource == this.brickSound){
volume = 0.1;
}
var context = cc.audioEngine.play(audioSource, loop, volume);
if (callback){
cc.audioEngine.setFinishCallback(context, function(){
callback.call(this);
}.bind(this));
}
// cc.wwx.OutPut.log('play audio effect isBrowser: ' + context.src);
this.mAudioMap[audioSource] = context;
return audioSource;
} else {
var context = wx.createInnerAudioContext();
context.autoplay = true;
context.loop = loop;
context.obeyMuteSwitch = true;
context.volume = volume;
if (callback) {
context.onEnded(function() {
callback.call(this);
}.bind(this));
} else {
context.offEnded();
}
// var audioPath = cc.url.raw("resources/BallMaster/sounds" + audioSource.name + ".mp3");
context.src = audioSource.nativeUrl;
context.play()
this.mAudioMap[audioSource] = context;
// cc.wwx.OutPut.log('play audio effect wx: ' + audioSource.nativeUrl);
return audioSource;
}
},
save: function () {
// cc.wwx.Storage.setItem(cc.wwx.Storage.Key_Setting_Music_Volume, this.mMusicSwitch);
// cc.wwx.Storage.setItem(cc.wwx.Storage.Key_Setting_Effect_Volume, this.mEffectSwitch);
},
onLoad() {
// cc.wwx.Storage.getItem(cc.wwx.Storage.Key_Setting_Music_Volume, function(volume) {
// this.mMusicSwitch = parseInt(volume);
// }.bind(this), 1);
// cc.wwx.Storage.getItem(cc.wwx.Storage.Key_Setting_Effect_Volume, function(volume) {
// this.mEffectSwitch = parseInt(volume);
// }.bind(this), 1);
this.reward = false;
this.finish = false;
this.rewardCount = 0;
window.addEventListener('rewardCanUse', () => {
// 此时激励可用 游戏内部逻辑
this.reward = true;
});
'undefined' != typeof window['reward'] ?
window.dispatchEvent(window['reward']):""
this.preload();
},
preload : function() {
if (!(cc.sys.platform === cc.sys.WECHAT_GAME)) { return; }
var musics = [
this.audioGameBgm0,
];
musics.forEach(function(path) {
// var musicPath = wxDownloader.REMOTE_SERVER_ROOT + path;
// if (musicPath != wxDownloader.REMOTE_SERVER_ROOT && musicPath.endsWith('.mp3')) {
// cc.loader.load(musicPath, function(err, remoteUrl) {
// if (err) {
// cc.error(err.message || err);
// return;
// }
// });
// }
})
},
getAudioMusicSwitch()
{
return this.mMusicSwitch;
},
getAudioEffectSwitch()
{
return this.mEffectSwitch;
},
trunAudioSound(on)
{
this.switchMusic(on);
this.switchEffect(on)
},
switchMusic : function(on) {
if (this.mMusicSwitch != (on?1:0))
{
this.mMusicSwitch = 1-this.mMusicSwitch;
// this.save();
}
if(on)
{
this.playMusicGame();
}
else
{
this.stopMusic();
}
},
switchEffect : function(on) {
if (this.mEffectSwitch != (on?1:0)){
this.mEffectSwitch = 1-this.mEffectSwitch;
// this.save();
}
},
onHide () {
cc.audioEngine.pauseAll();
// if (CC_JSB) {
// } else {
// for (var key in this.mAudioMap) {
// if (key === this.mMusicKey) {
// this.mAudioMap[key].pause();
// } else {
// this.mAudioMap[key].stop();
// }
// }
// }
},
onShow () {
cc.audioEngine.resumeAll();
// if (CC_JSB) {
// } else {
// if (!this.mMusicSwitch) return;
// var context = this.mAudioMap[this.mMusicKey];
// if (context) {
// context.play();
// }
// }
},
playMusic : function(key, callback, loop) {
loop = typeof loop == 'undefined' || loop ? true : false;
this.stopMusic();
this.mMusicKey = this.play(key, loop, callback, true);
},
/**
* 游戏背景音乐
*/
playMusicGame () {
this.playMusic(this.audioGameBgm0);
},
/**
* 停止背景音乐播放
*/
stopMusic () {
// cc.wwx.OutPut.log('stopMusic audio effect wx: ' + this.mMusicKey);
var context = this.mAudioMap[this.mMusicKey];
if (typeof(context) != 'undefined') {
if (cc.sys.isBrowser) {
cc.audioEngine.stop(context);
} else {
context.stop();
}
cc.audioEngine.stop(context);
}
},
// 炸弹、火箭爆炸音效
playBomb () {
return this.play(this.audioBomb, false);
},
//激光音效
playJiGuang()
{
return this.play(this.audioJiGuang, false);
},
//技能音效
playItem1()
{
return this.play(this.audioItem1, false);
},
/*
* 游戏开始音效
*
*/
playGameStart()
{
return this.play(this.audioGameStart, false);
},
/*
* 失败的游戏结束
*/
playGameOver()
{
return this.play(this.audioGameOver,false);
},
/*
* 成功的游戏结束
*/
playGameResultFailed()
{
return this.play(this.audioGameResultFail,false);
},
/*
* 成功的游戏结束
*/
playGameResultSuccess()
{
return this.play(this.audioGameResultSuccess,false);
},
/**
* 报警的音效
*/
playWarning()
{
return this.play(this.audioWarning,false);
},
playWin()
{
return this.play(this.audioWin,false);
},
/*
* 方块碰撞的声音
*/
playBrick()
{
return this.play(this.brickSound,false);
},
//方块破碎的声音
brickBoom(){
return this.play(this.audioObj,false);
},
/**
* 按钮
*/
playAudioButton () {
return this.play(this.audioButtonClick, false);
},
playRandomMatch()
{
this.playMusic(this.randomMatchSound);
},
playMatchFoundSound()
{
this.stopMusic();
return this.play(this.matchFoundSound, false);
}
});