|
1
|
1 // screen layout, top bar, tower shop and end screens. icons are placeholder
|
|
|
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;
|
|
|
15 var INK = "#3a3530";
|
|
|
16 var PAPER = "#f6f1e6";
|
|
|
17 var FONT = "bold 22px system-ui, -apple-system, Segoe UI, sans-serif";
|
|
|
18 var TOWERS = ["t0", "t1", "t2", "t3"];
|
|
|
19
|
|
|
20 // positions of everything on screen for a window of w x h css pixels
|
|
|
21 function layout(w, h, map) {
|
|
|
22 var land = w >= h;
|
|
|
23 var area = land ? { x: 0, y: TOP, w: w - SHOP, h: h - TOP }
|
|
|
24 : { x: 0, y: TOP, w: w, h: h - TOP - SHOP };
|
|
|
25 var scale = Math.min(area.w / map.width, area.h / map.height);
|
|
|
26 var L = {
|
|
|
27 w: w, h: h, land: land, area: area, scale: scale,
|
|
|
28 ox: area.x + (area.w - map.width * scale) / 2,
|
|
|
29 oy: area.y + (area.h - map.height * scale) / 2,
|
|
|
30 shop: land ? { x: w - SHOP, y: TOP, w: SHOP, h: h - TOP }
|
|
|
31 : { x: 0, y: h - SHOP, w: w, h: SHOP },
|
|
|
32 coinIcon: { x: 26, y: TOP / 2, size: 28 }
|
|
|
33 };
|
|
|
34 return L;
|
|
|
35 }
|
|
|
36
|
|
|
37 // the buttons for the current state, each {id, x, y, w, h}
|
|
|
38 function buttons(game, L) {
|
|
|
39 var out = [];
|
|
|
40 var y = (TOP - BUTTON) / 2;
|
|
|
41 if (game.result) {
|
|
|
42 out.push({ id: "restart", x: L.w / 2 - 48, y: L.h / 2 + 50, w: 96, h: 96 });
|
|
|
43 return out;
|
|
|
44 }
|
|
|
45 out.push({ id: "pause", x: L.w - BUTTON - 8, y: y, w: BUTTON, h: BUTTON });
|
|
|
46 out.push({ id: "speed", x: L.w - 2 * BUTTON - 16, y: y, w: BUTTON, h: BUTTON });
|
|
|
47 if (game.waves.phase === "break") {
|
|
|
48 out.push({ id: "early", x: L.w - 2 * BUTTON - 150, y: y, w: 126, h: BUTTON });
|
|
|
49 }
|
|
|
50 TOWERS.forEach(function (t, i) {
|
|
|
51 var pos = 10 + i * (SHOP_BUTTON + 10);
|
|
|
52 var b = L.land ? { x: L.shop.x + (SHOP - SHOP_BUTTON) / 2, y: L.shop.y + pos }
|
|
|
53 : { x: L.shop.x + pos, y: L.shop.y + (SHOP - SHOP_BUTTON) / 2 };
|
|
|
54 b.id = "shop";
|
|
|
55 b.tower = t;
|
|
|
56 b.w = b.h = SHOP_BUTTON;
|
|
|
57 out.push(b);
|
|
|
58 });
|
|
|
59 return out;
|
|
|
60 }
|
|
|
61
|
|
|
62 function hit(game, L, x, y) {
|
|
|
63 var list = buttons(game, L);
|
|
|
64 for (var i = 0; i < list.length; i++) {
|
|
|
65 var b = list[i];
|
|
|
66 if (x >= b.x && x < b.x + b.w && y >= b.y && y < b.y + b.h) return b;
|
|
|
67 }
|
|
|
68 return null;
|
|
|
69 }
|
|
|
70
|
|
|
71 function text(ctx, s, x, y, align, size) {
|
|
|
72 ctx.font = size ? FONT.replace("22px", size + "px") : FONT;
|
|
|
73 ctx.textAlign = align || "left";
|
|
|
74 ctx.textBaseline = "middle";
|
|
|
75 ctx.lineWidth = 4;
|
|
|
76 ctx.strokeStyle = PAPER;
|
|
|
77 ctx.strokeText(s, x, y);
|
|
|
78 ctx.fillStyle = INK;
|
|
|
79 ctx.fillText(s, x, y);
|
|
|
80 }
|
|
|
81
|
|
|
82 // a sprite fitted into a box of size s, centred on x, y
|
|
|
83 function icon(ctx, name, x, y, s) {
|
|
|
84 var img = A.img(name);
|
|
|
85 var k = s / Math.max(img.naturalWidth, img.naturalHeight);
|
|
|
86 var w = img.naturalWidth * k, h = img.naturalHeight * k;
|
|
|
87 ctx.drawImage(img, x - w / 2, y - h / 2, w, h);
|
|
|
88 }
|
|
|
89
|
|
|
90 function heart(ctx, x, y, s, fill) {
|
|
|
91 ctx.beginPath();
|
|
|
92 ctx.moveTo(x, y + s * 0.4);
|
|
|
93 ctx.bezierCurveTo(x - s * 0.6, y, x - s * 0.5, y - s * 0.5, x, y - s * 0.2);
|
|
|
94 ctx.bezierCurveTo(x + s * 0.5, y - s * 0.5, x + s * 0.6, y, x, y + s * 0.4);
|
|
|
95 ctx.fillStyle = fill;
|
|
|
96 ctx.fill();
|
|
|
97 ctx.lineWidth = 2;
|
|
|
98 ctx.strokeStyle = INK;
|
|
|
99 ctx.stroke();
|
|
|
100 }
|
|
|
101
|
|
|
102 function triangle(ctx, x, y, s) {
|
|
|
103 ctx.beginPath();
|
|
|
104 ctx.moveTo(x - s * 0.35, y - s * 0.45);
|
|
|
105 ctx.lineTo(x + s * 0.45, y);
|
|
|
106 ctx.lineTo(x - s * 0.35, y + s * 0.45);
|
|
|
107 ctx.closePath();
|
|
|
108 ctx.fill();
|
|
|
109 }
|
|
|
110
|
|
|
111 function star(ctx, x, y, r) {
|
|
|
112 ctx.beginPath();
|
|
|
113 for (var i = 0; i < 10; i++) {
|
|
|
114 var a = -Math.PI / 2 + i * Math.PI / 5, rr = i % 2 ? r * 0.45 : r;
|
|
|
115 ctx.lineTo(x + Math.cos(a) * rr, y + Math.sin(a) * rr);
|
|
|
116 }
|
|
|
117 ctx.closePath();
|
|
|
118 ctx.fillStyle = "#f2c230";
|
|
|
119 ctx.fill();
|
|
|
120 ctx.lineWidth = 3;
|
|
|
121 ctx.strokeStyle = INK;
|
|
|
122 ctx.stroke();
|
|
|
123 }
|
|
|
124
|
|
|
125 function restartIcon(ctx, x, y, r) {
|
|
|
126 ctx.lineWidth = 7;
|
|
|
127 ctx.strokeStyle = INK;
|
|
|
128 ctx.beginPath();
|
|
|
129 ctx.arc(x, y, r, -Math.PI * 0.35, Math.PI * 1.45);
|
|
|
130 ctx.stroke();
|
|
|
131 var a = -Math.PI * 0.35, tx = x + Math.cos(a) * r, ty = y + Math.sin(a) * r;
|
|
|
132 ctx.fillStyle = INK;
|
|
|
133 ctx.beginPath();
|
|
|
134 ctx.moveTo(tx + 12, ty - 4);
|
|
|
135 ctx.lineTo(tx - 8, ty - 12);
|
|
|
136 ctx.lineTo(tx - 2, ty + 10);
|
|
|
137 ctx.closePath();
|
|
|
138 ctx.fill();
|
|
|
139 }
|
|
|
140
|
|
|
141 function box(ctx, b, fill, line) {
|
|
|
142 ctx.fillStyle = fill;
|
|
|
143 ctx.strokeStyle = line || INK;
|
|
|
144 ctx.lineWidth = 2;
|
|
|
145 ctx.beginPath();
|
|
|
146 if (ctx.roundRect) ctx.roundRect(b.x, b.y, b.w, b.h, 10);
|
|
|
147 else ctx.rect(b.x, b.y, b.w, b.h);
|
|
|
148 ctx.fill();
|
|
|
149 ctx.stroke();
|
|
|
150 }
|
|
|
151
|
|
|
152 function drawButton(ctx, game, b) {
|
|
|
153 var cx = b.x + b.w / 2, cy = b.y + b.h / 2;
|
|
|
154 if (b.id === "pause") {
|
|
|
155 box(ctx, b, game.paused ? "#ffe9a8" : PAPER);
|
|
|
156 ctx.fillStyle = INK;
|
|
|
157 if (game.paused) {
|
|
|
158 triangle(ctx, cx, cy, 26);
|
|
|
159 } else {
|
|
|
160 ctx.fillRect(cx - 10, cy - 12, 7, 24);
|
|
|
161 ctx.fillRect(cx + 3, cy - 12, 7, 24);
|
|
|
162 }
|
|
|
163 } else if (b.id === "speed") {
|
|
|
164 // one triangle per speed step; numbers for the debug speeds
|
|
|
165 var sp = game.speed();
|
|
|
166 box(ctx, b, sp > 1 ? "#ffe9a8" : PAPER);
|
|
|
167 ctx.fillStyle = INK;
|
|
|
168 if (sp <= 3) {
|
|
|
169 for (var i = 0; i < sp; i++) triangle(ctx, cx + (i - (sp - 1) / 2) * 12, cy, 20);
|
|
|
170 } else {
|
|
|
171 text(ctx, sp + "", cx, cy, "center");
|
|
|
172 }
|
|
|
173 } else if (b.id === "early") {
|
|
|
174 box(ctx, b, PAPER);
|
|
|
175 ctx.fillStyle = INK;
|
|
|
176 triangle(ctx, b.x + 20, cy, 22);
|
|
|
177 ctx.fillRect(b.x + 29, cy - 10, 4, 20);
|
|
|
178 text(ctx, Math.ceil(game.waves.left) + "", b.x + 58, cy, "center");
|
|
|
179 var bonus = game.waves.bonus();
|
|
|
180 if (bonus > 0) text(ctx, "+" + bonus, b.x + b.w - 6, cy, "right", 18);
|
|
|
181 } else if (b.id === "shop") {
|
|
|
182 var def = cfg.tower[b.tower];
|
|
|
183 var ok = game.canAfford(def.cost);
|
|
|
184 box(ctx, b, game.shop === b.tower ? "#ffe9a8" : PAPER,
|
|
|
185 game.shop === b.tower ? "#c08000" : INK);
|
|
|
186 ctx.globalAlpha = ok ? 1 : 0.35;
|
|
|
187 icon(ctx, b.tower, cx, b.y + 30, 46);
|
|
|
188 icon(ctx, "c0", b.x + 22, b.y + b.h - 14, 16);
|
|
|
189 text(ctx, def.cost + "", b.x + 34, b.y + b.h - 13, "left", 18);
|
|
|
190 ctx.globalAlpha = 1;
|
|
|
191 } else if (b.id === "restart") {
|
|
|
192 box(ctx, b, PAPER);
|
|
|
193 restartIcon(ctx, cx, cy, 26);
|
|
|
194 }
|
|
|
195 }
|
|
|
196
|
|
|
197 function draw(ctx, game, L, view) {
|
|
|
198 var dpr = view.dpr;
|
|
|
199 ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
|
200
|
|
|
201 // top bar: coins, hearts, wave
|
|
|
202 ctx.fillStyle = "rgba(246, 241, 230, 0.92)";
|
|
|
203 ctx.fillRect(0, 0, L.w, TOP);
|
|
|
204 ctx.fillStyle = "rgba(246, 241, 230, 0.92)";
|
|
|
205 ctx.fillRect(L.shop.x, L.shop.y, L.shop.w, L.shop.h);
|
|
|
206 ctx.fillStyle = "rgba(58, 53, 48, 0.25)";
|
|
|
207 ctx.fillRect(0, TOP - 2, L.w, 2);
|
|
|
208 icon(ctx, "c0", L.coinIcon.x, L.coinIcon.y, L.coinIcon.size);
|
|
|
209 text(ctx, game.coins + "", 46, TOP / 2);
|
|
|
210 heart(ctx, 126, TOP / 2 + 2, 30, "#e03a3a");
|
|
|
211 text(ctx, game.hearts + "", 146, TOP / 2);
|
|
|
212 icon(ctx, "m0", 206, TOP / 2, 30);
|
|
|
213 text(ctx, game.waves.shown() + "/" + game.waves.list.length, 226, TOP / 2);
|
|
|
214
|
|
|
215 if (!game.result) buttons(game, L).forEach(function (b) { drawButton(ctx, game, b); });
|
|
|
216
|
|
|
217 if (game.paused && !game.result) {
|
|
|
218 ctx.fillStyle = "rgba(255, 255, 255, 0.18)";
|
|
|
219 ctx.fillRect(L.ox, L.oy, game.map.width * L.scale, game.map.height * L.scale);
|
|
|
220 }
|
|
|
221 if (game.result) {
|
|
|
222 ctx.fillStyle = "rgba(40, 35, 30, 0.45)";
|
|
|
223 ctx.fillRect(0, 0, L.w, L.h);
|
|
|
224 if (game.result === "won") {
|
|
|
225 star(ctx, L.w / 2, L.h / 2 - 40, 70);
|
|
|
226 } else {
|
|
|
227 heart(ctx, L.w / 2, L.h / 2 - 30, 140, "#8a8a8a");
|
|
|
228 ctx.strokeStyle = INK;
|
|
|
229 ctx.lineWidth = 5;
|
|
|
230 ctx.beginPath();
|
|
|
231 ctx.moveTo(L.w / 2 - 6, L.h / 2 - 72);
|
|
|
232 ctx.lineTo(L.w / 2 + 10, L.h / 2 - 40);
|
|
|
233 ctx.lineTo(L.w / 2 - 10, L.h / 2 - 20);
|
|
|
234 ctx.lineTo(L.w / 2 + 4, L.h / 2 + 22);
|
|
|
235 ctx.stroke();
|
|
|
236 }
|
|
|
237 buttons(game, L).forEach(function (b) { drawButton(ctx, game, b); });
|
|
|
238 }
|
|
|
239
|
|
|
240 if (cfg.debug.show_stats && view.stats) {
|
|
|
241 var st = view.stats;
|
|
|
242 text(ctx, st.ms.toFixed(1) + " ms " + game.monsters.length + " m " +
|
|
|
243 game.towers.length + " t " + game.projectiles.length + " p seed " + game.seed,
|
|
|
244 8, L.h - 14, "left", 14);
|
|
|
245 }
|
|
|
246 }
|
|
|
247
|
|
|
248 return { layout: layout, hit: hit, draw: draw };
|
|
|
249 })();
|