wobblyhead hg > minor_monsters
view doc/architecture.md @ 36:a464bd43b76c 1.0.0
clean documentation rewrite
| author | ark |
|---|---|
| date | 2026-10-21 22:34 +0200 |
| parents | 85d23328a215 |
| children |
line wrap: on
line source
Architecture ------------ The game is a static HTML page with plain JavaScript, drawn on a 2D canvas. It uses no JavaScript libraries. A Python build script prepares everything that can be calculated in advance: processed images, map data, route data, embedded sounds and the config. It writes them to www/, together with a copy of the hand-written game files from game/. The www/ directory is the runnable game. It is not kept in the repository, so the game has to be built before it can be run. The details of the algorithms and their constants are in implementation.md. Directory layout ---------------- art/ drawings and recordings as supplied, and files cut from them art_sources/ the scans the drawings were cut from digits/ digits cut from digits.png (tools/cut_digits.py) sound_sources/ voice recordings as supplied sounds/ sounds cut from the recordings (tools/cut_sounds.py) config/ game.toml every game setting, the wave lists, debug switches doc/ documentation game/ hand-written game files index.html style.css js/ game code tools/ build, cutting, balancing and release scripts www/ the built game (generated, ignored) index.html copied from game/, as are style.css and js/ assets/ processed images data/ config.js, maps.js, sounds.js dist/ release tarballs (ignored) temp/ build cache and previews (ignored) build/maps.json route and placement data of the last build preview/ preview images, balance and simulation reports Generated files have a header line saying so and naming the script that made them. Running and deploying --------------------- - Local: build, then open www/index.html from disk, or serve www/ with any static web server - Server: `tools/make_dist.sh` runs a release build and packs www/ into dist/minor_monsters_VERSION.tar.gz, which unpacks into a directory minor_monsters. That directory is what the web server serves The page also runs from disk (file://), where Chrome blocks JavaScript modules, fetch() and reading pixels back from a canvas. So: - Scripts are loaded with plain script tags in a fixed order, and share one global namespace object, MM - Data files are JavaScript files that assign to MM, not JSON files - The game never reads mask pixels; the build turns the masks into data - Images are loaded with image elements - Sounds are embedded as base64 in a JavaScript file, so they can be decoded without fetch() Build ----- `python3 tools/build.py` runs all steps. It needs Python 3.11 or later, for the built-in TOML reader, and Pillow. A step is skipped when its outputs are newer than its inputs and the build scripts; `--force` runs every step. 1. Sprites: trim transparent edges and write to www/assets/. This covers monsters, towers, projectiles, coins, HP bars, icons, buttons, screens and the digits, written as digit_NAME_N. The HP bars also get an empty variant and blue variants for slowed monsters 2. Tower levels: split each t*p strip into levels 2 to 5, fill the inside of each outline with white, trim, and write t0_2 to t3_5 3. Maps: write the map scans as JPEG 4. Masks: classify the mask pixels, extract the routes and write the placement grid to www/data/maps.js. The result is also kept in temp/build/maps.json, for the later steps and the balance tools. This step also reruns when the placement settings or a map's road and spawn settings change 5. Sounds: embed art/sounds/*.mp3 in www/data/sounds.js, grouped by event 6. Config: check config/game.toml, fill in the tower values for every level, add the size of every sprite, and write www/data/config.js 7. Checks: print per map its routes with length, turning and walk times, the monster counts and shares, the coins and hit points of its waves, and the early start bonus for skipping every break. Warn on problems, such as a wave count that does not match the lists, or a boss missing from the last wave 8. Previews: write temp/preview/mapN.png with routes, entrances, exits and placement grid, mapN_routes.png for maps with several routes, and towers.png with every tower level, its rotation point and footprint 9. Game files: copy the files in game/ to www/ when their time or size differs. Files no longer in game/ are removed from www/, apart from www/assets/ and www/data/ Options: - `--watch` reruns the build when a file in art/, art/sounds/, art/digits/, config/ or game/, or a build script, changes. Changes to the game code show in www/ only after the build has copied them - `--release` writes www/js/debug.js empty and leaves [debug] out of config.js. The game then runs as with every debug switch off `tools/make_dist.sh` takes its version from `tools/get_version.sh -f`: the latest Mercurial tag, with a timestamp appended when the working directory is not clean or not at the tagged revision. `--debug` packs a build with the debug code. The www/ directory keeps the build the script made, so a plain build afterwards brings the debug code back. Scripts run by hand, not by the build: - tools/cut_sheet.py cuts the monster, tower, projectile and HP bar sprites from a drawing sheet into art/ (art.md) - tools/cut_digits.py cuts art/digits.png into art/digits/ - tools/cut_sounds.py cuts the recordings into art/sounds/. It needs FFmpeg with MP3 support, which the build does not - tools/balance.py and tools/simulate.js estimate and test the balance of the game (balancing.md) Config ------ The file config/game.toml holds every game setting. The build checks it and turns it into www/data/config.js. There is no in-game editor: values are changed in the file and the game is rebuilt. Units are map pixels (pixels of the map scan), seconds, and map pixels per second. Sections: - [scale]: drawing scale of maps, sprites, coins and HP bars - [monster.*]: hit points, coins and speed of each monster; boss marks the bosses - [animation], [movement]: hops, puffs, and the spread of monsters on the road - [route_preference]: the route weights (game.md, routes) - [tower.*], [tower_rules]: tower values at level 1 and level 5, attack time, projectile, slow, sell refund - [placement]: where towers may stand - [waves], [hearts], [coins], [controls]: breaks, heart costs, flying coins, game speeds - [sound], [sound.event]: volume, the minimum time between two plays, and a placeholder beep per event - [art.angle], [art.pivot], [art.screen]: drawing direction and rotation point of turning sprites, and where buttons sit on the screen drawings (art.md) - [links]: the logo's web page and the email address of the help popup - [debug]: debug switches - [map.N]: per map the starting coins, early start bonus, spawn interval, road half width, distances from spawn and exit points, the draft numbers for tools/balance.py, and the wave lists [[map.N.wave]] Debug switches, all off by default: - Start coins, unlimited coins, no heart loss - Start map and wave. The start map counts as the first map: the play button starts it, and a lost map returns to it. Only that map starts at the start wave - Show routes, placement grid, tower ranges, hit areas - Frame time and entity counts on screen - A fixed random seed; 0 picks a new one per map The file js/debug.js holds the code for all of them. It replaces functions of the game and the UI, and render.js calls it to draw over the map image and over the monsters. The start-up in main.js takes the start map, wave and seed from it. In a release build the file is empty and none of this is left. Runtime ------- Script files, in load order: data/config.js generated settings data/maps.js generated map data data/sounds.js generated sound data js/util.js vector maths, seeded random numbers, easing js/storage.js settings kept in the browser js/assets.js image loading, sprite drawing with angle and pivot js/audio.js recordings, placeholder beeps, splash screen music js/map.js routes and sideways offsets, placement lookup, snapping, t0 spike sides, route weights js/monster.js movement along a route, hops, slows js/tower.js targeting, attacks, upgrade cost, refund js/projectile.js flight to the target, damage, p2 freeze splash js/wave.js breaks, spawning, early start bonus js/game.js game state and rules: coins, hearts, building, upgrade, sell, win and loss js/render.js drawing of the map and everything on it js/ui.js layout, top bar, shop, tower menu, screens, help popup js/input.js pointer events js/debug.js debug switches, empty in a release build js/main.js start-up, screen flow and main loop Game loop: - The simulation advances in fixed steps of 1/60 s, driven by the browser's frame clock. Drawing happens once per frame - Game speed n runs n times as many steps per frame - Pause stops the simulation; drawing and the top bar buttons go on - A long frame runs at most a few steps per speed step and drops the rest, so the game slows down rather than freezing after the tab was in the background State: - The game state is plain objects: monsters, towers, projectiles, coins in flight, puffs, freeze splashes, wave state, coins and hearts. Rendering reads it and does not change it - The game's random numbers come from one seeded generator, so a run can be repeated with the same seed. Sound variants use Math.random, so sound does not change a run Display: - The canvas fills the window. The map is scaled to fit the area left by the top bar and the shop, and centred - The canvas resolution follows the device pixel ratio - Buttons are at least 48 CSS pixels for touch Input: - Pointer events cover mouse and touch alike - Buttons and tower selection act when the pointer goes down. Placing follows the pointer until it is let go. The logo and the email address act on release, as some browsers open a page or a mail only then - The page blocks browser zoom, scroll, double tap and context menu gestures on the canvas Persistence: - Game progress is not stored. Settings, so far only sound on or off, are kept by js/storage.js in one localStorage entry with a format version - All access is wrapped, so the game still runs when storage is unavailable, for example in private browsing; settings are then not kept
