MultiplayerMinesweeper/www/scripts/interface/game/mouse.js

51 lines
1.1 KiB
JavaScript

import {game} from "../../game/game.js";
import {renderHud } from '../../display/draw.js';
import { clickCanvas } from "/scripts/net/netcode.js";
import { getButton } from "/scripts/interface/game/picker.js";
class Cursor {
constructor(){
this.x = 0
this.y = 0
this.xold = 0
this.yold = 0
}
}
export var cursor = new Cursor();
hud.addEventListener('mousemove', e => {
mouseMoved(e)
});
hud.addEventListener("mousedown", function(e)
{
getMousePosition(canvas, e);
});
function mouseMoved(event) {
const x = Math.floor(event.offsetX/game.tileSize)
const y = Math.floor(event.offsetY/game.tileSize)
if (cursor.xold != x || cursor.yold != y) {
cursor.x = x
cursor.y = y
renderHud(x, y)
cursor.xold = x
cursor.yold = y
}
}
function getMousePosition(canvas, event) {
// Get mouse position on canvas
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
const xu = cursor.x
const yu = cursor.y
console.log("Click!")
console.log(xu,yu)
clickCanvas({tilePosition: [xu,yu], structure: getButton()*1})
}