@vibechain/os 0.2.1 → 0.4.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/AGENTS.md +11 -5
- package/README.md +76 -5
- package/SECURITY.md +22 -3
- package/dist/app-runtime.d.ts +1 -1
- package/dist/app-runtime.js +11 -3
- package/dist/bundle.js +10 -0
- package/dist/canvas.d.ts +40 -0
- package/dist/canvas.js +41 -0
- package/dist/listing.d.ts +35 -0
- package/dist/listing.js +56 -0
- package/dist/loop.d.ts +34 -0
- package/dist/loop.js +47 -0
- package/dist/manifest.d.ts +70 -1
- package/dist/manifest.js +117 -9
- package/dist/preview-ui.js +1 -1
- package/dist/scaffold.js +10 -3
- package/dist/testing.d.ts +29 -0
- package/dist/testing.js +49 -0
- package/examples/paw-beat/README.md +50 -0
- package/examples/paw-beat/cat.ts +129 -0
- package/examples/paw-beat/game.ts +151 -0
- package/examples/paw-beat/index.html +65 -0
- package/examples/paw-beat/main.ts +243 -0
- package/examples/paw-beat/music.ts +123 -0
- package/examples/paw-beat/store/icon.png +0 -0
- package/examples/paw-beat/store/screenshot-1.webp +0 -0
- package/examples/paw-beat/store/screenshot-2.webp +0 -0
- package/examples/paw-beat/vibemarket.json +21 -0
- package/examples/tsconfig.json +2 -2
- package/package.json +17 -1
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
6
|
+
<title>Paw Beat</title>
|
|
7
|
+
<style>
|
|
8
|
+
html, body { margin: 0; height: 100%; overflow: hidden; background: #150f24; color: #fff6ea; font: 16px/1.4 system-ui, sans-serif; }
|
|
9
|
+
canvas { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
|
|
10
|
+
.hud { position: fixed; left: 0; right: 0; top: 0; display: flex; gap: 24px; padding: 12px 16px; pointer-events: none; font-weight: 700; text-shadow: 0 1px 2px #000a; }
|
|
11
|
+
.hud span { font-variant-numeric: tabular-nums; font-size: 22px; }
|
|
12
|
+
.hud small { display: block; font-weight: 500; opacity: .7; letter-spacing: .08em; font-size: 11px; }
|
|
13
|
+
#call { position: fixed; left: 0; right: 0; top: 22%; text-align: center; font: 800 40px system-ui, sans-serif; letter-spacing: .04em; pointer-events: none; opacity: 0; text-shadow: 0 2px 8px #000c; }
|
|
14
|
+
#call.show { animation: pop .45s ease-out; }
|
|
15
|
+
#call[data-kind="perfect"] { color: #ffd166; }
|
|
16
|
+
#call[data-kind="great"] { color: #7bd88f; }
|
|
17
|
+
#call[data-kind="good"] { color: #4cc9f0; }
|
|
18
|
+
#call[data-kind="miss"] { color: #ff5c8a; }
|
|
19
|
+
@keyframes pop { 0% { opacity: 1; transform: scale(1.25); } 100% { opacity: 0; transform: scale(1); } }
|
|
20
|
+
@media (prefers-reduced-motion: reduce) { #call.show { animation: none; opacity: 1; } }
|
|
21
|
+
.pads { position: fixed; left: 0; right: 0; bottom: 0; display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; padding: 10px; }
|
|
22
|
+
.pads button { margin: 0; padding: 14px 0; border-radius: 14px; border: 2px solid #ffffff38; background: #ffffff14; color: #fff; font: 700 24px system-ui, sans-serif; touch-action: none; }
|
|
23
|
+
.pads button:active, .pads button.down { background: #ffffff40; }
|
|
24
|
+
.card { position: fixed; inset: 0; display: grid; place-items: center; background: #150f24e0; text-align: center; padding: 24px; }
|
|
25
|
+
.card[hidden], .hud[hidden], .pads[hidden] { display: none; }
|
|
26
|
+
.card h1 { font-size: 46px; margin: 0 0 8px; }
|
|
27
|
+
.card p { max-width: 440px; margin: 8px auto; opacity: .85; }
|
|
28
|
+
.card button { margin-top: 16px; border: 0; border-radius: 999px; padding: 14px 30px; font: 700 18px system-ui, sans-serif; background: #ffd166; color: #150f24; cursor: pointer; }
|
|
29
|
+
button:disabled { opacity: .5; cursor: default; }
|
|
30
|
+
button:focus-visible { outline: 3px solid #fff; outline-offset: 3px; }
|
|
31
|
+
kbd { padding: 1px 6px; border-radius: 5px; background: #ffffff26; font: inherit; }
|
|
32
|
+
</style>
|
|
33
|
+
</head>
|
|
34
|
+
<body>
|
|
35
|
+
<canvas id="scene" aria-label="Biscuit the cat dancing on a stage, with step arrows moving toward four targets."></canvas>
|
|
36
|
+
<div class="hud" id="hud" hidden>
|
|
37
|
+
<div><small>SCORE</small><span id="score">0</span></div>
|
|
38
|
+
<div><small>COMBO</small><span id="combo">0</span></div>
|
|
39
|
+
<div><small>TIME</small><span id="time">58</span></div>
|
|
40
|
+
</div>
|
|
41
|
+
<div id="call" role="status" aria-live="polite"></div>
|
|
42
|
+
<div class="pads" id="pads" hidden>
|
|
43
|
+
<button type="button" data-lane="left" aria-label="Left step">←</button>
|
|
44
|
+
<button type="button" data-lane="down" aria-label="Down step">↓</button>
|
|
45
|
+
<button type="button" data-lane="up" aria-label="Up step">↑</button>
|
|
46
|
+
<button type="button" data-lane="right" aria-label="Right step">→</button>
|
|
47
|
+
</div>
|
|
48
|
+
<section class="card" id="intro">
|
|
49
|
+
<div>
|
|
50
|
+
<h1>Paw Beat</h1>
|
|
51
|
+
<p>Arrows slide toward the targets. Press the matching key as each one lands: <kbd>←</kbd> <kbd>↓</kbd> <kbd>↑</kbd> <kbd>→</kbd> or <kbd>A</kbd> <kbd>S</kbd> <kbd>W</kbd> <kbd>D</kbd>, or tap the pads. Biscuit dances every step you hit.</p>
|
|
52
|
+
<p id="best"></p>
|
|
53
|
+
<button id="start" type="button" disabled>Connecting…</button>
|
|
54
|
+
</div>
|
|
55
|
+
</section>
|
|
56
|
+
<section class="card" id="done" hidden>
|
|
57
|
+
<div>
|
|
58
|
+
<h1 id="done-score">0</h1>
|
|
59
|
+
<p id="done-note"></p>
|
|
60
|
+
<button id="again" type="button">Dance again</button>
|
|
61
|
+
</div>
|
|
62
|
+
</section>
|
|
63
|
+
<script type="module" src="./game.js"></script>
|
|
64
|
+
</body>
|
|
65
|
+
</html>
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import * as THREE from "three";
|
|
2
|
+
import { connectVibeMarketApp, type AppValue, type VibeMarketAppSdk } from "@vibechain/os";
|
|
3
|
+
import { createLoop } from "@vibechain/os/loop";
|
|
4
|
+
import { fitCanvas } from "@vibechain/os/canvas";
|
|
5
|
+
import { createCat } from "./cat";
|
|
6
|
+
import { BEAT_SECONDS, createGame, LANES, SONG_SECONDS, type DanceGame, type Judgement, type Lane } from "./game";
|
|
7
|
+
import { createMusic } from "./music";
|
|
8
|
+
|
|
9
|
+
function element<T extends HTMLElement>(id: string): T {
|
|
10
|
+
const node = document.getElementById(id);
|
|
11
|
+
if (!node) throw Error(`Missing element: ${id}`);
|
|
12
|
+
return node as T;
|
|
13
|
+
}
|
|
14
|
+
const canvas = element<HTMLCanvasElement>("scene"), hud = element("hud"), pads = element("pads"), call = element("call");
|
|
15
|
+
const intro = element("intro"), done = element("done"), bestLine = element("best");
|
|
16
|
+
const startButton = element<HTMLButtonElement>("start"), againButton = element<HTMLButtonElement>("again");
|
|
17
|
+
const scoreText = element("score"), comboText = element("combo"), timeText = element("time");
|
|
18
|
+
|
|
19
|
+
// ---- Stage layout. Two lanes run past each side of the cat toward the targets. ----
|
|
20
|
+
const LANE_X: Readonly<Record<Lane, number>> = { left: -3.1, down: -1.75, up: 1.75, right: 3.1 };
|
|
21
|
+
const LANE_COLOR: Readonly<Record<Lane, string>> = { left: "#ff5c8a", down: "#4cc9f0", up: "#ffd166", right: "#7bd88f" };
|
|
22
|
+
/** Which way each arrow points when lying on the floor (rotation about the vertical axis). */
|
|
23
|
+
const LANE_TURN: Readonly<Record<Lane, number>> = { up: 0, left: Math.PI / 2, down: Math.PI, right: -Math.PI / 2 };
|
|
24
|
+
const HIT_Z = 1.6, SPEED = 7.5, LOOKAHEAD = 2.2;
|
|
25
|
+
|
|
26
|
+
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true, powerPreference: "low-power" });
|
|
27
|
+
renderer.setPixelRatio(1); // fitCanvas already sizes the backing store in device pixels.
|
|
28
|
+
const scene = new THREE.Scene();
|
|
29
|
+
scene.background = new THREE.Color("#150f24");
|
|
30
|
+
scene.fog = new THREE.Fog("#150f24", 12, 24);
|
|
31
|
+
const camera = new THREE.PerspectiveCamera(40, 1, 0.1, 60);
|
|
32
|
+
scene.add(new THREE.HemisphereLight("#fff1dc", "#2a1d44", 1.2));
|
|
33
|
+
const spot = new THREE.DirectionalLight("#ffe2b8", 1.5);
|
|
34
|
+
spot.position.set(2, 6, 5);
|
|
35
|
+
scene.add(spot);
|
|
36
|
+
|
|
37
|
+
const floor = new THREE.Mesh(new THREE.PlaneGeometry(40, 60), new THREE.MeshStandardMaterial({ color: "#1d1533", roughness: 1 }));
|
|
38
|
+
floor.rotation.x = -Math.PI / 2;
|
|
39
|
+
floor.position.z = -12;
|
|
40
|
+
scene.add(floor);
|
|
41
|
+
const stageGlow = new THREE.MeshStandardMaterial({ color: "#3a2a66", emissive: "#8f6bff", emissiveIntensity: 0.2, roughness: 0.8 });
|
|
42
|
+
const stage = new THREE.Mesh(new THREE.CylinderGeometry(1.25, 1.35, 0.18, 40), stageGlow);
|
|
43
|
+
stage.position.y = 0.09;
|
|
44
|
+
scene.add(stage);
|
|
45
|
+
|
|
46
|
+
// One flat arrow shape, reused for every note and target.
|
|
47
|
+
const arrowShape = new THREE.Shape();
|
|
48
|
+
for (const [x, y] of [[0, 0.55], [0.5, 0.02], [0.2, 0.02], [0.2, -0.5], [-0.2, -0.5], [-0.2, 0.02], [-0.5, 0.02]] as const) {
|
|
49
|
+
if (x === 0 && y === 0.55) arrowShape.moveTo(x, y);
|
|
50
|
+
else arrowShape.lineTo(x, y);
|
|
51
|
+
}
|
|
52
|
+
const arrowGeometry = new THREE.ExtrudeGeometry(arrowShape, { depth: 0.1, bevelEnabled: false });
|
|
53
|
+
arrowGeometry.rotateX(-Math.PI / 2); // lie flat, pointing away from the camera
|
|
54
|
+
|
|
55
|
+
const targets = {} as Record<Lane, THREE.MeshStandardMaterial>;
|
|
56
|
+
const noteMaterial = {} as Record<Lane, THREE.MeshStandardMaterial>;
|
|
57
|
+
for (const lane of LANES) {
|
|
58
|
+
const strip = new THREE.Mesh(new THREE.PlaneGeometry(1.15, 30), new THREE.MeshBasicMaterial({ color: LANE_COLOR[lane], transparent: true, opacity: 0.07 }));
|
|
59
|
+
strip.rotation.x = -Math.PI / 2;
|
|
60
|
+
strip.position.set(LANE_X[lane], 0.01, HIT_Z - 14);
|
|
61
|
+
scene.add(strip);
|
|
62
|
+
targets[lane] = new THREE.MeshStandardMaterial({ color: "#2b2147", emissive: LANE_COLOR[lane], emissiveIntensity: 0.25, roughness: 0.6, side: THREE.DoubleSide });
|
|
63
|
+
const target = new THREE.Mesh(arrowGeometry, targets[lane]);
|
|
64
|
+
target.position.set(LANE_X[lane], 0.02, HIT_Z);
|
|
65
|
+
target.rotation.y = LANE_TURN[lane];
|
|
66
|
+
target.scale.setScalar(1.12);
|
|
67
|
+
scene.add(target);
|
|
68
|
+
noteMaterial[lane] = new THREE.MeshStandardMaterial({ color: LANE_COLOR[lane], emissive: LANE_COLOR[lane], emissiveIntensity: 0.9, roughness: 0.4, side: THREE.DoubleSide });
|
|
69
|
+
}
|
|
70
|
+
// A fixed pool is enough: only a couple of seconds of notes are ever on screen.
|
|
71
|
+
const pool = Array.from({ length: 28 }, () => {
|
|
72
|
+
const mesh = new THREE.Mesh(arrowGeometry, noteMaterial.left);
|
|
73
|
+
mesh.scale.setScalar(1.2);
|
|
74
|
+
mesh.visible = false;
|
|
75
|
+
scene.add(mesh);
|
|
76
|
+
return mesh;
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const cat = createCat();
|
|
80
|
+
cat.root.position.y = 0.18;
|
|
81
|
+
scene.add(cat.root);
|
|
82
|
+
|
|
83
|
+
// ---- Round state ----
|
|
84
|
+
interface Saved { best: number; rounds: number }
|
|
85
|
+
function parseSaved(value: AppValue): Saved {
|
|
86
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return { best: 0, rounds: 0 };
|
|
87
|
+
const count = (input: AppValue | undefined) => (typeof input === "number" && Number.isFinite(input) && input >= 0 ? Math.floor(input) : 0);
|
|
88
|
+
return { best: count(value.best), rounds: count(value.rounds) };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let app: VibeMarketAppSdk | undefined;
|
|
92
|
+
let game: DanceGame = createGame();
|
|
93
|
+
let saved: Saved = { best: 0, rounds: 0 };
|
|
94
|
+
let running = false, silentClock = 0, shownScore = -1, shownCombo = -1, shownTime = -1;
|
|
95
|
+
const flash: Record<Lane, number> = { left: 0, down: 0, up: 0, right: 0 };
|
|
96
|
+
const music = createMusic();
|
|
97
|
+
|
|
98
|
+
function announce(kind: Judgement) {
|
|
99
|
+
call.dataset.kind = kind;
|
|
100
|
+
call.textContent = kind === "miss" ? "Miss" : kind === "perfect" ? "Perfect!" : kind === "great" ? "Great" : "Good";
|
|
101
|
+
call.classList.remove("show");
|
|
102
|
+
void call.offsetWidth; // restart the CSS animation
|
|
103
|
+
call.classList.add("show");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function press(lane: Lane) {
|
|
107
|
+
if (!running) return;
|
|
108
|
+
flash[lane] = 1;
|
|
109
|
+
const hit = game.press(lane);
|
|
110
|
+
if (!hit?.judged) return; // a stray press costs nothing
|
|
111
|
+
cat.dance(lane);
|
|
112
|
+
announce(hit.judged);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function begin() {
|
|
116
|
+
const seed = saved.rounds + 1;
|
|
117
|
+
game = createGame(seed);
|
|
118
|
+
silentClock = 0;
|
|
119
|
+
music.start(seed);
|
|
120
|
+
running = true;
|
|
121
|
+
intro.hidden = done.hidden = true;
|
|
122
|
+
hud.hidden = pads.hidden = false;
|
|
123
|
+
canvas.focus();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function end() {
|
|
127
|
+
running = false;
|
|
128
|
+
music.stop();
|
|
129
|
+
const { score, bestCombo, counts } = game.state;
|
|
130
|
+
saved = { best: Math.max(saved.best, score), rounds: saved.rounds + 1 };
|
|
131
|
+
// A failed write only loses the personal best; the result still goes to the host.
|
|
132
|
+
void app?.checkpoint.write({ best: saved.best, rounds: saved.rounds }).catch(() => {});
|
|
133
|
+
hud.hidden = pads.hidden = true;
|
|
134
|
+
element("done-score").textContent = String(score);
|
|
135
|
+
element("done-note").textContent = `${counts.perfect} perfect · ${counts.great} great · ${counts.good} good · ${counts.miss} missed. Best run of steps: ${bestCombo}.`;
|
|
136
|
+
done.hidden = false;
|
|
137
|
+
// The host owns the results screen and normally replaces this frame. If it declines
|
|
138
|
+
// (it accepts one score per open window), the card above stays as the result.
|
|
139
|
+
void app?.game.finish({ score }).catch(() => {});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// ---- Input: arrow keys, WASD, or the on-screen pads. ----
|
|
143
|
+
const KEY_LANE: Readonly<Record<string, Lane>> = {
|
|
144
|
+
ArrowLeft: "left", ArrowDown: "down", ArrowUp: "up", ArrowRight: "right", KeyA: "left", KeyS: "down", KeyW: "up", KeyD: "right",
|
|
145
|
+
};
|
|
146
|
+
window.addEventListener("keydown", event => {
|
|
147
|
+
const lane = KEY_LANE[event.code];
|
|
148
|
+
if (!lane) return;
|
|
149
|
+
event.preventDefault(); // arrow keys must not scroll the frame
|
|
150
|
+
if (!event.repeat) press(lane);
|
|
151
|
+
});
|
|
152
|
+
for (const pad of Array.from(pads.querySelectorAll<HTMLButtonElement>("button[data-lane]"))) {
|
|
153
|
+
const lane = pad.dataset.lane as Lane;
|
|
154
|
+
pad.addEventListener("pointerdown", event => {
|
|
155
|
+
event.preventDefault();
|
|
156
|
+
press(lane);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function drawHud(score: number, combo: number, secondsLeft: number) {
|
|
161
|
+
// Touch the DOM only when a number actually changed.
|
|
162
|
+
if (score !== shownScore) scoreText.textContent = String((shownScore = score));
|
|
163
|
+
if (combo !== shownCombo) comboText.textContent = String((shownCombo = combo));
|
|
164
|
+
if (secondsLeft !== shownTime) timeText.textContent = String((shownTime = secondsLeft));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// ---- Boot: connect, restore, then let the host know the first frame is up. ----
|
|
168
|
+
async function boot() {
|
|
169
|
+
try {
|
|
170
|
+
app = await connectVibeMarketApp();
|
|
171
|
+
} catch {
|
|
172
|
+
startButton.textContent = "Open in VibeMarket OS";
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
const host = app;
|
|
176
|
+
fitCanvas(host, canvas, {
|
|
177
|
+
onResize({ width, height }) {
|
|
178
|
+
renderer.setSize(width, height, false);
|
|
179
|
+
camera.aspect = width / height;
|
|
180
|
+
// Pull back on tall, narrow windows so all four lanes stay in frame.
|
|
181
|
+
const pull = Math.max(1, 1.25 / camera.aspect);
|
|
182
|
+
camera.position.set(0, 5.4 * pull, 10.5 * pull);
|
|
183
|
+
camera.lookAt(0, 0.9, -1.5);
|
|
184
|
+
camera.updateProjectionMatrix();
|
|
185
|
+
},
|
|
186
|
+
onContextLost: () => { running = false; },
|
|
187
|
+
});
|
|
188
|
+
createLoop(host, {
|
|
189
|
+
update(stepMs) {
|
|
190
|
+
const seconds = stepMs / 1000;
|
|
191
|
+
silentClock += running ? seconds : 0;
|
|
192
|
+
// The audio clock is the truth while there is sound; otherwise the loop keeps time.
|
|
193
|
+
const now = running ? music.time() ?? silentClock : 0;
|
|
194
|
+
if (running) {
|
|
195
|
+
music.pump();
|
|
196
|
+
if (game.advance(now).length > 0) {
|
|
197
|
+
cat.stumble();
|
|
198
|
+
announce("miss");
|
|
199
|
+
}
|
|
200
|
+
const state = game.state;
|
|
201
|
+
drawHud(state.score, state.combo, Math.max(0, Math.ceil(SONG_SECONDS - state.time)));
|
|
202
|
+
if (state.finished) end();
|
|
203
|
+
}
|
|
204
|
+
const beatPhase = ((now / BEAT_SECONDS) % 1 + 1) % 1, pulse = Math.pow(1 - beatPhase, 3);
|
|
205
|
+
cat.animate(seconds, beatPhase);
|
|
206
|
+
stageGlow.emissiveIntensity = 0.2 + pulse * 0.7;
|
|
207
|
+
for (const lane of LANES) {
|
|
208
|
+
flash[lane] = Math.max(0, flash[lane] - seconds * 5);
|
|
209
|
+
targets[lane].emissiveIntensity = 0.25 + pulse * 0.2 + flash[lane] * 1.4;
|
|
210
|
+
}
|
|
211
|
+
// Lay out the notes that are on screen right now.
|
|
212
|
+
let used = 0;
|
|
213
|
+
if (running) {
|
|
214
|
+
game.each(now - 0.35, now + LOOKAHEAD, (note, judged) => {
|
|
215
|
+
// A step that was hit disappears; a missed one slides on past the target.
|
|
216
|
+
const mesh = judged && judged !== "miss" ? undefined : pool[used];
|
|
217
|
+
if (!mesh) return;
|
|
218
|
+
used += 1;
|
|
219
|
+
mesh.material = noteMaterial[note.lane];
|
|
220
|
+
mesh.position.set(LANE_X[note.lane], 0.06, HIT_Z - (note.time - now) * SPEED);
|
|
221
|
+
mesh.rotation.y = LANE_TURN[note.lane];
|
|
222
|
+
mesh.visible = true;
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
for (let index = used; index < pool.length; index++) pool[index]!.visible = false;
|
|
226
|
+
},
|
|
227
|
+
render() {
|
|
228
|
+
renderer.render(scene, camera);
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
host.lifecycle.onChanged(state => music.setActive(state.active));
|
|
232
|
+
|
|
233
|
+
saved = parseSaved(await host.checkpoint.read().catch(() => null));
|
|
234
|
+
bestLine.textContent = saved.rounds > 0 ? `Best: ${saved.best} over ${saved.rounds} song${saved.rounds === 1 ? "" : "s"}.` : "One song is about a minute.";
|
|
235
|
+
startButton.textContent = "Start dancing";
|
|
236
|
+
startButton.disabled = false;
|
|
237
|
+
startButton.addEventListener("click", begin);
|
|
238
|
+
againButton.addEventListener("click", begin);
|
|
239
|
+
await host.ready();
|
|
240
|
+
window.addEventListener("pagehide", () => { music.dispose(); cat.dispose(); host.destroy(); }, { once: true });
|
|
241
|
+
}
|
|
242
|
+
canvas.tabIndex = 0;
|
|
243
|
+
void boot();
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { BEAT_SECONDS, COUNT_IN_BEATS, SONG_BEATS } from "./game";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The song, synthesized with Web Audio. Nothing is downloaded or sampled: a kick, a
|
|
5
|
+
* clap, a hat, a bass line and a lead are built from oscillators and noise, scheduled a
|
|
6
|
+
* little ahead of the audio clock. That same clock times the steps, so what you hear
|
|
7
|
+
* and what you hit stay together.
|
|
8
|
+
*/
|
|
9
|
+
export interface Music {
|
|
10
|
+
/** Start the song from the top. Call from a user gesture. */
|
|
11
|
+
start(seed: number): void;
|
|
12
|
+
/** Seconds into the song by the audio clock, or null when there is no audio. */
|
|
13
|
+
time(): number | null;
|
|
14
|
+
/** Schedule the next few beats. Call every frame while the song plays. */
|
|
15
|
+
pump(): void;
|
|
16
|
+
/** Pause or resume with the app's lifecycle. */
|
|
17
|
+
setActive(active: boolean): void;
|
|
18
|
+
stop(): void;
|
|
19
|
+
dispose(): void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// A minor, F, C, G: one chord per four-beat bar. Bass roots in Hz.
|
|
23
|
+
const ROOTS = [55, 43.65, 65.41, 49];
|
|
24
|
+
// A minor pentatonic across two octaves, for the lead.
|
|
25
|
+
const SCALE = [220, 261.63, 293.66, 329.63, 392, 440, 523.25, 587.33, 659.25];
|
|
26
|
+
const LOOKAHEAD = 0.4;
|
|
27
|
+
|
|
28
|
+
export function createMusic(): Music {
|
|
29
|
+
let context: AudioContext | undefined, master: GainNode | undefined, noise: AudioBuffer | undefined;
|
|
30
|
+
let startAt = 0, nextBeat = 0, playing = false, melody: number[] = [];
|
|
31
|
+
|
|
32
|
+
const tone = (type: OscillatorType, from: number, to: number, at: number, length: number, volume: number) => {
|
|
33
|
+
if (!context || !master) return;
|
|
34
|
+
const voice = context.createOscillator(), envelope = context.createGain();
|
|
35
|
+
voice.type = type;
|
|
36
|
+
voice.frequency.setValueAtTime(from, at);
|
|
37
|
+
if (to !== from) voice.frequency.exponentialRampToValueAtTime(to, at + length);
|
|
38
|
+
envelope.gain.setValueAtTime(volume, at);
|
|
39
|
+
envelope.gain.exponentialRampToValueAtTime(0.0001, at + length);
|
|
40
|
+
voice.connect(envelope).connect(master);
|
|
41
|
+
voice.start(at);
|
|
42
|
+
voice.stop(at + length + 0.02);
|
|
43
|
+
};
|
|
44
|
+
const hiss = (filter: BiquadFilterType, frequency: number, at: number, length: number, volume: number) => {
|
|
45
|
+
if (!context || !master || !noise) return;
|
|
46
|
+
const source = context.createBufferSource(), shape = context.createBiquadFilter(), envelope = context.createGain();
|
|
47
|
+
source.buffer = noise;
|
|
48
|
+
shape.type = filter;
|
|
49
|
+
shape.frequency.value = frequency;
|
|
50
|
+
envelope.gain.setValueAtTime(volume, at);
|
|
51
|
+
envelope.gain.exponentialRampToValueAtTime(0.0001, at + length);
|
|
52
|
+
source.connect(shape).connect(envelope).connect(master);
|
|
53
|
+
source.start(at);
|
|
54
|
+
source.stop(at + length + 0.02);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const beat = (index: number) => {
|
|
58
|
+
const at = startAt + index * BEAT_SECONDS, half = at + BEAT_SECONDS / 2;
|
|
59
|
+
if (index < COUNT_IN_BEATS) {
|
|
60
|
+
tone("square", 1320, 1320, at, 0.05, 0.12); // count-in tick
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const inBar = index % 4, root = ROOTS[Math.floor(index / 4) % ROOTS.length] ?? 55;
|
|
64
|
+
tone("sine", 150, 45, at, 0.14, 0.7); // kick
|
|
65
|
+
if (inBar === 1 || inBar === 3) hiss("bandpass", 1800, at, 0.12, 0.35); // clap
|
|
66
|
+
hiss("highpass", 7000, half, 0.04, 0.16); // off-beat hat
|
|
67
|
+
tone("triangle", root, root, at, 0.2, 0.4); // bass
|
|
68
|
+
tone("triangle", root * 2, root * 2, half, 0.14, 0.26);
|
|
69
|
+
// Two lead notes per beat; a zero in the pattern is a rest.
|
|
70
|
+
for (const [slot, when] of [[index * 2, at], [index * 2 + 1, half]] as const) {
|
|
71
|
+
const pitch = melody[slot % melody.length] ?? 0;
|
|
72
|
+
if (pitch > 0) tone("square", pitch, pitch, when, 0.16, 0.07);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
start(seed) {
|
|
78
|
+
try {
|
|
79
|
+
context ??= new AudioContext();
|
|
80
|
+
} catch {
|
|
81
|
+
return; // No audio: the game falls back to its own clock and plays silent.
|
|
82
|
+
}
|
|
83
|
+
void context.resume().catch(() => {});
|
|
84
|
+
if (!master) {
|
|
85
|
+
master = context.createGain();
|
|
86
|
+
master.gain.value = 0.35;
|
|
87
|
+
master.connect(context.destination);
|
|
88
|
+
noise = context.createBuffer(1, context.sampleRate / 2, context.sampleRate);
|
|
89
|
+
const samples = noise.getChannelData(0);
|
|
90
|
+
for (let i = 0; i < samples.length; i++) samples[i] = Math.random() * 2 - 1;
|
|
91
|
+
}
|
|
92
|
+
// A 32-step lead pattern that depends on the seed, so each round sounds a little different.
|
|
93
|
+
let state = (seed >>> 0) || 1;
|
|
94
|
+
melody = Array.from({ length: 32 }, () => {
|
|
95
|
+
state = (Math.imul(state, 1664525) + 1013904223) >>> 0;
|
|
96
|
+
return state % 5 < 2 ? 0 : SCALE[state % SCALE.length] ?? 0;
|
|
97
|
+
});
|
|
98
|
+
startAt = context.currentTime + 0.12;
|
|
99
|
+
nextBeat = 0;
|
|
100
|
+
playing = true;
|
|
101
|
+
},
|
|
102
|
+
time() {
|
|
103
|
+
return context && playing && context.state === "running" ? context.currentTime - startAt : null;
|
|
104
|
+
},
|
|
105
|
+
pump() {
|
|
106
|
+
if (!context || !playing) return;
|
|
107
|
+
while (nextBeat < SONG_BEATS && startAt + nextBeat * BEAT_SECONDS < context.currentTime + LOOKAHEAD) beat(nextBeat++);
|
|
108
|
+
},
|
|
109
|
+
setActive(active) {
|
|
110
|
+
if (!context) return;
|
|
111
|
+
void (active ? context.resume() : context.suspend()).catch(() => {});
|
|
112
|
+
},
|
|
113
|
+
stop() {
|
|
114
|
+
playing = false;
|
|
115
|
+
},
|
|
116
|
+
dispose() {
|
|
117
|
+
playing = false;
|
|
118
|
+
void context?.close().catch(() => {});
|
|
119
|
+
context = undefined;
|
|
120
|
+
master = undefined;
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schemaVersion": 2,
|
|
3
|
+
"sdkVersion": 1,
|
|
4
|
+
"id": "paw-beat",
|
|
5
|
+
"version": "1.0.0",
|
|
6
|
+
"name": "Paw Beat",
|
|
7
|
+
"tagline": "A 3D cat, four arrows and a beat. Hit the steps, keep the combo.",
|
|
8
|
+
"category": "vibexp-app",
|
|
9
|
+
"entry": "index.html",
|
|
10
|
+
"permissions": [],
|
|
11
|
+
"collectionId": null,
|
|
12
|
+
"window": { "width": 960, "height": 600, "resizable": true },
|
|
13
|
+
"store": {
|
|
14
|
+
"icon": "store/icon.png",
|
|
15
|
+
"screenshots": ["store/screenshot-1.webp", "store/screenshot-2.webp"],
|
|
16
|
+
"description": "Biscuit the cat dances to a one-minute song made entirely in code. Arrows slide down four lanes toward their targets: press the matching arrow key or WASD, or tap the on-screen pads, as each one lands, and Biscuit dances the step.\n\nTiming is judged perfect, great or good, and every ten steps in a row adds 10% to each step, up to double. The steps change a little every round. Your best score is kept while the window stays open.",
|
|
17
|
+
"category": "game",
|
|
18
|
+
"rating": { "violence": "none", "language": "none", "mature": false },
|
|
19
|
+
"support": "https://vibechain.com/os/docs"
|
|
20
|
+
}
|
|
21
|
+
}
|
package/examples/tsconfig.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
{
|
|
2
|
-
"compilerOptions": { "target": "ES2022", "module": "ESNext", "moduleResolution": "Bundler", "lib": ["ES2022", "DOM"], "strict": true, "skipLibCheck": true, "noEmit": true, "allowImportingTsExtensions": true, "paths": { "@vibechain/os": ["../src/index.ts"] } },
|
|
3
|
-
"include": ["collection-room/main.ts", "chonk-stack/*.ts", "vibexp-app/src/*.ts", "../tests/**/*.ts", "../vitest.config.ts"]
|
|
2
|
+
"compilerOptions": { "target": "ES2022", "module": "ESNext", "moduleResolution": "Bundler", "lib": ["ES2022", "DOM"], "strict": true, "skipLibCheck": true, "noEmit": true, "allowImportingTsExtensions": true, "paths": { "@vibechain/os": ["../src/index.ts"], "@vibechain/os/loop": ["../src/loop.ts"], "@vibechain/os/canvas": ["../src/canvas.ts"], "@vibechain/os/testing": ["../src/testing.ts"] } },
|
|
3
|
+
"include": ["../tools/*.ts", "../tools/*.mts", "collection-room/main.ts", "chonk-stack/*.ts", "paw-beat/*.ts", "vibexp-app/src/*.ts", "../tests/**/*.ts", "../vitest.config.ts"]
|
|
4
4
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vibechain/os",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
@@ -28,6 +28,22 @@
|
|
|
28
28
|
"./manifest": {
|
|
29
29
|
"types": "./dist/manifest.d.ts",
|
|
30
30
|
"import": "./dist/manifest.js"
|
|
31
|
+
},
|
|
32
|
+
"./listing": {
|
|
33
|
+
"types": "./dist/listing.d.ts",
|
|
34
|
+
"import": "./dist/listing.js"
|
|
35
|
+
},
|
|
36
|
+
"./loop": {
|
|
37
|
+
"types": "./dist/loop.d.ts",
|
|
38
|
+
"import": "./dist/loop.js"
|
|
39
|
+
},
|
|
40
|
+
"./canvas": {
|
|
41
|
+
"types": "./dist/canvas.d.ts",
|
|
42
|
+
"import": "./dist/canvas.js"
|
|
43
|
+
},
|
|
44
|
+
"./testing": {
|
|
45
|
+
"types": "./dist/testing.d.ts",
|
|
46
|
+
"import": "./dist/testing.js"
|
|
31
47
|
}
|
|
32
48
|
},
|
|
33
49
|
"bin": {
|