diff doc/architecture.md @ 0:7af9dd4e2595

initial art and design/planning docs
author ark
date 2026-10-18 01:26 +0200
parents
children 2d46729a022e
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/architecture.md	Sun Oct 18 01:26:39 2026 +0200
@@ -0,0 +1,231 @@
+architecture
+------------
+
+the game is a static html page with plain javascript, drawn on a 2d canvas. it
+uses no javascript libraries and needs no build step to run. a python build
+script prepares everything that can be calculated in advance: processed
+images, map data, route data and the config. its output is committed, so the
+game runs straight from the source tree.
+
+this is a plan; nothing is implemented yet.
+
+
+directory layout
+----------------
+
+proposed:
+
+    art/            drawings as supplied: scans, masks, sprites
+    config/         hand-edited game settings (toml)
+        game.toml       numbers from balance.md, waves, debug switches
+        routes.toml     manual corrections to the extracted routes
+    tools/          python build scripts
+    game/           the runnable game, opened as game/index.html
+        index.html
+        style.css
+        js/             game code, hand-written
+        assets/         processed images (generated, committed)
+        data/           maps.js, config.js (generated, committed)
+    doc/            design and technical notes
+    www/            copy of game/ prepared for publishing (ignored)
+    dist/           zip archives for distribution (ignored)
+    temp/           scratch files, build previews (ignored)
+
+generated files carry a header line saying so and naming the script that made
+them.
+
+
+running the game
+----------------
+
+- desktop: open game/index.html directly from disk, or serve the directory
+  with any static web server
+- tablet: tablets cannot easily open local html files, so the game is served
+  over http, from a computer on the home network or from a web host. www/ is
+  the copy meant for that
+- an optional later step makes it installable to the home screen (web app
+  manifest, offline cache). that requires https
+
+opening the page from disk (file://) limits what the browser allows. chrome
+blocks javascript modules, fetch() and reading pixels back from a canvas
+there. so:
+
+- scripts are loaded with plain script tags in a fixed order, and share one
+  global namespace object
+- data files are javascript files that assign to that object, not json files
+- the game never reads mask pixels; the build turns the masks into data
+- images are loaded with image elements, which works from disk
+- recorded sounds will be embedded by the build as base64 data in a
+  javascript file, so they can be decoded without fetch()
+
+
+build
+-----
+
+one command, `python3 tools/build.py`, runs all steps. each step skips its work
+when its inputs have not changed. requires python 3.11 or later (for the
+built-in toml reader) and pillow. numpy is optional, if the pure python image
+steps turn out too slow.
+
+steps:
+
+1. sprites: trim transparent edges, write to game/assets/
+2. tower levels: t0-t3 are level 1. split each t*p strip into four images,
+   levels 2-5. fill the inside of the outline with white (flood fill from
+   outside, everything not reached is inside), trim, write as t0_2..t0_5 and
+   so on. see art.md
+3. maps: copy the map images, possibly converted to a smaller format
+4. masks: for each map
+    - classify pixels as path, entrance, exit or free, using colour
+      thresholds (the mask edges are soft)
+    - compute the distance from every free pixel to the nearest path pixel
+    - write a placement grid: cells where a tower footprint fits next to the
+      path, within the limits in balance.md
+    - extract routes, see below
+5. config: read config/game.toml, check it, write game/data/config.js
+6. checks: print a summary per map (routes with length and turning, monster
+   counts, coins and hp per map against the target) and warn on problems
+7. previews: draw each map with its routes, entrances, exits and placement
+   grid into temp/preview/, for checking without starting the game
+
+a watch mode (`tools/build.py --watch`) reruns the build when a file in
+config/ or art/ changes. tuning then means: edit the toml, save, reload the
+browser.
+
+`tools/publish.py` copies game/ to www/ with debug switches off, and writes a
+zip into dist/.
+
+
+routes
+------
+
+a route is a list of points from an entrance to an exit. a map has one or
+more routes. a monster picks a route at spawn (weights in balance.md) and
+follows it to the end. this handles the branches on map1 and map2 without
+junction logic at runtime.
+
+extraction in the build:
+
+1. thin the path pixels to a one-pixel centre line (skeleton)
+2. turn the skeleton into a graph of junctions and segments, and drop short
+   dead ends left by uneven strokes
+3. find the graph nodes nearest to each entrance and exit blob
+4. list every path through the graph from an entrance to an exit that visits
+   no junction twice
+5. smooth each route and reduce it to a few points per bend
+6. record length and turning for each route
+
+map0 and map3 give one route each. map1 and map2 give several; map2 may give
+more than wanted. config/routes.toml can drop routes, set fixed weights, or
+replace a route with hand-placed points, for cases where the automatic result
+is wrong.
+
+
+config
+------
+
+config/game.toml holds every number in balance.md, the wave lists, and debug
+switches. the build converts it into game/data/config.js. toml allows comments,
+so each value can carry its source note (guess, decided).
+
+wave format (sketch):
+
+    [[map.0.wave]]
+    groups = [
+        { monster = "m0", count = 6, interval = 1.0 },
+        { monster = "m1", count = 2, interval = 1.5, delay = 3.0 },
+    ]
+
+a group starts after the previous one has finished spawning plus its delay.
+the boss is a group in the last wave.
+
+debug switches, all off by default:
+
+- start coins override, unlimited coins
+- start at a given map and wave
+- unlock all maps
+- no heart loss
+- show routes, placement grid, tower ranges, hit areas
+- higher game speeds
+- on-screen frame time and entity counts
+
+there is no in-game config editor: values are changed in the toml file.
+
+
+runtime
+-------
+
+script files, in load order:
+
+    data/config.js  generated settings
+    data/maps.js    generated map data
+    js/util.js      vector maths, random numbers, easing
+    js/assets.js    image loading, sprite table
+    js/audio.js     placeholder beeps, later recorded sounds
+    js/storage.js   localStorage wrapper
+    js/map.js       placement lookup, snapping, route sampling
+    js/monster.js   movement along a route, hops, slow, hp
+    js/tower.js     targeting, attacks, upgrade, sell
+    js/projectile.js
+    js/wave.js      wave timing, countdown, early start bonus
+    js/game.js      game state, rules, win and loss
+    js/render.js    drawing
+    js/ui.js        top bar, shop, tower menu, screens
+    js/input.js     pointer events
+    js/main.js      start-up and main loop
+
+game loop:
+
+- the simulation advances in fixed steps of 1/60 s. drawing happens once per
+  browser frame and is independent of it
+- game speed 2x runs two simulation steps per frame
+- pause stops the simulation; drawing and input continue
+- a frame that takes too long runs at most a few steps and drops the rest, so
+  the game slows down rather than freezing after the tab was in the background
+
+state:
+
+- game state is plain objects: monsters, towers, projectiles, coins in flight,
+  wave state, coins, hearts. rendering reads it and does not change it
+- random numbers come from one seeded generator, so a run can be repeated with
+  the same seed when chasing a bug
+
+display:
+
+- the canvas fills the window. the map is scaled to fit and centred, with
+  borders where the shapes differ
+- the canvas resolution follows the device pixel ratio, so drawings stay sharp
+  on high-resolution screens
+- layout: top bar with coins, hearts, wave, countdown, pause and speed. the
+  tower shop sits on the right in landscape and at the bottom in portrait
+- buttons are at least 48 css pixels for touch
+
+input:
+
+- pointer events cover mouse and touch alike
+- nothing relies on hover; selection and confirmation are by tap
+- the page blocks browser zoom and scroll gestures on the canvas
+
+placement:
+
+- the build's placement grid marks every cell where a tower may stand
+- on a tap, the game searches outward from the tap for the nearest legal cell
+  that keeps the minimum distance to other towers, within the snap radius
+- t0 checks once, at placement, which of its four sides face the path, and
+  only draws spikes on those
+
+persistence:
+
+- one localStorage entry holds unlocked maps and settings (sound on or off)
+- all access is wrapped so the game still runs when storage is unavailable,
+  for example in private browsing; progress is then not saved
+- the entry carries a version number for later format changes
+
+
+not planned
+-----------
+
+- in-browser editors or tuning panels
+- automatic balance simulation: tower placement is up to the player, so a
+  simulation would need a placement strategy first. could be revisited once
+  the game is playable