|
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
|
|
|
64 build
|
|
|
65 -----
|
|
|
66
|
|
|
67 one command, `python3 tools/build.py`, runs all steps. each step skips its work
|
|
|
68 when its inputs have not changed. requires python 3.11 or later (for the
|
|
|
69 built-in toml reader) and pillow. numpy is optional, if the pure python image
|
|
|
70 steps turn out too slow.
|
|
|
71
|
|
4
|
72 tools/cut_sheet.py and tools/cut_sounds.py are run by hand and write to art/.
|
|
|
73 cut_sounds.py also needs ffmpeg with mp3 support, so the build itself does not
|
|
|
74 depend on it. see sound.md.
|
|
|
75
|
|
0
|
76 steps:
|
|
|
77
|
|
|
78 1. sprites: trim transparent edges, write to game/assets/
|
|
|
79 2. tower levels: t0-t3 are level 1. split each t*p strip into four images,
|
|
|
80 levels 2-5. fill the inside of the outline with white (flood fill from
|
|
|
81 outside, everything not reached is inside), trim, write as t0_2..t0_5 and
|
|
|
82 so on. see art.md
|
|
|
83 3. maps: copy the map images, possibly converted to a smaller format
|
|
|
84 4. masks: for each map
|
|
|
85 - classify pixels as path, entrance, exit or free, using colour
|
|
|
86 thresholds (the mask edges are soft)
|
|
|
87 - compute the distance from every free pixel to the nearest path pixel
|
|
|
88 - write a placement grid: cells where a tower footprint fits next to the
|
|
|
89 path, within the limits in balance.md
|
|
|
90 - extract routes, see below
|
|
4
|
91 5. sounds: embed art/sounds/*.mp3 as base64 in game/data/sounds.js,
|
|
|
92 grouped by event
|
|
|
93 6. config: read config/game.toml, check it, write game/data/config.js
|
|
|
94 7. checks: print a summary per map (routes with length and turning, monster
|
|
0
|
95 counts, coins and hp per map against the target) and warn on problems
|
|
4
|
96 8. previews: draw each map with its routes, entrances, exits and placement
|
|
0
|
97 grid into temp/preview/, for checking without starting the game
|
|
|
98
|
|
|
99 a watch mode (`tools/build.py --watch`) reruns the build when a file in
|
|
4
|
100 config/, art/ or art/sounds/ changes. tuning then means: edit the toml, save, reload the
|
|
0
|
101 browser.
|
|
|
102
|
|
|
103 `tools/publish.py` copies game/ to www/ with debug switches off, and writes a
|
|
|
104 zip into dist/.
|
|
|
105
|
|
|
106
|
|
|
107 routes
|
|
|
108 ------
|
|
|
109
|
|
|
110 a route is a list of points from an entrance to an exit. a map has one or
|
|
|
111 more routes. a monster picks a route at spawn (weights in balance.md) and
|
|
|
112 follows it to the end. this handles the branches on map1 and map2 without
|
|
|
113 junction logic at runtime.
|
|
|
114
|
|
|
115 extraction in the build:
|
|
|
116
|
|
|
117 1. thin the path pixels to a one-pixel centre line (skeleton)
|
|
|
118 2. turn the skeleton into a graph of junctions and segments, and drop short
|
|
|
119 dead ends left by uneven strokes
|
|
|
120 3. find the graph nodes nearest to each entrance and exit blob
|
|
|
121 4. list every path through the graph from an entrance to an exit that visits
|
|
|
122 no junction twice
|
|
|
123 5. smooth each route and reduce it to a few points per bend
|
|
|
124 6. record length and turning for each route
|
|
|
125
|
|
|
126 map0 and map3 give one route each. map1 and map2 give several; map2 may give
|
|
|
127 more than wanted. config/routes.toml can drop routes, set fixed weights, or
|
|
|
128 replace a route with hand-placed points, for cases where the automatic result
|
|
|
129 is wrong.
|
|
|
130
|
|
|
131
|
|
|
132 config
|
|
|
133 ------
|
|
|
134
|
|
|
135 config/game.toml holds every number in balance.md, the wave lists, and debug
|
|
|
136 switches. the build converts it into game/data/config.js. toml allows comments,
|
|
|
137 so each value can carry its source note (guess, decided).
|
|
|
138
|
|
|
139 wave format (sketch):
|
|
|
140
|
|
|
141 [[map.0.wave]]
|
|
|
142 groups = [
|
|
|
143 { monster = "m0", count = 6, interval = 1.0 },
|
|
|
144 { monster = "m1", count = 2, interval = 1.5, delay = 3.0 },
|
|
|
145 ]
|
|
|
146
|
|
|
147 a group starts after the previous one has finished spawning plus its delay.
|
|
|
148 the boss is a group in the last wave.
|
|
|
149
|
|
|
150 debug switches, all off by default:
|
|
|
151
|
|
|
152 - start coins override, unlimited coins
|
|
|
153 - start at a given map and wave
|
|
|
154 - unlock all maps
|
|
|
155 - no heart loss
|
|
|
156 - show routes, placement grid, tower ranges, hit areas
|
|
|
157 - higher game speeds
|
|
|
158 - on-screen frame time and entity counts
|
|
|
159
|
|
|
160 there is no in-game config editor: values are changed in the toml file.
|
|
|
161
|
|
|
162
|
|
|
163 runtime
|
|
|
164 -------
|
|
|
165
|
|
|
166 script files, in load order:
|
|
|
167
|
|
|
168 data/config.js generated settings
|
|
|
169 data/maps.js generated map data
|
|
4
|
170 data/sounds.js generated sound data
|
|
0
|
171 js/util.js vector maths, random numbers, easing
|
|
|
172 js/assets.js image loading, sprite table
|
|
4
|
173 js/audio.js recorded sounds, placeholder beeps for the other events
|
|
0
|
174 js/storage.js localStorage wrapper
|
|
|
175 js/map.js placement lookup, snapping, route sampling
|
|
|
176 js/monster.js movement along a route, hops, slow, hp
|
|
|
177 js/tower.js targeting, attacks, upgrade, sell
|
|
|
178 js/projectile.js
|
|
|
179 js/wave.js wave timing, countdown, early start bonus
|
|
|
180 js/game.js game state, rules, win and loss
|
|
|
181 js/render.js drawing
|
|
|
182 js/ui.js top bar, shop, tower menu, screens
|
|
|
183 js/input.js pointer events
|
|
|
184 js/main.js start-up and main loop
|
|
|
185
|
|
|
186 game loop:
|
|
|
187
|
|
|
188 - the simulation advances in fixed steps of 1/60 s. drawing happens once per
|
|
|
189 browser frame and is independent of it
|
|
|
190 - game speed 2x runs two simulation steps per frame
|
|
|
191 - pause stops the simulation; drawing and input continue
|
|
|
192 - a frame that takes too long runs at most a few steps and drops the rest, so
|
|
|
193 the game slows down rather than freezing after the tab was in the background
|
|
|
194
|
|
|
195 state:
|
|
|
196
|
|
|
197 - game state is plain objects: monsters, towers, projectiles, coins in flight,
|
|
|
198 wave state, coins, hearts. rendering reads it and does not change it
|
|
|
199 - random numbers come from one seeded generator, so a run can be repeated with
|
|
|
200 the same seed when chasing a bug
|
|
|
201
|
|
|
202 display:
|
|
|
203
|
|
|
204 - the canvas fills the window. the map is scaled to fit and centred, with
|
|
|
205 borders where the shapes differ
|
|
|
206 - the canvas resolution follows the device pixel ratio, so drawings stay sharp
|
|
|
207 on high-resolution screens
|
|
|
208 - layout: top bar with coins, hearts, wave, countdown, pause and speed. the
|
|
|
209 tower shop sits on the right in landscape and at the bottom in portrait
|
|
|
210 - buttons are at least 48 css pixels for touch
|
|
|
211
|
|
|
212 input:
|
|
|
213
|
|
|
214 - pointer events cover mouse and touch alike
|
|
|
215 - nothing relies on hover; selection and confirmation are by tap
|
|
|
216 - the page blocks browser zoom and scroll gestures on the canvas
|
|
|
217
|
|
|
218 placement:
|
|
|
219
|
|
|
220 - the build's placement grid marks every cell where a tower may stand
|
|
|
221 - on a tap, the game searches outward from the tap for the nearest legal cell
|
|
|
222 that keeps the minimum distance to other towers, within the snap radius
|
|
|
223 - t0 checks once, at placement, which of its four sides face the path, and
|
|
|
224 only draws spikes on those
|
|
|
225
|
|
|
226 persistence:
|
|
|
227
|
|
|
228 - one localStorage entry holds unlocked maps and settings (sound on or off)
|
|
|
229 - all access is wrapped so the game still runs when storage is unavailable,
|
|
|
230 for example in private browsing; progress is then not saved
|
|
|
231 - the entry carries a version number for later format changes
|
|
|
232
|
|
|
233
|
|
|
234 not planned
|
|
|
235 -----------
|
|
|
236
|
|
|
237 - in-browser editors or tuning panels
|
|
|
238 - automatic balance simulation: tower placement is up to the player, so a
|
|
|
239 simulation would need a placement strategy first. could be revisited once
|
|
|
240 the game is playable
|