2022-06-20 00:18:47 +00:00
|
|
|
import {game} from "../../game/game.js";
|
2022-06-24 19:13:56 +00:00
|
|
|
import * as net from "../../net/netcode.js";
|
2022-06-23 00:16:57 +00:00
|
|
|
import { canvas } from "../../display/html.js"
|
2022-06-20 00:18:47 +00:00
|
|
|
|
|
|
|
class Cursor {
|
|
|
|
constructor(){
|
|
|
|
this.x = 0
|
|
|
|
this.y = 0
|
|
|
|
this.xold = 0
|
|
|
|
this.yold = 0
|
|
|
|
}
|
|
|
|
}
|
2022-06-22 20:01:58 +00:00
|
|
|
|
2022-06-20 00:18:47 +00:00
|
|
|
export var cursor = new Cursor();
|
|
|
|
|
2022-06-22 20:01:58 +00:00
|
|
|
canvas.addEventListener('mousemove', e => {
|
2022-06-20 00:18:47 +00:00
|
|
|
mouseMoved(e)
|
|
|
|
});
|
|
|
|
|
2022-06-24 19:13:56 +00:00
|
|
|
canvas.addEventListener("mousedown", e => {
|
|
|
|
getMousePosition(e);
|
2022-06-20 00:18:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function mouseMoved(event) {
|
2022-06-21 14:46:34 +00:00
|
|
|
const tileSize = game.tileSize
|
|
|
|
const x = Math.floor(event.offsetX/tileSize)
|
|
|
|
const y = Math.floor(event.offsetY/tileSize)
|
2022-06-20 00:18:47 +00:00
|
|
|
if (cursor.xold != x || cursor.yold != y) {
|
|
|
|
cursor.x = x
|
|
|
|
cursor.y = y
|
2022-06-21 14:46:34 +00:00
|
|
|
// renderHud(x, y)
|
2022-06-20 00:18:47 +00:00
|
|
|
cursor.xold = x
|
|
|
|
cursor.yold = y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-24 19:13:56 +00:00
|
|
|
function getMousePosition(event) {
|
2022-06-23 00:16:57 +00:00
|
|
|
const tileSize = game.tileSize
|
2022-06-20 00:18:47 +00:00
|
|
|
// Get mouse position on canvas
|
|
|
|
const rect = canvas.getBoundingClientRect();
|
2022-06-23 00:16:57 +00:00
|
|
|
const mouseX = event.clientX - rect.left;
|
|
|
|
const mouseY = event.clientY - rect.top;
|
|
|
|
// scale mouse coordinates to canvas coordinates
|
|
|
|
const xu = Math.floor((mouseX * canvas.width / canvas.clientWidth)/tileSize);
|
|
|
|
const yu = Math.floor((mouseY * canvas.height / canvas.clientHeight)/tileSize);
|
2022-06-22 01:51:09 +00:00
|
|
|
const button = event.button
|
2022-06-20 00:18:47 +00:00
|
|
|
|
|
|
|
console.log("Click!")
|
|
|
|
console.log(xu,yu)
|
2022-06-24 19:13:56 +00:00
|
|
|
net.clickCanvas({tilePosition: [xu,yu], mode: button})
|
2022-06-20 00:18:47 +00:00
|
|
|
|
|
|
|
}
|