|
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
|
|
6
|
12 // app: {canvas, view, game(), layout(), and the screen actions play(),
|
|
|
13 // next(), retry(), restart()}
|
|
1
|
14 function attach(app) {
|
|
|
15 var c = app.canvas;
|
|
|
16 c.addEventListener("pointerdown", function (e) {
|
|
|
17 e.preventDefault();
|
|
4
|
18 MM.audio.unlock();
|
|
1
|
19 var r = c.getBoundingClientRect();
|
|
|
20 tap(app, e.clientX - r.left, e.clientY - r.top);
|
|
|
21 });
|
|
4
|
22 // browsers count only some events as a user interaction that may
|
|
|
23 // start sound: pointerdown for the mouse, pointerup and touchend
|
|
|
24 // for touch
|
|
|
25 document.addEventListener("pointerup", MM.audio.unlock);
|
|
|
26 document.addEventListener("touchend", MM.audio.unlock);
|
|
1
|
27 // block the context menu, double tap zoom and pinch gestures
|
|
|
28 c.addEventListener("contextmenu", function (e) { e.preventDefault(); });
|
|
|
29 c.addEventListener("touchstart", function (e) { e.preventDefault(); }, { passive: false });
|
|
|
30 document.addEventListener("gesturestart", function (e) { e.preventDefault(); });
|
|
|
31 document.addEventListener("dblclick", function (e) { e.preventDefault(); });
|
|
|
32 }
|
|
|
33
|
|
|
34 function tap(app, x, y) {
|
|
|
35 var game = app.game(), L = app.layout();
|
|
|
36 var b = MM.ui.hit(game, L, x, y);
|
|
|
37 if (b) {
|
|
6
|
38 // the play button starts the sound
|
|
|
39 if (b.id === "play") MM.audio.start();
|
|
4
|
40 MM.audio.play("button_tap");
|
|
6
|
41 if (b.id === "play") app.play();
|
|
|
42 else if (b.id === "next") app.next();
|
|
|
43 else if (b.id === "retry") app.retry();
|
|
|
44 else if (b.id === "restart") app.restart();
|
|
|
45 else if (b.id === "pause") game.togglePause();
|
|
1
|
46 else if (b.id === "speed") game.speedIndex = (game.speedIndex + 1) % game.speeds.length;
|
|
|
47 else if (b.id === "early") game.startEarly();
|
|
|
48 else if (b.id === "shop" && game.canAfford(MM.config.tower[b.tower].cost)) game.chooseShop(b.tower);
|
|
6
|
49 else if (b.id === "upgrade") game.upgrade();
|
|
|
50 else if (b.id === "sell") game.sell();
|
|
1
|
51 return;
|
|
|
52 }
|
|
6
|
53 if (game.result || game.splash) return;
|
|
1
|
54 var mx = (x - L.ox) / L.scale, my = (y - L.oy) / L.scale;
|
|
|
55 if (mx < 0 || my < 0 || mx > game.map.width || my > game.map.height) return;
|
|
|
56 var hitR = Math.max(MM.config.placement.footprint_radius, PREVIEW_HIT / L.scale);
|
|
|
57 if (!game.tapMap(mx, my, hitR)) {
|
|
4
|
58 MM.audio.play("placement_refused");
|
|
1
|
59 app.view.refused = { x: mx, y: my, until: performance.now() + REFUSED_TIME };
|
|
|
60 }
|
|
|
61 }
|
|
|
62
|
|
|
63 return { attach: attach };
|
|
|
64 })();
|