Achievement Manager
Achievements with progress (“defeat 100 enemies”), hidden ones, saving, and an unlock event for showing a message or telling a game portal.
Files: achievement-manager.js (the component, no dependencies) and achievement-manager.test.mjs (4 tests).
Copy achievement-manager.js into your game, for example to src/components/, and import it as an ES module.
Usage
import { createAchievements } from "./achievement-manager.js";
const ach = createAchievements({
key: "my-game:achievements",
list: [
{ id: "first-win", name: "First win", description: "Win a round" },
{ id: "hunter", name: "Hunter", description: "Defeat 100 enemies", goal: 100 },
{ id: "secret", name: "???", description: "Find the hidden room", hidden: true },
],
onUnlock: (a) => notify.push({ text: `Achievement: ${a.name}`, type: "success" }),
});
events.on("enemy:killed", () => ach.progress("hunter"));
API
createAchievements({ list, key, storage, onUnlock }) → achach.progress(id, amount = 1) → true if this call unlocked itach.setProgress(id, value)for “best” style goals (highest combo); never goes downach.unlock(id) / ach.isUnlocked(id)ach.list() → [{ id, name, description, goal, value, unlocked, unlockedAt, hidden }] hidden ones show "???" until unlockedach.stats → { unlocked, total, percent }ach.reset()clears progress (for a “reset data” button)ach.onUnlock(fn) → offextra listeners (portal adapter, sound)
Unknown ids throw, so typos show up during testing.
Engines
| Engine | Notes |
|---|---|
| Vanilla, Phaser, PixiJS, Three.js | yes. To report to a portal, call platform.unlockAchievement(a.id) in onUnlock (see Platform Adapter). |
Tests
Node tests: counting to the goal, unlocking once, best-value goals, hidden entries, saving, damaged saves, reset, unknown ids.
Run them with node --test toolkit/components/achievement-manager/ (Node 20 or newer, nothing to install).