2023-08-05 12:12:02 +08:00
|
|
|
import { EmitableEvent, EventEmitter } from '../common/eventEmitter';
|
2023-11-12 19:41:07 +08:00
|
|
|
import { loading } from '../loader/load';
|
2023-08-05 12:12:02 +08:00
|
|
|
|
|
|
|
export interface GameEvent extends EmitableEvent {
|
2023-10-29 22:13:37 +08:00
|
|
|
/** Emitted in events.prototype.resetGame. */
|
2023-08-05 12:12:02 +08:00
|
|
|
reset: () => void;
|
2023-10-29 22:13:37 +08:00
|
|
|
/** Emitted in src/App.vue setup. */
|
|
|
|
mounted: () => void;
|
2023-11-14 18:21:38 +08:00
|
|
|
/** Emitted in plugin/ui.js */
|
|
|
|
statusBarUpdate: () => void;
|
2023-08-05 12:12:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const hook = new EventEmitter<GameEvent>();
|
2023-11-12 19:41:07 +08:00
|
|
|
|
|
|
|
interface ListenerEvent extends EmitableEvent {
|
2023-11-12 22:54:19 +08:00
|
|
|
// 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;
|
2023-11-12 19:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class GameListener extends EventEmitter<ListenerEvent> {
|
|
|
|
static num: number = 0;
|
|
|
|
|
|
|
|
num: number = GameListener.num++;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2023-11-12 22:54:19 +08:00
|
|
|
if (!!window.core) {
|
2023-11-12 19:41:07 +08:00
|
|
|
this.init();
|
2023-11-12 22:54:19 +08:00
|
|
|
} else {
|
|
|
|
loading.once('coreInit', () => {
|
|
|
|
this.init();
|
|
|
|
});
|
|
|
|
}
|
2023-11-12 19:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private init() {
|
2023-11-12 22:54:19 +08:00
|
|
|
// ----- block
|
2023-11-12 19:41:07 +08:00
|
|
|
let lastHoverX = -1;
|
|
|
|
let lastHoverY = -1;
|
2023-11-12 20:47:46 +08:00
|
|
|
|
2023-11-12 22:54:19 +08:00
|
|
|
const data = core.canvas.data.canvas;
|
|
|
|
|
|
|
|
const getBlockLoc = (px: number, py: number, size: number) => {
|
2023-11-12 20:47:46 +08:00
|
|
|
return [
|
2023-11-12 22:54:19 +08:00
|
|
|
Math.floor(((px * 32) / size - core.bigmap.offsetX) / 32),
|
|
|
|
Math.floor(((py * 32) / size - core.bigmap.offsetY) / 32)
|
2023-11-12 20:47:46 +08:00
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2023-11-14 18:21:38 +08:00
|
|
|
// hover & leave & mouseMove
|
2023-11-12 22:54:19 +08:00
|
|
|
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);
|
2023-11-12 19:41:07 +08:00
|
|
|
}
|
2023-11-12 22:54:19 +08:00
|
|
|
if (!!block) {
|
|
|
|
this.emit('hoverBlock', block, e);
|
|
|
|
lastHoverX = bx;
|
|
|
|
lastHoverY = by;
|
|
|
|
} else {
|
|
|
|
lastHoverX = -1;
|
|
|
|
lastHoverY = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
data.addEventListener('mouseleave', e => {
|
2023-11-12 19:41:07 +08:00
|
|
|
if (core.status.lockControl || !core.isPlaying()) return;
|
|
|
|
const blocks = core.getMapBlocksObj();
|
|
|
|
const lastBlock = blocks[`${lastHoverX},${lastHoverY}`];
|
|
|
|
if (!!lastBlock) {
|
2023-11-12 22:54:19 +08:00
|
|
|
this.emit('leaveBlock', lastBlock, e, true);
|
2023-11-12 19:41:07 +08:00
|
|
|
}
|
|
|
|
lastHoverX = -1;
|
|
|
|
lastHoverY = -1;
|
|
|
|
});
|
2023-11-12 22:54:19 +08:00
|
|
|
// 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
|
2023-11-12 19:41:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const gameListener = new GameListener();
|