From 9adcb5543f90ea3f6efb8ef03e032f07981497a8 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Fri, 1 Dec 2023 21:47:43 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=84=9A=E6=9C=AC=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E9=83=A8=E5=88=86=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components.d.ts | 1 + public/libs/actions.js | 2 - public/libs/events.js | 10 ++-- public/project/functions.js | 109 ------------------------------------ src/core/main/game.ts | 8 +++ src/plugin/game/hook.ts | 61 ++++++++++++++++++++ src/plugin/ui/equipbox.tsx | 8 +-- src/plugin/utils.ts | 26 +++++++++ src/styles.less | 18 ++++++ src/types/action.d.ts | 5 -- src/types/control.d.ts | 6 -- src/types/function.d.ts | 6 -- src/types/map.d.ts | 2 +- src/ui/equipbox.vue | 10 ++-- 14 files changed, 127 insertions(+), 145 deletions(-) create mode 100644 src/plugin/game/hook.ts diff --git a/components.d.ts b/components.d.ts index b7530c5..3a6c095 100644 --- a/components.d.ts +++ b/components.d.ts @@ -17,6 +17,7 @@ declare module '@vue/runtime-core' { ASwitch: typeof import('ant-design-vue/es')['Switch'] Box: typeof import('./src/components/box.vue')['default'] BoxAnimate: typeof import('./src/components/boxAnimate.vue')['default'] + Changable: typeof import('./src/components/changable.vue')['default'] Colomn: typeof import('./src/components/colomn.vue')['default'] EnemyOne: typeof import('./src/components/enemyOne.vue')['default'] Scroll: typeof import('./src/components/scroll.vue')['default'] diff --git a/public/libs/actions.js b/public/libs/actions.js index cb1d5d0..20cbd9e 100644 --- a/public/libs/actions.js +++ b/public/libs/actions.js @@ -18,7 +18,6 @@ function actions() { } actions.prototype._init = function () { - this.actionsdata = functions_d6ad677b_427a_4623_b50f_a445a3b0ef8a.actions; this.actions = {}; // --- onkeyDown注册 this.registerAction( @@ -531,7 +530,6 @@ actions.prototype._sys_keyUp_lockControl = function (keyCode, altKey) { actions.prototype._sys_keyUp = function (keyCode, altKey) { if (!core.status.played) return true; - this.actionsdata.onKeyUp(keyCode, altKey); if (core.status.automaticRoute && core.status.automaticRoute.autoHeroMove) { core.stopAutomaticRoute(); } diff --git a/public/libs/events.js b/public/libs/events.js index ca0051f..1ca09e0 100644 --- a/public/libs/events.js +++ b/public/libs/events.js @@ -471,7 +471,7 @@ events.prototype.openDoor = function (x, y, needKey, callback) { core.removeBlock(x, y); setTimeout(function () { core.status.replay.animate = false; - core.events.afterOpenDoor(block.event.id, x, y); + mota.game.hook.emit('afterOpenDoor', block.event.id, x, y); if (callback) callback(); }, 1); // +1是为了录像检测系统 } else { @@ -556,7 +556,7 @@ events.prototype._openDoor_animate = function (block, x, y, callback) { core.maps._removeBlockFromMap(core.status.floorId, block); if (!locked) core.unlockControl(); core.status.replay.animate = false; - core.events.afterOpenDoor(block.event.id, x, y); + mota.game.hook.emit('afterOpenDoor', block.event.id, x, y); if (callback) callback(); }; @@ -582,7 +582,7 @@ events.prototype._openDoor_animate = function (block, x, y, callback) { ////// 开一个门后触发的事件 ////// events.prototype.afterOpenDoor = function (doorId, x, y) { - return this.eventdata.afterOpenDoor(doorId, x, y); + // Deprecated. See hook#afterOpenDoor. }; events.prototype._sys_getItem = function (data, callback) { @@ -636,12 +636,12 @@ events.prototype.getItem = function (id, num, x, y, isGentleClick, callback) { itemHint.push(id); } - this.afterGetItem(id, x, y, isGentleClick); + mota.game.hook.emit('afterGetItem', id, x, y, isGentleClick); if (callback) callback(); }; events.prototype.afterGetItem = function (id, x, y, isGentleClick) { - this.eventdata.afterGetItem(id, x, y, isGentleClick); + // Deprecated. See hook#afterGetItem }; ////// 获得面前的物品(轻按) ////// diff --git a/public/project/functions.js b/public/project/functions.js index ddee5e7..42d43b4 100644 --- a/public/project/functions.js +++ b/public/project/functions.js @@ -260,90 +260,6 @@ var functions_d6ad677b_427a_4623_b50f_a445a3b0ef8a = { core.changeFloor(toId, stair, loc, null, callback); return true; - }, - afterOpenDoor: function (doorId, x, y) { - // 开一个门后触发的事件 - - var todo = []; - // 检查该点的获得开门后事件。 - if (core.status.floorId == null) return; - var event = - core.floors[core.status.floorId].afterOpenDoor[x + ',' + y]; - if (event) core.unshift(todo, event); - - if (todo.length > 0) core.insertAction(todo, x, y); - - if (core.status.event.id == null) core.continueAutomaticRoute(); - else core.clearContinueAutomaticRoute(); - }, - afterGetItem: function (itemId, x, y, isGentleClick) { - // 获得一个道具后触发的事件 - // itemId:获得的道具ID;x和y是该道具所在的坐标 - // isGentleClick:是否是轻按触发的 - if ( - (itemId.endsWith('Potion') || - itemId == 'I482' || - itemId == 'I484' || - itemId == 'I487' || - itemId == 'I491') && - core.material.items[itemId].cls == 'items' - ) - core.playSound('回血'); - else core.playSound('获得道具'); - - var todo = []; - // 检查该点的获得道具后事件。 - if (core.status.floorId == null) return; - var event = - core.floors[core.status.floorId].afterGetItem[x + ',' + y]; - if ( - event && - (event instanceof Array || - !isGentleClick || - !event.disableOnGentleClick) - ) { - core.unshift(todo, event); - } - if (core.hasFlag('spring')) { - if (!core.hasFlag('springCount')) - core.setFlag('springCount', 0); - if ( - itemId.endsWith('Potion') || - itemId == 'I482' || - itemId == 'I484' || - itemId == 'I487' || - itemId == 'I491' - ) { - core.addFlag('springCount', 1); - } - if (core.getFlag('springCount', 0) == 50) { - core.setFlag('springCount', 0); - core.status.hero.hpmax += - core.getNakedStatus('hpmax') * 0.1; - } - core.updateStatusBar(); - } - - if (todo.length > 0) core.insertAction(todo, x, y); - } - }, - actions: { - onKeyUp: function (keyCode, altKey) { - // 键盘按键处理,可以在这里自定义快捷键列表 - // keyCode:当前按键的keyCode(每个键的keyCode自行百度) - // altKey:Alt键是否被按下,为true代表同时按下了Alt键 - // 可以在这里任意增加或编辑每个按键的行为 - - if (core.status.lockControl) return; - - // 如果处于正在行走状态,则不处理 - if (core.isMoving()) return; - - // Alt+0~9,快捷换上套装 - if (altKey && keyCode >= 48 && keyCode <= 57) { - core.items.quickLoadEquip(keyCode - 48); - return; - } } }, control: { @@ -436,31 +352,6 @@ var functions_d6ad677b_427a_4623_b50f_a445a3b0ef8a = { } }); }, - getStatusLabel: function (name) { - // 返回某个状态英文名的对应中文标签,如atk -> 攻击,def -> 防御等。 - // 请注意此项仅影响 libs/ 下的内容(如绘制怪物手册、数据统计等) - // 自行定义的(比如获得道具效果)中用到的“攻击+3”等需要自己去对应地方修改 - - return ( - { - name: '名称', - lv: '等级', - hpmax: '生命回复', - hp: '生命', - manamax: '魔力上限', - mana: '额外攻击', - atk: '攻击', - def: '防御', - mdef: '智慧', - money: '金币', - exp: '经验', - point: '加点', - steps: '步数', - up: '升级', - none: '无' - }[name] || name - ); - }, updateStatusBar: function () { // 更新状态栏 diff --git a/src/core/main/game.ts b/src/core/main/game.ts index a94ad13..6007db6 100644 --- a/src/core/main/game.ts +++ b/src/core/main/game.ts @@ -8,6 +8,14 @@ export interface GameEvent extends EmitableEvent { mounted: () => void; /** Emitted in plugin/ui.js */ statusBarUpdate: () => void; + /** Emitted in libs/events.js */ + afterGetItem: ( + itemId: AllIdsOf<'items'>, + x: number, + y: number, + isGentleClick: boolean + ) => void; + afterOpenDoor: (doorId: AllIdsOf<'animates'>, x: number, y: number) => void; } export const hook = new EventEmitter(); diff --git a/src/plugin/game/hook.ts b/src/plugin/game/hook.ts new file mode 100644 index 0000000..ee9ecad --- /dev/null +++ b/src/plugin/game/hook.ts @@ -0,0 +1,61 @@ +export {}; + +const potionItems: AllIdsOf<'items'>[] = [ + 'redPotion', + 'bluePotion', + 'yellowPotion', + 'greenPotion', + 'I482', + 'I484', + 'I487', + 'I491' +]; + +mota.game.hook.on('afterGetItem', (itemId, x, y, isGentleClick) => { + // 获得一个道具后触发的事件 + // itemId:获得的道具ID;x和y是该道具所在的坐标 + // isGentleClick:是否是轻按触发的 + if (potionItems.includes(itemId)) core.playSound('回血'); + else core.playSound('获得道具'); + + const todo: any[] = []; + // 检查该点的获得道具后事件。 + if (core.status.floorId == null) return; + const event = core.floors[core.status.floorId].afterGetItem[`${x},${y}`]; + if ( + event && + (event instanceof Array || + !isGentleClick || + !event.disableOnGentleClick) + ) { + core.unshift(todo, event as any[]); + } + if (core.hasFlag('spring')) { + if (!core.hasFlag('springCount')) core.setFlag('springCount', 0); + if (potionItems.includes(itemId)) { + core.addFlag('springCount', 1); + } + if (core.getFlag('springCount', 0) === 50) { + core.setFlag('springCount', 0); + core.status.hero.hpmax += core.getNakedStatus('hpmax') * 0.1; + } + core.updateStatusBar(); + } + + if (todo.length > 0) core.insertAction(todo, x, y); +}); + +mota.game.hook.on('afterOpenDoor', (doorId, x, y) => { + // 开一个门后触发的事件 + + const todo: any[] = []; + // 检查该点的获得开门后事件。 + if (core.status.floorId == null) return; + const event = core.floors[core.status.floorId].afterOpenDoor[`${x},${y}`]; + if (event) core.unshift(todo, event as any[]); + + if (todo.length > 0) core.insertAction(todo, x, y); + + if (core.status.event.id == null) core.continueAutomaticRoute(); + else core.clearContinueAutomaticRoute(); +}); diff --git a/src/plugin/ui/equipbox.tsx b/src/plugin/ui/equipbox.tsx index 922b3c1..e29c9ad 100644 --- a/src/plugin/ui/equipbox.tsx +++ b/src/plugin/ui/equipbox.tsx @@ -1,4 +1,4 @@ -import { has } from '../utils'; +import { getStatusLabel, has } from '../utils'; /** * 获取所有装备 @@ -28,9 +28,7 @@ export function getAddStatus(equip: Equip) { return ( - - {core.getStatusLabel(v)} - + {getStatusLabel(v)}      - {core.getStatusLabel(v)} + {getStatusLabel(v)}
{status} diff --git a/src/plugin/utils.ts b/src/plugin/utils.ts index 27f3768..d858f20 100644 --- a/src/plugin/utils.ts +++ b/src/plugin/utils.ts @@ -307,3 +307,29 @@ export function generateBinary(arr: boolean[]) { }); return num; } + +/** + * 获得某个状态的中文名 + * @param name 要获取的属性名 + */ +export function getStatusLabel(name: string) { + return ( + { + name: '名称', + lv: '等级', + hpmax: '生命回复', + hp: '生命', + manamax: '魔力上限', + mana: '额外攻击', + atk: '攻击', + def: '防御', + mdef: '智慧', + money: '金币', + exp: '经验', + point: '加点', + steps: '步数', + up: '升级', + none: '无' + }[name] || name + ); +} diff --git a/src/styles.less b/src/styles.less index ca50296..43e769c 100644 --- a/src/styles.less +++ b/src/styles.less @@ -93,3 +93,21 @@ z-index: 999; user-select: none; } + +.changable { + border-radius: 4px; + padding: 2px 4px; +} + +.changable[change='true'] { + animation: changable-animate 1s linear alternate 1; +} + +@keyframes changable-animate { + 0% { + background-color: #7cc9; + } + 100% { + background-color: #0000; + } +} diff --git a/src/types/action.d.ts b/src/types/action.d.ts index f667b4c..88a18e7 100644 --- a/src/types/action.d.ts +++ b/src/types/action.d.ts @@ -86,11 +86,6 @@ interface Actions extends VoidedActionFuncs { */ readonly _HY_: number; - /** - * 脚本编辑中的交互函数 - */ - readonly actionsdata: ActionData; - /** * 所有已注册的交互操作 */ diff --git a/src/types/control.d.ts b/src/types/control.d.ts index 639573b..21fd83f 100644 --- a/src/types/control.d.ts +++ b/src/types/control.d.ts @@ -698,12 +698,6 @@ interface Control { */ getNakedStatus(name?: keyof NumbericHeroStatus): number; - /** - * 获得某个状态的中文名 - * @param name 要获取的属性名 - */ - getStatusLabel(name: string): string; - /** * 设置主角某个属性的百分比修正倍率,初始值为1, * 倍率存放在flag: `__${name}_${buff}__` 中 diff --git a/src/types/function.d.ts b/src/types/function.d.ts index 647af35..e6f0015 100644 --- a/src/types/function.d.ts +++ b/src/types/function.d.ts @@ -20,12 +20,6 @@ interface ControlData { */ loadData(data: Save, callback?: () => void): void; - /** - * 获取一个属性对应的中文名 - * @param name 要获取的状态名称 - */ - getStatusLabel(name: string): string; - /** * 立即仅更新状态栏 */ diff --git a/src/types/map.d.ts b/src/types/map.d.ts index 74be353..cddf381 100644 --- a/src/types/map.d.ts +++ b/src/types/map.d.ts @@ -215,7 +215,7 @@ interface ResolvedFloor extends FloorBase { /** * 获得道具后事件 */ - afterGetItem: Record; + afterGetItem: Record; /** * 开门后事件 diff --git a/src/ui/equipbox.vue b/src/ui/equipbox.vue index 53dfc1f..afbadc9 100644 --- a/src/ui/equipbox.vue +++ b/src/ui/equipbox.vue @@ -13,7 +13,7 @@
{{ - t === 'none' ? '所有' : label(t) + t === 'none' ? '所有' : getStatusLabel(t) }} @@ -191,12 +191,12 @@ import { import Scroll from '../components/scroll.vue'; import { getAddStatus, getEquips, getNowStatus } from '../plugin/ui/equipbox'; import BoxAnimate from '../components/boxAnimate.vue'; -import { has, keycode, tip, type } from '../plugin/utils'; +import { has, tip, type } from '../plugin/utils'; import { cancelGlobalDrag, isMobile, useDrag } from '../plugin/use'; -import { hyper, sleep } from 'mutate-animate'; -import { KeyCode } from '../plugin/keyCodes'; +import { hyper } from 'mutate-animate'; import { GameUi } from '@/core/main/custom/ui'; import { gameKey } from '@/core/main/init/hotkey'; +import { getStatusLabel } from '../plugin/utils'; const props = defineProps<{ num: number; @@ -223,8 +223,6 @@ const sType = ref<'value' | 'percentage'>('value'); // 攻击 防御 回血 额外攻击 const normList = ['none', 'atk', 'def', 'hpmax', 'mana']; -const label = core.getStatusLabel; - watch(sort, n => { selected.value = toShow.value.length - selected.value - 1; });