I successfully achieved what I needed.
- On each mouse move I save the last mouse position client side.
- When someone presses some key, then I send it to the backend information through the socket with the last mousePosition.
It does not work when a player joins the game and has not move the mouse yet, and also I don't know if it's efficient solution, but for now, it's sufficient for me:
import { RpgClient, RpgModule, RpgScene } from '@rpgjs/client'
import { PositionXY } from '@rpgjs/types';
let mousePosition: PositionXY = { x: 0, y: 0 };
/** @ts-ignore */
@RpgModule<RpgClient>({
scenes: {
map: {
onAfterLoading(scene: RpgScene) {
scene.on('mousemove', (pos: any) => {
mousePosition = { x: pos.x, y: pos.y };
})
}
}
},
engine: {
onInput(engine: RpgClientEngine, inputs: any) {
inputs.forEach((input: any) => {
engine.getScene().game.clientEngine.socket.emit('key.pressed', {
input,
mousePosition
});
});
}
}
})
export default class RpgClientEngine { }