comparison game/js/input.js @ 17:fe680946164b

reorganised repo
author ark
date 2026-10-20 00:12 +0200
parents
children 3f2811aebb3d
comparison
equal deleted inserted replaced
16:30bd10284fd8 17:fe680946164b
1 // pointer events: mouse and touch alike. hover only highlights the screen
2 // buttons and sets the cursor; nothing relies on it
3
4 window.MM = window.MM || {};
5
6 MM.input = (function () {
7 "use strict";
8
9 // how close a tap must be to the placement preview to build, css pixels
10 var PREVIEW_HIT = 28;
11 // how far a press on the preview may move and still count as a tap that
12 // builds, css pixels. a longer move drags the preview
13 var DRAG_SLOP = 10;
14 var REFUSED_TIME = 500;
15
16 // app: {canvas, view, game(), layout(), and the screen actions play(),
17 // next(), retry(), restart()}
18 function attach(app) {
19 var c = app.canvas;
20 // the press that moves the placement preview, followed until the
21 // pointer is released. other pointers are ignored meanwhile
22 var drag = null;
23
24 function pos(e) {
25 var r = c.getBoundingClientRect();
26 return { x: e.clientX - r.left, y: e.clientY - r.top };
27 }
28
29 c.addEventListener("pointerdown", function (e) {
30 e.preventDefault();
31 MM.audio.unlock();
32 if (drag) return;
33 var p = pos(e);
34 drag = press(app, p.x, p.y);
35 if (!drag) return;
36 drag.id = e.pointerId;
37 // keep receiving the pointer when it leaves the canvas
38 try { c.setPointerCapture(e.pointerId); } catch (err) { /* not supported */ }
39 });
40 c.addEventListener("pointermove", function (e) {
41 var p = pos(e);
42 if (drag && e.pointerId === drag.id) move(app, drag, p.x, p.y);
43 // mouse only: the position for the hover highlight, and the
44 // pointer cursor over buttons
45 if (e.pointerType !== "mouse" || !app.game()) return;
46 app.view.hover = p;
47 c.style.cursor = MM.ui.hit(app.game(), app.layout(), p.x, p.y) ? "pointer" : "";
48 });
49 c.addEventListener("pointerup", function (e) {
50 if (!drag || e.pointerId !== drag.id) return;
51 var p = pos(e);
52 release(app, drag, p.x, p.y);
53 drag = null;
54 });
55 // the browser took the pointer, for example for a system gesture:
56 // the preview stays where it is and nothing is built
57 c.addEventListener("pointercancel", function (e) {
58 if (drag && e.pointerId === drag.id) drag = null;
59 });
60 c.addEventListener("pointerleave", function () {
61 app.view.hover = null;
62 c.style.cursor = "";
63 });
64 // browsers count only some events as a user interaction that may
65 // start sound: pointerdown for the mouse, pointerup and touchend
66 // for touch
67 document.addEventListener("pointerup", MM.audio.unlock);
68 document.addEventListener("touchend", MM.audio.unlock);
69 // block the context menu, double tap zoom and pinch gestures
70 c.addEventListener("contextmenu", function (e) { e.preventDefault(); });
71 c.addEventListener("touchstart", function (e) { e.preventDefault(); }, { passive: false });
72 document.addEventListener("gesturestart", function (e) { e.preventDefault(); });
73 document.addEventListener("dblclick", function (e) { e.preventDefault(); });
74 }
75
76 // map position of css position x, y, or null outside the map
77 function mapPos(game, L, x, y) {
78 var mx = (x - L.ox) / L.scale, my = (y - L.oy) / L.scale;
79 if (mx < 0 || my < 0 || mx > game.map.width || my > game.map.height) return null;
80 return { x: mx, y: my };
81 }
82
83 // a press at css position x, y. buttons and tower selection act at once.
84 // a press on the map while a shop tower is chosen returns the drag that
85 // places the preview:
86 // game the game it belongs to
87 // onPreview the press was on the preview; it builds on release unless
88 // the pointer moved further than DRAG_SLOP
89 // moved the pointer moved further than DRAG_SLOP
90 // found a legal position was found during the drag
91 // sx, sy css position of the press
92 // dx, dy offset from the pointer to the preview in map pixels, so
93 // a preview grabbed off centre does not jump
94 function press(app, x, y) {
95 var game = app.game(), L = app.layout();
96 var b = MM.ui.hit(game, L, x, y);
97 if (b) {
98 // the play button starts the sound. switching sound on or off
99 // comes first, so the tap is heard when it goes on
100 if (b.id === "play") MM.audio.start();
101 if (b.id === "sound") MM.audio.toggle();
102 MM.audio.play("button_tap");
103 // any other button cancels an armed sell, and any button outside
104 // the tower menu closes it
105 if (b.id !== "sell") game.sellArmed = false;
106 if (b.id !== "sell" && b.id !== "upgrade") game.selected = null;
107 if (b.id === "play") app.play();
108 else if (b.id === "next") app.next();
109 else if (b.id === "retry") app.retry();
110 else if (b.id === "restart") app.restart();
111 else if (b.id === "pause") game.togglePause();
112 else if (b.id === "speed") game.speedIndex = (game.speedIndex + 1) % game.speeds.length;
113 else if (b.id === "early") game.startEarly();
114 else if (b.id === "shop" && !game.paused && game.canAfford(MM.config.tower[b.tower].cost[0])) game.chooseShop(b.tower);
115 else if (b.id === "upgrade") game.upgrade();
116 else if (b.id === "sell") game.sell();
117 return null;
118 }
119 if (game.result || game.paused) return null;
120 var m = mapPos(game, L, x, y);
121 if (!m) {
122 game.sellArmed = false;
123 game.selected = null;
124 return null;
125 }
126 var hitR = Math.max(MM.config.placement.footprint_radius, PREVIEW_HIT / L.scale);
127 var kind = game.pressMap(m.x, m.y, hitR);
128 if (!kind) return null;
129 var d = { game: game, onPreview: kind === "preview", moved: false, found: false,
130 sx: x, sy: y, dx: 0, dy: 0 };
131 if (d.onPreview) {
132 d.dx = game.preview.x - m.x;
133 d.dy = game.preview.y - m.y;
134 d.found = true;
135 } else {
136 d.found = game.movePreview(m.x, m.y);
137 }
138 return d;
139 }
140
141 // the pointer of a drag moved to css position x, y
142 function move(app, d, x, y) {
143 var game = app.game();
144 if (game !== d.game || game.result || !game.shop) return;
145 if (!d.moved && Math.hypot(x - d.sx, y - d.sy) <= DRAG_SLOP) {
146 // a press on the preview stays a tap until it moves far enough
147 if (d.onPreview) return;
148 } else {
149 d.moved = true;
150 }
151 var m = mapPos(game, app.layout(), x, y);
152 if (m && game.movePreview(m.x + d.dx, m.y + d.dy)) d.found = true;
153 }
154
155 // the pointer of a drag was released at css position x, y. a tap on the
156 // preview builds; a drag that never found a legal position is refused
157 function release(app, d, x, y) {
158 var game = app.game(), L = app.layout();
159 if (game !== d.game || game.result || game.paused) return;
160 var ok = d.onPreview && !d.moved ? game.build() : d.found;
161 if (ok) return;
162 MM.audio.play("placement_refused");
163 app.view.refused = { x: (x - L.ox) / L.scale, y: (y - L.oy) / L.scale,
164 until: performance.now() + REFUSED_TIME };
165 }
166
167 return { attach: attach };
168 })();