annotate game/js/ui.js @ 34:83e8b0e0f010 0.10.1

homepage link on splash does not open a new tab anymore
author ark
date 2026-10-20 19:54 +0200
parents 85d23328a215
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17
fe680946164b reorganised repo
ark
parents:
diff changeset
1 // screen layout, top bar, tower shop, tower menu, splash and end screens,
24
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
2 // help popup, drawn from the supplied icons and drawings (art.md). numbers use the
17
fe680946164b reorganised repo
ark
parents:
diff changeset
3 // hand-drawn digits.
fe680946164b reorganised repo
ark
parents:
diff changeset
4
fe680946164b reorganised repo
ark
parents:
diff changeset
5 window.MM = window.MM || {};
fe680946164b reorganised repo
ark
parents:
diff changeset
6
fe680946164b reorganised repo
ark
parents:
diff changeset
7 MM.ui = (function () {
fe680946164b reorganised repo
ark
parents:
diff changeset
8 "use strict";
fe680946164b reorganised repo
ark
parents:
diff changeset
9
fe680946164b reorganised repo
ark
parents:
diff changeset
10 var cfg = MM.config;
fe680946164b reorganised repo
ark
parents:
diff changeset
11 var A = MM.assets;
23
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
12 var TOP = 60; // height of one top bar row, css pixels
17
fe680946164b reorganised repo
ark
parents:
diff changeset
13 var SHOP = 96; // shop width (landscape) or height (portrait)
fe680946164b reorganised repo
ark
parents:
diff changeset
14 var BUTTON = 52; // top bar buttons, at least 48 for touch
fe680946164b reorganised repo
ark
parents:
diff changeset
15 var SHOP_W = 84; // shop items
fe680946164b reorganised repo
ark
parents:
diff changeset
16 var SHOP_H = 72;
fe680946164b reorganised repo
ark
parents:
diff changeset
17 var POPUP_W = 168; // tower menu bubble; its height follows the drawing
fe680946164b reorganised repo
ark
parents:
diff changeset
18 var DIGIT = 28; // digit height in the top bar
fe680946164b reorganised repo
ark
parents:
diff changeset
19 var PRICE = 18; // digit height of prices
fe680946164b reorganised repo
ark
parents:
diff changeset
20 var DIGIT_GAP = 0.08; // space between digits, a share of their height
fe680946164b reorganised repo
ark
parents:
diff changeset
21 var BUBBLE_FLIP = 400; // ms between the two drawings of the chosen shop item
fe680946164b reorganised repo
ark
parents:
diff changeset
22 var PULSE = 1.2; // seconds of one pulse of a screen button
fe680946164b reorganised repo
ark
parents:
diff changeset
23 var WARN_PULSE = 0.5; // seconds of one pulse of an armed sell button
fe680946164b reorganised repo
ark
parents:
diff changeset
24 var TOWERS = ["t0", "t1", "t2", "t3"];
23
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
25 // widest usual values of the coin, heart and wave counters
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
26 var USUAL = ["000", "00", "00/00"];
17
fe680946164b reorganised repo
ark
parents:
diff changeset
27 // each screen drawing, its separate button image and the button's action
fe680946164b reorganised repo
ark
parents:
diff changeset
28 var SCREENS = {
fe680946164b reorganised repo
ark
parents:
diff changeset
29 splash: { image: "splash_play", id: "play" },
fe680946164b reorganised repo
ark
parents:
diff changeset
30 map_won: { image: "map_won_button", id: "next" },
fe680946164b reorganised repo
ark
parents:
diff changeset
31 game_lost: { image: "game_lost_button", id: "retry" },
fe680946164b reorganised repo
ark
parents:
diff changeset
32 game_won: { image: "game_won_button", id: "restart" }
fe680946164b reorganised repo
ark
parents:
diff changeset
33 };
24
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
34 // the extra buttons of the splash screen beside the play button, placed
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
35 // by [art.screen] in the config
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
36 var SPLASH_EXTRA = [
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
37 { image: "wobbly_logo", id: "logo", at: "splash_logo" },
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
38 { image: "help_splash", id: "help", at: "splash_help" }
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
39 ];
17
fe680946164b reorganised repo
ark
parents:
diff changeset
40
fe680946164b reorganised repo
ark
parents:
diff changeset
41 // digit variants from the sprite table: character -> sprite names
fe680946164b reorganised repo
ark
parents:
diff changeset
42 var DIGITS = {};
fe680946164b reorganised repo
ark
parents:
diff changeset
43 Object.keys(cfg.sprites).forEach(function (n) {
fe680946164b reorganised repo
ark
parents:
diff changeset
44 var m = /^digit_(\d|slash)_\d+$/.exec(n);
fe680946164b reorganised repo
ark
parents:
diff changeset
45 if (!m) return;
fe680946164b reorganised repo
ark
parents:
diff changeset
46 var ch = m[1] === "slash" ? "/" : m[1];
fe680946164b reorganised repo
ark
parents:
diff changeset
47 (DIGITS[ch] = DIGITS[ch] || []).push(n);
fe680946164b reorganised repo
ark
parents:
diff changeset
48 });
fe680946164b reorganised repo
ark
parents:
diff changeset
49
fe680946164b reorganised repo
ark
parents:
diff changeset
50 // all screens share one scale, set by the largest drawing, so they keep
fe680946164b reorganised repo
ark
parents:
diff changeset
51 // the sizes of the drawings relative to each other
fe680946164b reorganised repo
ark
parents:
diff changeset
52 function screenScale(w, h) {
fe680946164b reorganised repo
ark
parents:
diff changeset
53 var mw = 0, mh = 0;
fe680946164b reorganised repo
ark
parents:
diff changeset
54 Object.keys(SCREENS).forEach(function (n) {
fe680946164b reorganised repo
ark
parents:
diff changeset
55 mw = Math.max(mw, cfg.sprites[n][0]);
fe680946164b reorganised repo
ark
parents:
diff changeset
56 mh = Math.max(mh, cfg.sprites[n][1]);
fe680946164b reorganised repo
ark
parents:
diff changeset
57 });
fe680946164b reorganised repo
ark
parents:
diff changeset
58 return Math.min(1, 0.92 * w / mw, 0.9 * h / mh);
fe680946164b reorganised repo
ark
parents:
diff changeset
59 }
fe680946164b reorganised repo
ark
parents:
diff changeset
60
23
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
61 // width of the top bar counters, each at its widest usual value, for
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
62 // icons of the given size
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
63 function countersWidth(icon) {
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
64 var w = 0;
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
65 USUAL.forEach(function (s) { w += icon + 6 + numberWidth(s, DIGIT) + 18; });
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
66 return w - 18;
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
67 }
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
68
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
69 // width of the top bar buttons, the early start button included
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
70 function buttonsWidth() {
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
71 var s = cfg.sprites.next_wave;
24
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
72 return 4 * (BUTTON + 8) + BUTTON * s[0] / s[1];
23
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
73 }
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
74
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
75 // positions of everything on screen for a window of w x h css pixels.
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
76 // when the counters and the buttons do not fit side by side, the buttons
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
77 // get a second top bar row under the counters.
17
fe680946164b reorganised repo
ark
parents:
diff changeset
78 function layout(w, h, map) {
23
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
79 var land = w >= h, icon = 32;
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
80 var rows = 8 + countersWidth(icon) + 16 + buttonsWidth() + 8 > w ? 2 : 1;
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
81 var top = rows * TOP;
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
82 var area = land ? { x: 0, y: top, w: w - SHOP, h: h - top }
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
83 : { x: 0, y: top, w: w, h: h - top - SHOP };
17
fe680946164b reorganised repo
ark
parents:
diff changeset
84 var scale = Math.min(area.w / map.width, area.h / map.height);
fe680946164b reorganised repo
ark
parents:
diff changeset
85 var L = {
fe680946164b reorganised repo
ark
parents:
diff changeset
86 w: w, h: h, land: land, area: area, scale: scale,
23
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
87 top: top, // top bar height
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
88 buttonRow: top - TOP, // top of the button row
17
fe680946164b reorganised repo
ark
parents:
diff changeset
89 ox: area.x + (area.w - map.width * scale) / 2,
fe680946164b reorganised repo
ark
parents:
diff changeset
90 oy: area.y + (area.h - map.height * scale) / 2,
23
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
91 shop: land ? { x: w - SHOP, y: top, w: SHOP, h: h - top }
17
fe680946164b reorganised repo
ark
parents:
diff changeset
92 : { x: 0, y: h - SHOP, w: w, h: SHOP },
23
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
93 coinIcon: { x: 8 + icon / 2, y: TOP / 2, size: icon },
17
fe680946164b reorganised repo
ark
parents:
diff changeset
94 screen: screenScale(w, h)
fe680946164b reorganised repo
ark
parents:
diff changeset
95 };
fe680946164b reorganised repo
ark
parents:
diff changeset
96 return L;
fe680946164b reorganised repo
ark
parents:
diff changeset
97 }
fe680946164b reorganised repo
ark
parents:
diff changeset
98
fe680946164b reorganised repo
ark
parents:
diff changeset
99 // the screen shown over the map, or null during play
fe680946164b reorganised repo
ark
parents:
diff changeset
100 function screenName(game) {
24
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
101 if (game.help) return "help_info";
17
fe680946164b reorganised repo
ark
parents:
diff changeset
102 if (game.splash) return "splash";
fe680946164b reorganised repo
ark
parents:
diff changeset
103 if (game.result === "lost") return "game_lost";
fe680946164b reorganised repo
ark
parents:
diff changeset
104 if (game.result) return game.lastMap() ? "game_won" : "map_won";
fe680946164b reorganised repo
ark
parents:
diff changeset
105 return null;
fe680946164b reorganised repo
ark
parents:
diff changeset
106 }
fe680946164b reorganised repo
ark
parents:
diff changeset
107
24
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
108 // the help popup centred in the window, scaled on its own to fit 96 %
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
109 // of the window, at most 1. its buttons are the email address, when
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
110 // there is one, and the whole window, which closes the popup. they are
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
111 // not drawn
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
112 function helpRects(L) {
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
113 var s = cfg.sprites.help_info, e = cfg.art.screen.help_email;
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
114 var k = Math.min(1, 0.96 * L.w / s[0], 0.96 * L.h / s[1]);
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
115 var p = { x: (L.w - s[0] * k) / 2, y: (L.h - s[1] * k) / 2, w: s[0] * k, h: s[1] * k };
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
116 var list = [];
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
117 if (cfg.links.email) {
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
118 list.push({ id: "email", x: p.x + e[0] * k, y: p.y + e[1] * k, w: e[2] * k, h: e[3] * k });
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
119 }
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
120 list.push({ id: "close_help", x: 0, y: 0, w: L.w, h: L.h });
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
121 return { panel: p, buttons: list };
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
122 }
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
123
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
124 // the drawing of a screen centred in the window, and its buttons: the
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
125 // screen's own button, and on the splash screen the extra buttons. each
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
126 // button is {id, image, screen, x, y, w, h}
17
fe680946164b reorganised repo
ark
parents:
diff changeset
127 function screenRects(name, L) {
24
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
128 if (name === "help_info") return helpRects(L);
17
fe680946164b reorganised repo
ark
parents:
diff changeset
129 var k = L.screen, s = cfg.sprites[name];
fe680946164b reorganised repo
ark
parents:
diff changeset
130 var bs = cfg.sprites[SCREENS[name].image], at = cfg.art.screen[name];
fe680946164b reorganised repo
ark
parents:
diff changeset
131 var p = { x: (L.w - s[0] * k) / 2, y: (L.h - s[1] * k) / 2, w: s[0] * k, h: s[1] * k };
24
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
132 var list = [{ id: SCREENS[name].id, image: SCREENS[name].image, screen: name,
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
133 x: p.x + at[0] * k, y: p.y + at[1] * k, w: bs[0] * k, h: bs[1] * k }];
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
134 if (name === "splash") {
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
135 // these are placed by height; the width follows the drawing
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
136 SPLASH_EXTRA.forEach(function (e) {
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
137 var es = cfg.sprites[e.image], ea = cfg.art.screen[e.at];
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
138 var h = ea[2] * k;
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
139 list.push({ id: e.id, image: e.image, screen: name,
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
140 x: p.x + ea[0] * k, y: p.y + ea[1] * k, w: h * es[0] / es[1], h: h });
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
141 });
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
142 }
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
143 return { panel: p, buttons: list };
17
fe680946164b reorganised repo
ark
parents:
diff changeset
144 }
fe680946164b reorganised repo
ark
parents:
diff changeset
145
fe680946164b reorganised repo
ark
parents:
diff changeset
146 // the tower menu: a bubble above the selected tower, or below it when
fe680946164b reorganised repo
ark
parents:
diff changeset
147 // there is no room above, kept inside the map area. upgrade and sell each
fe680946164b reorganised repo
ark
parents:
diff changeset
148 // take half of the bubble; at the top level there is no upgrade and sell
fe680946164b reorganised repo
ark
parents:
diff changeset
149 // takes all of it. cx is where the button's drawing is centred.
fe680946164b reorganised repo
ark
parents:
diff changeset
150 function towerMenu(game, L) {
fe680946164b reorganised repo
ark
parents:
diff changeset
151 var t = game.selected, a = L.area, gap = 8;
fe680946164b reorganised repo
ark
parents:
diff changeset
152 var s = cfg.sprites.tower_popup, w = POPUP_W, h = POPUP_W * s[1] / s[0];
fe680946164b reorganised repo
ark
parents:
diff changeset
153 var x = L.ox + t.x * L.scale, y = L.oy + t.y * L.scale;
fe680946164b reorganised repo
ark
parents:
diff changeset
154 var half = A.size(MM.tower.sprite(t), cfg.scale.sprite)[1] / 2 * L.scale + gap;
fe680946164b reorganised repo
ark
parents:
diff changeset
155 var top = y - half - h;
fe680946164b reorganised repo
ark
parents:
diff changeset
156 if (top < a.y + gap) top = y + half;
fe680946164b reorganised repo
ark
parents:
diff changeset
157 var left = MM.util.clamp(x - w / 2, a.x + gap, a.x + a.w - w - gap);
fe680946164b reorganised repo
ark
parents:
diff changeset
158 var bubble = { x: left, y: top, w: w, h: h };
fe680946164b reorganised repo
ark
parents:
diff changeset
159 if (!MM.tower.upgradeCost(t)) {
fe680946164b reorganised repo
ark
parents:
diff changeset
160 return [{ id: "sell", x: left, y: top, w: w, h: h, cx: left + w / 2, bubble: bubble }];
fe680946164b reorganised repo
ark
parents:
diff changeset
161 }
fe680946164b reorganised repo
ark
parents:
diff changeset
162 return [
fe680946164b reorganised repo
ark
parents:
diff changeset
163 { id: "upgrade", x: left, y: top, w: w / 2, h: h, cx: left + w * 0.34, bubble: bubble },
fe680946164b reorganised repo
ark
parents:
diff changeset
164 { id: "sell", x: left + w / 2, y: top, w: w / 2, h: h, cx: left + w * 0.66, bubble: bubble }
fe680946164b reorganised repo
ark
parents:
diff changeset
165 ];
fe680946164b reorganised repo
ark
parents:
diff changeset
166 }
fe680946164b reorganised repo
ark
parents:
diff changeset
167
fe680946164b reorganised repo
ark
parents:
diff changeset
168 // the buttons for the current state, each {id, x, y, w, h}
fe680946164b reorganised repo
ark
parents:
diff changeset
169 function buttons(game, L) {
fe680946164b reorganised repo
ark
parents:
diff changeset
170 var out = [];
fe680946164b reorganised repo
ark
parents:
diff changeset
171 var name = screenName(game);
32
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
172 if (name) {
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
173 out = screenRects(name, L).buttons;
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
174 // the sound button in place of the play button, until sound starts
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
175 if (name === "splash" && game.askSound) {
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
176 var p = out[0];
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
177 out[0] = { id: "start_sound", x: p.x, y: p.y, w: p.w, h: p.h };
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
178 }
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
179 return out;
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
180 }
17
fe680946164b reorganised repo
ark
parents:
diff changeset
181 // top bar, right to left
23
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
182 var y = L.buttonRow + (TOP - BUTTON) / 2, x = L.w - 8;
24
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
183 ["help", "pause", "speed", "sound"].forEach(function (id) {
17
fe680946164b reorganised repo
ark
parents:
diff changeset
184 x -= BUTTON;
fe680946164b reorganised repo
ark
parents:
diff changeset
185 out.push({ id: id, x: x, y: y, w: BUTTON, h: BUTTON });
fe680946164b reorganised repo
ark
parents:
diff changeset
186 x -= 8;
fe680946164b reorganised repo
ark
parents:
diff changeset
187 });
fe680946164b reorganised repo
ark
parents:
diff changeset
188 if (game.waves.phase === "break") {
fe680946164b reorganised repo
ark
parents:
diff changeset
189 var s = cfg.sprites.next_wave, w = BUTTON * s[0] / s[1];
fe680946164b reorganised repo
ark
parents:
diff changeset
190 out.push({ id: "early", x: x - w, y: y, w: w, h: BUTTON });
fe680946164b reorganised repo
ark
parents:
diff changeset
191 }
fe680946164b reorganised repo
ark
parents:
diff changeset
192 TOWERS.forEach(function (t, i) {
fe680946164b reorganised repo
ark
parents:
diff changeset
193 var b = L.land ? { x: L.shop.x + (SHOP - SHOP_W) / 2, y: L.shop.y + 10 + i * (SHOP_H + 10) }
fe680946164b reorganised repo
ark
parents:
diff changeset
194 : { x: L.shop.x + 10 + i * (SHOP_W + 10), y: L.shop.y + (SHOP - SHOP_H) / 2 };
fe680946164b reorganised repo
ark
parents:
diff changeset
195 b.id = "shop";
fe680946164b reorganised repo
ark
parents:
diff changeset
196 b.tower = t;
fe680946164b reorganised repo
ark
parents:
diff changeset
197 b.w = SHOP_W;
fe680946164b reorganised repo
ark
parents:
diff changeset
198 b.h = SHOP_H;
fe680946164b reorganised repo
ark
parents:
diff changeset
199 out.push(b);
fe680946164b reorganised repo
ark
parents:
diff changeset
200 });
fe680946164b reorganised repo
ark
parents:
diff changeset
201 if (game.selected) out = out.concat(towerMenu(game, L));
fe680946164b reorganised repo
ark
parents:
diff changeset
202 return out;
fe680946164b reorganised repo
ark
parents:
diff changeset
203 }
fe680946164b reorganised repo
ark
parents:
diff changeset
204
fe680946164b reorganised repo
ark
parents:
diff changeset
205 function inside(b, x, y) {
fe680946164b reorganised repo
ark
parents:
diff changeset
206 return x >= b.x && x < b.x + b.w && y >= b.y && y < b.y + b.h;
fe680946164b reorganised repo
ark
parents:
diff changeset
207 }
fe680946164b reorganised repo
ark
parents:
diff changeset
208
fe680946164b reorganised repo
ark
parents:
diff changeset
209 function hit(game, L, x, y) {
fe680946164b reorganised repo
ark
parents:
diff changeset
210 var list = buttons(game, L);
fe680946164b reorganised repo
ark
parents:
diff changeset
211 for (var i = 0; i < list.length; i++) {
fe680946164b reorganised repo
ark
parents:
diff changeset
212 if (inside(list[i], x, y)) return list[i];
fe680946164b reorganised repo
ark
parents:
diff changeset
213 }
fe680946164b reorganised repo
ark
parents:
diff changeset
214 return null;
fe680946164b reorganised repo
ark
parents:
diff changeset
215 }
fe680946164b reorganised repo
ark
parents:
diff changeset
216
fe680946164b reorganised repo
ark
parents:
diff changeset
217 // a sprite fitted into a box of w x h centred on x, y, keeping its shape
fe680946164b reorganised repo
ark
parents:
diff changeset
218 function fit(ctx, name, x, y, w, h) {
fe680946164b reorganised repo
ark
parents:
diff changeset
219 var s = cfg.sprites[name], k = Math.min(w / s[0], h / s[1]);
fe680946164b reorganised repo
ark
parents:
diff changeset
220 ctx.drawImage(A.img(name), x - s[0] * k / 2, y - s[1] * k / 2, s[0] * k, s[1] * k);
fe680946164b reorganised repo
ark
parents:
diff changeset
221 }
fe680946164b reorganised repo
ark
parents:
diff changeset
222
fe680946164b reorganised repo
ark
parents:
diff changeset
223 // the digit sprites for string s. the variant of each digit depends on
fe680946164b reorganised repo
ark
parents:
diff changeset
224 // the digit and its place counted from the right, so a number keeps its
fe680946164b reorganised repo
ark
parents:
diff changeset
225 // look while other places change.
fe680946164b reorganised repo
ark
parents:
diff changeset
226 function glyphs(s) {
fe680946164b reorganised repo
ark
parents:
diff changeset
227 var out = [];
fe680946164b reorganised repo
ark
parents:
diff changeset
228 for (var i = 0; i < s.length; i++) {
fe680946164b reorganised repo
ark
parents:
diff changeset
229 var list = DIGITS[s[i]];
fe680946164b reorganised repo
ark
parents:
diff changeset
230 if (!list) continue;
fe680946164b reorganised repo
ark
parents:
diff changeset
231 var place = s.length - 1 - i;
fe680946164b reorganised repo
ark
parents:
diff changeset
232 out.push(list[(place * 5 + s.charCodeAt(i)) % list.length]);
fe680946164b reorganised repo
ark
parents:
diff changeset
233 }
fe680946164b reorganised repo
ark
parents:
diff changeset
234 return out;
fe680946164b reorganised repo
ark
parents:
diff changeset
235 }
fe680946164b reorganised repo
ark
parents:
diff changeset
236
fe680946164b reorganised repo
ark
parents:
diff changeset
237 function numberWidth(s, h) {
fe680946164b reorganised repo
ark
parents:
diff changeset
238 var g = glyphs(s), w = 0;
fe680946164b reorganised repo
ark
parents:
diff changeset
239 g.forEach(function (n) { w += cfg.sprites[n][0] * h / cfg.sprites[n][1]; });
fe680946164b reorganised repo
ark
parents:
diff changeset
240 return w + Math.max(0, g.length - 1) * h * DIGIT_GAP;
fe680946164b reorganised repo
ark
parents:
diff changeset
241 }
fe680946164b reorganised repo
ark
parents:
diff changeset
242
fe680946164b reorganised repo
ark
parents:
diff changeset
243 // number s in digits of height h, vertically centred on y. align is
fe680946164b reorganised repo
ark
parents:
diff changeset
244 // "left", "center" or "right" of x.
fe680946164b reorganised repo
ark
parents:
diff changeset
245 function number(ctx, s, x, y, h, align) {
fe680946164b reorganised repo
ark
parents:
diff changeset
246 s = String(s);
fe680946164b reorganised repo
ark
parents:
diff changeset
247 var w = numberWidth(s, h);
fe680946164b reorganised repo
ark
parents:
diff changeset
248 if (align === "center") x -= w / 2;
fe680946164b reorganised repo
ark
parents:
diff changeset
249 else if (align === "right") x -= w;
fe680946164b reorganised repo
ark
parents:
diff changeset
250 glyphs(s).forEach(function (n) {
fe680946164b reorganised repo
ark
parents:
diff changeset
251 var gw = cfg.sprites[n][0] * h / cfg.sprites[n][1];
fe680946164b reorganised repo
ark
parents:
diff changeset
252 ctx.drawImage(A.img(n), x, y - h / 2, gw, h);
fe680946164b reorganised repo
ark
parents:
diff changeset
253 x += gw + h * DIGIT_GAP;
fe680946164b reorganised repo
ark
parents:
diff changeset
254 });
fe680946164b reorganised repo
ark
parents:
diff changeset
255 }
fe680946164b reorganised repo
ark
parents:
diff changeset
256
fe680946164b reorganised repo
ark
parents:
diff changeset
257 // a price, the coin icon followed by the amount, centred on x, y
fe680946164b reorganised repo
ark
parents:
diff changeset
258 function price(ctx, amount, x, y) {
fe680946164b reorganised repo
ark
parents:
diff changeset
259 var s = String(amount), coin = PRICE * 0.8, gap = 3;
fe680946164b reorganised repo
ark
parents:
diff changeset
260 var left = x - (coin + gap + numberWidth(s, PRICE)) / 2;
fe680946164b reorganised repo
ark
parents:
diff changeset
261 fit(ctx, "coin_icon", left + coin / 2, y, coin, coin);
fe680946164b reorganised repo
ark
parents:
diff changeset
262 number(ctx, s, left + coin + gap, y, PRICE, "left");
fe680946164b reorganised repo
ark
parents:
diff changeset
263 }
fe680946164b reorganised repo
ark
parents:
diff changeset
264
fe680946164b reorganised repo
ark
parents:
diff changeset
265 // a screen's button image over the same button in the drawing. it pulses
fe680946164b reorganised repo
ark
parents:
diff changeset
266 // slowly as a cue to tap; under the mouse it is drawn larger, lifted by
fe680946164b reorganised repo
ark
parents:
diff changeset
267 // a shadow.
fe680946164b reorganised repo
ark
parents:
diff changeset
268 function screenButton(ctx, b, view) {
fe680946164b reorganised repo
ark
parents:
diff changeset
269 var cx = b.x + b.w / 2, cy = b.y + b.h / 2;
fe680946164b reorganised repo
ark
parents:
diff changeset
270 var hover = view.hover && inside(b, view.hover.x, view.hover.y);
fe680946164b reorganised repo
ark
parents:
diff changeset
271 var k = hover ? 1.08 : 1 + 0.04 * (0.5 - 0.5 * Math.cos(2 * Math.PI * view.now / 1000 / PULSE));
fe680946164b reorganised repo
ark
parents:
diff changeset
272 var w = b.w * k, h = b.h * k;
fe680946164b reorganised repo
ark
parents:
diff changeset
273 ctx.save();
fe680946164b reorganised repo
ark
parents:
diff changeset
274 if (hover) {
fe680946164b reorganised repo
ark
parents:
diff changeset
275 ctx.shadowColor = "rgba(0, 0, 0, 0.35)";
fe680946164b reorganised repo
ark
parents:
diff changeset
276 ctx.shadowBlur = 12;
fe680946164b reorganised repo
ark
parents:
diff changeset
277 ctx.shadowOffsetY = 3;
fe680946164b reorganised repo
ark
parents:
diff changeset
278 }
24
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
279 ctx.drawImage(A.img(b.image), cx - w / 2, cy - h / 2, w, h);
17
fe680946164b reorganised repo
ark
parents:
diff changeset
280 ctx.restore();
fe680946164b reorganised repo
ark
parents:
diff changeset
281 }
fe680946164b reorganised repo
ark
parents:
diff changeset
282
32
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
283 // an armed button pulses fast with a red glow. sets the glow on ctx and
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
284 // returns the size factor of the pulse
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
285 function armed(ctx, view) {
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
286 ctx.shadowColor = "rgba(220, 30, 20, 0.9)";
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
287 ctx.shadowBlur = 14;
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
288 return 1 + 0.15 * (0.5 - 0.5 * Math.cos(2 * Math.PI * view.now / 1000 / WARN_PULSE));
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
289 }
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
290
17
fe680946164b reorganised repo
ark
parents:
diff changeset
291 function drawButton(ctx, game, b, view) {
fe680946164b reorganised repo
ark
parents:
diff changeset
292 var cx = b.x + b.w / 2, cy = b.y + b.h / 2;
fe680946164b reorganised repo
ark
parents:
diff changeset
293 if (b.screen) {
fe680946164b reorganised repo
ark
parents:
diff changeset
294 screenButton(ctx, b, view);
24
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
295 } else if (b.id === "help") {
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
296 fit(ctx, "help_button", cx, cy, b.w, b.h);
17
fe680946164b reorganised repo
ark
parents:
diff changeset
297 } else if (b.id === "pause") {
fe680946164b reorganised repo
ark
parents:
diff changeset
298 // shows what a tap does: pause while running, play while paused
fe680946164b reorganised repo
ark
parents:
diff changeset
299 fit(ctx, game.paused ? "play_button" : "pause_button", cx, cy, b.w, b.h);
fe680946164b reorganised repo
ark
parents:
diff changeset
300 } else if (b.id === "speed") {
27
75deda08db90 game debug functions separated into own file; release build option, distribution script
ark
parents: 24
diff changeset
301 fit(ctx, "speed_x" + game.speed(), cx, cy, b.w, b.h);
32
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
302 } else if (b.id === "start_sound") {
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
303 // covers the play button of the splash drawing with paper
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
304 ctx.fillStyle = "#fff";
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
305 ctx.fillRect(b.x, b.y, b.w, b.h);
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
306 ctx.save();
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
307 var a = armed(ctx, view);
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
308 fit(ctx, "sound_on", cx, cy, b.w * a, b.h * a);
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
309 ctx.restore();
17
fe680946164b reorganised repo
ark
parents:
diff changeset
310 } else if (b.id === "sound") {
fe680946164b reorganised repo
ark
parents:
diff changeset
311 fit(ctx, MM.audio.enabled() ? "sound_on" : "sound_off", cx, cy, b.w, b.h);
fe680946164b reorganised repo
ark
parents:
diff changeset
312 } else if (b.id === "early") {
fe680946164b reorganised repo
ark
parents:
diff changeset
313 // the countdown fills the empty right part of the drawing
fe680946164b reorganised repo
ark
parents:
diff changeset
314 ctx.drawImage(A.img("next_wave"), b.x, b.y, b.w, b.h);
fe680946164b reorganised repo
ark
parents:
diff changeset
315 number(ctx, Math.ceil(game.waves.left), b.x + b.w * 0.66, cy, DIGIT, "center");
fe680946164b reorganised repo
ark
parents:
diff changeset
316 } else if (b.id === "shop") {
fe680946164b reorganised repo
ark
parents:
diff changeset
317 var def = cfg.tower[b.tower];
fe680946164b reorganised repo
ark
parents:
diff changeset
318 // the chosen item's bubble switches between its two drawings
fe680946164b reorganised repo
ark
parents:
diff changeset
319 var bubble = game.shop !== b.tower ? "shop_bubble"
fe680946164b reorganised repo
ark
parents:
diff changeset
320 : Math.floor(view.now / BUBBLE_FLIP) % 2 ? "shop_bubble_dark2" : "shop_bubble_dark";
fe680946164b reorganised repo
ark
parents:
diff changeset
321 ctx.drawImage(A.img(bubble), b.x, b.y, b.w, b.h);
fe680946164b reorganised repo
ark
parents:
diff changeset
322 ctx.globalAlpha = game.canAfford(def.cost[0]) && !game.paused ? 1 : 0.35;
fe680946164b reorganised repo
ark
parents:
diff changeset
323 fit(ctx, b.tower, cx, b.y + 28, 44, 38);
fe680946164b reorganised repo
ark
parents:
diff changeset
324 price(ctx, def.cost[0], cx, b.y + b.h - 15);
fe680946164b reorganised repo
ark
parents:
diff changeset
325 ctx.globalAlpha = 1;
fe680946164b reorganised repo
ark
parents:
diff changeset
326 } else if (b.id === "upgrade") {
fe680946164b reorganised repo
ark
parents:
diff changeset
327 var cost = MM.tower.upgradeCost(game.selected);
fe680946164b reorganised repo
ark
parents:
diff changeset
328 ctx.globalAlpha = game.canAfford(cost) ? 1 : 0.35;
fe680946164b reorganised repo
ark
parents:
diff changeset
329 fit(ctx, "upgrade_button", b.cx, b.bubble.y + b.bubble.h * 0.4, 46, 44);
fe680946164b reorganised repo
ark
parents:
diff changeset
330 price(ctx, cost, b.cx, b.bubble.y + b.bubble.h * 0.7);
fe680946164b reorganised repo
ark
parents:
diff changeset
331 ctx.globalAlpha = 1;
fe680946164b reorganised repo
ark
parents:
diff changeset
332 } else if (b.id === "sell") {
fe680946164b reorganised repo
ark
parents:
diff changeset
333 // armed by a first tap: a fast pulse with a red glow until the
fe680946164b reorganised repo
ark
parents:
diff changeset
334 // second tap sells
fe680946164b reorganised repo
ark
parents:
diff changeset
335 ctx.save();
32
85d23328a215 added intro music, logic for browser audio triggering on splash screen
ark
parents: 27
diff changeset
336 var k = game.sellArmed ? armed(ctx, view) : 1;
17
fe680946164b reorganised repo
ark
parents:
diff changeset
337 fit(ctx, "sell_button", b.cx, b.bubble.y + b.bubble.h * 0.4, 46 * k, 44 * k);
fe680946164b reorganised repo
ark
parents:
diff changeset
338 ctx.restore();
fe680946164b reorganised repo
ark
parents:
diff changeset
339 price(ctx, MM.tower.refund(game.selected), b.cx,
fe680946164b reorganised repo
ark
parents:
diff changeset
340 b.bubble.y + b.bubble.h * 0.7);
fe680946164b reorganised repo
ark
parents:
diff changeset
341 }
fe680946164b reorganised repo
ark
parents:
diff changeset
342 }
fe680946164b reorganised repo
ark
parents:
diff changeset
343
fe680946164b reorganised repo
ark
parents:
diff changeset
344 function draw(ctx, game, L, view) {
fe680946164b reorganised repo
ark
parents:
diff changeset
345 var dpr = view.dpr;
fe680946164b reorganised repo
ark
parents:
diff changeset
346 ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
fe680946164b reorganised repo
ark
parents:
diff changeset
347
fe680946164b reorganised repo
ark
parents:
diff changeset
348 // top bar: coins, hearts, wave
fe680946164b reorganised repo
ark
parents:
diff changeset
349 ctx.fillStyle = "rgba(246, 241, 230, 0.92)";
23
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
350 ctx.fillRect(0, 0, L.w, L.top);
17
fe680946164b reorganised repo
ark
parents:
diff changeset
351 ctx.fillStyle = "rgba(246, 241, 230, 0.92)";
fe680946164b reorganised repo
ark
parents:
diff changeset
352 ctx.fillRect(L.shop.x, L.shop.y, L.shop.w, L.shop.h);
fe680946164b reorganised repo
ark
parents:
diff changeset
353 ctx.fillStyle = "rgba(58, 53, 48, 0.25)";
23
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
354 ctx.fillRect(0, L.top - 2, L.w, 2);
17
fe680946164b reorganised repo
ark
parents:
diff changeset
355 // each counter takes at least the width of its widest usual value, so
fe680946164b reorganised repo
ark
parents:
diff changeset
356 // the next one does not move while the numbers change
fe680946164b reorganised repo
ark
parents:
diff changeset
357 var ic = L.coinIcon, x = ic.x - ic.size / 2;
23
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
358 [["coin_icon", game.coins], ["heart", game.hearts],
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
359 ["monster_icon", game.waves.shown() + "/" + game.waves.list.length]
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
360 ].forEach(function (c, i) {
17
fe680946164b reorganised repo
ark
parents:
diff changeset
361 var s = String(c[1]);
23
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
362 fit(ctx, c[0], x + ic.size / 2, ic.y, ic.size, ic.size);
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
363 number(ctx, s, x + ic.size + 6, ic.y, DIGIT, "left");
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
364 x += ic.size + 6 + Math.max(numberWidth(s, DIGIT), numberWidth(USUAL[i], DIGIT)) + 18;
17
fe680946164b reorganised repo
ark
parents:
diff changeset
365 });
fe680946164b reorganised repo
ark
parents:
diff changeset
366
fe680946164b reorganised repo
ark
parents:
diff changeset
367 var screen = screenName(game);
fe680946164b reorganised repo
ark
parents:
diff changeset
368 var list = buttons(game, L);
fe680946164b reorganised repo
ark
parents:
diff changeset
369 if (game.paused && !screen) {
fe680946164b reorganised repo
ark
parents:
diff changeset
370 ctx.fillStyle = "rgba(255, 255, 255, 0.18)";
fe680946164b reorganised repo
ark
parents:
diff changeset
371 ctx.fillRect(L.ox, L.oy, game.map.width * L.scale, game.map.height * L.scale);
fe680946164b reorganised repo
ark
parents:
diff changeset
372 }
fe680946164b reorganised repo
ark
parents:
diff changeset
373 if (screen) {
fe680946164b reorganised repo
ark
parents:
diff changeset
374 ctx.fillStyle = game.splash ? "rgba(246, 241, 230, 0.55)" : "rgba(40, 35, 30, 0.45)";
fe680946164b reorganised repo
ark
parents:
diff changeset
375 ctx.fillRect(0, 0, L.w, L.h);
fe680946164b reorganised repo
ark
parents:
diff changeset
376 var p = screenRects(screen, L).panel;
24
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
377 // the help drawing is shrunk a lot on small screens
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
378 ctx.imageSmoothingQuality = "high";
17
fe680946164b reorganised repo
ark
parents:
diff changeset
379 ctx.drawImage(A.img(screen), p.x, p.y, p.w, p.h);
24
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
380 ctx.imageSmoothingQuality = "low";
17
fe680946164b reorganised repo
ark
parents:
diff changeset
381 } else if (game.selected) {
fe680946164b reorganised repo
ark
parents:
diff changeset
382 var bub = list[list.length - 1].bubble;
fe680946164b reorganised repo
ark
parents:
diff changeset
383 ctx.drawImage(A.img("tower_popup"), bub.x, bub.y, bub.w, bub.h);
fe680946164b reorganised repo
ark
parents:
diff changeset
384 }
fe680946164b reorganised repo
ark
parents:
diff changeset
385 list.forEach(function (b) { drawButton(ctx, game, b, view); });
fe680946164b reorganised repo
ark
parents:
diff changeset
386 }
fe680946164b reorganised repo
ark
parents:
diff changeset
387
fe680946164b reorganised repo
ark
parents:
diff changeset
388 return { layout: layout, hit: hit, draw: draw, BUBBLE_FLIP: BUBBLE_FLIP };
fe680946164b reorganised repo
ark
parents:
diff changeset
389 })();