From feaa250a9c8f826e5deb9c01f574a98b5969d560 Mon Sep 17 00:00:00 2001 From: oc Date: Wed, 19 Sep 2018 21:17:43 +0800 Subject: [PATCH] Async events --- _server/blockly/MotaAction.g4 | 70 +++++++++++++++++++---------------- docs/event.md | 22 ++++++++--- libs/events.js | 60 ++++++++++++++++++++++-------- 3 files changed, 101 insertions(+), 51 deletions(-) diff --git a/_server/blockly/MotaAction.g4 b/_server/blockly/MotaAction.g4 index b4e4f01d..b9729956 100644 --- a/_server/blockly/MotaAction.g4 +++ b/_server/blockly/MotaAction.g4 @@ -802,21 +802,22 @@ return code; */; viberate_s - : '画面震动' '时间' Int Newline + : '画面震动' '时间' Int '异步' Bool Newline /* viberate_s tooltip : viberate: 画面震动 helpUrl : https://ckcz123.github.io/mota-js/#/event?id=viberate%ef%bc%9a%e7%94%bb%e9%9d%a2%e9%9c%87%e5%8a%a8 -default : [2000] +default : [2000,false] colour : this.soundColor Int_0 = Int_0 ?(', "time": '+Int_0):''; -var code = '{"type": "viberate"' + Int_0 + '},\n'; +var async = Bool_0?', "async": true':'' +var code = '{"type": "viberate"' + Int_0 + async + '},\n'; return code; */; animate_s - : '显示动画' IdString '位置' EvalString? '不等待绘制完毕' Bool Newline + : '显示动画' IdString '位置' EvalString? '异步' Bool Newline /* animate_s @@ -835,7 +836,8 @@ if (EvalString_0) { throw new Error('此处只能填hero或者1,2形式的位置,或者不填代表当前事件点'); } } -var code = '{"type": "animate", "name": "'+IdString_0+'"'+EvalString_0+', "async": '+Bool_0+'},\n'; +var async = Bool_0?', "async": true':''; +var code = '{"type": "animate", "name": "'+IdString_0+'"'+EvalString_0+async+'},\n'; return code; */; @@ -865,28 +867,30 @@ return code; */; animateImage_0_s - : '图片淡入' EvalString '起点像素位置' 'x' PosString 'y' PosString '动画时间' Int Newline + : '图片淡入' EvalString '起点像素位置' 'x' PosString 'y' PosString '动画时间' Int '异步' Bool Newline /* animateImage_0_s tooltip : animageImage:图片淡入 helpUrl : https://ckcz123.github.io/mota-js/#/event?id=animateimage%EF%BC%9A%E5%9B%BE%E7%89%87%E6%B7%A1%E5%85%A5%E6%B7%A1%E5%87%BA -default : ["bg.jpg","0","0",500] +default : ["bg.jpg","0","0",500,false] colour : this.printColor -var code = '{"type": "animateImage", "action": "show", "name": "'+EvalString_0+'", "loc": ['+PosString_0+','+PosString_1+'], "time": '+Int_0+'},\n'; +var async = Bool_0?', "async": true':''; +var code = '{"type": "animateImage", "action": "show", "name": "'+EvalString_0+'", "loc": ['+PosString_0+','+PosString_1+'], "time": '+Int_0+async+'},\n'; return code; */; animateImage_1_s - : '图片淡出' EvalString '起点像素位置' 'x' PosString 'y' PosString '动画时间' Int Newline + : '图片淡出' EvalString '起点像素位置' 'x' PosString 'y' PosString '动画时间' Int '异步' Bool Newline /* animateImage_1_s tooltip : animageImage:图片淡出 helpUrl : https://ckcz123.github.io/mota-js/#/event?id=animateimage%EF%BC%9A%E5%9B%BE%E7%89%87%E6%B7%A1%E5%85%A5%E6%B7%A1%E5%87%BA -default : ["bg.jpg","0","0",500] +default : ["bg.jpg","0","0",500,false] colour : this.printColor -var code = '{"type": "animateImage", "action": "hide", "name": "'+EvalString_0+'", "loc": ['+PosString_0+','+PosString_1+'], "time": '+Int_0+'},\n'; +var async = Bool_0?', "async": true':''; +var code = '{"type": "animateImage", "action": "hide", "name": "'+EvalString_0+'", "loc": ['+PosString_0+','+PosString_1+'], "time": '+Int_0+async+'},\n'; return code; */; @@ -917,26 +921,27 @@ return code; moveImage_0_s : '图片移动' EvalString '起点像素位置' 'x' PosString 'y' PosString BGNL - '终点像素位置' 'x' PosString 'y' PosString '移动时间' Int Newline + '终点像素位置' 'x' PosString 'y' PosString '移动时间' Int '异步' Bool Newline /* moveImage_0_s tooltip : moveImage:图片移动 helpUrl : https://ckcz123.github.io/mota-js/#/event?id=moveimage%EF%BC%9A%E5%9B%BE%E7%89%87%E7%A7%BB%E5%8A%A8 -default : ["bg.jpg","0","0","0","0",500] +default : ["bg.jpg","0","0","0","0",500,false] colour : this.printColor -var code = '{"type": "moveImage", "name": "'+EvalString_0+'", "from": ['+PosString_0+','+PosString_1+'], "to": ['+PosString_2+','+PosString_3+'], "time": '+Int_0+'},\n'; +var async = Bool_0?', "async": true':''; +var code = '{"type": "moveImage", "name": "'+EvalString_0+'", "from": ['+PosString_0+','+PosString_1+'], "to": ['+PosString_2+','+PosString_3+'], "time": '+Int_0+async+'},\n'; return code; */; setFg_0_s - : '更改画面色调' Number ',' Number ',' Number ',' Number '动画时间' Int? Newline + : '更改画面色调' Number ',' Number ',' Number ',' Number '动画时间' Int? '异步' Bool Newline /* setFg_0_s tooltip : setFg: 更改画面色调,动画时间可不填 helpUrl : https://ckcz123.github.io/mota-js/#/event?id=setfg%EF%BC%9A%E6%9B%B4%E6%94%B9%E7%94%BB%E9%9D%A2%E8%89%B2%E8%B0%83 -default : [255,255,255,1,500] +default : [255,255,255,1,500,false] colour : this.soundColor var limit = function(v,min,max) { if(v>max) return max; @@ -948,21 +953,23 @@ Number_1 = limit(Number_1,0,255); Number_2 = limit(Number_2,0,255); Number_3 = limit(Number_3,0,1); Int_0 = Int_0 ?(', "time": '+Int_0):''; -var code = '{"type": "setFg", "color": ['+Number_0+','+Number_1+','+Number_2+','+Number_3+']'+Int_0 +'},\n'; +var async = Bool_0?', "async": true':''; +var code = '{"type": "setFg", "color": ['+Number_0+','+Number_1+','+Number_2+','+Number_3+']'+Int_0 +async+'},\n'; return code; */; setFg_1_s - : '恢复画面色调' '动画时间' Int? Newline + : '恢复画面色调' '动画时间' Int? '异步' Bool Newline /* setFg_1_s tooltip : setFg: 恢复画面色调,动画时间可不填 helpUrl : https://ckcz123.github.io/mota-js/#/event?id=setfg%EF%BC%9A%E6%9B%B4%E6%94%B9%E7%94%BB%E9%9D%A2%E8%89%B2%E8%B0%83 -default : [500] +default : [500,false] colour : this.soundColor Int_0 = Int_0 ?(', "time": '+Int_0):''; -var code = '{"type": "setFg"'+Int_0 +'},\n'; +var async = Bool_0?', "async": true':''; +var code = '{"type": "setFg"'+Int_0 +async+'},\n'; return code; */; @@ -1103,16 +1110,17 @@ return code; */; setVolume_s - : '设置音量' Int '渐变时间' Int? Newline + : '设置音量' Int '渐变时间' Int? '异步' Bool Newline /* setVolume_s tooltip : setVolume: 设置音量 helpUrl : https://ckcz123.github.io/mota-js/#/event?id=setvolume%EF%BC%9A%E8%AE%BE%E7%BD%AE%E9%9F%B3%E9%87%8F -default : [90, 500] +default : [90, 500, false] colour : this.soundColor Int_1 = Int_1?(', "time": '+Int_1):"" -var code = '{"type": "setVolume", "value": '+Int_0+Int_1+'},\n'; +var async = Bool_0?', "async": true':''; +var code = '{"type": "setVolume", "value": '+Int_0+Int_1+async+'},\n'; return code; */; @@ -1803,10 +1811,10 @@ ActionParser.prototype.parseAction = function() { var animate_loc = data.loc||''; if(animate_loc && animate_loc!=='hero')animate_loc = animate_loc[0]+','+animate_loc[1]; this.next = MotaActionBlocks['animate_s'].xmlText([ - data.name,animate_loc,data.async||0,this.next]); + data.name,animate_loc,data.async||false,this.next]); break; case "viberate": // 画面震动 - this.next = MotaActionBlocks['viberate_s'].xmlText([data.time||0, this.next]); + this.next = MotaActionBlocks['viberate_s'].xmlText([data.time||0, data.async||false, this.next]); break; case "showImage": // 显示图片 if(this.isset(data.name)){ @@ -1820,10 +1828,10 @@ ActionParser.prototype.parseAction = function() { case "animateImage": // 显示图片 if(data.action == 'show'){ this.next = MotaActionBlocks['animateImage_0_s'].xmlText([ - data.name,data.loc[0],data.loc[1],data.time,this.next]); + data.name,data.loc[0],data.loc[1],data.time,data.async||false,this.next]); } else if (data.action == 'hide') { this.next = MotaActionBlocks['animateImage_1_s'].xmlText([ - data.name,data.loc[0],data.loc[1],data.time,this.next]); + data.name,data.loc[0],data.loc[1],data.time,data.async||false,this.next]); } break; case "showGif": // 显示动图 @@ -1837,7 +1845,7 @@ ActionParser.prototype.parseAction = function() { break; case "moveImage": // 移动图片 this.next = MotaActionBlocks['moveImage_0_s'].xmlText([ - data.name, data.from[0], data.from[1], data.to[0], data.to[1], data.time, this.next + data.name, data.from[0], data.from[1], data.to[0], data.to[1], data.time, data.async||false, this.next ]); break; case "setFg": // 颜色渐变 @@ -1845,10 +1853,10 @@ ActionParser.prototype.parseAction = function() { var alpha = data.color[3]; if (alpha==undefined || alpha==null) alpha=1; this.next = MotaActionBlocks['setFg_0_s'].xmlText([ - data.color[0],data.color[1],data.color[2],alpha,data.time||0,this.next]); + data.color[0],data.color[1],data.color[2],alpha,data.time||0,data.async||false,this.next]); } else { this.next = MotaActionBlocks['setFg_1_s'].xmlText([ - data.time||0,this.next]); + data.time||0,data.async||false,this.next]); } break; case "setWeather": // 更改天气 @@ -1894,7 +1902,7 @@ ActionParser.prototype.parseAction = function() { break case "setVolume": this.next = MotaActionBlocks['setVolume_s'].xmlText([ - data.value, data.time, this.next]); + data.value, data.time, data.async||false, this.next]); break case "setValue": this.next = MotaActionBlocks['setValue_s'].xmlText([ diff --git a/docs/event.md b/docs/event.md index 0b788098..34ce372d 100644 --- a/docs/event.md +++ b/docs/event.md @@ -741,7 +741,11 @@ name为可选的,是要取消跟随的行走图文件名。 ### viberate:画面震动 -使用 `{"type": "viberate", "time": 2000}` 可以造成画面震动效果,后面time可以指定震动时间。 +使用 `{"type": "viberate", "time": 2000, "async": true}` 可以造成画面震动效果。 + +time可以指定震动时间,默认是2000毫秒。 + +async可选,如果为true则会异步执行(即不等待当前事件执行完毕,立刻执行下一个事件)。 ### animate:显示动画 @@ -795,7 +799,7 @@ loc为图片左上角坐标,以像素为单位进行计算。 ``` js "x,y": [ // 实际执行的事件列表 {"type": "animateImage", "action": "show", "name": "bg.jpg", "loc": [231,297], "time": 500}, // 在(231,297)淡入bg.jpg,动画时间500ms - {"type": "animateImage", "action": "hide", "name": "1.png", "loc": [109,167], "time": 300}, // 在(109,167)淡出1.png,动画时间300ms + {"type": "animateImage", "action": "hide", "name": "1.png", "loc": [109,167], "time": 300, "async": true}, // 在(109,167)淡出1.png,动画时间300ms,异步执行 ] ``` @@ -807,6 +811,8 @@ loc为图片左上角坐标,以像素为单位进行计算。 time为淡入淡出的时间,如果是0则忽略此项。 +async可选,如果为true则会异步执行(即不等待当前事件执行完毕,立刻执行下一个事件)。 + !> 淡入淡出图片只是会在顶层绘制“淡入”和“淡出”效果,动画结束即消失,并不会实际对图片的显示造成影响。请与showImage事件合用。 如果多张图片的淡入淡出可以采用以下方式(仅供参考): @@ -838,7 +844,7 @@ loc为动图左上角坐标,以像素为单位进行计算。 ``` js "x,y": [ // 实际执行的事件列表 - {"type": "moveImage", "name": "bg.jpg", "from": [231,297], "to": [22,333], "time": 500}, + {"type": "moveImage", "name": "bg.jpg", "from": [231,297], "to": [22,333], "time": 500, "async": true}, ] ``` @@ -850,6 +856,8 @@ to为终点图片左上角坐标,以像素为单位进行计算。 time为总移动的时间。 +async可选,如果为true则会异步执行(即不等待当前事件执行完毕,立刻执行下一个事件)。 + !> 移动图片只是会在顶层绘制“移动”效果,动画结束即消失,并不会实际对图片的显示造成影响。请与showImage事件合用。 ### setFg:更改画面色调 @@ -859,7 +867,7 @@ time为总移动的时间。 ``` js "x,y": [ // 实际执行的事件列表 {"type": "setFg", "color": [255,255,255,0.6], "time": 1000}, // 更改画面色调为纯白,不透明度0.6,动画时间1000毫秒 - {"type": "setFg", "color": [0,0,0]}, // 更改画面色调为纯黑,不透明度1,不指定动画时间(使用默认时间) + {"type": "setFg", "color": [0,0,0], "async": true}, // 更改画面色调为纯黑,不透明度1,不指定动画时间(使用默认时间),且异步执行 {"type": "setFg"} // 如果不指定color则恢复原样。 ] ``` @@ -872,6 +880,8 @@ color为需要更改画面色调的颜色。它是一个数组,分别指定目 time为可选的,如果指定,则会作为更改画面色调的时间。 +async可选,如果为true则会异步执行(即不等待当前事件执行完毕,立刻执行下一个事件)。 + ### setWeather:更改天气 我们可以使用 `{"type": "setWeather"}` 来更改天气。 @@ -1030,12 +1040,14 @@ time选项为该跳跃所需要用到的时间。 使用setVolume可以设置音量大小。 -使用方法: `{"type": "setVolume", "value": 90, "time": 500}` +使用方法: `{"type": "setVolume", "value": 90, "time": 500, "async": true}` value为音量大小,在0到100之间,默认为100。设置后,BGM和SE都将使用该音量进行播放。 可以设置time为音量渐变时间。 +async可选,如果为true则会异步执行(即不等待当前事件执行完毕,立刻执行下一个事件)。 + ### win:获得胜利 `{"type": "win", "reason": "xxx"}` 将会直接调用events.js中的win函数,并将reason作为结局传入。 diff --git a/libs/events.js b/libs/events.js index c39f31e7..0eca9ad7 100644 --- a/libs/events.js +++ b/libs/events.js @@ -594,9 +594,15 @@ events.prototype.doAction = function() { } else { if (core.isset(data.loc) && core.isset(core.material.images.images[data.name]) && (data.action=="show" || data.action=="hide")) { - core.events.animateImage(data.action, core.material.images.images[data.name], data.loc, data.time, function() { - core.events.doAction(); - }); + if (data.async) { + core.events.animateImage(data.action, core.material.images.images[data.name], data.loc, data.time); + this.doAction(); + } + else { + core.events.animateImage(data.action, core.material.images.images[data.name], data.loc, data.time, function() { + core.events.doAction(); + }); + } } else { this.doAction(); @@ -625,9 +631,15 @@ events.prototype.doAction = function() { } else { if (core.isset(data.from) && core.isset(data.to) && core.isset(core.material.images.images[data.name])) { - core.events.moveImage(core.material.images.images[data.name], data.from, data.to, data.time, function() { - core.events.doAction(); - }); + if (data.async) { + core.events.moveImage(core.material.images.images[data.name], data.from, data.to, data.time); + this.doAction(); + } + else { + core.events.moveImage(core.material.images.images[data.name], data.from, data.to, data.time, function() { + core.events.doAction(); + }); + } } else { this.doAction(); @@ -635,9 +647,15 @@ events.prototype.doAction = function() { } break; case "setFg": // 颜色渐变 - core.setFg(data.color, data.time, function() { - core.events.doAction(); - }); + if (data.async) { + core.setFg(data.color, data.time); + this.doAction(); + } + else { + core.setFg(data.color, data.time, function() { + core.events.doAction(); + }); + } break; case "setWeather": // 更改天气 core.setWeather(data.name, data.level); @@ -722,9 +740,15 @@ events.prototype.doAction = function() { data.value = parseInt(data.value||0); if (data.value<0) data.value=0; if (data.value>100) data.value=100; - this.setVolume(data.value/100, data.time, function() { - core.doAction(); - }); + if (data.async) { + this.setVolume(data.value/100, data.time); + this.doAction(); + } + else { + this.setVolume(data.value/100, data.time, function() { + core.events.doAction(); + }); + } break; case "setValue": try { @@ -896,9 +920,15 @@ events.prototype.doAction = function() { this.doAction(); break; case "viberate": - core.events.vibrate(data.time, function () { - core.events.doAction(); - }) + if (data.async) { + core.events.vibrate(data.time); + this.doAction(); + } + else { + core.events.vibrate(data.time, function () { + core.events.doAction(); + }) + } break; case "sleep": // 等待多少毫秒 if (core.status.replay.replaying)