wobblyhead hg > minor_monsters
annotate doc/architecture.md @ 37:a88effe4831e default tip
Added tag 1.0.0 for changeset a464bd43b76c
| author | ark |
|---|---|
| date | 2026-10-21 22:36 +0200 |
| parents | a464bd43b76c |
| children |
| rev | line source |
|---|---|
| 36 | 1 Architecture |
| 0 | 2 ------------ |
| 3 | |
| 36 | 4 The game is a static HTML page with plain JavaScript, drawn on a 2D canvas. |
| 5 It uses no JavaScript libraries. A Python build script prepares everything | |
| 6 that can be calculated in advance: processed images, map data, route data, | |
| 7 embedded sounds and the config. It writes them to www/, together with a | |
| 8 copy of the hand-written game files from game/. The www/ directory is the | |
| 9 runnable game. It is not kept in the repository, so the game has to be | |
| 10 built before it can be run. | |
| 0 | 11 |
| 36 | 12 The details of the algorithms and their constants are in implementation.md. |
| 0 | 13 |
| 14 | |
| 36 | 15 Directory layout |
| 0 | 16 ---------------- |
| 17 | |
| 36 | 18 art/ drawings and recordings as supplied, and files cut from them |
| 19 art_sources/ the scans the drawings were cut from | |
| 20 digits/ digits cut from digits.png (tools/cut_digits.py) | |
| 4 | 21 sound_sources/ voice recordings as supplied |
| 22 sounds/ sounds cut from the recordings (tools/cut_sounds.py) | |
| 36 | 23 config/ |
| 24 game.toml every game setting, the wave lists, debug switches | |
| 25 doc/ documentation | |
| 17 | 26 game/ hand-written game files |
| 0 | 27 index.html |
| 28 style.css | |
| 17 | 29 js/ game code |
| 36 | 30 tools/ build, cutting, balancing and release scripts |
| 31 www/ the built game (generated, ignored) | |
| 17 | 32 index.html copied from game/, as are style.css and js/ |
| 33 assets/ processed images | |
| 36 | 34 data/ config.js, maps.js, sounds.js |
| 35 dist/ release tarballs (ignored) | |
| 36 temp/ build cache and previews (ignored) | |
| 37 build/maps.json route and placement data of the last build | |
| 38 preview/ preview images, balance and simulation reports | |
| 0 | 39 |
| 36 | 40 Generated files have a header line saying so and naming the script that |
| 41 made them. | |
| 6 | 42 |
| 43 | |
| 36 | 44 Running and deploying |
| 45 --------------------- | |
| 0 | 46 |
| 36 | 47 - Local: build, then open www/index.html from disk, or serve www/ with any |
| 48 static web server | |
| 49 - Server: `tools/make_dist.sh` runs a release build and packs www/ into | |
| 50 dist/minor_monsters_VERSION.tar.gz, which unpacks into a directory | |
| 51 minor_monsters. That directory is what the web server serves | |
| 0 | 52 |
| 36 | 53 The page also runs from disk (file://), where Chrome blocks JavaScript |
| 54 modules, fetch() and reading pixels back from a canvas. So: | |
| 0 | 55 |
| 36 | 56 - Scripts are loaded with plain script tags in a fixed order, and share one |
| 57 global namespace object, MM | |
| 58 - Data files are JavaScript files that assign to MM, not JSON files | |
| 59 - The game never reads mask pixels; the build turns the masks into data | |
| 60 - Images are loaded with image elements | |
| 61 - Sounds are embedded as base64 in a JavaScript file, so they can be | |
| 62 decoded without fetch() | |
| 0 | 63 |
| 64 | |
| 36 | 65 Build |
| 66 ----- | |
| 22 | 67 |
| 36 | 68 `python3 tools/build.py` runs all steps. It needs Python 3.11 or later, for |
| 69 the built-in TOML reader, and Pillow. A step is skipped when its outputs | |
| 70 are newer than its inputs and the build scripts; `--force` runs every step. | |
| 23 | 71 |
| 36 | 72 1. Sprites: trim transparent edges and write to www/assets/. This covers |
| 73 monsters, towers, projectiles, coins, HP bars, icons, buttons, screens | |
| 74 and the digits, written as digit_NAME_N. The HP bars also get an empty | |
| 75 variant and blue variants for slowed monsters | |
| 76 2. Tower levels: split each t*p strip into levels 2 to 5, fill the inside | |
| 77 of each outline with white, trim, and write t0_2 to t3_5 | |
| 78 3. Maps: write the map scans as JPEG | |
| 79 4. Masks: classify the mask pixels, extract the routes and write the | |
| 80 placement grid to www/data/maps.js. The result is also kept in | |
| 81 temp/build/maps.json, for the later steps and the balance tools. This | |
| 82 step also reruns when the placement settings or a map's road and spawn | |
| 83 settings change | |
| 84 5. Sounds: embed art/sounds/*.mp3 in www/data/sounds.js, grouped by event | |
| 85 6. Config: check config/game.toml, fill in the tower values for every | |
| 86 level, add the size of every sprite, and write www/data/config.js | |
| 87 7. Checks: print per map its routes with length, turning and walk times, | |
| 88 the monster counts and shares, the coins and hit points of its waves, and | |
| 89 the early start bonus for skipping every break. Warn on problems, such as | |
| 90 a wave count that does not match the lists, or a boss missing from the | |
| 91 last wave | |
| 92 8. Previews: write temp/preview/mapN.png with routes, entrances, exits and | |
| 93 placement grid, mapN_routes.png for maps with several routes, and | |
| 94 towers.png with every tower level, its rotation point and footprint | |
| 95 9. Game files: copy the files in game/ to www/ when their time or size | |
| 96 differs. Files no longer in game/ are removed from www/, apart from | |
| 97 www/assets/ and www/data/ | |
| 23 | 98 |
| 36 | 99 Options: |
| 100 | |
| 101 - `--watch` reruns the build when a file in art/, art/sounds/, art/digits/, | |
| 102 config/ or game/, or a build script, changes. Changes to the game code | |
| 103 show in www/ only after the build has copied them | |
| 104 - `--release` writes www/js/debug.js empty and leaves [debug] out of | |
| 105 config.js. The game then runs as with every debug switch off | |
| 23 | 106 |
| 36 | 107 `tools/make_dist.sh` takes its version from `tools/get_version.sh -f`: the |
| 108 latest Mercurial tag, with a timestamp appended when the working directory | |
| 109 is not clean or not at the tagged revision. `--debug` packs a build with | |
| 110 the debug code. The www/ directory keeps the build the script made, so a | |
| 111 plain build afterwards brings the debug code back. | |
| 112 | |
| 113 Scripts run by hand, not by the build: | |
| 23 | 114 |
| 36 | 115 - tools/cut_sheet.py cuts the monster, tower, projectile and HP bar |
| 116 sprites from a drawing sheet into art/ (art.md) | |
| 117 - tools/cut_digits.py cuts art/digits.png into art/digits/ | |
| 118 - tools/cut_sounds.py cuts the recordings into art/sounds/. It needs FFmpeg | |
| 119 with MP3 support, which the build does not | |
| 120 - tools/balance.py and tools/simulate.js estimate and test the balance of | |
| 121 the game (balancing.md) | |
| 22 | 122 |
| 123 | |
| 36 | 124 Config |
| 0 | 125 ------ |
| 126 | |
| 36 | 127 The file config/game.toml holds every game setting. The build checks it and |
| 128 turns it into www/data/config.js. There is no in-game editor: values are | |
| 129 changed in the file and the game is rebuilt. Units are map pixels (pixels | |
| 130 of the map scan), seconds, and map pixels per second. | |
| 0 | 131 |
| 36 | 132 Sections: |
| 0 | 133 |
| 36 | 134 - [scale]: drawing scale of maps, sprites, coins and HP bars |
| 135 - [monster.*]: hit points, coins and speed of each monster; boss marks | |
| 136 the bosses | |
| 137 - [animation], [movement]: hops, puffs, and the spread of monsters on the | |
| 138 road | |
| 139 - [route_preference]: the route weights (game.md, routes) | |
| 140 - [tower.*], [tower_rules]: tower values at level 1 and level 5, attack | |
| 141 time, projectile, slow, sell refund | |
| 142 - [placement]: where towers may stand | |
| 143 - [waves], [hearts], [coins], [controls]: breaks, heart costs, flying | |
| 144 coins, game speeds | |
| 145 - [sound], [sound.event]: volume, the minimum time between two plays, and | |
| 146 a placeholder beep per event | |
| 147 - [art.angle], [art.pivot], [art.screen]: drawing direction and rotation | |
| 148 point of turning sprites, and where buttons sit on the screen drawings | |
| 149 (art.md) | |
| 150 - [links]: the logo's web page and the email address of the help popup | |
| 151 - [debug]: debug switches | |
| 152 - [map.N]: per map the starting coins, early start bonus, spawn interval, | |
| 153 road half width, distances from spawn and exit points, the draft numbers | |
| 154 for tools/balance.py, and the wave lists [[map.N.wave]] | |
| 0 | 155 |
| 36 | 156 Debug switches, all off by default: |
| 0 | 157 |
| 36 | 158 - Start coins, unlimited coins, no heart loss |
| 159 - Start map and wave. The start map counts as the first map: the play | |
| 160 button starts it, and a lost map returns to it. Only that map starts at | |
| 161 the start wave | |
| 162 - Show routes, placement grid, tower ranges, hit areas | |
| 163 - Frame time and entity counts on screen | |
| 164 - A fixed random seed; 0 picks a new one per map | |
| 165 | |
| 166 The file js/debug.js holds the code for all of them. It replaces functions | |
| 167 of the game and the UI, and render.js calls it to draw over the map image | |
| 168 and over the monsters. The start-up in main.js takes the start map, wave | |
| 169 and seed from it. | |
| 170 In a release build the file is empty and none of this is left. | |
| 10 | 171 |
| 0 | 172 |
| 36 | 173 Runtime |
| 0 | 174 ------- |
| 175 | |
| 36 | 176 Script files, in load order: |
| 0 | 177 |
| 178 data/config.js generated settings | |
| 179 data/maps.js generated map data | |
| 4 | 180 data/sounds.js generated sound data |
| 36 | 181 js/util.js vector maths, seeded random numbers, easing |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
182 js/storage.js settings kept in the browser |
| 36 | 183 js/assets.js image loading, sprite drawing with angle and pivot |
| 184 js/audio.js recordings, placeholder beeps, splash screen music | |
| 185 js/map.js routes and sideways offsets, placement lookup, snapping, | |
| 186 t0 spike sides, route weights | |
| 187 js/monster.js movement along a route, hops, slows | |
| 188 js/tower.js targeting, attacks, upgrade cost, refund | |
| 189 js/projectile.js flight to the target, damage, p2 freeze splash | |
| 190 js/wave.js breaks, spawning, early start bonus | |
| 191 js/game.js game state and rules: coins, hearts, building, upgrade, | |
| 192 sell, win and loss | |
| 193 js/render.js drawing of the map and everything on it | |
| 194 js/ui.js layout, top bar, shop, tower menu, screens, help popup | |
| 0 | 195 js/input.js pointer events |
|
27
75deda08db90
game debug functions separated into own file; release build option, distribution script
ark
parents:
24
diff
changeset
|
196 js/debug.js debug switches, empty in a release build |
| 36 | 197 js/main.js start-up, screen flow and main loop |
| 0 | 198 |
| 36 | 199 Game loop: |
| 0 | 200 |
| 36 | 201 - The simulation advances in fixed steps of 1/60 s, driven by the |
| 202 browser's frame clock. Drawing happens once per frame | |
| 203 - Game speed n runs n times as many steps per frame | |
| 204 - Pause stops the simulation; drawing and the top bar buttons go on | |
| 205 - A long frame runs at most a few steps per speed step and drops the rest, | |
| 206 so the game slows down rather than freezing after the tab was in the | |
| 207 background | |
| 0 | 208 |
| 36 | 209 State: |
| 0 | 210 |
| 36 | 211 - The game state is plain objects: monsters, towers, projectiles, coins in |
| 212 flight, puffs, freeze splashes, wave state, coins and hearts. Rendering | |
| 213 reads it and does not change it | |
| 214 - The game's random numbers come from one seeded generator, so a run can | |
| 215 be repeated with the same seed. Sound variants use Math.random, so sound | |
| 216 does not change a run | |
| 0 | 217 |
| 36 | 218 Display: |
| 0 | 219 |
| 36 | 220 - The canvas fills the window. The map is scaled to fit the area left by |
| 221 the top bar and the shop, and centred | |
| 222 - The canvas resolution follows the device pixel ratio | |
| 223 - Buttons are at least 48 CSS pixels for touch | |
| 0 | 224 |
| 36 | 225 Input: |
| 0 | 226 |
| 36 | 227 - Pointer events cover mouse and touch alike |
| 228 - Buttons and tower selection act when the pointer goes down. Placing | |
| 229 follows the pointer until it is let go. The logo and the email address | |
| 230 act on release, as some browsers open a page or a mail only then | |
| 231 - The page blocks browser zoom, scroll, double tap and context menu | |
| 232 gestures on the canvas | |
| 0 | 233 |
| 36 | 234 Persistence: |
| 0 | 235 |
| 36 | 236 - Game progress is not stored. Settings, so far only sound on or off, are |
| 237 kept by js/storage.js in one localStorage entry with a format version | |
| 238 - All access is wrapped, so the game still runs when storage is | |
| 239 unavailable, for example in private browsing; settings are then not kept |
