wobblyhead hg > minor_monsters
comparison game/js/input.js @ 4:2d46729a022e
sound processing and in-game sounds
| author | ark |
|---|---|
| date | 2026-10-18 20:58 +0200 |
| parents | 5492efd60e2c |
| children | 910b26feca76 |
comparison
equal
deleted
inserted
replaced
| 3:cc0c32e71090 | 4:2d46729a022e |
|---|---|
| 12 // app: {canvas, game(), layout(), restart(), view} | 12 // app: {canvas, game(), layout(), restart(), view} |
| 13 function attach(app) { | 13 function attach(app) { |
| 14 var c = app.canvas; | 14 var c = app.canvas; |
| 15 c.addEventListener("pointerdown", function (e) { | 15 c.addEventListener("pointerdown", function (e) { |
| 16 e.preventDefault(); | 16 e.preventDefault(); |
| 17 MM.audio.unlock(); | |
| 17 var r = c.getBoundingClientRect(); | 18 var r = c.getBoundingClientRect(); |
| 18 tap(app, e.clientX - r.left, e.clientY - r.top); | 19 tap(app, e.clientX - r.left, e.clientY - r.top); |
| 19 }); | 20 }); |
| 21 // browsers count only some events as a user interaction that may | |
| 22 // start sound: pointerdown for the mouse, pointerup and touchend | |
| 23 // for touch | |
| 24 document.addEventListener("pointerup", MM.audio.unlock); | |
| 25 document.addEventListener("touchend", MM.audio.unlock); | |
| 20 // block the context menu, double tap zoom and pinch gestures | 26 // block the context menu, double tap zoom and pinch gestures |
| 21 c.addEventListener("contextmenu", function (e) { e.preventDefault(); }); | 27 c.addEventListener("contextmenu", function (e) { e.preventDefault(); }); |
| 22 c.addEventListener("touchstart", function (e) { e.preventDefault(); }, { passive: false }); | 28 c.addEventListener("touchstart", function (e) { e.preventDefault(); }, { passive: false }); |
| 23 document.addEventListener("gesturestart", function (e) { e.preventDefault(); }); | 29 document.addEventListener("gesturestart", function (e) { e.preventDefault(); }); |
| 24 document.addEventListener("dblclick", function (e) { e.preventDefault(); }); | 30 document.addEventListener("dblclick", function (e) { e.preventDefault(); }); |
| 26 | 32 |
| 27 function tap(app, x, y) { | 33 function tap(app, x, y) { |
| 28 var game = app.game(), L = app.layout(); | 34 var game = app.game(), L = app.layout(); |
| 29 var b = MM.ui.hit(game, L, x, y); | 35 var b = MM.ui.hit(game, L, x, y); |
| 30 if (b) { | 36 if (b) { |
| 37 MM.audio.play("button_tap"); | |
| 31 if (b.id === "restart") app.restart(); | 38 if (b.id === "restart") app.restart(); |
| 32 else if (b.id === "pause") game.paused = !game.paused; | 39 else if (b.id === "pause") game.paused = !game.paused; |
| 33 else if (b.id === "speed") game.speedIndex = (game.speedIndex + 1) % game.speeds.length; | 40 else if (b.id === "speed") game.speedIndex = (game.speedIndex + 1) % game.speeds.length; |
| 34 else if (b.id === "early") game.startEarly(); | 41 else if (b.id === "early") game.startEarly(); |
| 35 else if (b.id === "shop" && game.canAfford(MM.config.tower[b.tower].cost)) game.chooseShop(b.tower); | 42 else if (b.id === "shop" && game.canAfford(MM.config.tower[b.tower].cost)) game.chooseShop(b.tower); |
| 38 if (game.result) return; | 45 if (game.result) return; |
| 39 var mx = (x - L.ox) / L.scale, my = (y - L.oy) / L.scale; | 46 var mx = (x - L.ox) / L.scale, my = (y - L.oy) / L.scale; |
| 40 if (mx < 0 || my < 0 || mx > game.map.width || my > game.map.height) return; | 47 if (mx < 0 || my < 0 || mx > game.map.width || my > game.map.height) return; |
| 41 var hitR = Math.max(MM.config.placement.footprint_radius, PREVIEW_HIT / L.scale); | 48 var hitR = Math.max(MM.config.placement.footprint_radius, PREVIEW_HIT / L.scale); |
| 42 if (!game.tapMap(mx, my, hitR)) { | 49 if (!game.tapMap(mx, my, hitR)) { |
| 50 MM.audio.play("placement_refused"); | |
| 43 app.view.refused = { x: mx, y: my, until: performance.now() + REFUSED_TIME }; | 51 app.view.refused = { x: mx, y: my, until: performance.now() + REFUSED_TIME }; |
| 44 } | 52 } |
| 45 } | 53 } |
| 46 | 54 |
| 47 return { attach: attach }; | 55 return { attach: attach }; |
