|
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();
|
|
|
17 var r = c.getBoundingClientRect();
|
|
|
18 tap(app, e.clientX - r.left, e.clientY - r.top);
|
|
|
19 });
|
|
|
20 // block the context menu, double tap zoom and pinch gestures
|
|
|
21 c.addEventListener("contextmenu", function (e) { e.preventDefault(); });
|
|
|
22 c.addEventListener("touchstart", function (e) { e.preventDefault(); }, { passive: false });
|
|
|
23 document.addEventListener("gesturestart", function (e) { e.preventDefault(); });
|
|
|
24 document.addEventListener("dblclick", function (e) { e.preventDefault(); });
|
|
|
25 }
|
|
|
26
|
|
|
27 function tap(app, x, y) {
|
|
|
28 var game = app.game(), L = app.layout();
|
|
|
29 var b = MM.ui.hit(game, L, x, y);
|
|
|
30 if (b) {
|
|
|
31 if (b.id === "restart") app.restart();
|
|
|
32 else if (b.id === "pause") game.paused = !game.paused;
|
|
|
33 else if (b.id === "speed") game.speedIndex = (game.speedIndex + 1) % game.speeds.length;
|
|
|
34 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);
|
|
|
36 return;
|
|
|
37 }
|
|
|
38 if (game.result) return;
|
|
|
39 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;
|
|
|
41 var hitR = Math.max(MM.config.placement.footprint_radius, PREVIEW_HIT / L.scale);
|
|
|
42 if (!game.tapMap(mx, my, hitR)) {
|
|
|
43 app.view.refused = { x: mx, y: my, until: performance.now() + REFUSED_TIME };
|
|
|
44 }
|
|
|
45 }
|
|
|
46
|
|
|
47 return { attach: attach };
|
|
|
48 })();
|