From feaac1735e830b928547d6433c5dbe3d01463e6b Mon Sep 17 00:00:00 2001 From: shijian <2954700422@qq.com> Date: Mon, 18 Dec 2023 02:12:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dasync=20content=E4=B8=AD?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E4=B8=8D=E4=BD=BF=E7=94=A8await=E4=BC=9A?= =?UTF-8?q?=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=982?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- character/standard.js | 2 ++ game/game.js | 66 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/character/standard.js b/character/standard.js index 63328a2be..1795fdb24 100755 --- a/character/standard.js +++ b/character/standard.js @@ -2211,6 +2211,8 @@ game.import('character',function(lib,game,ui,get,ai,_status){ frequent:true, preHidden:true, async content(event,trigger,player){ + player.damage(); + //player.recover(); await player.draw(); }, }, diff --git a/game/game.js b/game/game.js index 3e14af9d2..469e4a3dc 100644 --- a/game/game.js +++ b/game/game.js @@ -32148,24 +32148,56 @@ new Promise(resolve=>{ // 如果父级事件也是一个异步的话,那应该立即执行这个事件的 // 如果在AsyncFunction执行过程中在别的位置新建了一个异步事件,那也直接(等会set配置完)执行 if (eventPromise&&_status.event.content instanceof AsyncFunction){ - if(_status.event!=eventPromise){ - eventPromise.parent=_status.event; - _status.event=eventPromise; - game.getGlobalHistory('everything').push(eventPromise); - } // 异步执行game.loop // 不直接game.loop(event)是因为需要让别人可以手动set()和setContent() // 再执行game.loop是因为原有的game.loop被await卡住了, // 得新执行一个只执行这个异步事件的game.loop - game.executingAsyncEvent=Promise.resolve().then(()=>game.loop(eventPromise)) - .then(()=>{ - delete game.executingAsyncEvent; - }); + if(!game.executingAsyncEventMap.has(_status.event.toEvent())){ + game.executingAsyncEventMap.set(_status.event.toEvent(),Promise.resolve().then(()=>{ + console.log(event.name, '将要执行'); + if(_status.event!=eventPromise){ + eventPromise.parent=_status.event; + _status.event=eventPromise; + game.getGlobalHistory('everything').push(eventPromise); + } + return game.loop(eventPromise); + }).then(()=>{ + console.log(event.name, '执行完毕'); + return event.name; + })); + } + else{ + game.executingAsyncEventMap.set(_status.event.toEvent(),game.executingAsyncEventMap.get(_status.event.toEvent()).then(()=>{ + console.log(event.name,'将要执行'); + if (_status.event != eventPromise) { + eventPromise.parent = _status.event; + _status.event = eventPromise; + game.getGlobalHistory('everything').push(eventPromise); + } + return game.loop(eventPromise); + }).then(()=>{ + console.log(event.name, '执行完毕'); + return event.name; + })); + } + }else{ + console.error(event.name,'没有执行'); + console.error(_status.event.toEvent()); + return event.name; } }; + if(!game.executingAsyncEventMap){ + game.executingAsyncEventMap=new Map(); + } // 没有await上个事件(也就是_status.event) - if(game.executingAsyncEvent) game.executingAsyncEvent.then(callback); - else callback(); + if(game.executingAsyncEventMap.has(_status.event.toEvent())){ + console.log(event.name,'正在等待',_status.event.name,'执行'); + game.executingAsyncEventMap.set(_status.event.toEvent(),game.executingAsyncEventMap.get(_status.event.toEvent()).then(callback)); + } + else{ + console.log(event.name,'立即执行'); + callback(); + } }); }); this.#event=event; @@ -41642,8 +41674,16 @@ new Promise(resolve=>{ else if (event.content instanceof AsyncFunction) { // _status,lib,game,ui,get,ai六个变量由game.import提供 event.content(event, trigger, player).then(() => { - event.finish(); - resolve(); + if (game.executingAsyncEventMap && game.executingAsyncEventMap.has(event.toEvent())) { + game.executingAsyncEventMap.get(event.toEvent()).then(() => { + game.executingAsyncEventMap.delete(event.toEvent()); + event.finish(); + resolve(); + }); + } else { + event.finish(); + resolve(); + } }); } else {