From eafcfa4deb7cb8ea413c193715620d77a610bb3a Mon Sep 17 00:00:00 2001 From: oc Date: Sat, 5 May 2018 01:54:57 +0800 Subject: [PATCH] setVolume --- _server/blockly/MotaAction.g4 | 18 ++++++++++++++++++ _server/editor_blockly.js | 1 + docs/event.md | 8 ++++++++ libs/control.js | 6 ++++-- libs/core.js | 4 ++++ libs/events.js | 11 +++++++++++ 6 files changed, 46 insertions(+), 2 deletions(-) diff --git a/_server/blockly/MotaAction.g4 b/_server/blockly/MotaAction.g4 index c0cc73ff..6949caff 100644 --- a/_server/blockly/MotaAction.g4 +++ b/_server/blockly/MotaAction.g4 @@ -216,6 +216,7 @@ action | pauseBgm_s | resumeBgm_s | playSound_s + | setVolume_s | win_s | lose_s | if_s @@ -834,6 +835,19 @@ var code = '{"type": "playSound", "name": "'+EvalString_0+'"},\n'; return code; */ +setVolume_s + : '设置音量' Int Newline + ; + +/* setVolume_s +tooltip : setVolume: 设置音量 +helpUrl : https://ckcz123.github.io/mota-js/#/event?id=setVolume-%e8%ae%be%e7%bd%ae%e9%9f%b3%e9%87%8f +default : [90] +colour : this.soundColor +var code = '{"type": "setVolume", "value": '+Int_0+'},\n'; +return code; +*/ + win_s : '游戏胜利,结局' ':' EvalString? Newline ; @@ -1452,6 +1466,10 @@ ActionParser.prototype.parseAction = function() { this.next = MotaActionBlocks['resumeBgm_s'].xmlText([ this.next]); break + case "setVolume": + this.next = MotaActionBlocks['setVolume_s'].xmlText([ + data.value, this.next]); + break case "setValue": this.next = MotaActionBlocks['setValue_s'].xmlText([ MotaActionBlocks['idString_e'].xmlText([data.name]), diff --git a/_server/editor_blockly.js b/_server/editor_blockly.js index ba05260b..a7325e30 100644 --- a/_server/editor_blockly.js +++ b/_server/editor_blockly.js @@ -111,6 +111,7 @@ editor_blockly = function () { MotaActionBlocks['pauseBgm_s'].xmlText(), MotaActionBlocks['resumeBgm_s'].xmlText(), MotaActionBlocks['playSound_s'].xmlText(), + MotaActionBlocks['setVolume_s'].xmlText(), '', MotaActionBlocks['function_s'].xmlText(), ], diff --git a/docs/event.md b/docs/event.md index 89300fc6..1c936fa4 100644 --- a/docs/event.md +++ b/docs/event.md @@ -842,6 +842,14 @@ move完毕后移动的NPC/怪物一定会消失,只不过可以通过immediate 值得注意的是,如果是额外添加进文件的音效,则需在main.js中this.sounds里加载它。 +### setVolume:设置音量 + +使用setVolume可以设置音量大小。 + +使用方法: `{"type": "setVolume", "value": 90}` + +value为音量大小,在0到100之间,默认为100。设置后,BGM和SE都将使用该音量进行播放。 + ### win: 获得胜利 `{"type": "win", "reason": "xxx"}` 将会直接调用events.js中的win函数,并将reason作为结局传入。 diff --git a/libs/control.js b/libs/control.js index fdc58a67..0073f330 100644 --- a/libs/control.js +++ b/libs/control.js @@ -2110,6 +2110,7 @@ control.prototype.playBgm = function (bgm) { } // 播放当前BGM core.musicStatus.playingBgm = bgm; + core.material.bgms[bgm].volume = core.musicStatus.volume; core.material.bgms[bgm].play(); core.musicStatus.isPlaying = true; @@ -2177,7 +2178,7 @@ control.prototype.playSound = function (sound) { if (core.musicStatus.audioContext != null) { var source = core.musicStatus.audioContext.createBufferSource(); source.buffer = core.material.sounds[sound]; - source.connect(core.musicStatus.audioContext.destination); + source.connect(core.musicStatus.gainNode); try { source.start(0); } @@ -2190,11 +2191,12 @@ control.prototype.playSound = function (sound) { } } else { + core.material.sounds[sound].volume = core.musicStatus.volume; core.material.sounds[sound].play(); } } catch (eee) { - console.log("无法播放SE "+bgm); + console.log("无法播放SE "+sound); console.log(eee); } } diff --git a/libs/core.js b/libs/core.js index a1fe38db..2370e57b 100644 --- a/libs/core.js +++ b/libs/core.js @@ -48,6 +48,8 @@ function core() { 'soundStatus': true, // 是否播放SE 'playingBgm': null, // 正在播放的BGM 'isPlaying': false, + 'gainNode': null, + 'volume': 1.0, // 音量 } this.platform = { 'isOnline': true, // 是否http @@ -187,6 +189,8 @@ core.prototype.init = function (coreData, callback) { window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.msAudioContext; try { core.musicStatus.audioContext = new window.AudioContext(); + core.musicStatus.gainNode = core.musicStatus.audioContext.createGain(); + core.musicStatus.gainNode.connect(core.musicStatus.audioContext.destination); } catch (e) { console.log("该浏览器不支持AudioContext"); core.musicStatus.audioContext = null; diff --git a/libs/events.js b/libs/events.js index c5b5616a..c30ab341 100644 --- a/libs/events.js +++ b/libs/events.js @@ -537,6 +537,17 @@ events.prototype.doAction = function() { core.resumeBgm(); this.doAction(); break + case "setVolume": + data.value = parseInt(data.value||0); + if (data.value>100) data.value=100; + data.value = data.value / 100; + core.musicStatus.volume = data.value; + if (core.isset(core.musicStatus.playingBgm)) { + core.material.bgms[core.musicStatus.playingBgm].volume = data.value; + } + core.musicStatus.gainNode.gain.value = data.value; + this.doAction(); + break; case "setValue": try { var value=core.calValue(data.value);