|
0
|
1 architecture
|
|
|
2 ------------
|
|
|
3
|
|
|
4 the game is a static html page with plain javascript, drawn on a 2d canvas. it
|
|
|
5 uses no javascript libraries and needs no build step to run. a python build
|
|
|
6 script prepares everything that can be calculated in advance: processed
|
|
|
7 images, map data, route data and the config. its output is committed, so the
|
|
|
8 game runs straight from the source tree.
|
|
|
9
|
|
|
10 this is a plan; nothing is implemented yet.
|
|
|
11
|
|
|
12
|
|
|
13 directory layout
|
|
|
14 ----------------
|
|
|
15
|
|
|
16 proposed:
|
|
|
17
|
|
|
18 art/ drawings as supplied: scans, masks, sprites
|
|
4
|
19 sound_sources/ voice recordings as supplied
|
|
|
20 sounds/ sounds cut from the recordings (tools/cut_sounds.py)
|
|
0
|
21 config/ hand-edited game settings (toml)
|
|
|
22 game.toml numbers from balance.md, waves, debug switches
|
|
|
23 routes.toml manual corrections to the extracted routes
|
|
|
24 tools/ python build scripts
|
|
|
25 game/ the runnable game, opened as game/index.html
|
|
|
26 index.html
|
|
|
27 style.css
|
|
|
28 js/ game code, hand-written
|
|
|
29 assets/ processed images (generated, committed)
|
|
4
|
30 data/ maps.js, sounds.js, config.js (generated, committed)
|
|
0
|
31 doc/ design and technical notes
|
|
|
32 www/ copy of game/ prepared for publishing (ignored)
|
|
|
33 dist/ zip archives for distribution (ignored)
|
|
|
34 temp/ scratch files, build previews (ignored)
|
|
|
35
|
|
|
36 generated files carry a header line saying so and naming the script that made
|
|
|
37 them.
|
|
|
38
|
|
|
39
|
|
|
40 running the game
|
|
|
41 ----------------
|
|
|
42
|
|
|
43 - desktop: open game/index.html directly from disk, or serve the directory
|
|
|
44 with any static web server
|
|
|
45 - tablet: tablets cannot easily open local html files, so the game is served
|
|
|
46 over http, from a computer on the home network or from a web host. www/ is
|
|
|
47 the copy meant for that
|
|
|
48 - an optional later step makes it installable to the home screen (web app
|
|
|
49 manifest, offline cache). that requires https
|
|
|
50
|
|
|
51 opening the page from disk (file://) limits what the browser allows. chrome
|
|
|
52 blocks javascript modules, fetch() and reading pixels back from a canvas
|
|
|
53 there. so:
|
|
|
54
|
|
|
55 - scripts are loaded with plain script tags in a fixed order, and share one
|
|
|
56 global namespace object
|
|
|
57 - data files are javascript files that assign to that object, not json files
|
|
|
58 - the game never reads mask pixels; the build turns the masks into data
|
|
|
59 - images are loaded with image elements, which works from disk
|
|
4
|
60 - recorded sounds are embedded by the build as base64 data in a javascript
|
|
|
61 file, so they can be decoded without fetch()
|
|
0
|
62
|
|
|
63
|
|
6
|
64 screenshots
|
|
|
65 -----------
|
|
|
66
|
|
|
67 firefox's own --screenshot captures at the load event. the game loads its
|
|
|
68 images and draws its first frame after that, so the capture shows an empty
|
|
|
69 page. the ff_screenshot script (in $PATH, ~/bin/ff_screenshot) drives
|
|
|
70 headless firefox over marionette and waits before capturing:
|
|
|
71
|
|
|
72 ff_screenshot -w 1024,768 -s 2 file://$PWD/game/index.html temp/splash.png
|
|
|
73
|
|
|
74 -w sets the viewport, -s the wait after loading. the game state is not
|
|
|
75 reachable from outside, so other screens are reached by tapping: -J runs
|
|
|
76 javascript after the wait, and -S waits again before capturing. a pointer
|
|
|
77 event sent to the canvas counts as a tap at that position in css pixels.
|
|
|
78 this taps the play button on the splash screen at 1024 x 768:
|
|
|
79
|
|
|
80 ff_screenshot -w 1024,768 -s 2 -S 3 -J 'document.getElementById("game")
|
|
|
81 .dispatchEvent(new PointerEvent("pointerdown",
|
|
|
82 {clientX: 512, clientY: 444, bubbles: true}))' \
|
|
|
83 file://$PWD/game/index.html temp/play.png
|
|
|
84
|
|
|
85 `ff_screenshot -h` lists the other options.
|
|
|
86
|
|
|
87
|
|
0
|
88 build
|
|
|
89 -----
|
|
|
90
|
|
|
91 one command, `python3 tools/build.py`, runs all steps. each step skips its work
|
|
|
92 when its inputs have not changed. requires python 3.11 or later (for the
|
|
|
93 built-in toml reader) and pillow. numpy is optional, if the pure python image
|
|
|
94 steps turn out too slow.
|
|
|
95
|
|
4
|
96 tools/cut_sheet.py and tools/cut_sounds.py are run by hand and write to art/.
|
|
|
97 cut_sounds.py also needs ffmpeg with mp3 support, so the build itself does not
|
|
|
98 depend on it. see sound.md.
|
|
|
99
|
|
0
|
100 steps:
|
|
|
101
|
|
6
|
102 1. sprites: trim transparent edges, write to game/assets/. make the hp bar
|
|
|
103 variants: the empty part, and the blue bars for slowed monsters (art.md)
|
|
0
|
104 2. tower levels: t0-t3 are level 1. split each t*p strip into four images,
|
|
|
105 levels 2-5. fill the inside of the outline with white (flood fill from
|
|
|
106 outside, everything not reached is inside), trim, write as t0_2..t0_5 and
|
|
|
107 so on. see art.md
|
|
|
108 3. maps: copy the map images, possibly converted to a smaller format
|
|
|
109 4. masks: for each map
|
|
|
110 - classify pixels as path, entrance, exit or free, using colour
|
|
|
111 thresholds (the mask edges are soft)
|
|
|
112 - compute the distance from every free pixel to the nearest path pixel
|
|
|
113 - write a placement grid: cells where a tower footprint fits next to the
|
|
|
114 path, within the limits in balance.md
|
|
|
115 - extract routes, see below
|
|
4
|
116 5. sounds: embed art/sounds/*.mp3 as base64 in game/data/sounds.js,
|
|
|
117 grouped by event
|
|
|
118 6. config: read config/game.toml, check it, write game/data/config.js
|
|
|
119 7. checks: print a summary per map (routes with length and turning, monster
|
|
0
|
120 counts, coins and hp per map against the target) and warn on problems
|
|
4
|
121 8. previews: draw each map with its routes, entrances, exits and placement
|
|
0
|
122 grid into temp/preview/, for checking without starting the game
|
|
|
123
|
|
|
124 a watch mode (`tools/build.py --watch`) reruns the build when a file in
|
|
4
|
125 config/, art/ or art/sounds/ changes. tuning then means: edit the toml, save, reload the
|
|
0
|
126 browser.
|
|
|
127
|
|
|
128 `tools/publish.py` copies game/ to www/ with debug switches off, and writes a
|
|
|
129 zip into dist/.
|
|
|
130
|
|
|
131
|
|
|
132 routes
|
|
|
133 ------
|
|
|
134
|
|
|
135 a route is a list of points from an entrance to an exit. a map has one or
|
|
|
136 more routes. a monster picks a route at spawn (weights in balance.md) and
|
|
|
137 follows it to the end. this handles the branches on map1 and map2 without
|
|
|
138 junction logic at runtime.
|
|
|
139
|
|
|
140 extraction in the build:
|
|
|
141
|
|
|
142 1. thin the path pixels to a one-pixel centre line (skeleton)
|
|
|
143 2. turn the skeleton into a graph of junctions and segments, and drop short
|
|
|
144 dead ends left by uneven strokes
|
|
|
145 3. find the graph nodes nearest to each entrance and exit blob
|
|
|
146 4. list every path through the graph from an entrance to an exit that visits
|
|
|
147 no junction twice
|
|
|
148 5. smooth each route and reduce it to a few points per bend
|
|
|
149 6. record length and turning for each route
|
|
|
150
|
|
|
151 map0 and map3 give one route each. map1 and map2 give several; map2 may give
|
|
|
152 more than wanted. config/routes.toml can drop routes, set fixed weights, or
|
|
|
153 replace a route with hand-placed points, for cases where the automatic result
|
|
|
154 is wrong.
|
|
|
155
|
|
|
156
|
|
|
157 config
|
|
|
158 ------
|
|
|
159
|
|
|
160 config/game.toml holds every number in balance.md, the wave lists, and debug
|
|
|
161 switches. the build converts it into game/data/config.js. toml allows comments,
|
|
|
162 so each value can carry its source note (guess, decided).
|
|
|
163
|
|
|
164 wave format (sketch):
|
|
|
165
|
|
|
166 [[map.0.wave]]
|
|
|
167 groups = [
|
|
|
168 { monster = "m0", count = 6, interval = 1.0 },
|
|
|
169 { monster = "m1", count = 2, interval = 1.5, delay = 3.0 },
|
|
|
170 ]
|
|
|
171
|
|
|
172 a group starts after the previous one has finished spawning plus its delay.
|
|
|
173 the boss is a group in the last wave.
|
|
|
174
|
|
|
175 debug switches, all off by default:
|
|
|
176
|
|
|
177 - start coins override, unlimited coins
|
|
6
|
178 - start at a given map and wave. that map counts as the first map: the play
|
|
|
179 button starts it, and a lost map returns to it
|
|
0
|
180 - no heart loss
|
|
|
181 - show routes, placement grid, tower ranges, hit areas
|
|
|
182 - higher game speeds
|
|
|
183 - on-screen frame time and entity counts
|
|
|
184
|
|
|
185 there is no in-game config editor: values are changed in the toml file.
|
|
|
186
|
|
|
187
|
|
|
188 runtime
|
|
|
189 -------
|
|
|
190
|
|
|
191 script files, in load order:
|
|
|
192
|
|
|
193 data/config.js generated settings
|
|
|
194 data/maps.js generated map data
|
|
4
|
195 data/sounds.js generated sound data
|
|
0
|
196 js/util.js vector maths, random numbers, easing
|
|
|
197 js/assets.js image loading, sprite table
|
|
4
|
198 js/audio.js recorded sounds, placeholder beeps for the other events
|
|
0
|
199 js/map.js placement lookup, snapping, route sampling
|
|
|
200 js/monster.js movement along a route, hops, slow, hp
|
|
|
201 js/tower.js targeting, attacks, upgrade, sell
|
|
|
202 js/projectile.js
|
|
|
203 js/wave.js wave timing, countdown, early start bonus
|
|
|
204 js/game.js game state, rules, win and loss
|
|
|
205 js/render.js drawing
|
|
|
206 js/ui.js top bar, shop, tower menu, screens
|
|
|
207 js/input.js pointer events
|
|
|
208 js/main.js start-up and main loop
|
|
|
209
|
|
|
210 game loop:
|
|
|
211
|
|
|
212 - the simulation advances in fixed steps of 1/60 s. drawing happens once per
|
|
|
213 browser frame and is independent of it
|
|
|
214 - game speed 2x runs two simulation steps per frame
|
|
|
215 - pause stops the simulation; drawing and input continue
|
|
|
216 - a frame that takes too long runs at most a few steps and drops the rest, so
|
|
|
217 the game slows down rather than freezing after the tab was in the background
|
|
|
218
|
|
|
219 state:
|
|
|
220
|
|
|
221 - game state is plain objects: monsters, towers, projectiles, coins in flight,
|
|
|
222 wave state, coins, hearts. rendering reads it and does not change it
|
|
|
223 - random numbers come from one seeded generator, so a run can be repeated with
|
|
|
224 the same seed when chasing a bug
|
|
|
225
|
|
|
226 display:
|
|
|
227
|
|
|
228 - the canvas fills the window. the map is scaled to fit and centred, with
|
|
|
229 borders where the shapes differ
|
|
|
230 - the canvas resolution follows the device pixel ratio, so drawings stay sharp
|
|
|
231 on high-resolution screens
|
|
|
232 - layout: top bar with coins, hearts, wave, countdown, pause and speed. the
|
|
|
233 tower shop sits on the right in landscape and at the bottom in portrait
|
|
|
234 - buttons are at least 48 css pixels for touch
|
|
|
235
|
|
|
236 input:
|
|
|
237
|
|
|
238 - pointer events cover mouse and touch alike
|
|
|
239 - nothing relies on hover; selection and confirmation are by tap
|
|
|
240 - the page blocks browser zoom and scroll gestures on the canvas
|
|
|
241
|
|
|
242 placement:
|
|
|
243
|
|
|
244 - the build's placement grid marks every cell where a tower may stand
|
|
|
245 - on a tap, the game searches outward from the tap for the nearest legal cell
|
|
|
246 that keeps the minimum distance to other towers, within the snap radius
|
|
|
247 - t0 checks once, at placement, which of its four sides face the path, and
|
|
|
248 only draws spikes on those
|
|
|
249
|
|
|
250 persistence:
|
|
|
251
|
|
6
|
252 - nothing is stored for now. maps are played in order and a lost map goes
|
|
|
253 back to the first map, so there is no progress to keep. a reload starts at
|
|
|
254 the splash screen with a fresh game
|
|
|
255 - once the game has settings, such as sound on or off, js/storage.js wraps
|
|
|
256 localStorage, and one entry holds them
|
|
0
|
257 - all access is wrapped so the game still runs when storage is unavailable,
|
|
|
258 for example in private browsing; progress is then not saved
|
|
|
259 - the entry carries a version number for later format changes
|
|
|
260
|
|
|
261
|
|
|
262 not planned
|
|
|
263 -----------
|
|
|
264
|
|
|
265 - in-browser editors or tuning panels
|
|
|
266 - automatic balance simulation: tower placement is up to the player, so a
|
|
|
267 simulation would need a placement strategy first. could be revisited once
|
|
|
268 the game is playable
|