comparison game/js/input.js @ 11:a77857594150

adjusted tower building, added preview dragging
author ark
date 2026-10-19 17:56 +0200
parents 2c4fed8baed9
children 8df95050fc8b
comparison
equal deleted inserted replaced
10:22b604680c5a 11:a77857594150
6 MM.input = (function () { 6 MM.input = (function () {
7 "use strict"; 7 "use strict";
8 8
9 // how close a tap must be to the placement preview to build, css pixels 9 // how close a tap must be to the placement preview to build, css pixels
10 var PREVIEW_HIT = 28; 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;
11 var REFUSED_TIME = 500; 14 var REFUSED_TIME = 500;
12 15
13 // app: {canvas, view, game(), layout(), and the screen actions play(), 16 // app: {canvas, view, game(), layout(), and the screen actions play(),
14 // next(), retry(), restart()} 17 // next(), retry(), restart()}
15 function attach(app) { 18 function attach(app) {
16 var c = app.canvas; 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
17 c.addEventListener("pointerdown", function (e) { 29 c.addEventListener("pointerdown", function (e) {
18 e.preventDefault(); 30 e.preventDefault();
19 MM.audio.unlock(); 31 MM.audio.unlock();
20 var r = c.getBoundingClientRect(); 32 if (drag) return;
21 tap(app, e.clientX - r.left, e.clientY - r.top); 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 */ }
22 }); 39 });
23 // mouse only: the position for the hover highlight, and the pointer
24 // cursor over buttons
25 c.addEventListener("pointermove", function (e) { 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
26 if (e.pointerType !== "mouse" || !app.game()) return; 45 if (e.pointerType !== "mouse" || !app.game()) return;
27 var r = c.getBoundingClientRect(); 46 app.view.hover = p;
28 var h = { x: e.clientX - r.left, y: e.clientY - r.top }; 47 c.style.cursor = MM.ui.hit(app.game(), app.layout(), p.x, p.y) ? "pointer" : "";
29 app.view.hover = h; 48 });
30 c.style.cursor = MM.ui.hit(app.game(), app.layout(), h.x, h.y) ? "pointer" : ""; 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;
31 }); 59 });
32 c.addEventListener("pointerleave", function () { 60 c.addEventListener("pointerleave", function () {
33 app.view.hover = null; 61 app.view.hover = null;
34 c.style.cursor = ""; 62 c.style.cursor = "";
35 }); 63 });
43 c.addEventListener("touchstart", function (e) { e.preventDefault(); }, { passive: false }); 71 c.addEventListener("touchstart", function (e) { e.preventDefault(); }, { passive: false });
44 document.addEventListener("gesturestart", function (e) { e.preventDefault(); }); 72 document.addEventListener("gesturestart", function (e) { e.preventDefault(); });
45 document.addEventListener("dblclick", function (e) { e.preventDefault(); }); 73 document.addEventListener("dblclick", function (e) { e.preventDefault(); });
46 } 74 }
47 75
48 function tap(app, x, y) { 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) {
49 var game = app.game(), L = app.layout(); 95 var game = app.game(), L = app.layout();
50 var b = MM.ui.hit(game, L, x, y); 96 var b = MM.ui.hit(game, L, x, y);
51 if (b) { 97 if (b) {
52 // the play button starts the sound. switching sound on or off 98 // the play button starts the sound. switching sound on or off
53 // comes first, so the tap is heard when it goes on 99 // comes first, so the tap is heard when it goes on
64 else if (b.id === "speed") game.speedIndex = (game.speedIndex + 1) % game.speeds.length; 110 else if (b.id === "speed") game.speedIndex = (game.speedIndex + 1) % game.speeds.length;
65 else if (b.id === "early") game.startEarly(); 111 else if (b.id === "early") game.startEarly();
66 else if (b.id === "shop" && game.canAfford(MM.config.tower[b.tower].cost)) game.chooseShop(b.tower); 112 else if (b.id === "shop" && game.canAfford(MM.config.tower[b.tower].cost)) game.chooseShop(b.tower);
67 else if (b.id === "upgrade") game.upgrade(); 113 else if (b.id === "upgrade") game.upgrade();
68 else if (b.id === "sell") game.sell(); 114 else if (b.id === "sell") game.sell();
69 return; 115 return null;
70 } 116 }
71 if (game.result || game.splash) return; 117 if (game.result || game.splash) return null;
72 var mx = (x - L.ox) / L.scale, my = (y - L.oy) / L.scale; 118 var m = mapPos(game, L, x, y);
73 if (mx < 0 || my < 0 || mx > game.map.width || my > game.map.height) return; 119 if (!m) return null;
74 var hitR = Math.max(MM.config.placement.footprint_radius, PREVIEW_HIT / L.scale); 120 var hitR = Math.max(MM.config.placement.footprint_radius, PREVIEW_HIT / L.scale);
75 if (!game.tapMap(mx, my, hitR)) { 121 var kind = game.pressMap(m.x, m.y, hitR);
76 MM.audio.play("placement_refused"); 122 if (!kind) return null;
77 app.view.refused = { x: mx, y: my, until: performance.now() + REFUSED_TIME }; 123 var d = { game: game, onPreview: kind === "preview", moved: false, found: false,
124 sx: x, sy: y, dx: 0, dy: 0 };
125 if (d.onPreview) {
126 d.dx = game.preview.x - m.x;
127 d.dy = game.preview.y - m.y;
128 d.found = true;
129 } else {
130 d.found = game.movePreview(m.x, m.y);
78 } 131 }
132 return d;
133 }
134
135 // the pointer of a drag moved to css position x, y
136 function move(app, d, x, y) {
137 var game = app.game();
138 if (game !== d.game || game.result || !game.shop) return;
139 if (!d.moved && Math.hypot(x - d.sx, y - d.sy) <= DRAG_SLOP) {
140 // a press on the preview stays a tap until it moves far enough
141 if (d.onPreview) return;
142 } else {
143 d.moved = true;
144 }
145 var m = mapPos(game, app.layout(), x, y);
146 if (m && game.movePreview(m.x + d.dx, m.y + d.dy)) d.found = true;
147 }
148
149 // the pointer of a drag was released at css position x, y. a tap on the
150 // preview builds; a drag that never found a legal position is refused
151 function release(app, d, x, y) {
152 var game = app.game(), L = app.layout();
153 if (game !== d.game || game.result) return;
154 var ok = d.onPreview && !d.moved ? game.build() : d.found;
155 if (ok) return;
156 MM.audio.play("placement_refused");
157 app.view.refused = { x: (x - L.ox) / L.scale, y: (y - L.oy) / L.scale,
158 until: performance.now() + REFUSED_TIME };
79 } 159 }
80 160
81 return { attach: attach }; 161 return { attach: attach };
82 })(); 162 })();