Bash.tv → GitHub → itch.io

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

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).