Engine API
The Engine class is the heart of Doodle Engine. It holds the content registry (static) and game state (dynamic), processes player actions, and produces snapshots.
Constructor
Section titled “Constructor”new Engine(registry: ContentRegistry, state: GameState)| Parameter | Type | Description |
|---|---|---|
registry | ContentRegistry | All game content (locations, characters, dialogues, etc.) |
state | GameState | Initial game state |
Methods
Section titled “Methods”newGame
Section titled “newGame”newGame(config: GameConfig): SnapshotStart a new game. Initializes state from config, sets up character and item locations from the registry, and checks for triggered dialogues at the starting location.
const engine = new Engine(registry, state);const snapshot = engine.newGame(config);loadGame
Section titled “loadGame”loadGame(saveData: SaveData): SnapshotRestore game state from save data and return a snapshot.
const snapshot = engine.loadGame(saveData);saveGame
Section titled “saveGame”saveGame(): SaveDataCapture the current game state as serializable save data.
const saveData = engine.saveGame();localStorage.setItem('save', JSON.stringify(saveData));Returns:
interface SaveData { version: string; // "1.0" timestamp: string; // ISO 8601 state: GameState; // Complete state}selectChoice
Section titled “selectChoice”selectChoice(choiceId: string): SnapshotProcess a player’s dialogue choice. Applies the choice’s effects, advances to the next dialogue node, and applies that node’s effects.
If no next node exists, the dialogue ends. If not currently in dialogue, returns the current snapshot unchanged.
const snapshot = engine.selectChoice('choice_buy_drink');talkTo
Section titled “talkTo”talkTo(characterId: string): SnapshotStart a conversation with a character. Looks up the character’s dialogue field, finds the start node, and begins the dialogue.
const snapshot = engine.talkTo('bartender');takeItem
Section titled “takeItem”takeItem(itemId: string): SnapshotPick up an item at the current location. Only works if the item’s location matches the player’s current location. Moves the item to "inventory".
const snapshot = engine.takeItem('rusty_key');travelTo
Section titled “travelTo”travelTo(locationId: string): SnapshotTravel to a location on the map. Calculates travel time based on distance and map scale, advances time, ends any active dialogue, and checks for triggered dialogues at the destination.
Does nothing if the map is disabled (mapEnabled: false).
const snapshot = engine.travelTo('market');writeNote
Section titled “writeNote”writeNote(title: string, text: string): SnapshotAdd a player note to the journal. Notes have auto-generated IDs based on timestamp.
const snapshot = engine.writeNote('Clue', 'The bartender mentioned a coin...');deleteNote
Section titled “deleteNote”deleteNote(noteId: string): SnapshotRemove a player note from the journal.
const snapshot = engine.deleteNote('note_1234567890');setLocale
Section titled “setLocale”setLocale(locale: string): SnapshotChange the active language. The next snapshot will have all @key references resolved against the new locale.
const snapshot = engine.setLocale('es');getSnapshot
Section titled “getSnapshot”getSnapshot(): SnapshotGet the current snapshot without making any changes. Useful for initial rendering.
const snapshot = engine.getSnapshot();Data Flow
Section titled “Data Flow”All methods follow the same pattern:
- Validate inputs
- Mutate internal state
- Build and return a snapshot
- Clear transient state (notifications, pending sounds)
Transient state (notifications and pending sounds) appears in exactly one snapshot and is then cleared. This means each snapshot is a complete, self-contained view of the game.
Triggered Dialogues
Section titled “Triggered Dialogues”After newGame() and travelTo(), the engine checks for dialogues with a triggerLocation matching the current location. If a dialogue’s conditions pass, it auto-starts. Only one triggered dialogue fires per location change.