249 lines
5.9 KiB
TypeScript
249 lines
5.9 KiB
TypeScript
|
|
|
|
const { ccclass, property } = cc._decorator;
|
|
@ccclass
|
|
export default class AudioManager extends cc.Component {
|
|
static _instance: any;
|
|
//背景音乐
|
|
@property(cc.AudioClip)
|
|
audioGameBgm0: cc.AudioClip = null;
|
|
|
|
@property(cc.AudioClip)
|
|
xiaochu: cc.AudioClip = null;
|
|
@property(cc.AudioClip)
|
|
hit: cc.AudioClip = null;
|
|
@property(cc.AudioClip)
|
|
down: cc.AudioClip = null;
|
|
@property(cc.AudioClip)
|
|
fangxiang: cc.AudioClip = null;
|
|
@property(cc.AudioClip)
|
|
build: cc.AudioClip = null;
|
|
@property(cc.AudioClip)
|
|
win: cc.AudioClip = null;
|
|
@property(cc.AudioClip)
|
|
lose: cc.AudioClip = null;
|
|
@property(cc.AudioClip)
|
|
anniu_Big: cc.AudioClip = null;
|
|
@property(cc.AudioClip)
|
|
anniu_little: cc.AudioClip = null;
|
|
@property(cc.AudioClip)
|
|
zhuan1: cc.AudioClip = null;
|
|
@property(cc.AudioClip)
|
|
zhuan2: cc.AudioClip = null;
|
|
|
|
mAudioMap: {};
|
|
bgMusicVolume: number;
|
|
effectMusicVolume: number;
|
|
mMusicSwitch: number;
|
|
mEffectSwitch: number;
|
|
brickSound: any;
|
|
reward: boolean;
|
|
finish: boolean;
|
|
rewardCount: number;
|
|
mMusicKey: any;
|
|
|
|
onLoad() {
|
|
if (AudioManager._instance == null) {
|
|
AudioManager._instance = this;
|
|
cc.fx.AudioManager = AudioManager;
|
|
cc.game.addPersistRootNode(this.node);
|
|
}
|
|
else {
|
|
return;
|
|
}
|
|
this.reward = false;
|
|
this.finish = false;
|
|
this.rewardCount = 0;
|
|
this.ctor();
|
|
|
|
this.preload();
|
|
}
|
|
|
|
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;
|
|
cc.audioEngine.setEffectsVolume(1);
|
|
cc.audioEngine.setMusicVolume(1);
|
|
if (audioSource.name == "lose") {
|
|
cc.audioEngine.setEffectsVolume(0.5);
|
|
}
|
|
else {
|
|
cc.audioEngine.setEffectsVolume(1);
|
|
}
|
|
var context = cc.audioEngine.playEffect(audioSource, loop);
|
|
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);
|
|
}
|
|
|
|
// static get Instance()
|
|
// {
|
|
// if (this._instance == null)
|
|
// {
|
|
// this._instance = new AudioManager();
|
|
// }
|
|
// return this._instance;
|
|
// }
|
|
|
|
preload() {
|
|
if (!(cc.sys.platform === cc.sys.WECHAT_GAME)) { return; }
|
|
|
|
var musics = [
|
|
this.audioGameBgm0,
|
|
];
|
|
musics.forEach(function (path) {
|
|
})
|
|
console.log("音乐开关", cc.fx.GameConfig.GM_INFO.musicOpen);
|
|
this.playMusicGame();
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
onShow() {
|
|
cc.audioEngine.resumeAll();
|
|
}
|
|
|
|
//播放音效
|
|
playEffect(name, callback) {
|
|
if (!cc.fx.GameConfig.GM_INFO.effectOpen) {
|
|
return;
|
|
}
|
|
if (this[name])
|
|
return this.play(this[name], false, callback, this.mEffectSwitch);
|
|
}
|
|
playMusic(key, callback, loop) {
|
|
if (!cc.fx.GameConfig.GM_INFO.musicOpen) {
|
|
return;
|
|
}
|
|
loop = typeof loop == 'undefined' || loop ? true : false;
|
|
this.stopMusic();
|
|
this.mMusicKey = this.play(key, loop, callback, true);
|
|
|
|
}
|
|
/**
|
|
* 游戏背景音乐
|
|
*/
|
|
playMusicGame() {
|
|
this.stopMusic();
|
|
this.playMusic(this.audioGameBgm0, {}, true);
|
|
}
|
|
/**
|
|
* 停止背景音乐播放
|
|
*/
|
|
stopMusic() {
|
|
// cc.wwx.OutPut.log('stopMusic audio effect wx: ' + this.mMusicKey);
|
|
cc.audioEngine.stopAll();
|
|
}
|
|
|
|
/**
|
|
* 恢复被暂停的背景音乐播放
|
|
*/
|
|
resumeMusic() {
|
|
// 调用 cc.audioEngine 的 resumeMusic 方法恢复音乐播放
|
|
cc.audioEngine.resumeMusic();
|
|
}
|
|
|
|
/*
|
|
* 游戏开始音效
|
|
*
|
|
*/
|
|
playGameStart() {
|
|
|
|
}
|
|
/*
|
|
* 失败的游戏结束
|
|
*/
|
|
playGameOver() {
|
|
|
|
}
|
|
/*
|
|
* 成功的游戏结束
|
|
*/
|
|
playGameResultFailed() {
|
|
|
|
}
|
|
/*
|
|
* 成功的游戏结束
|
|
*/
|
|
playGameResultSuccess() {
|
|
|
|
}
|
|
/**
|
|
* 报警的音效
|
|
*/
|
|
|
|
/**
|
|
* 按钮
|
|
*/
|
|
playAudioButton() {
|
|
// return this.play(this.audioButtonClick, false,null,this.mEffectSwitch);
|
|
}
|
|
};
|
|
|
|
// export { AudioManager };
|