comparison game/js/input.js @ 24:3f2811aebb3d 0.9.0

splash screen buttons, help button, info screen
author ark
date 2026-10-20 15:58 +0200
parents fe680946164b
children 85d23328a215
comparison
equal deleted inserted replaced
23:2f555a54c332 24:3f2811aebb3d
78 var mx = (x - L.ox) / L.scale, my = (y - L.oy) / L.scale; 78 var mx = (x - L.ox) / L.scale, my = (y - L.oy) / L.scale;
79 if (mx < 0 || my < 0 || mx > game.map.width || my > game.map.height) return null; 79 if (mx < 0 || my < 0 || mx > game.map.width || my > game.map.height) return null;
80 return { x: mx, y: my }; 80 return { x: mx, y: my };
81 } 81 }
82 82
83 // a press at css position x, y. buttons and tower selection act at once. 83 // a press at css position x, y. buttons and tower selection act at once,
84 // a press on the map while a shop tower is chosen returns the drag that 84 // apart from the logo and the email address on the help popup: tablets
85 // places the preview: 85 // open a new tab only when the finger is lifted, so the press on either
86 // returns {game, button} and the release acts. a press on the map while a shop tower is chosen returns
87 // the drag that places the preview:
86 // game the game it belongs to 88 // game the game it belongs to
87 // onPreview the press was on the preview; it builds on release unless 89 // onPreview the press was on the preview; it builds on release unless
88 // the pointer moved further than DRAG_SLOP 90 // the pointer moved further than DRAG_SLOP
89 // moved the pointer moved further than DRAG_SLOP 91 // moved the pointer moved further than DRAG_SLOP
90 // found a legal position was found during the drag 92 // found a legal position was found during the drag
102 MM.audio.play("button_tap"); 104 MM.audio.play("button_tap");
103 // any other button cancels an armed sell, and any button outside 105 // any other button cancels an armed sell, and any button outside
104 // the tower menu closes it 106 // the tower menu closes it
105 if (b.id !== "sell") game.sellArmed = false; 107 if (b.id !== "sell") game.sellArmed = false;
106 if (b.id !== "sell" && b.id !== "upgrade") game.selected = null; 108 if (b.id !== "sell" && b.id !== "upgrade") game.selected = null;
109 if (b.id === "logo" || b.id === "email") return { game: game, button: b.id };
107 if (b.id === "play") app.play(); 110 if (b.id === "play") app.play();
108 else if (b.id === "next") app.next(); 111 else if (b.id === "next") app.next();
109 else if (b.id === "retry") app.retry(); 112 else if (b.id === "retry") app.retry();
110 else if (b.id === "restart") app.restart(); 113 else if (b.id === "restart") app.restart();
114 else if (b.id === "help") game.openHelp();
115 else if (b.id === "close_help") game.closeHelp();
111 else if (b.id === "pause") game.togglePause(); 116 else if (b.id === "pause") game.togglePause();
112 else if (b.id === "speed") game.speedIndex = (game.speedIndex + 1) % game.speeds.length; 117 else if (b.id === "speed") game.speedIndex = (game.speedIndex + 1) % game.speeds.length;
113 else if (b.id === "early") game.startEarly(); 118 else if (b.id === "early") game.startEarly();
114 else if (b.id === "shop" && !game.paused && game.canAfford(MM.config.tower[b.tower].cost[0])) game.chooseShop(b.tower); 119 else if (b.id === "shop" && !game.paused && game.canAfford(MM.config.tower[b.tower].cost[0])) game.chooseShop(b.tower);
115 else if (b.id === "upgrade") game.upgrade(); 120 else if (b.id === "upgrade") game.upgrade();
139 } 144 }
140 145
141 // the pointer of a drag moved to css position x, y 146 // the pointer of a drag moved to css position x, y
142 function move(app, d, x, y) { 147 function move(app, d, x, y) {
143 var game = app.game(); 148 var game = app.game();
144 if (game !== d.game || game.result || !game.shop) return; 149 if (d.button || game !== d.game || game.result || !game.shop) return;
145 if (!d.moved && Math.hypot(x - d.sx, y - d.sy) <= DRAG_SLOP) { 150 if (!d.moved && Math.hypot(x - d.sx, y - d.sy) <= DRAG_SLOP) {
146 // a press on the preview stays a tap until it moves far enough 151 // a press on the preview stays a tap until it moves far enough
147 if (d.onPreview) return; 152 if (d.onPreview) return;
148 } else { 153 } else {
149 d.moved = true; 154 d.moved = true;
150 } 155 }
151 var m = mapPos(game, app.layout(), x, y); 156 var m = mapPos(game, app.layout(), x, y);
152 if (m && game.movePreview(m.x + d.dx, m.y + d.dy)) d.found = true; 157 if (m && game.movePreview(m.x + d.dx, m.y + d.dy)) d.found = true;
153 } 158 }
154 159
155 // the pointer of a drag was released at css position x, y. a tap on the 160 // the pointer of a drag was released at css position x, y. the logo
156 // preview builds; a drag that never found a legal position is refused 161 // opens its web page and the email address a new mail when the pointer
162 // is still on it. a tap on the preview builds; a drag that never found a
163 // legal position is refused
157 function release(app, d, x, y) { 164 function release(app, d, x, y) {
158 var game = app.game(), L = app.layout(); 165 var game = app.game(), L = app.layout();
166 if (d.button) {
167 var b = MM.ui.hit(game, L, x, y), links = MM.config.links;
168 if (!b || b.id !== d.button) return;
169 if (b.id === "logo" && links.logo_url) window.open(links.logo_url, "_blank", "noopener");
170 if (b.id === "email") {
171 window.location.href = "mailto:" + links.email + "?subject=" +
172 encodeURIComponent(links.email_subject);
173 }
174 return;
175 }
159 if (game !== d.game || game.result || game.paused) return; 176 if (game !== d.game || game.result || game.paused) return;
160 var ok = d.onPreview && !d.moved ? game.build() : d.found; 177 var ok = d.onPreview && !d.moved ? game.build() : d.found;
161 if (ok) return; 178 if (ok) return;
162 MM.audio.play("placement_refused"); 179 MM.audio.play("placement_refused");
163 app.view.refused = { x: (x - L.ox) / L.scale, y: (y - L.oy) / L.scale, 180 app.view.refused = { x: (x - L.ox) / L.scale, y: (y - L.oy) / L.scale,