annotate game/js/ui.js @ 7:4fd2da999ba4

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