|
1
|
1 // pointer events: mouse and touch alike, nothing relies on hover
|
|
|
2
|
|
|
3 window.MM = window.MM || {};
|
|
|
4
|
|
|
5 MM.input = (function () {
|
|
|
6 "use strict";
|
|
|
7
|
|
|
8 // how close a tap must be to the placement preview to build, css pixels
|
|
|
9 var PREVIEW_HIT = 28;
|
|
|
10 var REFUSED_TIME = 500;
|
|
|
11
|
|
|
12 // app: {canvas, game(), layout(), restart(), view}
|
|
|
13 function attach(app) {
|
|
|
14 var c = app.canvas;
|
|
|
15 c.addEventListener("pointerdown", function (e) {
|
|
|
16 e.preventDefault();
|
|
4
|
17 MM.audio.unlock();
|
|
1
|
18 var r = c.getBoundingClientRect();
|
|
|
19 tap(app, e.clientX - r.left, e.clientY - r.top);
|
|
|
20 });
|
|
4
|
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);
|
|
1
|
26 // block the context menu, double tap zoom and pinch gestures
|
|
|
27 c.addEventListener("contextmenu", function (e) { e.preventDefault(); });
|
|
|
28 c.addEventListener("touchstart", function (e) { e.preventDefault(); }, { passive: false });
|
|
|
29 document.addEventListener("gesturestart", function (e) { e.preventDefault(); });
|
|
|
30 document.addEventListener("dblclick", function (e) { e.preventDefault(); });
|
|
|
31 }
|
|
|
32
|
|
|
33 function tap(app, x, y) {
|
|
|
34 var game = app.game(), L = app.layout();
|
|
|
35 var b = MM.ui.hit(game, L, x, y);
|
|
|
36 if (b) {
|
|
4
|
37 MM.audio.play("button_tap");
|
|
1
|
38 if (b.id === "restart") app.restart();
|
|
|
39 else if (b.id === "pause") game.paused = !game.paused;
|
|
|
40 else if (b.id === "speed") game.speedIndex = (game.speedIndex + 1) % game.speeds.length;
|
|
|
41 else if (b.id === "early") game.startEarly();
|
|
|
42 else if (b.id === "shop" && game.canAfford(MM.config.tower[b.tower].cost)) game.chooseShop(b.tower);
|
|
|
43 return;
|
|
|
44 }
|
|
|
45 if (game.result) return;
|
|
|
46 var mx = (x - L.ox) / L.scale, my = (y - L.oy) / L.scale;
|
|
|
47 if (mx < 0 || my < 0 || mx > game.map.width || my > game.map.height) return;
|
|
|
48 var hitR = Math.max(MM.config.placement.footprint_radius, PREVIEW_HIT / L.scale);
|
|
|
49 if (!game.tapMap(mx, my, hitR)) {
|
|
4
|
50 MM.audio.play("placement_refused");
|
|
1
|
51 app.view.refused = { x: mx, y: my, until: performance.now() + REFUSED_TIME };
|
|
|
52 }
|
|
|
53 }
|
|
|
54
|
|
|
55 return { attach: attach };
|
|
|
56 })();
|