From 7e156e81c9b3cacfb562a4f29d8f71090444147b Mon Sep 17 00:00:00 2001 From: ckcz123 Date: Mon, 11 May 2020 18:00:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0temp:A=E8=A1=A8=E7=A4=BA?= =?UTF-8?q?=E4=B8=B4=E6=97=B6=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _server/MotaAction.g4 | 35 ++++++++++++++++++++++++------- _server/editor_blockly.js | 5 +++-- libs/events.js | 44 ++++++++++++++++++++++----------------- libs/ui.js | 6 ++++++ libs/utils.js | 2 ++ 5 files changed, 63 insertions(+), 29 deletions(-) diff --git a/_server/MotaAction.g4 b/_server/MotaAction.g4 index 27af623c..2a55f521 100644 --- a/_server/MotaAction.g4 +++ b/_server/MotaAction.g4 @@ -1888,8 +1888,8 @@ for_s tooltip : for:循环遍历 helpUrl : https://h5mota.com/games/template/_docs/#/event?id=while%ef%bc%9a%e5%89%8d%e7%bd%ae%e6%9d%a1%e4%bb%b6%e5%be%aa%e7%8e%af colour : this.eventColor -if (!/^switch:[A-Z]$/.test(expression_0)) { - throw new Error('循环遍历仅允许使用独立开关!'); +if (!/^temp:[A-Z]$/.test(expression_0)) { + throw new Error('循环遍历仅允许使用临时变量!'); } return '{"type": "for", "name": "'+expression_0+'", "from": "'+EvalString_0+'", "to": "'+EvalString_1+'", "step": "'+EvalString_2+'",\n"data": [\n'+action_0+']},\n'; */; @@ -1901,8 +1901,8 @@ forEach_s tooltip : forEach:循环遍历列表 helpUrl : https://h5mota.com/games/template/_docs/#/event?id=while%ef%bc%9a%e5%89%8d%e7%bd%ae%e6%9d%a1%e4%bb%b6%e5%be%aa%e7%8e%af colour : this.eventColor -if (!/^switch:[A-Z]$/.test(expression_0)) { - throw new Error('循环遍历仅允许使用独立开关!'); +if (!/^temp:[A-Z]$/.test(expression_0)) { + throw new Error('循环遍历仅允许使用临时变量!'); } if (JsonEvalString_0 == '' || !(JSON.parse(JsonEvalString_0) instanceof Array)) { throw new Error('参数列表必须是个有效的数组!'); @@ -2461,6 +2461,7 @@ expression | bool_e | idString_e | evFlag_e + | evTemp_e | evalString_e @@ -2613,6 +2614,18 @@ return [code, Blockly.JavaScript.ORDER_ATOMIC]; */; +evTemp_e + : '临时变量' Letter_List + + +/* evTemp_e +colour : this.idstring_eColor +default : ["A"] +var code = "temp:"+Letter_List_0; +return [code, Blockly.JavaScript.ORDER_ATOMIC]; +*/; + + evalString_e : EvalString @@ -2773,8 +2786,8 @@ FixedId_List /*FixedId_List ['status:hp','status:atk','status:def','status:mdef','item:yellowKey','item:blueKey','item:redKey','status:money','status:exp']*/; Id_List - : '变量' | '状态' | '物品' | '独立开关' | '全局存储' - /*Id_List ['flag','status','item', 'switch', 'global']*/; + : '变量' | '状态' | '物品' | '独立开关' | '临时变量' |'全局存储' + /*Id_List ['flag','status','item', 'switch', 'temp', 'global']*/; EnemyId_List : '生命'|'攻击'|'防御'|'金币'|'经验'|'加点'|'属性'|'名称'|'映射名'|'value'|'atkValue'|'defValue'|'notBomb'|'zoneSquare'|'range'|'n'|'add'|'damage' @@ -2845,6 +2858,7 @@ this.block('idString_4_e').output='idString_e'; this.block('idString_5_e').output='idString_e'; this.block('idString_6_e').output='idString_e'; this.block('evFlag_e').output='idString_e'; +this.block('evTemp_e').output='idString_e'; */ /* Functions @@ -3752,6 +3766,11 @@ ActionParser.prototype.tryToUseEvFlag_e = function(defaultType, args, isShadow, args[0]=match[1] return MotaActionBlocks['evFlag_e'].xmlText(args, isShadow, comment); } + match=/^temp:([A-Z])$/.exec(args[0]) + if(match){ + args[0]=match[1] + return MotaActionBlocks['evTemp_e'].xmlText(args, isShadow, comment); + } return MotaActionBlocks[defaultType||'evalString_e'].xmlText(args, isShadow, comment); } @@ -3958,7 +3977,7 @@ MotaActionFunctions.replaceToName = function (str) { str = str.replace(new RegExp("item:(" + list.join("|") + ")", "g"), function (a, b) { return map[b] ? ("物品:" + map[b]) : b; }).replace(/item:/g, "物品:"); - str = str.replace(/flag:/g, "变量:").replace(/switch:/g, "独立开关:").replace(/global:/g, "全局存储:"); + str = str.replace(/flag:/g, "变量:").replace(/switch:/g, "独立开关:").replace(/global:/g, "全局存储:").replace(/temp:/g, "临时变量:"); map = {}; list = []; MotaActionFunctions.pattern.replaceEnemyList.forEach(function (v) { @@ -3988,7 +4007,7 @@ MotaActionFunctions.replaceFromName = function (str) { str = str.replace(new RegExp("物品[::](" + list.join("|") + ")", "g"), function (a, b) { return map[b] ? ("item:" + map[b]) : b; }).replace(/物品[::]/g, "item:"); - str = str.replace(/变量[::]/g, "flag:").replace(/独立开关[::]/g, "switch:").replace(/全局存储[::]/g, "global:"); + str = str.replace(/临时变量[::]/g, "temp:").replace(/变量[::]/g, "flag:").replace(/独立开关[::]/g, "switch:").replace(/全局存储[::]/g, "global:"); map = {}; list = []; MotaActionFunctions.pattern.replaceEnemyList.forEach(function (v) { diff --git a/_server/editor_blockly.js b/_server/editor_blockly.js index 3a75f8df..b8c3f36f 100644 --- a/_server/editor_blockly.js +++ b/_server/editor_blockly.js @@ -143,8 +143,8 @@ editor_blockly = function () { {"action": [{"type": "comment", "text": "当判别值是值的场合执行此事件"}]}, {"case": "default", "action": [{"type": "comment", "text": "当没有符合的值的场合执行default事件"}]}, ]}), - MotaActionFunctions.actionParser.parseList({"type": "for", "name": "switch:A", "from": "0", "to": "12", "step": "1", "data": []}), - MotaActionFunctions.actionParser.parseList({"type": "forEach", "name": "switch:A", "list": ["status:atk","status:def"], "data": []}), + MotaActionFunctions.actionParser.parseList({"type": "for", "name": "temp:A", "from": "0", "to": "12", "step": "1", "data": []}), + MotaActionFunctions.actionParser.parseList({"type": "forEach", "name": "temp:A", "list": ["status:atk","status:def"], "data": []}), MotaActionBlocks['while_s'].xmlText(), MotaActionBlocks['dowhile_s'].xmlText(), MotaActionBlocks['break_s'].xmlText(), @@ -220,6 +220,7 @@ editor_blockly = function () { ]), MotaActionBlocks['expression_arithmetic_0'].xmlText(), MotaActionBlocks['evFlag_e'].xmlText(), + MotaActionBlocks['evTemp_e'].xmlText(), MotaActionBlocks['negate_e'].xmlText(), MotaActionBlocks['bool_e'].xmlText(), MotaActionBlocks['idString_e'].xmlText(), diff --git a/libs/events.js b/libs/events.js index 450d8146..6778633c 100644 --- a/libs/events.js +++ b/libs/events.js @@ -1824,9 +1824,9 @@ events.prototype._precompile_confirm = function (data) { } events.prototype._action_for = function (data, x, y, prefix) { - // Only support switch:A - if (!/^switch:[A-Z]$/.test(data.name)) { - core.insertAction(['循环遍历事件只支持独立开关!']); + // Only support temp:A + if (!/^temp:[A-Z]$/.test(data.name)) { + core.insertAction('循环遍历事件只支持临时变量!'); return core.doAction(); } var from = core.calValue(data.from); @@ -1834,22 +1834,22 @@ events.prototype._action_for = function (data, x, y, prefix) { core.insertAction('循环遍历事件要求【起始点】仅能是数字!'); return core.doAction(); } - this._setValue_setSwitch(data.name, from, prefix); - var toName = '__for@to@' + prefix + '@' + data.name.substring(7) + '__'; - var stepName = '__for@step@' + prefix + '@' + data.name.substring(7) + '__'; + var letter = data.name.substring(5); + core.setFlag('@temp@' + letter, from); + var toName = '@temp@for-to@' + letter; + var stepName = '@temp@for-step@' + letter; core.setFlag(toName, data.to); core.setFlag(stepName, data.step); var condition = "(function () {"+ - "var clearAndReturn = function (v) { if (!v) { core.removeFlag('"+toName+"'); core.removeFlag('"+stepName+"'); } return v; };"+ "var to = core.calValue(core.getFlag('" + toName + "'));"+ "var step = core.calValue(core.getFlag('" + stepName + "'));"+ - "if (typeof step != 'number' || typeof to != 'number') return clearAndReturn(false);"+ - "if (step == 0) return clearAndReturn(true);"+ - "var currentValue = core.calValue('switch:'+'" + data.name.substring(7) + "', '"+prefix+"');"+ + "if (typeof step != 'number' || typeof to != 'number') return false;"+ + "if (step == 0) return true;"+ + "var currentValue = core.getFlag('@temp@" + letter + "');"+ "currentValue += step;"+ - "core.events._setValue_setSwitch('switch:'+'" + data.name.substring(7) + "', currentValue, '"+prefix+"');"+ - "if (step > 0) { return clearAndReturn(currentValue <= to); }"+ - "else { return clearAndReturn(currentValue >= to); }"+ + "core.setFlag('@temp@" + letter + "', currentValue);"+ + "if (step > 0) { return currentValue <= to; }"+ + "else { return currentValue >= to; }"+ "})()"; return this._action_dowhile({"condition": condition, "data": data.data}, x, y, prefix); } @@ -1863,17 +1863,17 @@ events.prototype._precompile_for = function (data) { } events.prototype._action_forEach = function (data, x, y, prefix) { - // Only support switch:A - if (!/^switch:[A-Z]$/.test(data.name)) { - core.insertAction(['循环遍历事件只支持独立开关!']); + // Only support temp:A + if (!/^temp:[A-Z]$/.test(data.name)) { + core.insertAction(['循环遍历事件只支持临时变量!']); return core.doAction(); } - var listName = '__forEach@' + prefix + '@' + data.name.substring(7) + '__'; + var listName = '@temp@forEach@' + data.name.substring(5); core.setFlag(listName, core.clone(data.list)); var condition = "(function () {" + "var list = core.getFlag('"+listName+"', []);"+ - "if (list.length == 0) { core.removeFlag('" + listName + "'); return false; }"+ - "core.events._setValue_setSwitch('switch:'+'" + data.name.substring(7) + "', list.shift(), '"+prefix+"');"+ + "if (list.length == 0) return false;"+ + "core.setFlag('@temp@'+'" + data.name.substring(5) + "', list.shift());"+ "return true;"+ "})()"; return this._action_while({"condition": condition, "data": data.data}, x, y, prefix); @@ -2456,6 +2456,7 @@ events.prototype.setValue = function (name, operator, value, prefix) { this._setValue_setItem(name, value); this._setValue_setFlag(name, value); this._setValue_setSwitch(name, value, prefix); + this._setValue_setTemp(name, value, prefix); this._setValue_setGlobal(name, value); } @@ -2481,6 +2482,11 @@ events.prototype._setValue_setSwitch = function (name, value, prefix) { core.setFlag((prefix || ":f@x@y") + "@" + name.substring(7), value); } +events.prototype._setValue_setTemp = function (name, value) { + if (name.indexOf("temp:") !== 0) return; + core.setFlag("@temp@" + name.substring(5), value); +} + events.prototype._setValue_setGlobal = function (name, value) { if (name.indexOf("global:") !== 0) return; core.setGlobal(name.substring(7), value); diff --git a/libs/ui.js b/libs/ui.js index 965d4b80..c69461fd 100644 --- a/libs/ui.js +++ b/libs/ui.js @@ -443,6 +443,12 @@ ui.prototype._uievent_drawIcon = function (data) { ////// 结束一切事件和绘制,关闭UI窗口,返回游戏进程 ////// ui.prototype.closePanel = function () { + // 清除全部临时变量 + Object.keys(core.status.hero.flags).forEach(function (name) { + if (name.startsWith("@temp@")) { + delete core.status.hero.flags[name]; + } + }); this.clearUI(); core.maps.generateGroundPattern(); core.updateStatusBar(true); diff --git a/libs/utils.js b/libs/utils.js index ba920a20..d7e0f870 100644 --- a/libs/utils.js +++ b/libs/utils.js @@ -89,6 +89,8 @@ utils.prototype.replaceValue = function (value) { value = value.replace(/blockCls:(\d+),(\d+)/g, "core.getBlockCls($1, $2)"); if (value.indexOf('equip:')>=0) value = value.replace(/equip:(\d)/g, "core.getEquip($1)"); + if (value.indexOf('temp:')>=0) + value = value.replace(/temp:([a-zA-Z0-9_]+)/g, "core.getFlag('@temp@$1', 0)"); } return value; }