wobblyhead hg > minor_monsters
annotate game/js/input.js @ 8:2c4fed8baed9
art update, persistent sound setting, tower selling as double action
| author | ark |
|---|---|
| date | 2026-10-19 13:37 +0200 |
| parents | 910b26feca76 |
| children | a77857594150 |
| 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 var REFUSED_TIME = 500; | |
| 12 | |
| 6 | 13 // app: {canvas, view, game(), layout(), and the screen actions play(), |
| 14 // next(), retry(), restart()} | |
| 1 | 15 function attach(app) { |
| 16 var c = app.canvas; | |
| 17 c.addEventListener("pointerdown", function (e) { | |
| 18 e.preventDefault(); | |
| 4 | 19 MM.audio.unlock(); |
| 1 | 20 var r = c.getBoundingClientRect(); |
| 21 tap(app, e.clientX - r.left, e.clientY - r.top); | |
| 22 }); | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
23 // mouse only: the position for the hover highlight, and the pointer |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
24 // cursor over buttons |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
25 c.addEventListener("pointermove", function (e) { |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
26 if (e.pointerType !== "mouse" || !app.game()) return; |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
27 var r = c.getBoundingClientRect(); |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
28 var h = { x: e.clientX - r.left, y: e.clientY - r.top }; |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
29 app.view.hover = h; |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
30 c.style.cursor = MM.ui.hit(app.game(), app.layout(), h.x, h.y) ? "pointer" : ""; |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
31 }); |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
32 c.addEventListener("pointerleave", function () { |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
33 app.view.hover = null; |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
34 c.style.cursor = ""; |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
35 }); |
| 4 | 36 // browsers count only some events as a user interaction that may |
| 37 // start sound: pointerdown for the mouse, pointerup and touchend | |
| 38 // for touch | |
| 39 document.addEventListener("pointerup", MM.audio.unlock); | |
| 40 document.addEventListener("touchend", MM.audio.unlock); | |
| 1 | 41 // block the context menu, double tap zoom and pinch gestures |
| 42 c.addEventListener("contextmenu", function (e) { e.preventDefault(); }); | |
| 43 c.addEventListener("touchstart", function (e) { e.preventDefault(); }, { passive: false }); | |
| 44 document.addEventListener("gesturestart", function (e) { e.preventDefault(); }); | |
| 45 document.addEventListener("dblclick", function (e) { e.preventDefault(); }); | |
| 46 } | |
| 47 | |
| 48 function tap(app, x, y) { | |
| 49 var game = app.game(), L = app.layout(); | |
| 50 var b = MM.ui.hit(game, L, x, y); | |
| 51 if (b) { | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
52 // 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
|
53 // comes first, so the tap is heard when it goes on |
| 6 | 54 if (b.id === "play") MM.audio.start(); |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
55 if (b.id === "sound") MM.audio.toggle(); |
| 4 | 56 MM.audio.play("button_tap"); |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
57 // any other button cancels an armed sell |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
58 if (b.id !== "sell") game.sellArmed = false; |
| 6 | 59 if (b.id === "play") app.play(); |
| 60 else if (b.id === "next") app.next(); | |
| 61 else if (b.id === "retry") app.retry(); | |
| 62 else if (b.id === "restart") app.restart(); | |
| 63 else if (b.id === "pause") game.togglePause(); | |
| 1 | 64 else if (b.id === "speed") game.speedIndex = (game.speedIndex + 1) % game.speeds.length; |
| 65 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); | |
| 6 | 67 else if (b.id === "upgrade") game.upgrade(); |
| 68 else if (b.id === "sell") game.sell(); | |
| 1 | 69 return; |
| 70 } | |
| 6 | 71 if (game.result || game.splash) return; |
| 1 | 72 var mx = (x - L.ox) / L.scale, my = (y - L.oy) / L.scale; |
| 73 if (mx < 0 || my < 0 || mx > game.map.width || my > game.map.height) return; | |
| 74 var hitR = Math.max(MM.config.placement.footprint_radius, PREVIEW_HIT / L.scale); | |
| 75 if (!game.tapMap(mx, my, hitR)) { | |
| 4 | 76 MM.audio.play("placement_refused"); |
| 1 | 77 app.view.refused = { x: mx, y: my, until: performance.now() + REFUSED_TIME }; |
| 78 } | |
| 79 } | |
| 80 | |
| 81 return { attach: attach }; | |
| 82 })(); |
