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