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