mirror of
https://github.com/unanmed/HumanBreak.git
synced 2025-01-19 20:59:37 +08:00
feat: hoverBlock & leaveBlock
This commit is contained in:
parent
8fc7e03c97
commit
7be7764c04
@ -1,4 +1,5 @@
|
||||
import { EmitableEvent, EventEmitter } from '../common/eventEmitter';
|
||||
import { loading } from '../loader/load';
|
||||
|
||||
export interface GameEvent extends EmitableEvent {
|
||||
/** Emitted in events.prototype.resetGame. */
|
||||
@ -8,3 +9,67 @@ export interface GameEvent extends EmitableEvent {
|
||||
}
|
||||
|
||||
export const hook = new EventEmitter<GameEvent>();
|
||||
|
||||
interface ListenerEvent extends EmitableEvent {
|
||||
hoverBlock: (block: Block) => void;
|
||||
leaveBlock: (block: Block) => void;
|
||||
}
|
||||
|
||||
class GameListener extends EventEmitter<ListenerEvent> {
|
||||
static num: number = 0;
|
||||
|
||||
num: number = GameListener.num++;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
loading.once('coreInit', () => {
|
||||
this.init();
|
||||
});
|
||||
}
|
||||
|
||||
private init() {
|
||||
// block
|
||||
let lastHoverX = -1;
|
||||
let lastHoverY = -1;
|
||||
core.registerAction(
|
||||
'onmove',
|
||||
`@GameListener_${this.num}_block`,
|
||||
(x, y, px, py) => {
|
||||
if (core.status.lockControl || !core.isPlaying()) return false;
|
||||
const bx = Math.floor((px - core.bigmap.offsetX) / 32);
|
||||
const by = Math.floor((py - core.bigmap.offsetY) / 32);
|
||||
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;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
},
|
||||
50
|
||||
);
|
||||
core.canvas.data.canvas.addEventListener('mouseleave', () => {
|
||||
if (core.status.lockControl || !core.isPlaying()) return;
|
||||
const blocks = core.getMapBlocksObj();
|
||||
const lastBlock = blocks[`${lastHoverX},${lastHoverY}`];
|
||||
if (!!lastBlock) {
|
||||
this.emit('leaveBlock', lastBlock);
|
||||
}
|
||||
lastHoverX = -1;
|
||||
lastHoverY = -1;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const gameListener = new GameListener();
|
||||
|
Loading…
Reference in New Issue
Block a user