wobblyhead hg > minor_monsters
annotate doc/architecture.md @ 14:7fda634d15ad
adjustments to wave logic, tower pivot points
| author | ark |
|---|---|
| date | 2026-10-19 22:50 +0200 |
| parents | 8df95050fc8b |
| children | fe680946164b |
| 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 | |
| 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", | |
| 11 | 82 {clientX: 497, clientY: 560, bubbles: true}))' \ |
| 6 | 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 | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
96 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
|
97 hand and write to art/. |
| 4 | 98 cut_sounds.py also needs ffmpeg with mp3 support, so the build itself does not |
| 99 depend on it. see sound.md. | |
| 100 | |
| 0 | 101 steps: |
| 102 | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
103 1. sprites: trim transparent edges, write to game/assets/. this covers the |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
104 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
|
105 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
|
106 for slowed monsters (art.md) |
| 0 | 107 2. tower levels: t0-t3 are level 1. split each t*p strip into four images, |
| 108 levels 2-5. fill the inside of the outline with white (flood fill from | |
| 109 outside, everything not reached is inside), trim, write as t0_2..t0_5 and | |
| 110 so on. see art.md | |
| 111 3. maps: copy the map images, possibly converted to a smaller format | |
| 112 4. masks: for each map | |
| 9 | 113 - classify pixels as path, entrance, exit or free by their exact colour. |
| 114 masks must have hard edges: a pixel that is neither fully transparent | |
| 115 nor opaque pure red, blue or green stops the build (art.md) | |
| 116 - extract routes, see below | |
| 0 | 117 - write a placement grid: cells where a tower footprint fits next to the |
| 9 | 118 road, within the limits in balance.md. the red path in the mask is the |
| 119 centre line of the road; the road's half width is set per map | |
| 4 | 120 5. sounds: embed art/sounds/*.mp3 as base64 in game/data/sounds.js, |
| 121 grouped by event | |
| 122 6. config: read config/game.toml, check it, write game/data/config.js | |
| 123 7. checks: print a summary per map (routes with length and turning, monster | |
| 0 | 124 counts, coins and hp per map against the target) and warn on problems |
| 4 | 125 8. previews: draw each map with its routes, entrances, exits and placement |
| 0 | 126 grid into temp/preview/, for checking without starting the game |
| 127 | |
| 128 a watch mode (`tools/build.py --watch`) reruns the build when a file in | |
| 4 | 129 config/, art/ or art/sounds/ changes. tuning then means: edit the toml, save, reload the |
| 0 | 130 browser. |
| 131 | |
| 132 `tools/publish.py` copies game/ to www/ with debug switches off, and writes a | |
| 133 zip into dist/. | |
| 134 | |
| 135 | |
| 136 routes | |
| 137 ------ | |
| 138 | |
| 139 a route is a list of points from an entrance to an exit. a map has one or | |
| 140 more routes. a monster picks a route at spawn (weights in balance.md) and | |
| 141 follows it to the end. this handles the branches on map1 and map2 without | |
| 142 junction logic at runtime. | |
| 143 | |
| 144 extraction in the build: | |
| 145 | |
| 146 1. thin the path pixels to a one-pixel centre line (skeleton) | |
| 147 2. turn the skeleton into a graph of junctions and segments, and drop short | |
| 148 dead ends left by uneven strokes | |
| 149 3. find the graph nodes nearest to each entrance and exit blob | |
| 150 4. list every path through the graph from an entrance to an exit that visits | |
| 151 no junction twice | |
| 152 5. smooth each route and reduce it to a few points per bend | |
| 153 6. record length and turning for each route | |
| 154 | |
| 155 map0 and map3 give one route each. map1 and map2 give several; map2 may give | |
| 156 more than wanted. config/routes.toml can drop routes, set fixed weights, or | |
| 157 replace a route with hand-placed points, for cases where the automatic result | |
| 158 is wrong. | |
| 159 | |
| 10 | 160 the game walks a monster along the route's centre line, by distance, and |
| 161 draws it beside the line. the offset is set per monster at spawn | |
| 162 (balance.md, monsters) and needs no route data from the build. when a map | |
| 163 loads, each route works out the sideways direction at its points and how far | |
| 164 an offset may go on the inside of each bend. towers rank monsters by their | |
| 165 distance along the centre line, so the offset does not change the order. | |
| 166 | |
| 0 | 167 |
| 168 config | |
| 169 ------ | |
| 170 | |
| 171 config/game.toml holds every number in balance.md, the wave lists, and debug | |
| 172 switches. the build converts it into game/data/config.js. toml allows comments, | |
| 173 so each value can carry its source note (guess, decided). | |
| 174 | |
| 14 | 175 wave format: |
| 0 | 176 |
| 177 [[map.0.wave]] | |
| 14 | 178 monsters = { m0 = 5, m1 = 1, m2 = 1, b0 = 1 } |
| 0 | 179 |
| 14 | 180 a wave lists how many of each monster it holds. when the wave starts, the |
| 181 boss spawns first: it is the slowest monster and the others overtake it. the | |
| 182 rest follow one per spawn interval, each drawn at random from the monsters | |
| 183 left in the wave. the spawn interval is set per map. the boss is in the last | |
| 184 wave. | |
| 0 | 185 |
| 186 debug switches, all off by default: | |
| 187 | |
| 188 - start coins override, unlimited coins | |
| 6 | 189 - start at a given map and wave. that map counts as the first map: the play |
| 190 button starts it, and a lost map returns to it | |
| 0 | 191 - no heart loss |
| 192 - show routes, placement grid, tower ranges, hit areas | |
| 193 - higher game speeds | |
| 194 - on-screen frame time and entity counts | |
| 195 | |
| 196 there is no in-game config editor: values are changed in the toml file. | |
| 197 | |
| 198 | |
| 199 runtime | |
| 200 ------- | |
| 201 | |
| 202 script files, in load order: | |
| 203 | |
| 204 data/config.js generated settings | |
| 205 data/maps.js generated map data | |
| 4 | 206 data/sounds.js generated sound data |
| 0 | 207 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
|
208 js/storage.js settings kept in the browser |
| 0 | 209 js/assets.js image loading, sprite table |
| 4 | 210 js/audio.js recorded sounds, placeholder beeps for the other events |
| 0 | 211 js/map.js placement lookup, snapping, route sampling |
| 212 js/monster.js movement along a route, hops, slow, hp | |
| 213 js/tower.js targeting, attacks, upgrade, sell | |
| 214 js/projectile.js | |
| 215 js/wave.js wave timing, countdown, early start bonus | |
| 216 js/game.js game state, rules, win and loss | |
| 217 js/render.js drawing | |
| 218 js/ui.js top bar, shop, tower menu, screens | |
| 219 js/input.js pointer events | |
| 220 js/main.js start-up and main loop | |
| 221 | |
| 222 game loop: | |
| 223 | |
| 224 - the simulation advances in fixed steps of 1/60 s. drawing happens once per | |
| 225 browser frame and is independent of it | |
| 226 - 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
|
227 - pause stops the simulation; drawing and the top bar buttons continue |
| 0 | 228 - a frame that takes too long runs at most a few steps and drops the rest, so |
| 229 the game slows down rather than freezing after the tab was in the background | |
| 230 | |
| 231 state: | |
| 232 | |
| 233 - game state is plain objects: monsters, towers, projectiles, coins in flight, | |
| 234 wave state, coins, hearts. rendering reads it and does not change it | |
| 235 - random numbers come from one seeded generator, so a run can be repeated with | |
| 236 the same seed when chasing a bug | |
| 237 | |
| 238 display: | |
| 239 | |
| 240 - the canvas fills the window. the map is scaled to fit and centred, with | |
| 241 borders where the shapes differ | |
| 242 - the canvas resolution follows the device pixel ratio, so drawings stay sharp | |
| 243 on high-resolution screens | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
244 - 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
|
245 pause. the |
| 0 | 246 tower shop sits on the right in landscape and at the bottom in portrait |
| 247 - buttons are at least 48 css pixels for touch | |
| 248 | |
| 249 input: | |
| 250 | |
| 251 - pointer events cover mouse and touch alike | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
252 - 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
|
253 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
|
254 any button |
| 11 | 255 - buttons and tower selection act when the pointer goes down. placing |
| 256 follows the pointer until it is let go, and building happens then | |
| 0 | 257 - the page blocks browser zoom and scroll gestures on the canvas |
| 258 | |
| 259 placement: | |
| 260 | |
| 261 - the build's placement grid marks every cell where a tower may stand | |
| 11 | 262 - while the pointer is down, the game searches outward from it for the |
| 263 nearest legal cell that keeps the minimum distance to other towers, within | |
| 264 the snap radius. where there is none, the preview stays where it was | |
| 0 | 265 - t0 checks once, at placement, which of its four sides face the path, and |
| 266 only draws spikes on those | |
| 267 | |
| 268 persistence: | |
| 269 | |
| 6 | 270 - nothing is stored for now. maps are played in order and a lost map goes |
| 271 back to the first map, so there is no progress to keep. a reload starts at | |
| 272 the splash screen with a fresh game | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
273 - 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
|
274 wraps localStorage, and one entry holds them |
| 0 | 275 - 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
|
276 for example in private browsing; settings are then not kept |
| 0 | 277 - the entry carries a version number for later format changes |
| 278 | |
| 279 | |
| 280 not planned | |
| 281 ----------- | |
| 282 | |
| 283 - in-browser editors or tuning panels | |
| 284 - automatic balance simulation: tower placement is up to the player, so a | |
| 285 simulation would need a placement strategy first. could be revisited once | |
| 286 the game is playable |
