From 5e4a8c1eb34491388b81027670203c6fa4824f96 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Sun, 12 Nov 2023 22:54:19 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E5=8A=A8=E9=BC=A0?= =?UTF-8?q?=E6=A0=87=E5=AE=9A=E7=82=B9=E6=9F=A5=E7=9C=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/libs/control.js | 2 +- public/project/functions.js | 8 +- src/core/index.ts | 1 + src/core/main/custom/hotkey.ts | 8 +- src/core/main/custom/ui.ts | 9 +- src/core/main/game.ts | 114 ++++++++++++++----------- src/core/main/init/fixed.ts | 55 ++++++++++++ src/core/main/init/index.ts | 1 + src/core/plugin.ts | 2 - src/plugin/game/enemy/damage.ts | 4 + src/plugin/game/ui.js | 2 +- src/plugin/ui/fixed.ts | 66 +++++++-------- src/types/action.d.ts | 9 ++ src/types/plugin.d.ts | 3 - src/ui/fixed.vue | 145 ++++++++++++++++++-------------- 15 files changed, 264 insertions(+), 165 deletions(-) create mode 100644 src/core/main/init/fixed.ts create mode 100644 src/core/main/init/index.ts diff --git a/public/libs/control.js b/public/libs/control.js index 572f746..c4fa532 100644 --- a/public/libs/control.js +++ b/public/libs/control.js @@ -3013,7 +3013,7 @@ control.prototype.removeSwitch = function (x, y, floorId, name) { control.prototype.lockControl = function () { core.status.lockControl = true; if (!main.replayChecking) { - mota.plugin.fixed.showFixed.value = false; + // mota.plugin.fixed.showFixed.value = false; } }; diff --git a/public/project/functions.js b/public/project/functions.js index 59e7cdd..c634bc4 100644 --- a/public/project/functions.js +++ b/public/project/functions.js @@ -405,16 +405,16 @@ var functions_d6ad677b_427a_4623_b50f_a445a3b0ef8a = { break; case 67: // C:怪物临界 if (core.getBlockCls(mx, my)?.startsWith('enemy')) { - mota.plugin.fixed.showFixed.value = false; - mota.ui.main.open('fixedDetail', void 0, { + // mota.plugin.fixed.showFixed.value = false; + mota.ui.main.open('fixedDetail', { panel: 'critical' }); } break; case 69: // E:怪物属性 if (core.getBlockCls(mx, my)?.startsWith('enemy')) { - mota.plugin.fixed.showFixed.value = false; - mota.ui.main.open('fixedDetail', void 0, { + // mota.plugin.fixed.showFixed.value = false; + mota.ui.main.open('fixedDetail', { panel: 'special' }); } diff --git a/src/core/index.ts b/src/core/index.ts index 2c411bf..49b6083 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -8,6 +8,7 @@ import { GameEvent, hook } from './main/game'; import { fixedUi, mainUi } from './main/init/ui'; import { GameStorage } from './main/storage'; import { resolvePlugin } from './plugin'; +import './main/init/'; interface AncTePlugin { pop: ReturnType; diff --git a/src/core/main/custom/hotkey.ts b/src/core/main/custom/hotkey.ts index 4a9b763..df5e802 100644 --- a/src/core/main/custom/hotkey.ts +++ b/src/core/main/custom/hotkey.ts @@ -302,8 +302,8 @@ hotkey const [x, y] = flags.mouseLoc ?? []; const [mx, my] = getLocFromMouseLoc(x, y); if (core.getBlockCls(mx, my)?.startsWith('enemy')) { - mota.plugin.fixed.showFixed.value = false; - mota.ui.main.open('fixedDetail', void 0, { + // mota.plugin.fixed.showFixed.value = false; + mota.ui.main.open('fixedDetail', { panel: 'special' }); } @@ -315,8 +315,8 @@ hotkey const [x, y] = flags.mouseLoc ?? []; const [mx, my] = getLocFromMouseLoc(x, y); if (core.getBlockCls(mx, my)?.startsWith('enemy')) { - mota.plugin.fixed.showFixed.value = false; - mota.ui.main.open('fixedDetail', void 0, { + // mota.plugin.fixed.showFixed.value = false; + mota.ui.main.open('fixedDetail', { panel: 'critical' }); } diff --git a/src/core/main/custom/ui.ts b/src/core/main/custom/ui.ts index 1b0dfd4..ab0bdd0 100644 --- a/src/core/main/custom/ui.ts +++ b/src/core/main/custom/ui.ts @@ -150,7 +150,7 @@ export class GameUi extends EventEmitter { * @param vOn 监听的事件 * @param vBind 绑定的数据 */ - with(vOn?: UiVOn, vBind?: UiVBind): ShowableGameUi { + with(vBind?: UiVBind, vOn?: UiVOn): ShowableGameUi { return { ui: this, vOn, vBind }; } } @@ -257,15 +257,16 @@ export class UiController extends Focus { * @param vBind 绑定的数据 * @returns ui的唯一标识符 */ - open(id: string, vOn?: UiVOn, vBind?: UiVBind) { + open(id: string, vBind?: UiVBind, vOn?: UiVOn) { const ui = this.get(id); if (!ui) return -1; const num = this.num++; - const sui = ui.with(vOn, { + const bind = { num, ui, ...(vBind ?? {}) - }); + }; + const sui = ui.with(bind, vOn); this.add({ num, ...sui diff --git a/src/core/main/game.ts b/src/core/main/game.ts index 26633ff..8255d4c 100644 --- a/src/core/main/game.ts +++ b/src/core/main/game.ts @@ -11,9 +11,12 @@ export interface GameEvent extends EmitableEvent { export const hook = new EventEmitter(); interface ListenerEvent extends EmitableEvent { - hoverBlock: (block: Block) => void; - leaveBlock: (block: Block) => void; - clickBlock: (block: Block) => void; + // block + hoverBlock: (block: Block, ev: MouseEvent) => void; + leaveBlock: (block: Block, ev: MouseEvent, leaveGame: boolean) => void; + clickBlock: (block: Block, ev: MouseEvent) => void; + // mouse + mouseMove: (ev: MouseEvent) => void; } class GameListener extends EventEmitter { @@ -24,74 +27,83 @@ class GameListener extends EventEmitter { constructor() { super(); - loading.once('coreInit', () => { + if (!!window.core) { this.init(); - }); + } else { + loading.once('coreInit', () => { + this.init(); + }); + } } private init() { - // block + // ----- block let lastHoverX = -1; let lastHoverY = -1; - const getBlockLoc = (px: number, py: number) => { + const data = core.canvas.data.canvas; + + const getBlockLoc = (px: number, py: number, size: number) => { return [ - Math.floor((px - core.bigmap.offsetX) / 32), - Math.floor((py - core.bigmap.offsetY) / 32) + Math.floor(((px * 32) / size - core.bigmap.offsetX) / 32), + Math.floor(((py * 32) / size - core.bigmap.offsetY) / 32) ]; }; - core.registerAction( - 'onmove', - `@GameListener_${this.num}_block`, - (x, y, px, py) => { - if (core.status.lockControl || !core.isPlaying()) return false; - const [bx, by] = getBlockLoc(px, py); - const blocks = core.getMapBlocksObj(); - if (lastHoverX !== bx || lastHoverY !== by) { - const lastBlock = blocks[`${lastHoverX},${lastHoverY}`]; - const block = blocks[`${bx},${by}`]; - if (!!lastBlock) { - this.emit('leaveBlock', lastBlock); - } - if (!!block) { - this.emit('hoverBlock', block); - lastHoverX = bx; - lastHoverY = by; - } else { - lastHoverX = -1; - lastHoverY = -1; - } + // hover & leave + data.addEventListener('mousemove', e => { + if (core.status.lockControl || !core.isPlaying()) return; + this.emit('mouseMove', e); + const { + x: px, + y: py, + size + } = core.actions._getClickLoc(e.clientX, e.clientY); + const [bx, by] = getBlockLoc(px, py, size); + const blocks = core.getMapBlocksObj(); + if (lastHoverX !== bx || lastHoverY !== by) { + const lastBlock = blocks[`${lastHoverX},${lastHoverY}`]; + const block = blocks[`${bx},${by}`]; + if (!!lastBlock) { + this.emit('leaveBlock', lastBlock, e, false); } - return false; - }, - 50 - ); - core.canvas.data.canvas.addEventListener('mouseleave', () => { + if (!!block) { + this.emit('hoverBlock', block, e); + lastHoverX = bx; + lastHoverY = by; + } else { + lastHoverX = -1; + lastHoverY = -1; + } + } + }); + data.addEventListener('mouseleave', e => { if (core.status.lockControl || !core.isPlaying()) return; const blocks = core.getMapBlocksObj(); const lastBlock = blocks[`${lastHoverX},${lastHoverY}`]; if (!!lastBlock) { - this.emit('leaveBlock', lastBlock); + this.emit('leaveBlock', lastBlock, e, true); } lastHoverX = -1; lastHoverY = -1; }); - core.registerAction( - 'onup', - `@GameListener_${this.num}_block`, - (x, y, px, py) => { - if (core.status.lockControl || !core.isPlaying()) return false; - const [bx, by] = getBlockLoc(px, py); - const blocks = core.getMapBlocksObj(); - const block = blocks[`${bx},${by}`]; - if (!!block) { - this.emit('clickBlock', block); - } - return false; - }, - 50 - ); + // click + data.addEventListener('click', e => { + if (core.status.lockControl || !core.isPlaying()) return; + const { + x: px, + y: py, + size + } = core.actions._getClickLoc(e.clientX, e.clientY); + const [bx, by] = getBlockLoc(px, py, size); + const blocks = core.getMapBlocksObj(); + const block = blocks[`${bx},${by}`]; + if (!!block) { + this.emit('clickBlock', block, e); + } + }); + + // ----- mouse } } diff --git a/src/core/main/init/fixed.ts b/src/core/main/init/fixed.ts new file mode 100644 index 0000000..b60f17a --- /dev/null +++ b/src/core/main/init/fixed.ts @@ -0,0 +1,55 @@ +import { debounce } from 'lodash-es'; +import { gameListener } from '../game'; +import { fixedUi } from './ui'; +import { ref } from 'vue'; +import { sleep } from 'mutate-animate'; + +const close = ref(false); + +let cx = 0; +let cy = 0; + +/** + * 显示定点查看 + */ +const showFixed = debounce((block: Block) => { + const e = core.material.enemys[block.event.id as EnemyIds]; + if (!e) return; + const enemy = core.status.thisMap.enemy.get(block.x, block.y); + fixedUi.open( + 'fixed', + { enemy, close, loc: [cx, cy] }, + { close: closeFixed } + ); +}, 200); + +/** + * 关闭定点查看 + */ +const closeFixed = () => { + close.value = true; + sleep(200).then(() => { + fixedUi.closeByName('fixed'); + close.value = false; + }); +}; + +let hovered: Block | null; + +gameListener.on('hoverBlock', block => { + closeFixed(); + hovered = block; +}); +gameListener.on('leaveBlock', (_, __, leaveGame) => { + showFixed.cancel(); + if (!leaveGame) closeFixed(); + hovered = null; +}); +gameListener.on('mouseMove', e => { + cx = e.clientX; + cy = e.clientY; + showFixed.cancel(); + if (hovered) { + showFixed(hovered); + } +}); diff --git a/src/core/main/init/index.ts b/src/core/main/init/index.ts new file mode 100644 index 0000000..12a857d --- /dev/null +++ b/src/core/main/init/index.ts @@ -0,0 +1 @@ +import './fixed'; diff --git a/src/core/plugin.ts b/src/core/plugin.ts index 4d890bd..4005c22 100644 --- a/src/core/plugin.ts +++ b/src/core/plugin.ts @@ -8,7 +8,6 @@ import mark from '@/plugin/mark'; import chapter from '@/plugin/ui/chapter'; import fly from '@/plugin/ui/fly'; import chase from '@/plugin/chase/chase'; -import fixed from '@/plugin/ui/fixed'; import webglUtils from '@/plugin/webgl/utils'; import shadow from '@/plugin/shadow/shadow'; import gameShadow from '@/plugin/shadow/gameShadow'; @@ -33,7 +32,6 @@ export function resolvePlugin() { ['chapter', chapter()], ['fly', fly()], ['chase', chase()], - ['fixed', fixed()], ['webglUtils', webglUtils()], ['shadow', shadow()], ['gameShadow', gameShadow()], diff --git a/src/plugin/game/enemy/damage.ts b/src/plugin/game/enemy/damage.ts index 2751c4c..3665a13 100644 --- a/src/plugin/game/enemy/damage.ts +++ b/src/plugin/game/enemy/damage.ts @@ -74,6 +74,10 @@ export class EnemyCollection implements RangeCollection { this.extract(); } + get(x: number, y: number) { + return this.list.find(v => v.x === x && v.y === y); + } + /** * 解析本地图的怪物信息 */ diff --git a/src/plugin/game/ui.js b/src/plugin/game/ui.js index 071299e..c54c1fe 100644 --- a/src/plugin/game/ui.js +++ b/src/plugin/game/ui.js @@ -11,7 +11,7 @@ export {}; function openItemShop(itemShopId) { if (!core.isReplaying()) { - mota.ui.main.open('shop', void 0, { + mota.ui.main.open('shop', { shopId: itemShopId }); } diff --git a/src/plugin/ui/fixed.ts b/src/plugin/ui/fixed.ts index 3aee28d..3ade2f7 100644 --- a/src/plugin/ui/fixed.ts +++ b/src/plugin/ui/fixed.ts @@ -5,46 +5,46 @@ import { ToShowEnemy, detailInfo } from './book'; import { DamageEnemy } from '../game/enemy/damage'; import { isMobile } from '../use'; -export const showFixed = ref(false); +// export const showFixed = ref(false); -let lastId: EnemyIds; +// let lastId: EnemyIds; -const show = debounce((ev: MouseEvent) => { - if (!window.flags) return; - if (!flags.mouseLoc) return; - flags.clientLoc = [ev.clientX, ev.clientY]; - const [mx, my] = getLocFromMouseLoc(...flags.mouseLoc); +// const show = debounce((ev: MouseEvent) => { +// if (!window.flags) return; +// if (!flags.mouseLoc) return; +// flags.clientLoc = [ev.clientX, ev.clientY]; +// const [mx, my] = getLocFromMouseLoc(...flags.mouseLoc); - const e = core.status.thisMap.enemy.list.find(v => { - return v.x === mx && v.y === my; - }); +// const e = core.status.thisMap.enemy.list.find(v => { +// return v.x === mx && v.y === my; +// }); - if (!e) return; +// if (!e) return; - lastId = e.id; - const detail = getDetailedEnemy(e); - detailInfo.enemy = detail; - showFixed.value = true; -}, 200); +// lastId = e.id; +// const detail = getDetailedEnemy(e); +// detailInfo.enemy = detail; +// showFixed.value = true; +// }, 200); -export default function init() { - const data = core.canvas.data.canvas; - data.addEventListener('mousemove', ev => { - if (!core.isPlaying() || core.status.lockControl) return; - const [mx, my] = getLocFromMouseLoc(...flags.mouseLoc); - const e = core.getBlockId(mx, my); - if (e !== lastId) showFixed.value = false; - if (!e) return; - show(ev); - }); - data.addEventListener('mousedown', ev => { - showFixed.value = false; - }); +// export default function init() { +// const data = core.canvas.data.canvas; +// data.addEventListener('mousemove', ev => { +// if (!core.isPlaying() || core.status.lockControl) return; +// const [mx, my] = getLocFromMouseLoc(...flags.mouseLoc); +// const e = core.getBlockId(mx, my); +// if (e !== lastId) showFixed.value = false; +// if (!e) return; +// show(ev); +// }); +// data.addEventListener('mousedown', ev => { +// showFixed.value = false; +// }); - return { - showFixed - }; -} +// return { +// showFixed +// }; +// } export function getLocFromMouseLoc(x: number, y: number): LocArr { const mx = Math.round(x + core.bigmap.offsetX / 32); diff --git a/src/types/action.d.ts b/src/types/action.d.ts index 862e1d7..f667b4c 100644 --- a/src/types/action.d.ts +++ b/src/types/action.d.ts @@ -136,6 +136,15 @@ interface Actions extends VoidedActionFuncs { _getNextFlyFloor(delta: number, index: number): number; _clickGameInfo_openComments(); + + _getClickLoc( + x: number, + y: number + ): { + x: number; + y: number; + size: number; + }; } declare const actions: new () => Actions; diff --git a/src/types/plugin.d.ts b/src/types/plugin.d.ts index be61d72..435a1d8 100644 --- a/src/types/plugin.d.ts +++ b/src/types/plugin.d.ts @@ -177,9 +177,6 @@ interface PluginUis { /** 是否展示标记的怪物 */ readonly showMarkedEnemy: Ref; - /** 是否展示移动鼠标显示怪物信息的盒子 */ - readonly showFixed: Ref; - /** * 显示章节 * @param chapter 显示的文字 diff --git a/src/ui/fixed.vue b/src/ui/fixed.vue index 49a1db7..f344e4a 100644 --- a/src/ui/fixed.vue +++ b/src/ui/fixed.vue @@ -1,63 +1,94 @@