297 lines
6.6 KiB
TypeScript
297 lines
6.6 KiB
TypeScript
const {ccclass, property} = cc._decorator;
|
|
@ccclass
|
|
export default class AudioManager extends cc.Component {
|
|
|
|
//背景音乐
|
|
@property(cc.AudioClip)
|
|
audioGameBgm0: cc.AudioClip = null;
|
|
//跳跃
|
|
@property(cc.AudioClip)
|
|
audioButtonClick: cc.AudioClip = null;
|
|
//落地上
|
|
@property(cc.AudioClip)
|
|
audioWarning: cc.AudioClip = null;
|
|
//碰撞
|
|
@property(cc.AudioClip)
|
|
audioWin: cc.AudioClip = null;
|
|
//落方块上
|
|
@property(cc.AudioClip)
|
|
luodui: cc.AudioClip = null;
|
|
|
|
mAudioMap: {};
|
|
bgMusicVolume: number;
|
|
effectMusicVolume: number;
|
|
mMusicSwitch: number;
|
|
mEffectSwitch: number;
|
|
brickSound: any;
|
|
reward: boolean;
|
|
finish: boolean;
|
|
rewardCount: number;
|
|
|
|
mMusicKey: any;
|
|
static _instance: any;
|
|
|
|
ctor () {
|
|
|
|
this.mAudioMap = {};
|
|
/**
|
|
* 默认音量大小
|
|
* @type {number}
|
|
*/
|
|
this.bgMusicVolume = 0.1;
|
|
this.effectMusicVolume = 1;
|
|
|
|
this.mMusicSwitch = 1;
|
|
this.mEffectSwitch = 1;
|
|
}
|
|
play (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;
|
|
}
|
|
volume = 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 {
|
|
return audioSource;
|
|
}
|
|
}
|
|
|
|
save () {
|
|
// 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() {
|
|
|
|
if (AudioManager._instance == null) {
|
|
AudioManager._instance = this;
|
|
cc.game.addPersistRootNode(this.node);
|
|
}
|
|
else {
|
|
this.node.destroy();
|
|
return;
|
|
}
|
|
this.reward = false;
|
|
this.finish = false;
|
|
this.rewardCount = 0;
|
|
this.ctor();
|
|
|
|
this.preload();
|
|
}
|
|
preload () {
|
|
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 (on) {
|
|
if (this.mMusicSwitch != (on?1:0))
|
|
{
|
|
this.mMusicSwitch = 1-this.mMusicSwitch;
|
|
// this.save();
|
|
|
|
}
|
|
if(on)
|
|
{
|
|
this.playMusicGame();
|
|
}
|
|
else
|
|
{
|
|
this.stopMusic();
|
|
}
|
|
}
|
|
switchEffect (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 (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,{},true);
|
|
}
|
|
/**
|
|
* 停止背景音乐播放
|
|
*/
|
|
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);
|
|
|
|
}
|
|
}
|
|
// 炸弹、火箭爆炸音效
|
|
playJump () {
|
|
|
|
}
|
|
//激光音效
|
|
playLuo()
|
|
{
|
|
return this.play(this.luodui, false,null,this.mEffectSwitch);
|
|
}
|
|
//技能音效
|
|
playGround()
|
|
{
|
|
|
|
}
|
|
|
|
//技能音效
|
|
playPz()
|
|
{
|
|
|
|
}
|
|
/*
|
|
* 游戏开始音效
|
|
*
|
|
*/
|
|
playGameStart()
|
|
{
|
|
|
|
}
|
|
/*
|
|
* 失败的游戏结束
|
|
*/
|
|
playGameOver()
|
|
{
|
|
|
|
}
|
|
/*
|
|
* 成功的游戏结束
|
|
*/
|
|
playGameResultFailed()
|
|
{
|
|
|
|
}
|
|
/*
|
|
* 成功的游戏结束
|
|
*/
|
|
playGameResultSuccess()
|
|
{
|
|
|
|
}
|
|
/**
|
|
* 报警的音效
|
|
*/
|
|
playWarning()
|
|
{
|
|
return this.play(this.audioWarning, false,null,this.mEffectSwitch);
|
|
}
|
|
|
|
/*
|
|
* 方块碰撞的声音
|
|
*/
|
|
playBrick()
|
|
{
|
|
// return this.play(this.brickSound,false);
|
|
}
|
|
//方块破碎的声音
|
|
brickBoom(){
|
|
|
|
}
|
|
/**
|
|
* 按钮
|
|
*/
|
|
playAudioButton () {
|
|
return this.play(this.audioButtonClick, false,null,this.mEffectSwitch);
|
|
}
|
|
|
|
playWin()
|
|
{
|
|
return this.play(this.audioWin, false,null,this.mEffectSwitch);
|
|
}
|
|
|
|
playRandomMatch()
|
|
{
|
|
this.playMusic(this.audioGameBgm0,{},true);
|
|
}
|
|
playMatchFoundSound()
|
|
{
|
|
this.stopMusic();
|
|
// return this.play(this.matchFoundSound, false);
|
|
}
|
|
|
|
};
|
|
|
|
// export { AudioManager };
|