wobblyhead hg > minor_monsters
view game/js/input.js @ 9:53f015966118
map, path, tower placement adjustments
| author | ark |
|---|---|
| date | 2026-10-19 15:55 +0200 |
| parents | 2c4fed8baed9 |
| children | a77857594150 |
line wrap: on
line source
// pointer events: mouse and touch alike. hover only highlights the screen // buttons and sets the cursor; nothing relies on it window.MM = window.MM || {}; MM.input = (function () { "use strict"; // how close a tap must be to the placement preview to build, css pixels var PREVIEW_HIT = 28; var REFUSED_TIME = 500; // app: {canvas, view, game(), layout(), and the screen actions play(), // next(), retry(), restart()} function attach(app) { var c = app.canvas; c.addEventListener("pointerdown", function (e) { e.preventDefault(); MM.audio.unlock(); var r = c.getBoundingClientRect(); tap(app, e.clientX - r.left, e.clientY - r.top); }); // mouse only: the position for the hover highlight, and the pointer // cursor over buttons c.addEventListener("pointermove", function (e) { if (e.pointerType !== "mouse" || !app.game()) return; var r = c.getBoundingClientRect(); var h = { x: e.clientX - r.left, y: e.clientY - r.top }; app.view.hover = h; c.style.cursor = MM.ui.hit(app.game(), app.layout(), h.x, h.y) ? "pointer" : ""; }); c.addEventListener("pointerleave", function () { app.view.hover = null; c.style.cursor = ""; }); // browsers count only some events as a user interaction that may // start sound: pointerdown for the mouse, pointerup and touchend // for touch document.addEventListener("pointerup", MM.audio.unlock); document.addEventListener("touchend", MM.audio.unlock); // block the context menu, double tap zoom and pinch gestures c.addEventListener("contextmenu", function (e) { e.preventDefault(); }); c.addEventListener("touchstart", function (e) { e.preventDefault(); }, { passive: false }); document.addEventListener("gesturestart", function (e) { e.preventDefault(); }); document.addEventListener("dblclick", function (e) { e.preventDefault(); }); } function tap(app, x, y) { var game = app.game(), L = app.layout(); var b = MM.ui.hit(game, L, x, y); if (b) { // the play button starts the sound. switching sound on or off // comes first, so the tap is heard when it goes on if (b.id === "play") MM.audio.start(); if (b.id === "sound") MM.audio.toggle(); MM.audio.play("button_tap"); // any other button cancels an armed sell if (b.id !== "sell") game.sellArmed = false; if (b.id === "play") app.play(); else if (b.id === "next") app.next(); else if (b.id === "retry") app.retry(); else if (b.id === "restart") app.restart(); else if (b.id === "pause") game.togglePause(); else if (b.id === "speed") game.speedIndex = (game.speedIndex + 1) % game.speeds.length; else if (b.id === "early") game.startEarly(); else if (b.id === "shop" && game.canAfford(MM.config.tower[b.tower].cost)) game.chooseShop(b.tower); else if (b.id === "upgrade") game.upgrade(); else if (b.id === "sell") game.sell(); return; } if (game.result || game.splash) return; var mx = (x - L.ox) / L.scale, my = (y - L.oy) / L.scale; if (mx < 0 || my < 0 || mx > game.map.width || my > game.map.height) return; var hitR = Math.max(MM.config.placement.footprint_radius, PREVIEW_HIT / L.scale); if (!game.tapMap(mx, my, hitR)) { MM.audio.play("placement_refused"); app.view.refused = { x: mx, y: my, until: performance.now() + REFUSED_TIME }; } } return { attach: attach }; })();
