@clarity-js/engine 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +212 -2
- package/index.d.ts +206 -0
- package/package.json +24 -4
- package/schema.json +628 -0
- package/src/art.js +115 -0
- package/src/bots.js +154 -0
- package/src/cardboard.js +304 -0
- package/src/common.js +100 -0
- package/src/editor.js +285 -0
- package/src/explore.js +258 -0
- package/src/grid.js +375 -0
- package/src/index.js +176 -0
- package/src/make.js +273 -0
- package/src/node.js +61 -0
- package/src/physicstoy.js +268 -0
- package/src/platformer.js +253 -0
- package/src/racing.js +228 -0
- package/src/shmup.js +239 -0
- package/src/skins.js +433 -0
- package/src/sound.js +179 -0
- package/src/topdown.js +319 -0
- package/src/validate.js +225 -0
- package/src/view.js +340 -0
package/src/racing.js
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// @clarity-js/engine — the racing kit: a closed track, a car that grips and slides, laps, and rivals to beat.
|
|
2
|
+
//
|
|
3
|
+
// A level is plain data: the track's control points (a closed curve through them), its width, the laps, the rivals
|
|
4
|
+
// and how fast they go (a share of the fastest they could lap), and the place to finish in.
|
|
5
|
+
//
|
|
6
|
+
// { "id": "l1", "track": [[0, -40], [38, -12], [24, 34], [-26, 30], [-40, -10]], "width": 9, "laps": 2,
|
|
7
|
+
// "rivals": 3, "rivalPace": 0.82, "place": 1 }
|
|
8
|
+
//
|
|
9
|
+
// Input is held: input(steer, throttle), each from −1 to 1 (a throttle below 0 brakes). Off the track the grass
|
|
10
|
+
// slows the car. Rivals keep to the racing line; they do not crash into you.
|
|
11
|
+
|
|
12
|
+
import { createSim } from '@ozsarman/clarityjs/sim';
|
|
13
|
+
import { DT, clamp, round3, settingsFor, hooked } from './common.js';
|
|
14
|
+
|
|
15
|
+
export const kit = {
|
|
16
|
+
name: 'racing',
|
|
17
|
+
title: 'Racing',
|
|
18
|
+
describe: 'A closed track, a car that grips and slides, laps, and rivals to finish ahead of.',
|
|
19
|
+
settings: {
|
|
20
|
+
top: { value: 30, min: 8, max: 80, unit: 'units/s' },
|
|
21
|
+
accel: { value: 16, min: 4, max: 60, unit: 'units/s²' },
|
|
22
|
+
brake: { value: 30, min: 4, max: 90, unit: 'units/s²' },
|
|
23
|
+
turn: { value: 2.3, min: 0.5, max: 6, unit: 'rad/s' },
|
|
24
|
+
grip: { value: 6, min: 0.5, max: 20 },
|
|
25
|
+
grass: { value: 0.45, min: 0.05, max: 1, unit: 'of top speed' },
|
|
26
|
+
},
|
|
27
|
+
level: { track: [[0, -40], [38, -12], [24, 34], [-26, 30], [-40, -10]], width: 9, laps: 2, rivals: 3, rivalPace: 0.8, place: 1 },
|
|
28
|
+
examples: ['A night race along the coast road', 'Toy cars round a kitchen table track', 'Drift through a neon city circuit'],
|
|
29
|
+
input: ['keys', 'touch', 'pad'],
|
|
30
|
+
continuous: true,
|
|
31
|
+
controls: 'axes',
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** The centre line, sampled evenly: points, headings, the length so far, and how sharply it bends. */
|
|
35
|
+
export function buildTrack(points, samples = 240) {
|
|
36
|
+
const p = points.map(([x, y]) => ({ x, y })), n = p.length;
|
|
37
|
+
const at = (i) => p[((i % n) + n) % n];
|
|
38
|
+
const raw = [];
|
|
39
|
+
for (let i = 0; i < n; i++) for (let k = 0; k < 24; k++) {
|
|
40
|
+
const t = k / 24, a = at(i - 1), b = at(i), c = at(i + 1), d = at(i + 2);
|
|
41
|
+
const t2 = t * t, t3 = t2 * t;
|
|
42
|
+
raw.push({ x: 0.5 * (2 * b.x + (-a.x + c.x) * t + (2 * a.x - 5 * b.x + 4 * c.x - d.x) * t2 + (-a.x + 3 * b.x - 3 * c.x + d.x) * t3), y: 0.5 * (2 * b.y + (-a.y + c.y) * t + (2 * a.y - 5 * b.y + 4 * c.y - d.y) * t2 + (-a.y + 3 * b.y - 3 * c.y + d.y) * t3) });
|
|
43
|
+
}
|
|
44
|
+
const cum = [0];
|
|
45
|
+
for (let i = 1; i <= raw.length; i++) cum.push(cum[i - 1] + Math.hypot(raw[i % raw.length].x - raw[i - 1].x, raw[i % raw.length].y - raw[i - 1].y));
|
|
46
|
+
const total = cum[raw.length], out = [];
|
|
47
|
+
let j = 0;
|
|
48
|
+
for (let k = 0; k < samples; k++) {
|
|
49
|
+
const s = (k / samples) * total;
|
|
50
|
+
while (cum[j + 1] < s) j++;
|
|
51
|
+
const f = (s - cum[j]) / ((cum[j + 1] - cum[j]) || 1), a = raw[j], b = raw[(j + 1) % raw.length];
|
|
52
|
+
out.push({ x: a.x + (b.x - a.x) * f, y: a.y + (b.y - a.y) * f, s });
|
|
53
|
+
}
|
|
54
|
+
for (let k = 0; k < samples; k++) { const a = out[(k - 1 + samples) % samples], b = out[(k + 1) % samples]; out[k].h = Math.atan2(b.y - a.y, b.x - a.x); }
|
|
55
|
+
for (let k = 0; k < samples; k++) { let d = out[(k + 1) % samples].h - out[(k - 1 + samples) % samples].h; d = Math.atan2(Math.sin(d), Math.cos(d)); out[k].bend = Math.abs(d) / ((total / samples) * 2); }
|
|
56
|
+
return { pts: out, total, n: samples };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The nearest sample to (x, y), searched near a guess. → { i, off } (off: how far from the centre line, signed) */
|
|
60
|
+
function nearest(track, x, y, guess) {
|
|
61
|
+
const n = track.n;
|
|
62
|
+
let bi = guess, bd = Infinity;
|
|
63
|
+
const span = guess == null ? n : 30;
|
|
64
|
+
for (let k = -span; k <= span; k++) {
|
|
65
|
+
const i = guess == null ? (k + span) % n : ((guess + k) % n + n) % n, p = track.pts[i], d = (p.x - x) ** 2 + (p.y - y) ** 2;
|
|
66
|
+
if (d < bd) { bd = d; bi = i; }
|
|
67
|
+
}
|
|
68
|
+
const p = track.pts[bi], side = Math.sign(Math.cos(p.h) * (y - p.y) - Math.sin(p.h) * (x - p.x)) || 1;
|
|
69
|
+
return { i: bi, off: side * Math.sqrt(bd) };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The fastest a car can take the bend at sample i, by the grip. */
|
|
73
|
+
const cornerSpeed = (cfg, bend) => Math.min(cfg.top, Math.sqrt((cfg.grip * 2.2) / Math.max(bend, 1e-4)));
|
|
74
|
+
|
|
75
|
+
function setup(level, cfg, rng) {
|
|
76
|
+
const track = buildTrack(level.track ?? kit.level.track);
|
|
77
|
+
const p0 = track.pts[0], laps = clamp(Math.round(level.laps ?? 2), 1, 9);
|
|
78
|
+
const rivals = [];
|
|
79
|
+
const pace = clamp(level.rivalPace ?? 0.8, 0.3, 1.1);
|
|
80
|
+
for (let r = 0; r < clamp(Math.round(level.rivals ?? 3), 0, 7); r++) rivals.push({ d: -(r + 1) * 6, pace: pace * (0.96 + rng() * 0.08), done: false, at: 0, wob: rng() * 6.28 });
|
|
81
|
+
return {
|
|
82
|
+
cfg, track: { pts: track.pts.map((p) => ({ x: round3(p.x), y: round3(p.y), h: round3(p.h), bend: round3(p.bend) })), total: track.total, n: track.n },
|
|
83
|
+
width: clamp(level.width ?? 9, 3, 30), laps, place: clamp(Math.round(level.place ?? 1), 1, rivals.length + 1),
|
|
84
|
+
x: p0.x - Math.cos(p0.h) * 3, y: p0.y - Math.sin(p0.h) * 3, h: p0.h, vx: 0, vy: 0, steer: 0, throttle: 0,
|
|
85
|
+
i: track.n - Math.max(1, Math.round(3 / (track.total / track.n))), d: -3, lap: 0, rivals, t: 0, off: 0, grassT: 0, finished: null, deaths: [], score: 0, won: false, lost: false,
|
|
86
|
+
time: Number(level.time ?? 0) || laps * (track.total / (cfg.top * 0.45)) + 20,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function step(s, dt, ctx) {
|
|
91
|
+
if (s.won || s.lost) return;
|
|
92
|
+
const cfg = s.cfg, tr = s.track;
|
|
93
|
+
s.t += dt;
|
|
94
|
+
// the car: speed along its heading, a slide across it that grip takes away
|
|
95
|
+
const fwd = Math.cos(s.h) * s.vx + Math.sin(s.h) * s.vy, side = -Math.sin(s.h) * s.vx + Math.cos(s.h) * s.vy;
|
|
96
|
+
const near = nearest(tr, s.x, s.y, s.i), onGrass = Math.abs(near.off) > s.width / 2;
|
|
97
|
+
const top = cfg.top * (onGrass ? cfg.grass : 1);
|
|
98
|
+
let f = fwd;
|
|
99
|
+
if (s.throttle >= 0) f += s.throttle * cfg.accel * dt * (1 - Math.max(0, f) / top); else f += s.throttle * cfg.brake * dt;
|
|
100
|
+
if (f > top) f -= (f - top) * Math.min(1, dt * 3);
|
|
101
|
+
f = clamp(f, -6, cfg.top * 1.05);
|
|
102
|
+
f *= 1 - dt * 0.15;
|
|
103
|
+
const grip = cfg.grip * (onGrass ? 0.5 : 1);
|
|
104
|
+
const sd = side * Math.max(0, 1 - grip * dt);
|
|
105
|
+
// the wheel turns the car no faster than the grip holds it: a bend taken too fast runs wide
|
|
106
|
+
const aMax = cfg.grip * 2.2 * (onGrass ? 0.6 : 1);
|
|
107
|
+
s.h += s.steer * Math.min(cfg.turn, aMax / Math.max(Math.abs(f), 1)) * dt * clamp(Math.abs(f) / 6, 0, 1) * Math.sign(f || 1);
|
|
108
|
+
s.vx = Math.cos(s.h) * f - Math.sin(s.h) * sd; s.vy = Math.sin(s.h) * f + Math.cos(s.h) * sd;
|
|
109
|
+
s.x += s.vx * dt; s.y += s.vy * dt;
|
|
110
|
+
if (onGrass) s.grassT += dt;
|
|
111
|
+
// a wall far off the track (twice its width) stops the car
|
|
112
|
+
const n2 = nearest(tr, s.x, s.y, near.i);
|
|
113
|
+
if (Math.abs(n2.off) > s.width * 1.6) { const p = tr.pts[n2.i]; s.x = p.x - Math.sin(p.h) * Math.sign(n2.off) * s.width * 1.5 * -1; s.y = p.y + Math.cos(p.h) * Math.sign(n2.off) * s.width * 1.5; s.vx *= 0.2; s.vy *= 0.2; s.deaths.push({ x: round3(s.x), y: round3(s.y), why: 'wall', t: round3(s.t) }); ctx.emit({ type: 'crash' }); }
|
|
114
|
+
// how far round: the distance along the line, laps counted as it wraps
|
|
115
|
+
const prev = s.i; s.i = n2.i; s.off = n2.off;
|
|
116
|
+
let step_ = s.i - prev; if (step_ > tr.n / 2) step_ -= tr.n; if (step_ < -tr.n / 2) step_ += tr.n;
|
|
117
|
+
s.d += step_ * (tr.total / tr.n);
|
|
118
|
+
const lap = Math.floor(Math.max(0, s.d) / tr.total);
|
|
119
|
+
if (lap > s.lap) { s.lap = lap; ctx.emit({ type: 'lap', lap }); }
|
|
120
|
+
// rivals along the line, at the speed the bends allow times their pace
|
|
121
|
+
for (const r of s.rivals) {
|
|
122
|
+
if (r.done) continue;
|
|
123
|
+
const k = ((Math.floor(((r.d % tr.total) + tr.total) % tr.total / tr.total * tr.n)) + tr.n) % tr.n;
|
|
124
|
+
let slow = cfg.top;
|
|
125
|
+
for (let a = 0; a < 14; a++) slow = Math.min(slow, cornerSpeed(cfg, tr.pts[(k + a) % tr.n].bend));
|
|
126
|
+
r.v = (r.v ?? 0) + clamp(slow * r.pace - (r.v ?? 0), -cfg.brake * dt, cfg.accel * dt * 0.9);
|
|
127
|
+
r.d += r.v * dt;
|
|
128
|
+
if (r.d >= s.laps * tr.total) { r.done = true; r.at = s.t; ctx.emit({ type: 'rival-finished' }); }
|
|
129
|
+
}
|
|
130
|
+
if (s.d >= s.laps * tr.total) {
|
|
131
|
+
const ahead = s.rivals.filter((r) => r.done).length;
|
|
132
|
+
s.finished = ahead + 1;
|
|
133
|
+
if (s.finished <= s.place) { s.won = true; s.score = Math.max(0, Math.round(1000 - s.t * 5)) + (s.place - s.finished) * 200; ctx.emit({ type: 'won', place: s.finished }); }
|
|
134
|
+
else { s.lost = true; ctx.emit({ type: 'lost', why: 'place', place: s.finished }); }
|
|
135
|
+
}
|
|
136
|
+
if (!s.won && !s.lost && s.t >= s.time) { s.lost = true; ctx.emit({ type: 'lost', why: 'time' }); }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const ACTIONS = {
|
|
140
|
+
/** Hold the wheel (steer −1 left … 1 right) and the pedals (throttle 1, brake −1). */
|
|
141
|
+
input(s, ctx, steer = 0, throttle = 0) { s.steer = clamp(Number(steer) || 0, -1, 1); s.throttle = clamp(Number(throttle) || 0, -1, 1); },
|
|
142
|
+
stop(s) { s.steer = 0; s.throttle = 0; },
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const placeNow = (s) => 1 + s.rivals.filter((r) => r.d > s.d).length;
|
|
146
|
+
|
|
147
|
+
function value(s) {
|
|
148
|
+
if (s.won) return 1e6 - s.t;
|
|
149
|
+
if (s.lost) return -1e6;
|
|
150
|
+
return s.d * 3 - placeNow(s) * 20 - Math.max(0, Math.abs(s.off) - s.width / 2) * 6;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Steer to a point on the line ahead (further at speed) and drive at the speed the bends ahead allow. */
|
|
154
|
+
function drive(s, { look = 1, careful = true } = {}) {
|
|
155
|
+
const tr = s.track, speed = Math.hypot(s.vx, s.vy);
|
|
156
|
+
const ahead = Math.round((6 + speed * 0.45 * look) / (tr.total / tr.n));
|
|
157
|
+
const p = tr.pts[(s.i + ahead) % tr.n];
|
|
158
|
+
let want = Math.atan2(p.y - s.y, p.x - s.x) - s.h; want = Math.atan2(Math.sin(want), Math.cos(want));
|
|
159
|
+
const steer = clamp(want * 2.2, -1, 1);
|
|
160
|
+
if (!careful) return [steer, 1];
|
|
161
|
+
let limit = s.cfg.top;
|
|
162
|
+
const span = Math.round((speed * speed / (2 * s.cfg.brake) + 8) / (tr.total / tr.n));
|
|
163
|
+
for (let a = 0; a <= span; a++) {
|
|
164
|
+
const v = cornerSpeed(s.cfg, tr.pts[(s.i + a) % tr.n].bend), d = a * (tr.total / tr.n);
|
|
165
|
+
limit = Math.min(limit, Math.sqrt(v * v + 2 * s.cfg.brake * 0.85 * d));
|
|
166
|
+
}
|
|
167
|
+
return [steer, speed < limit - 0.5 ? 1 : speed > limit + 1 ? -1 : 0.2];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export const bots = {
|
|
171
|
+
random(sim, rng, memo) {
|
|
172
|
+
if (!memo.in || rng.chance(0.2)) memo.in = [rng.range(-1, 1), rng.pick([1, 1, 0.5, -1])];
|
|
173
|
+
return { act: 'input', args: memo.in, for: 0.1 };
|
|
174
|
+
},
|
|
175
|
+
greedy(sim) { return { act: 'input', args: drive(sim.state, { look: 0.6, careful: false }), for: 0.1 }; },
|
|
176
|
+
planner(sim) { return { act: 'input', args: drive(sim.state, { look: 1, careful: true }), for: 0.1 }; },
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
export function create({ game, level, seed = 1, hooks = {} }) {
|
|
180
|
+
const cfg = settingsFor(kit.settings, game), h = hooked(hooks, ACTIONS);
|
|
181
|
+
const sim = createSim({
|
|
182
|
+
seed,
|
|
183
|
+
setup: (ctx) => { const s = setup(level, cfg, ctx.rng); hooks.setup?.(s, ctx, level); return s; },
|
|
184
|
+
step: (s, dt, ctx) => { step(s, dt, ctx); hooks.step?.(s, dt, ctx); h.end(s, ctx); },
|
|
185
|
+
actions: h.actions,
|
|
186
|
+
report: (s) => ({
|
|
187
|
+
won: s.won, lost: s.lost, x: round3(s.x), y: round3(s.y), heading: round3(s.h), speed: round3(Math.hypot(s.vx, s.vy)),
|
|
188
|
+
lap: Math.min(s.lap + 1, s.laps), laps: s.laps, place: s.finished ?? placeNow(s), of: s.rivals.length + 1, want: s.place,
|
|
189
|
+
progress: round3(Math.max(0, s.d) / (s.laps * s.track.total)), offTrack: Math.abs(s.off) > s.width / 2, t: round3(s.t),
|
|
190
|
+
score: hooks.score ? hooks.score(s) : s.score, deaths: s.deaths.length,
|
|
191
|
+
}),
|
|
192
|
+
});
|
|
193
|
+
sim.kit = kit; sim.bots = bots;
|
|
194
|
+
sim.value = () => (hooks.value ? hooks.value(sim.state) : value(sim.state));
|
|
195
|
+
return sim;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// ─── levels ──────────────────────────────────────────────────────────────────
|
|
199
|
+
|
|
200
|
+
/** A closed track round a middle: points at even angles, each at a radius with some chance in it. */
|
|
201
|
+
export function trackPoints(rng, { points = 9, radius = 55, wobble = 0.35 } = {}) {
|
|
202
|
+
const out = [];
|
|
203
|
+
for (let i = 0; i < points; i++) {
|
|
204
|
+
const a = (i / points) * Math.PI * 2 + rng.range(-0.12, 0.12), r = radius * (1 - wobble / 2 + rng() * wobble);
|
|
205
|
+
out.push([Math.round(Math.cos(a) * r * 10) / 10, Math.round(Math.sin(a) * r * 0.8 * 10) / 10]);
|
|
206
|
+
}
|
|
207
|
+
return out;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** The fastest lap a careful driver makes on a track (the planner, alone): for setting the rivals' pace. */
|
|
211
|
+
export function bestLap(level, game = {}) {
|
|
212
|
+
const sim = create({ game, level: { ...level, rivals: 0, laps: 1, place: 1 }, seed: 1 });
|
|
213
|
+
for (let t = 0; t < 300 && !sim.state.won && !sim.state.lost; t += 0.1) { sim.act('input', ...drive(sim.state)); for (let k = 0; k < 6; k++) sim.step(DT); }
|
|
214
|
+
return sim.state.won ? sim.state.t : null;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Easier versions of a level, the smallest change first (for play()'s fixes). */
|
|
218
|
+
export function easier(lv) {
|
|
219
|
+
const pace = lv.rivalPace ?? 0.8, out = [];
|
|
220
|
+
for (const d of [0.05, 0.1, 0.2]) out.push({ change: { rivalPace: Math.round((pace - d) * 100) / 100 }, say: `rivals' pace ${pace} → ${Math.round((pace - d) * 100) / 100}` });
|
|
221
|
+
if ((lv.place ?? 1) <= (lv.rivals ?? 3)) out.push({ change: { place: (lv.place ?? 1) + 1 }, say: `finish in the first ${(lv.place ?? 1) + 1}` });
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
224
|
+
export function harder(lv) {
|
|
225
|
+
const pace = lv.rivalPace ?? 0.8, out = [{ change: { rivalPace: Math.min(1.1, Math.round((pace + 0.04) * 100) / 100) }, say: `rivals' pace ${pace} → ${Math.min(1.1, Math.round((pace + 0.04) * 100) / 100)}` }];
|
|
226
|
+
if ((lv.place ?? 1) > 1) out.push({ change: { place: lv.place - 1 }, say: `finish in the first ${lv.place - 1}` });
|
|
227
|
+
return out;
|
|
228
|
+
}
|
package/src/shmup.js
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// @clarity-js/engine — the shoot-'em-up kit: a ship that fires by itself, waves that come down the screen, a
|
|
2
|
+
// boss at the end.
|
|
3
|
+
//
|
|
4
|
+
// The field is `width` units across and 16 high, its top the far end; the ship stays in the bottom part. A level
|
|
5
|
+
// is plain data: how long it is, its waves and its boss.
|
|
6
|
+
//
|
|
7
|
+
// { "id": "l1", "length": 40, "waves": [{ "t": 2, "kind": "line", "n": 5, "x": 0.5, "hp": 1, "fire": 0 }],
|
|
8
|
+
// "boss": { "hp": 60, "pattern": "ring" } }
|
|
9
|
+
//
|
|
10
|
+
// Wave kinds: line (across, straight down), vee, sine (weaving down), dive (aim at the ship), column. `fire` is
|
|
11
|
+
// how often each enemy shoots at the ship (shots a second). Boss patterns: ring, spiral, fan. Power-ups (P) widen
|
|
12
|
+
// the shot. Input is held: input(dx, dy, focus) — focus moves slowly, for threading between shots.
|
|
13
|
+
|
|
14
|
+
import { createSim } from '@ozsarman/clarityjs/sim';
|
|
15
|
+
import { DT, clamp, round3, dist, settingsFor, lookAhead, hooked } from './common.js';
|
|
16
|
+
|
|
17
|
+
export const kit = {
|
|
18
|
+
name: 'shmup',
|
|
19
|
+
title: 'Shoot-\'em-up',
|
|
20
|
+
describe: 'A ship that fires by itself, waves of enemies down the screen, shots to weave between, and a boss at the end.',
|
|
21
|
+
settings: {
|
|
22
|
+
width: { value: 9, min: 5, max: 16, unit: 'units' },
|
|
23
|
+
speed: { value: 7.5, min: 2, max: 16, unit: 'units/s' },
|
|
24
|
+
fireRate: { value: 7, min: 1, max: 20, unit: 'shots/s' },
|
|
25
|
+
lives: { value: 3, min: 1, max: 9 },
|
|
26
|
+
bulletSpeed: { value: 5, min: 1, max: 14, unit: 'units/s' },
|
|
27
|
+
hitbox: { value: 0.16, min: 0.05, max: 0.6, unit: 'units' },
|
|
28
|
+
},
|
|
29
|
+
level: { length: 30, waves: [{ t: 1, kind: 'line', n: 5, x: 0.5, hp: 1, fire: 0 }, { t: 6, kind: 'vee', n: 5, x: 0.5, hp: 2, fire: 0.3 }], boss: { hp: 40, pattern: 'ring' } },
|
|
30
|
+
examples: ['A vertical shoot-\'em-up of pastry ships over a candy sea', 'Dodge glowing shots in a neon nebula', 'Paper planes against a storm of ink'],
|
|
31
|
+
input: ['keys', 'touch', 'pad'],
|
|
32
|
+
continuous: true,
|
|
33
|
+
controls: 'axes',
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const H = 16, SHIP_Y = 14;
|
|
37
|
+
|
|
38
|
+
function setup(level, cfg, rng) {
|
|
39
|
+
const waves = (level.waves ?? []).map((w, i) => ({ id: i, t: Number(w.t) || 0, kind: w.kind ?? 'line', n: clamp(Math.round(w.n ?? 5), 1, 14), x: clamp(w.x ?? 0.5, 0, 1), hp: Math.max(1, w.hp ?? 1), fire: Math.max(0, w.fire ?? 0), done: false })).sort((a, b) => a.t - b.t);
|
|
40
|
+
const boss = level.boss ? { hp: Math.max(1, level.boss.hp ?? 50), max: Math.max(1, level.boss.hp ?? 50), pattern: level.boss.pattern ?? 'ring', x: cfg.width / 2, y: -3, on: false, phase: 0, cool: 1.5 } : null;
|
|
41
|
+
return {
|
|
42
|
+
cfg, W: cfg.width, x: cfg.width / 2, y: SHIP_Y, ix: 0, iy: 0, focus: false,
|
|
43
|
+
lives: cfg.lives, hurt: 1.5, cool: 0, power: 1, shots: [], foes: [], bullets: [], items: [],
|
|
44
|
+
waves, boss, length: Math.max(5, Number(level.length) || 30), t: 0, scroll: 0, killed: 0, total: waves.reduce((n, w) => n + w.n, 0),
|
|
45
|
+
deaths: [], score: 0, won: false, lost: false, seed: rng.int(1e6),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function spawn(s, w, rng) {
|
|
50
|
+
const W = s.W;
|
|
51
|
+
for (let i = 0; i < w.n; i++) {
|
|
52
|
+
const f = { x: 0, y: -1, vx: 0, vy: 2.2, hp: w.hp, max: w.hp, kind: w.kind, fire: w.fire, cool: 0.6 + rng() * (w.fire ? 1 / w.fire : 1), t: 0, i, alive: true, wave: w.id, x0: 0, drops: rng.chance(0.08) };
|
|
53
|
+
if (w.kind === 'line') { f.x = clamp(W * w.x + (i - (w.n - 1) / 2) * 1.1, 0.5, W - 0.5); f.y = -1; }
|
|
54
|
+
else if (w.kind === 'vee') { f.x = clamp(W * w.x + (i - (w.n - 1) / 2) * 0.9, 0.5, W - 0.5); f.y = -1 - Math.abs(i - (w.n - 1) / 2) * 0.8; }
|
|
55
|
+
else if (w.kind === 'sine') { f.x = W * w.x; f.y = -1 - i * 1.0; f.vy = 2.6; }
|
|
56
|
+
else if (w.kind === 'dive') { f.x = clamp(W * (0.15 + 0.7 * ((i + 0.5) / w.n)), 0.5, W - 0.5); f.y = -1 - i * 0.6; f.vy = 1.5; }
|
|
57
|
+
else { f.x = clamp(W * w.x, 0.5, W - 0.5); f.y = -1 - i * 1.1; f.vy = 2.8; } // column
|
|
58
|
+
f.x0 = f.x;
|
|
59
|
+
s.foes.push(f);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function hurt(s, ctx, why) {
|
|
64
|
+
if (s.hurt > 0 || s.won || s.lost) return;
|
|
65
|
+
s.lives--; s.hurt = 2; s.power = Math.max(1, s.power - 1);
|
|
66
|
+
s.deaths.push({ x: round3(s.x), y: round3(s.y), why, t: round3(s.t) });
|
|
67
|
+
s.bullets = s.bullets.filter((b) => dist(b.x, b.y, s.x, s.y) > 3); // a moment to breathe
|
|
68
|
+
ctx.emit({ type: 'hurt', why, lives: s.lives });
|
|
69
|
+
if (s.lives <= 0) { s.lost = true; ctx.emit({ type: 'lost', why }); }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function aim(s, f, speed, spread = 0) {
|
|
73
|
+
const dx = s.x - f.x, dy = s.y - f.y, l = Math.hypot(dx, dy) || 1, a = Math.atan2(dy, dx) + spread;
|
|
74
|
+
void l;
|
|
75
|
+
return { x: f.x, y: f.y, vx: Math.cos(a) * speed, vy: Math.sin(a) * speed, r: 0.14 };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function step(s, dt, ctx) {
|
|
79
|
+
if (s.won || s.lost) return;
|
|
80
|
+
const cfg = s.cfg, rng = ctx.rng;
|
|
81
|
+
s.t += dt; s.scroll += dt * 2; s.hurt = Math.max(0, s.hurt - dt); s.cool -= dt;
|
|
82
|
+
// the ship
|
|
83
|
+
const sp = cfg.speed * (s.focus ? 0.45 : 1);
|
|
84
|
+
let ix = s.ix, iy = s.iy; const l = Math.hypot(ix, iy); if (l > 1) { ix /= l; iy /= l; }
|
|
85
|
+
s.x = clamp(s.x + ix * sp * dt, 0.4, s.W - 0.4); s.y = clamp(s.y + iy * sp * dt, H * 0.45, H - 0.6);
|
|
86
|
+
if (s.cool <= 0) {
|
|
87
|
+
s.cool = 1 / cfg.fireRate;
|
|
88
|
+
const n = s.power >= 3 ? 3 : s.power === 2 ? 2 : 1;
|
|
89
|
+
for (let k = 0; k < n; k++) { const off = n === 1 ? 0 : (k - (n - 1) / 2) * 0.32; s.shots.push({ x: s.x + off, y: s.y - 0.5, vx: off * 2.5, vy: -18 }); }
|
|
90
|
+
}
|
|
91
|
+
// waves as their time comes; the boss when the level's length is reached and the screen is clear enough
|
|
92
|
+
for (const w of s.waves) if (!w.done && s.t >= w.t) { w.done = true; spawn(s, w, rng); ctx.emit({ type: 'wave', id: w.id }); }
|
|
93
|
+
const b = s.boss;
|
|
94
|
+
if (b && !b.on && s.t >= s.length && s.foes.filter((f) => f.alive).length <= 2) { b.on = true; ctx.emit({ type: 'boss' }); }
|
|
95
|
+
// enemies
|
|
96
|
+
for (const f of s.foes) {
|
|
97
|
+
if (!f.alive) continue;
|
|
98
|
+
f.t += dt;
|
|
99
|
+
if (f.kind === 'sine') f.x = clamp(f.x0 + Math.sin(f.t * 2.2 + f.i) * 2.2, 0.5, s.W - 0.5);
|
|
100
|
+
if (f.kind === 'dive' && f.t > 1.4 && f.t < 1.5) { const d = aim(s, f, 4.5); f.vx = d.vx; f.vy = Math.max(2.5, d.vy); }
|
|
101
|
+
f.x += f.vx * dt; f.y += f.vy * dt;
|
|
102
|
+
if (f.fire && f.y > 0 && f.y < H * 0.6) { f.cool -= dt; if (f.cool <= 0) { f.cool = 1 / f.fire; s.bullets.push(aim(s, f, cfg.bulletSpeed)); } }
|
|
103
|
+
if (f.y > H + 1 || f.x < -2 || f.x > s.W + 2) f.alive = false;
|
|
104
|
+
if (dist(f.x, f.y, s.x, s.y) < 0.45 + cfg.hitbox) { hurt(s, ctx, 'ram'); f.alive = false; }
|
|
105
|
+
}
|
|
106
|
+
if (b && b.on && b.hp > 0) {
|
|
107
|
+
b.y = Math.min(3, b.y + dt * 2); b.x = s.W / 2 + Math.sin(s.t * 0.6) * (s.W / 2 - 1.6); b.phase += dt; b.cool -= dt;
|
|
108
|
+
if (b.cool <= 0 && b.y >= 2.9) {
|
|
109
|
+
const enraged = b.hp < b.max / 2;
|
|
110
|
+
if (b.pattern === 'ring') { const n = enraged ? 14 : 10; for (let k = 0; k < n; k++) { const a = (k / n) * Math.PI * 2 + b.phase; s.bullets.push({ x: b.x, y: b.y + 0.8, vx: Math.cos(a) * cfg.bulletSpeed * 0.8, vy: Math.sin(a) * cfg.bulletSpeed * 0.8, r: 0.16 }); } b.cool = enraged ? 1.1 : 1.5; }
|
|
111
|
+
else if (b.pattern === 'spiral') { for (let k = 0; k < 2; k++) { const a = b.phase * 3 + k * Math.PI; s.bullets.push({ x: b.x, y: b.y + 0.8, vx: Math.cos(a) * cfg.bulletSpeed * 0.7, vy: Math.abs(Math.sin(a)) * cfg.bulletSpeed * 0.7 + 0.8, r: 0.15 }); } b.cool = enraged ? 0.12 : 0.18; }
|
|
112
|
+
else { const n = enraged ? 7 : 5; for (let k = 0; k < n; k++) s.bullets.push(aim(s, { x: b.x, y: b.y + 0.8 }, cfg.bulletSpeed, (k - (n - 1) / 2) * 0.2)); b.cool = enraged ? 0.9 : 1.3; }
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// shots against enemies and the boss
|
|
116
|
+
for (const sh of s.shots) {
|
|
117
|
+
sh.x += sh.vx * dt; sh.y += sh.vy * dt;
|
|
118
|
+
for (const f of s.foes) if (f.alive && Math.abs(f.x - sh.x) < 0.5 && Math.abs(f.y - sh.y) < 0.5) {
|
|
119
|
+
sh.y = -9; f.hp--; ctx.emit({ type: 'strike', x: f.x, y: f.y });
|
|
120
|
+
if (f.hp <= 0) { f.alive = false; s.killed++; s.score += 10 * f.max; ctx.emit({ type: 'defeat', x: f.x, y: f.y }); if (f.drops) s.items.push({ x: f.x, y: f.y, kind: 'P' }); }
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
if (b && b.on && b.hp > 0 && Math.abs(b.x - sh.x) < 1.5 && Math.abs(b.y - sh.y) < 1) {
|
|
124
|
+
sh.y = -9; b.hp--; if (b.hp <= 0) { s.score += 500; ctx.emit({ type: 'defeat', boss: true, x: b.x, y: b.y }); s.won = true; s.score += s.lives * 100; ctx.emit({ type: 'won' }); }
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
s.shots = s.shots.filter((sh) => sh.y > -1);
|
|
128
|
+
for (const bl of s.bullets) { bl.x += bl.vx * dt; bl.y += bl.vy * dt; if (dist(bl.x, bl.y, s.x, s.y) < bl.r + cfg.hitbox) { bl.y = 99; hurt(s, ctx, 'shot'); } }
|
|
129
|
+
s.bullets = s.bullets.filter((bl) => bl.y < H + 1 && bl.y > -2 && bl.x > -1 && bl.x < s.W + 1);
|
|
130
|
+
for (const it of s.items) { it.y += dt * 2; if (dist(it.x, it.y, s.x, s.y) < 0.7) { it.y = 99; s.power = Math.min(3, s.power + 1); ctx.emit({ type: 'power', power: s.power }); } }
|
|
131
|
+
s.items = s.items.filter((it) => it.y < H + 1);
|
|
132
|
+
s.foes = s.foes.filter((f) => f.alive);
|
|
133
|
+
// no boss: the level is won when its length has passed and the last wave is gone
|
|
134
|
+
if (!b && s.t >= s.length && s.waves.every((w) => w.done) && !s.foes.length) { s.won = true; s.score += s.lives * 100; ctx.emit({ type: 'won' }); }
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const ACTIONS = {
|
|
138
|
+
/** Hold a direction (dx, dy from −1 to 1) and focus (slow, for threading between shots). */
|
|
139
|
+
input(s, ctx, dx = 0, dy = 0, focus = false) { s.ix = clamp(Number(dx) || 0, -1, 1); s.iy = clamp(Number(dy) || 0, -1, 1); s.focus = !!focus; },
|
|
140
|
+
stop(s) { s.ix = 0; s.iy = 0; },
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
function value(s) {
|
|
144
|
+
if (s.won) return 1e6 + s.lives * 1000 - s.t;
|
|
145
|
+
if (s.lost) return -1e6;
|
|
146
|
+
const b = s.boss, bossPart = b ? (b.on ? (b.max - b.hp) / b.max : 0) : 0;
|
|
147
|
+
return s.lives * 500 - (s.hurt > 1.9 ? 400 : 0) + s.killed * 4 + bossPart * 600 + s.t * 2;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Where the ship should line up: under the nearest enemy (the lowest that is still above), or the boss. */
|
|
151
|
+
function lineUp(s) {
|
|
152
|
+
const b = s.boss;
|
|
153
|
+
if (b && b.on && b.hp > 0) return b.x;
|
|
154
|
+
const up = s.foes.filter((f) => f.y < s.y - 1 && f.y > -0.5).sort((a, c) => (c.y - a.y) || (Math.abs(a.x - s.x) - Math.abs(c.x - s.x)));
|
|
155
|
+
return up.length ? up[0].x : s.W / 2;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function danger(s, x, y, seconds) {
|
|
159
|
+
let d = 0;
|
|
160
|
+
for (const bl of [...s.bullets, ...s.foes.map((f) => ({ x: f.x, y: f.y, vx: f.vx, vy: f.vy, r: 0.45 }))]) {
|
|
161
|
+
for (let t = 0; t <= seconds; t += 0.1) { const e = dist(bl.x + bl.vx * t, bl.y + bl.vy * t, x, y); if (e < bl.r + s.cfg.hitbox + 0.35) { d += (seconds - t + 0.1) * 10; break; } }
|
|
162
|
+
}
|
|
163
|
+
return d;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export const bots = {
|
|
167
|
+
random(sim, rng, memo) {
|
|
168
|
+
if (!memo.in || rng.chance(0.25)) memo.in = [rng.pick([-1, 0, 1]), rng.pick([-1, 0, 1, 0]), false];
|
|
169
|
+
return { act: 'input', args: memo.in, for: 0.1 };
|
|
170
|
+
},
|
|
171
|
+
greedy(sim) {
|
|
172
|
+
// line up under what is above; step aside from the nearest thing coming at the ship
|
|
173
|
+
const s = sim.state, tx = lineUp(s);
|
|
174
|
+
let dx = clamp((tx - s.x) * 2, -1, 1), dy = 0;
|
|
175
|
+
const near = s.bullets.filter((b) => b.vy > 0 && b.y < s.y && s.y - b.y < 2.5 && Math.abs(b.x - s.x) < 0.7).sort((a, b) => b.y - a.y)[0];
|
|
176
|
+
if (near) dx = s.x > near.x ? 1 : -1;
|
|
177
|
+
return { act: 'input', args: [dx, dy, false], for: 0.1 };
|
|
178
|
+
},
|
|
179
|
+
planner(sim) {
|
|
180
|
+
// each of nine ways held for a moment: the one with the least coming at it, lined up for firing, wins
|
|
181
|
+
const s = sim.state, tx = lineUp(s);
|
|
182
|
+
let best = -Infinity, pick = [0, 0, false];
|
|
183
|
+
for (const dx of [-1, 0, 1]) for (const dy of [-1, 0, 1]) {
|
|
184
|
+
const v = lookAhead(sim, () => sim.act('input', dx, dy, false), 0.3);
|
|
185
|
+
const st = sim.state;
|
|
186
|
+
const nx = clamp(st.x + dx * st.cfg.speed * 0.3, 0.4, st.W - 0.4), ny = clamp(st.y + dy * st.cfg.speed * 0.3, H * 0.45, H - 0.6);
|
|
187
|
+
const score = v - danger(st, nx, ny, 0.8) * 8 - Math.abs(nx - tx) * 6 - Math.abs(ny - (H - 2.2)) * 2;
|
|
188
|
+
if (score > best) { best = score; pick = [dx, dy, false]; }
|
|
189
|
+
}
|
|
190
|
+
return { act: 'input', args: pick, for: 0.1 };
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
export function create({ game, level, seed = 1, hooks = {} }) {
|
|
195
|
+
const cfg = settingsFor(kit.settings, game), h = hooked(hooks, ACTIONS);
|
|
196
|
+
const sim = createSim({
|
|
197
|
+
seed,
|
|
198
|
+
setup: (ctx) => { const s = setup(level, cfg, ctx.rng); hooks.setup?.(s, ctx, level); return s; },
|
|
199
|
+
step: (s, dt, ctx) => { step(s, dt, ctx); hooks.step?.(s, dt, ctx); h.end(s, ctx); },
|
|
200
|
+
actions: h.actions,
|
|
201
|
+
report: (s) => ({
|
|
202
|
+
won: s.won, lost: s.lost, x: round3(s.x), y: round3(s.y), lives: s.lives, power: s.power, killed: s.killed, total: s.total,
|
|
203
|
+
boss: s.boss ? { on: s.boss.on, hp: s.boss.hp, max: s.boss.max } : null, bullets: s.bullets.length, foes: s.foes.length,
|
|
204
|
+
t: round3(s.t), length: s.length, w: s.W, h: H, score: hooks.score ? hooks.score(s) : s.score, deaths: s.deaths.length,
|
|
205
|
+
}),
|
|
206
|
+
});
|
|
207
|
+
sim.kit = kit; sim.bots = bots;
|
|
208
|
+
sim.value = () => (hooks.value ? hooks.value(sim.state) : value(sim.state));
|
|
209
|
+
return sim;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// ─── levels ──────────────────────────────────────────────────────────────────
|
|
213
|
+
|
|
214
|
+
/** Waves over `length` seconds, thicker and firing more as `hard` rises, and a boss. → a level (without id) */
|
|
215
|
+
export function shmupLevel(rng, { length = 30, density = 1, fire = 0.3, bossHp = 50, pattern } = {}) {
|
|
216
|
+
const kinds = ['line', 'vee', 'sine', 'dive', 'column'];
|
|
217
|
+
const waves = [];
|
|
218
|
+
for (let t = 1; t < length - 2; t += rng.range(3.2, 5) / density) {
|
|
219
|
+
const kind = waves.length < 2 ? (waves.length ? 'vee' : 'line') : rng.pick(kinds);
|
|
220
|
+
const late = t / length;
|
|
221
|
+
waves.push({ t: Math.round(t * 10) / 10, kind, n: Math.round(clamp(4 + late * 3 * density + rng.int(0, 2), 3, 9)), x: Math.round(rng.range(0.25, 0.75) * 100) / 100, hp: late > 0.6 && rng.chance(0.4) ? 2 : 1, fire: waves.length < 2 ? 0 : Math.round(fire * (0.5 + late) * 100) / 100 });
|
|
222
|
+
}
|
|
223
|
+
return { length, waves, boss: { hp: bossHp, pattern: pattern ?? rng.pick(['ring', 'fan', 'spiral']) } };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Easier versions of a level, the smallest change first (for play()'s fixes). */
|
|
227
|
+
export function easier(lv) {
|
|
228
|
+
const out = [];
|
|
229
|
+
if (lv.boss) out.push({ change: { boss: { ...lv.boss, hp: Math.max(10, Math.round(lv.boss.hp * 0.7)) } }, say: `boss ${lv.boss.hp} → ${Math.max(10, Math.round(lv.boss.hp * 0.7))} hits` });
|
|
230
|
+
if (lv.waves?.some((w) => w.fire)) out.push({ change: { waves: lv.waves.map((w) => ({ ...w, fire: Math.round((w.fire ?? 0) * 50) / 100 })) }, say: 'enemies firing half as often' });
|
|
231
|
+
if (lv.waves?.length) out.push({ change: { waves: lv.waves.map((w) => ({ ...w, fire: 0 })), ...(lv.boss ? { boss: { ...lv.boss, hp: Math.max(10, Math.round(lv.boss.hp * 0.5)) } } : {}) }, say: 'enemies that do not fire and a boss of half the hits' });
|
|
232
|
+
return out;
|
|
233
|
+
}
|
|
234
|
+
export function harder(lv) {
|
|
235
|
+
const out = [];
|
|
236
|
+
if (lv.waves?.length) out.push({ change: { waves: lv.waves.map((w, i) => ({ ...w, fire: Math.round(((w.fire ?? 0) * 1.5 + (i > 1 ? 0.1 : 0)) * 100) / 100 })) }, say: 'enemies firing more' });
|
|
237
|
+
if (lv.boss) out.push({ change: { boss: { ...lv.boss, hp: Math.round(lv.boss.hp * 1.3) } }, say: `boss ${lv.boss.hp} → ${Math.round(lv.boss.hp * 1.3)} hits` });
|
|
238
|
+
return out;
|
|
239
|
+
}
|