comparison 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
comparison
equal deleted inserted replaced
7:4fd2da999ba4 8:2c4fed8baed9
1 // pointer events: mouse and touch alike, nothing relies on hover 1 // pointer events: mouse and touch alike. hover only highlights the screen
2 // buttons and sets the cursor; nothing relies on it
2 3
3 window.MM = window.MM || {}; 4 window.MM = window.MM || {};
4 5
5 MM.input = (function () { 6 MM.input = (function () {
6 "use strict"; 7 "use strict";
17 e.preventDefault(); 18 e.preventDefault();
18 MM.audio.unlock(); 19 MM.audio.unlock();
19 var r = c.getBoundingClientRect(); 20 var r = c.getBoundingClientRect();
20 tap(app, e.clientX - r.left, e.clientY - r.top); 21 tap(app, e.clientX - r.left, e.clientY - r.top);
21 }); 22 });
23 // mouse only: the position for the hover highlight, and the pointer
24 // cursor over buttons
25 c.addEventListener("pointermove", function (e) {
26 if (e.pointerType !== "mouse" || !app.game()) return;
27 var r = c.getBoundingClientRect();
28 var h = { x: e.clientX - r.left, y: e.clientY - r.top };
29 app.view.hover = h;
30 c.style.cursor = MM.ui.hit(app.game(), app.layout(), h.x, h.y) ? "pointer" : "";
31 });
32 c.addEventListener("pointerleave", function () {
33 app.view.hover = null;
34 c.style.cursor = "";
35 });
22 // browsers count only some events as a user interaction that may 36 // browsers count only some events as a user interaction that may
23 // start sound: pointerdown for the mouse, pointerup and touchend 37 // start sound: pointerdown for the mouse, pointerup and touchend
24 // for touch 38 // for touch
25 document.addEventListener("pointerup", MM.audio.unlock); 39 document.addEventListener("pointerup", MM.audio.unlock);
26 document.addEventListener("touchend", MM.audio.unlock); 40 document.addEventListener("touchend", MM.audio.unlock);
33 47
34 function tap(app, x, y) { 48 function tap(app, x, y) {
35 var game = app.game(), L = app.layout(); 49 var game = app.game(), L = app.layout();
36 var b = MM.ui.hit(game, L, x, y); 50 var b = MM.ui.hit(game, L, x, y);
37 if (b) { 51 if (b) {
38 // the play button starts the sound 52 // the play button starts the sound. switching sound on or off
53 // comes first, so the tap is heard when it goes on
39 if (b.id === "play") MM.audio.start(); 54 if (b.id === "play") MM.audio.start();
55 if (b.id === "sound") MM.audio.toggle();
40 MM.audio.play("button_tap"); 56 MM.audio.play("button_tap");
57 // any other button cancels an armed sell
58 if (b.id !== "sell") game.sellArmed = false;
41 if (b.id === "play") app.play(); 59 if (b.id === "play") app.play();
42 else if (b.id === "next") app.next(); 60 else if (b.id === "next") app.next();
43 else if (b.id === "retry") app.retry(); 61 else if (b.id === "retry") app.retry();
44 else if (b.id === "restart") app.restart(); 62 else if (b.id === "restart") app.restart();
45 else if (b.id === "pause") game.togglePause(); 63 else if (b.id === "pause") game.togglePause();