|
6
|
1 // screen layout, top bar, tower shop, tower menu, splash and end screens. icons are placeholder
|
|
1
|
2 // shapes until the drawings arrive (art.md); numbers use a system font.
|
|
|
3
|
|
|
4 window.MM = window.MM || {};
|
|
|
5
|
|
|
6 MM.ui = (function () {
|
|
|
7 "use strict";
|
|
|
8
|
|
|
9 var cfg = MM.config;
|
|
|
10 var A = MM.assets;
|
|
|
11 var TOP = 60; // top bar height, css pixels
|
|
|
12 var SHOP = 96; // shop width (landscape) or height (portrait)
|
|
|
13 var BUTTON = 52; // top bar buttons, at least 48 for touch
|
|
|
14 var SHOP_BUTTON = 80;
|
|
6
|
15 var MENU_BUTTON = 64; // upgrade and sell, next to the selected tower
|
|
1
|
16 var INK = "#3a3530";
|
|
|
17 var PAPER = "#f6f1e6";
|
|
|
18 var FONT = "bold 22px system-ui, -apple-system, Segoe UI, sans-serif";
|
|
|
19 var TOWERS = ["t0", "t1", "t2", "t3"];
|
|
|
20
|
|
|
21 // positions of everything on screen for a window of w x h css pixels
|
|
|
22 function layout(w, h, map) {
|
|
|
23 var land = w >= h;
|
|
|
24 var area = land ? { x: 0, y: TOP, w: w - SHOP, h: h - TOP }
|
|
|
25 : { x: 0, y: TOP, w: w, h: h - TOP - SHOP };
|
|
|
26 var scale = Math.min(area.w / map.width, area.h / map.height);
|
|
|
27 var L = {
|
|
|
28 w: w, h: h, land: land, area: area, scale: scale,
|
|
|
29 ox: area.x + (area.w - map.width * scale) / 2,
|
|
|
30 oy: area.y + (area.h - map.height * scale) / 2,
|
|
|
31 shop: land ? { x: w - SHOP, y: TOP, w: SHOP, h: h - TOP }
|
|
|
32 : { x: 0, y: h - SHOP, w: w, h: SHOP },
|
|
|
33 coinIcon: { x: 26, y: TOP / 2, size: 28 }
|
|
|
34 };
|
|
|
35 return L;
|
|
|
36 }
|
|
|
37
|
|
6
|
38 // upgrade and sell buttons above the selected tower, or below it when
|
|
|
39 // there is no room above, kept inside the map area. no upgrade button
|
|
|
40 // at the top level.
|
|
|
41 function towerMenu(game, L) {
|
|
|
42 var t = game.selected, a = L.area, gap = 8, out = [];
|
|
|
43 var x = L.ox + t.x * L.scale, y = L.oy + t.y * L.scale;
|
|
|
44 var half = A.size(MM.tower.sprite(t), cfg.scale.sprite)[1] / 2 * L.scale + gap;
|
|
|
45 var w = 2 * MENU_BUTTON + gap;
|
|
|
46 var top = y - half - MENU_BUTTON;
|
|
|
47 if (top < a.y + gap) top = y + half;
|
|
|
48 var left = MM.util.clamp(x - w / 2, a.x + gap, a.x + a.w - w - gap);
|
|
|
49 if (MM.tower.upgradeCost(t)) {
|
|
|
50 out.push({ id: "upgrade", x: left, y: top, w: MENU_BUTTON, h: MENU_BUTTON });
|
|
|
51 }
|
|
|
52 out.push({ id: "sell", x: left + MENU_BUTTON + gap, y: top, w: MENU_BUTTON, h: MENU_BUTTON });
|
|
|
53 return out;
|
|
|
54 }
|
|
|
55
|
|
1
|
56 // the buttons for the current state, each {id, x, y, w, h}
|
|
|
57 function buttons(game, L) {
|
|
|
58 var out = [];
|
|
|
59 var y = (TOP - BUTTON) / 2;
|
|
6
|
60 if (game.splash) {
|
|
|
61 out.push({ id: "play", x: L.w / 2 - 56, y: L.h / 2 + 10, w: 112, h: 112 });
|
|
|
62 return out;
|
|
|
63 }
|
|
|
64 // map lost: back to the first map. map won: on to the next map, or
|
|
|
65 // after the last map back to the splash screen
|
|
1
|
66 if (game.result) {
|
|
6
|
67 var id = game.result === "lost" ? "retry" : game.lastMap() ? "restart" : "next";
|
|
|
68 out.push({ id: id, x: L.w / 2 - 48, y: L.h / 2 + 50, w: 96, h: 96 });
|
|
1
|
69 return out;
|
|
|
70 }
|
|
|
71 out.push({ id: "pause", x: L.w - BUTTON - 8, y: y, w: BUTTON, h: BUTTON });
|
|
|
72 out.push({ id: "speed", x: L.w - 2 * BUTTON - 16, y: y, w: BUTTON, h: BUTTON });
|
|
|
73 if (game.waves.phase === "break") {
|
|
|
74 out.push({ id: "early", x: L.w - 2 * BUTTON - 150, y: y, w: 126, h: BUTTON });
|
|
|
75 }
|
|
|
76 TOWERS.forEach(function (t, i) {
|
|
|
77 var pos = 10 + i * (SHOP_BUTTON + 10);
|
|
|
78 var b = L.land ? { x: L.shop.x + (SHOP - SHOP_BUTTON) / 2, y: L.shop.y + pos }
|
|
|
79 : { x: L.shop.x + pos, y: L.shop.y + (SHOP - SHOP_BUTTON) / 2 };
|
|
|
80 b.id = "shop";
|
|
|
81 b.tower = t;
|
|
|
82 b.w = b.h = SHOP_BUTTON;
|
|
|
83 out.push(b);
|
|
|
84 });
|
|
6
|
85 if (game.selected) out = out.concat(towerMenu(game, L));
|
|
1
|
86 return out;
|
|
|
87 }
|
|
|
88
|
|
|
89 function hit(game, L, x, y) {
|
|
|
90 var list = buttons(game, L);
|
|
|
91 for (var i = 0; i < list.length; i++) {
|
|
|
92 var b = list[i];
|
|
|
93 if (x >= b.x && x < b.x + b.w && y >= b.y && y < b.y + b.h) return b;
|
|
|
94 }
|
|
|
95 return null;
|
|
|
96 }
|
|
|
97
|
|
|
98 function text(ctx, s, x, y, align, size) {
|
|
|
99 ctx.font = size ? FONT.replace("22px", size + "px") : FONT;
|
|
|
100 ctx.textAlign = align || "left";
|
|
|
101 ctx.textBaseline = "middle";
|
|
|
102 ctx.lineWidth = 4;
|
|
|
103 ctx.strokeStyle = PAPER;
|
|
|
104 ctx.strokeText(s, x, y);
|
|
|
105 ctx.fillStyle = INK;
|
|
|
106 ctx.fillText(s, x, y);
|
|
|
107 }
|
|
|
108
|
|
|
109 // a sprite fitted into a box of size s, centred on x, y
|
|
|
110 function icon(ctx, name, x, y, s) {
|
|
|
111 var img = A.img(name);
|
|
|
112 var k = s / Math.max(img.naturalWidth, img.naturalHeight);
|
|
|
113 var w = img.naturalWidth * k, h = img.naturalHeight * k;
|
|
|
114 ctx.drawImage(img, x - w / 2, y - h / 2, w, h);
|
|
|
115 }
|
|
|
116
|
|
|
117 function heart(ctx, x, y, s, fill) {
|
|
|
118 ctx.beginPath();
|
|
|
119 ctx.moveTo(x, y + s * 0.4);
|
|
|
120 ctx.bezierCurveTo(x - s * 0.6, y, x - s * 0.5, y - s * 0.5, x, y - s * 0.2);
|
|
|
121 ctx.bezierCurveTo(x + s * 0.5, y - s * 0.5, x + s * 0.6, y, x, y + s * 0.4);
|
|
|
122 ctx.fillStyle = fill;
|
|
|
123 ctx.fill();
|
|
|
124 ctx.lineWidth = 2;
|
|
|
125 ctx.strokeStyle = INK;
|
|
|
126 ctx.stroke();
|
|
|
127 }
|
|
|
128
|
|
|
129 function triangle(ctx, x, y, s) {
|
|
|
130 ctx.beginPath();
|
|
|
131 ctx.moveTo(x - s * 0.35, y - s * 0.45);
|
|
|
132 ctx.lineTo(x + s * 0.45, y);
|
|
|
133 ctx.lineTo(x - s * 0.35, y + s * 0.45);
|
|
|
134 ctx.closePath();
|
|
|
135 ctx.fill();
|
|
|
136 }
|
|
|
137
|
|
|
138 function star(ctx, x, y, r) {
|
|
|
139 ctx.beginPath();
|
|
|
140 for (var i = 0; i < 10; i++) {
|
|
|
141 var a = -Math.PI / 2 + i * Math.PI / 5, rr = i % 2 ? r * 0.45 : r;
|
|
|
142 ctx.lineTo(x + Math.cos(a) * rr, y + Math.sin(a) * rr);
|
|
|
143 }
|
|
|
144 ctx.closePath();
|
|
|
145 ctx.fillStyle = "#f2c230";
|
|
|
146 ctx.fill();
|
|
|
147 ctx.lineWidth = 3;
|
|
|
148 ctx.strokeStyle = INK;
|
|
|
149 ctx.stroke();
|
|
|
150 }
|
|
|
151
|
|
|
152 function restartIcon(ctx, x, y, r) {
|
|
|
153 ctx.lineWidth = 7;
|
|
|
154 ctx.strokeStyle = INK;
|
|
|
155 ctx.beginPath();
|
|
|
156 ctx.arc(x, y, r, -Math.PI * 0.35, Math.PI * 1.45);
|
|
|
157 ctx.stroke();
|
|
|
158 var a = -Math.PI * 0.35, tx = x + Math.cos(a) * r, ty = y + Math.sin(a) * r;
|
|
|
159 ctx.fillStyle = INK;
|
|
|
160 ctx.beginPath();
|
|
|
161 ctx.moveTo(tx + 12, ty - 4);
|
|
|
162 ctx.lineTo(tx - 8, ty - 12);
|
|
|
163 ctx.lineTo(tx - 2, ty + 10);
|
|
|
164 ctx.closePath();
|
|
|
165 ctx.fill();
|
|
|
166 }
|
|
|
167
|
|
6
|
168 function arrowRight(ctx, x, y, s) {
|
|
|
169 ctx.save();
|
|
|
170 ctx.translate(x, y);
|
|
|
171 ctx.rotate(Math.PI / 2);
|
|
|
172 arrowUp(ctx, 0, 0, s);
|
|
|
173 ctx.restore();
|
|
|
174 }
|
|
|
175
|
|
|
176 function arrowUp(ctx, x, y, s) {
|
|
|
177 ctx.beginPath();
|
|
|
178 ctx.moveTo(x, y - s * 0.5);
|
|
|
179 ctx.lineTo(x + s * 0.45, y);
|
|
|
180 ctx.lineTo(x + s * 0.17, y);
|
|
|
181 ctx.lineTo(x + s * 0.17, y + s * 0.5);
|
|
|
182 ctx.lineTo(x - s * 0.17, y + s * 0.5);
|
|
|
183 ctx.lineTo(x - s * 0.17, y);
|
|
|
184 ctx.lineTo(x - s * 0.45, y);
|
|
|
185 ctx.closePath();
|
|
|
186 ctx.fill();
|
|
|
187 }
|
|
|
188
|
|
|
189 function cross(ctx, x, y, s) {
|
|
|
190 ctx.lineWidth = 6;
|
|
|
191 ctx.lineCap = "round";
|
|
|
192 ctx.beginPath();
|
|
|
193 ctx.moveTo(x - s / 2, y - s / 2);
|
|
|
194 ctx.lineTo(x + s / 2, y + s / 2);
|
|
|
195 ctx.moveTo(x + s / 2, y - s / 2);
|
|
|
196 ctx.lineTo(x - s / 2, y + s / 2);
|
|
|
197 ctx.stroke();
|
|
|
198 ctx.lineCap = "butt";
|
|
|
199 }
|
|
|
200
|
|
|
201 // a coin and an amount along the bottom of button b
|
|
|
202 function price(ctx, b, s) {
|
|
|
203 icon(ctx, "c0", b.x + 16, b.y + b.h - 13, 16);
|
|
|
204 text(ctx, s, b.x + 27, b.y + b.h - 12, "left", 18);
|
|
|
205 }
|
|
|
206
|
|
1
|
207 function box(ctx, b, fill, line) {
|
|
|
208 ctx.fillStyle = fill;
|
|
|
209 ctx.strokeStyle = line || INK;
|
|
|
210 ctx.lineWidth = 2;
|
|
|
211 ctx.beginPath();
|
|
|
212 if (ctx.roundRect) ctx.roundRect(b.x, b.y, b.w, b.h, 10);
|
|
|
213 else ctx.rect(b.x, b.y, b.w, b.h);
|
|
|
214 ctx.fill();
|
|
|
215 ctx.stroke();
|
|
|
216 }
|
|
|
217
|
|
|
218 function drawButton(ctx, game, b) {
|
|
|
219 var cx = b.x + b.w / 2, cy = b.y + b.h / 2;
|
|
|
220 if (b.id === "pause") {
|
|
|
221 box(ctx, b, game.paused ? "#ffe9a8" : PAPER);
|
|
|
222 ctx.fillStyle = INK;
|
|
|
223 if (game.paused) {
|
|
|
224 triangle(ctx, cx, cy, 26);
|
|
|
225 } else {
|
|
|
226 ctx.fillRect(cx - 10, cy - 12, 7, 24);
|
|
|
227 ctx.fillRect(cx + 3, cy - 12, 7, 24);
|
|
|
228 }
|
|
|
229 } else if (b.id === "speed") {
|
|
|
230 // one triangle per speed step; numbers for the debug speeds
|
|
|
231 var sp = game.speed();
|
|
|
232 box(ctx, b, sp > 1 ? "#ffe9a8" : PAPER);
|
|
|
233 ctx.fillStyle = INK;
|
|
|
234 if (sp <= 3) {
|
|
|
235 for (var i = 0; i < sp; i++) triangle(ctx, cx + (i - (sp - 1) / 2) * 12, cy, 20);
|
|
|
236 } else {
|
|
|
237 text(ctx, sp + "", cx, cy, "center");
|
|
|
238 }
|
|
|
239 } else if (b.id === "early") {
|
|
|
240 box(ctx, b, PAPER);
|
|
|
241 ctx.fillStyle = INK;
|
|
|
242 triangle(ctx, b.x + 20, cy, 22);
|
|
|
243 ctx.fillRect(b.x + 29, cy - 10, 4, 20);
|
|
|
244 text(ctx, Math.ceil(game.waves.left) + "", b.x + 58, cy, "center");
|
|
|
245 var bonus = game.waves.bonus();
|
|
|
246 if (bonus > 0) text(ctx, "+" + bonus, b.x + b.w - 6, cy, "right", 18);
|
|
|
247 } else if (b.id === "shop") {
|
|
|
248 var def = cfg.tower[b.tower];
|
|
|
249 var ok = game.canAfford(def.cost);
|
|
|
250 box(ctx, b, game.shop === b.tower ? "#ffe9a8" : PAPER,
|
|
|
251 game.shop === b.tower ? "#c08000" : INK);
|
|
|
252 ctx.globalAlpha = ok ? 1 : 0.35;
|
|
|
253 icon(ctx, b.tower, cx, b.y + 30, 46);
|
|
|
254 icon(ctx, "c0", b.x + 22, b.y + b.h - 14, 16);
|
|
|
255 text(ctx, def.cost + "", b.x + 34, b.y + b.h - 13, "left", 18);
|
|
|
256 ctx.globalAlpha = 1;
|
|
6
|
257 } else if (b.id === "upgrade") {
|
|
|
258 var cost = MM.tower.upgradeCost(game.selected);
|
|
|
259 box(ctx, b, PAPER);
|
|
|
260 ctx.globalAlpha = game.canAfford(cost) ? 1 : 0.35;
|
|
|
261 ctx.fillStyle = INK;
|
|
|
262 arrowUp(ctx, cx, b.y + 22, 28);
|
|
|
263 price(ctx, b, cost + "");
|
|
|
264 ctx.globalAlpha = 1;
|
|
|
265 } else if (b.id === "sell") {
|
|
|
266 box(ctx, b, PAPER);
|
|
|
267 ctx.strokeStyle = "#c03030";
|
|
|
268 cross(ctx, cx, b.y + 22, 20);
|
|
|
269 price(ctx, b, "+" + MM.tower.refund(game.selected, game.pauseNumber()));
|
|
|
270 } else if (b.id === "play") {
|
|
|
271 box(ctx, b, PAPER);
|
|
|
272 ctx.fillStyle = INK;
|
|
|
273 triangle(ctx, cx + 4, cy, 60);
|
|
|
274 } else if (b.id === "next") {
|
|
|
275 box(ctx, b, PAPER);
|
|
|
276 ctx.fillStyle = INK;
|
|
|
277 arrowRight(ctx, cx, cy, 52);
|
|
|
278 } else if (b.id === "retry" || b.id === "restart") {
|
|
1
|
279 box(ctx, b, PAPER);
|
|
|
280 restartIcon(ctx, cx, cy, 26);
|
|
|
281 }
|
|
|
282 }
|
|
|
283
|
|
|
284 function draw(ctx, game, L, view) {
|
|
|
285 var dpr = view.dpr;
|
|
|
286 ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
|
287
|
|
|
288 // top bar: coins, hearts, wave
|
|
|
289 ctx.fillStyle = "rgba(246, 241, 230, 0.92)";
|
|
|
290 ctx.fillRect(0, 0, L.w, TOP);
|
|
|
291 ctx.fillStyle = "rgba(246, 241, 230, 0.92)";
|
|
|
292 ctx.fillRect(L.shop.x, L.shop.y, L.shop.w, L.shop.h);
|
|
|
293 ctx.fillStyle = "rgba(58, 53, 48, 0.25)";
|
|
|
294 ctx.fillRect(0, TOP - 2, L.w, 2);
|
|
|
295 icon(ctx, "c0", L.coinIcon.x, L.coinIcon.y, L.coinIcon.size);
|
|
|
296 text(ctx, game.coins + "", 46, TOP / 2);
|
|
|
297 heart(ctx, 126, TOP / 2 + 2, 30, "#e03a3a");
|
|
|
298 text(ctx, game.hearts + "", 146, TOP / 2);
|
|
|
299 icon(ctx, "m0", 206, TOP / 2, 30);
|
|
|
300 text(ctx, game.waves.shown() + "/" + game.waves.list.length, 226, TOP / 2);
|
|
|
301
|
|
6
|
302 var screen = game.splash || game.result;
|
|
|
303 if (!screen) buttons(game, L).forEach(function (b) { drawButton(ctx, game, b); });
|
|
1
|
304
|
|
6
|
305 if (game.paused && !screen) {
|
|
1
|
306 ctx.fillStyle = "rgba(255, 255, 255, 0.18)";
|
|
|
307 ctx.fillRect(L.ox, L.oy, game.map.width * L.scale, game.map.height * L.scale);
|
|
|
308 }
|
|
6
|
309 if (game.splash) {
|
|
|
310 // title and byline, placeholders until the drawings arrive
|
|
|
311 ctx.fillStyle = "rgba(246, 241, 230, 0.55)";
|
|
|
312 ctx.fillRect(0, 0, L.w, L.h);
|
|
|
313 text(ctx, "minor monsters", L.w / 2, L.h / 2 - 90, "center", 60);
|
|
|
314 text(ctx, "By M.A. and J.A. Juszczyk", L.w / 2, L.h / 2 - 38, "center", 24);
|
|
|
315 } else if (game.result) {
|
|
1
|
316 ctx.fillStyle = "rgba(40, 35, 30, 0.45)";
|
|
|
317 ctx.fillRect(0, 0, L.w, L.h);
|
|
6
|
318 if (game.result === "won" && game.lastMap()) {
|
|
|
319 // game won: three stars
|
|
|
320 star(ctx, L.w / 2 - 125, L.h / 2 - 15, 50);
|
|
|
321 star(ctx, L.w / 2 + 125, L.h / 2 - 15, 50);
|
|
|
322 star(ctx, L.w / 2, L.h / 2 - 45, 70);
|
|
|
323 } else if (game.result === "won") {
|
|
1
|
324 star(ctx, L.w / 2, L.h / 2 - 40, 70);
|
|
|
325 } else {
|
|
|
326 heart(ctx, L.w / 2, L.h / 2 - 30, 140, "#8a8a8a");
|
|
|
327 ctx.strokeStyle = INK;
|
|
|
328 ctx.lineWidth = 5;
|
|
|
329 ctx.beginPath();
|
|
|
330 ctx.moveTo(L.w / 2 - 6, L.h / 2 - 72);
|
|
|
331 ctx.lineTo(L.w / 2 + 10, L.h / 2 - 40);
|
|
|
332 ctx.lineTo(L.w / 2 - 10, L.h / 2 - 20);
|
|
|
333 ctx.lineTo(L.w / 2 + 4, L.h / 2 + 22);
|
|
|
334 ctx.stroke();
|
|
|
335 }
|
|
|
336 }
|
|
6
|
337 if (screen) buttons(game, L).forEach(function (b) { drawButton(ctx, game, b); });
|
|
1
|
338
|
|
|
339 if (cfg.debug.show_stats && view.stats) {
|
|
|
340 var st = view.stats;
|
|
|
341 text(ctx, st.ms.toFixed(1) + " ms " + game.monsters.length + " m " +
|
|
|
342 game.towers.length + " t " + game.projectiles.length + " p seed " + game.seed,
|
|
|
343 8, L.h - 14, "left", 14);
|
|
|
344 }
|
|
|
345 }
|
|
|
346
|
|
|
347 return { layout: layout, hit: hit, draw: draw };
|
|
|
348 })();
|