wobblyhead hg > minor_monsters
annotate game/js/input.js @ 14:7fda634d15ad
adjustments to wave logic, tower pivot points
| author | ark |
|---|---|
| date | 2026-10-19 22:50 +0200 |
| parents | 8df95050fc8b |
| children | c1c7e4f54bf6 |
| rev | line source |
|---|---|
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
1 // pointer events: mouse and touch alike. hover only highlights the screen |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
2 // buttons and sets the cursor; nothing relies on it |
| 1 | 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 | 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; | |
| 1 | 14 var REFUSED_TIME = 500; |
| 15 | |
| 6 | 16 // app: {canvas, view, game(), layout(), and the screen actions play(), |
| 17 // next(), retry(), restart()} | |
| 1 | 18 function attach(app) { |
| 19 var c = app.canvas; | |
| 11 | 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 | |
| 1 | 29 c.addEventListener("pointerdown", function (e) { |
| 30 e.preventDefault(); | |
| 4 | 31 MM.audio.unlock(); |
| 11 | 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 */ } | |
| 1 | 39 }); |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
40 c.addEventListener("pointermove", function (e) { |
| 11 | 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 | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
45 if (e.pointerType !== "mouse" || !app.game()) return; |
| 11 | 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; | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
59 }); |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
60 c.addEventListener("pointerleave", function () { |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
61 app.view.hover = null; |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
62 c.style.cursor = ""; |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
63 }); |
| 4 | 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); | |
| 1 | 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 | |
| 11 | 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) { | |
| 1 | 95 var game = app.game(), L = app.layout(); |
| 96 var b = MM.ui.hit(game, L, x, y); | |
| 97 if (b) { | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
98 // the play button starts the sound. switching sound on or off |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
99 // comes first, so the tap is heard when it goes on |
| 6 | 100 if (b.id === "play") MM.audio.start(); |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
101 if (b.id === "sound") MM.audio.toggle(); |
| 4 | 102 MM.audio.play("button_tap"); |
|
13
8df95050fc8b
no more build during pause; tower popup closes on any click outside it
ark
parents:
11
diff
changeset
|
103 // any other button cancels an armed sell, and any button outside |
|
8df95050fc8b
no more build during pause; tower popup closes on any click outside it
ark
parents:
11
diff
changeset
|
104 // the tower menu closes it |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
105 if (b.id !== "sell") game.sellArmed = false; |
|
13
8df95050fc8b
no more build during pause; tower popup closes on any click outside it
ark
parents:
11
diff
changeset
|
106 if (b.id !== "sell" && b.id !== "upgrade") game.selected = null; |
| 6 | 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(); | |
| 1 | 112 else if (b.id === "speed") game.speedIndex = (game.speedIndex + 1) % game.speeds.length; |
| 113 else if (b.id === "early") game.startEarly(); | |
|
13
8df95050fc8b
no more build during pause; tower popup closes on any click outside it
ark
parents:
11
diff
changeset
|
114 else if (b.id === "shop" && !game.paused && game.canAfford(MM.config.tower[b.tower].cost)) game.chooseShop(b.tower); |
| 6 | 115 else if (b.id === "upgrade") game.upgrade(); |
| 116 else if (b.id === "sell") game.sell(); | |
| 11 | 117 return null; |
| 118 } | |
|
13
8df95050fc8b
no more build during pause; tower popup closes on any click outside it
ark
parents:
11
diff
changeset
|
119 if (game.result || game.paused) return null; |
| 11 | 120 var m = mapPos(game, L, x, y); |
|
13
8df95050fc8b
no more build during pause; tower popup closes on any click outside it
ark
parents:
11
diff
changeset
|
121 if (!m) { |
|
8df95050fc8b
no more build during pause; tower popup closes on any click outside it
ark
parents:
11
diff
changeset
|
122 game.sellArmed = false; |
|
8df95050fc8b
no more build during pause; tower popup closes on any click outside it
ark
parents:
11
diff
changeset
|
123 game.selected = null; |
|
8df95050fc8b
no more build during pause; tower popup closes on any click outside it
ark
parents:
11
diff
changeset
|
124 return null; |
|
8df95050fc8b
no more build during pause; tower popup closes on any click outside it
ark
parents:
11
diff
changeset
|
125 } |
| 11 | 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); | |
| 1 | 137 } |
| 11 | 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; | |
| 1 | 150 } |
| 11 | 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(); | |
|
13
8df95050fc8b
no more build during pause; tower popup closes on any click outside it
ark
parents:
11
diff
changeset
|
159 if (game !== d.game || game.result || game.paused) return; |
| 11 | 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 }; | |
| 1 | 165 } |
| 166 | |
| 167 return { attach: attach }; | |
| 168 })(); |
