4.1 KiB
4.1 KiB
Animated Fiction Engine - Modular Refactoring
This directory contains the refactored, modular version of the interactive fiction engine, previously implemented monolithically in references/game.js. The refactoring follows the recommendations outlined in Chapter 8 of references/Documentation.md.
Overview
The engine is now broken down into several distinct, reusable components, each with a specific responsibility. This improves maintainability, testability, and allows for easier integration into different projects or replacement of individual components.
Modules
The core modules are:
animation-queue.js(AnimationQueue): Manages the timing and execution queue for animations, primarily text reveal. Handles scheduling, fast-forwarding, stopping, and speed control.text-processor.js(TextProcessor): Encapsulates text pre-processing steps (SmartyPants for typography, Hyphenopoly for hyphenation).paragraph-layout.js(ParagraphLayout): Interfaces with the Knuth-Plass line breaking algorithm (kapfunction) to calculate optimal paragraph layouts. Requires a text measurement function.layout-renderer.js(LayoutRenderer): Translates the calculated layout data into DOM elements, handles visual tag rendering (images, backgrounds, etc.), and schedules animations using theAnimationQueue.audio-manager.js(AudioManager): Manages loading and playback of non-TTS audio effects triggered by Ink tags (AUDIO,AUDIOLOOP).tts-player.js(TtsPlayer): Handles interactions with Text-to-Speech services (usingtts-factory.jsfor selection) and manages TTS playback.persistence-manager.js(PersistenceManager): Handles saving and loading the game state (Ink state JSON and rendered history) using a configurable storage backend (defaulting tolocalStorage).ink-story-player.js(InkStoryPlayer): Orchestrates the Ink narrative flow. Manages theinkjs.Storyinstance, processes story content and tags, and delegates tasks to other modules (text processing, layout, rendering, audio, TTS, persistence).ui-controller.js(UiController): Manages user interface interactions (buttons, sliders, keyboard shortcuts) and updates UI elements. Interacts with other modules to trigger actions (e.g., change speed, save/load, choose choice).animated-fiction.js(AnimatedFiction): The main application class that integrates all the modules. It handles initialization, loading the story, and starting the application flow.
Supporting Libraries
This engine relies on several external and internal libraries located in public/js:
ink.js(Loaded via CDN inmodular-index.html, wrapped inwindow.inkjs)smartypants.jslinked-list.js(Used bylinebreak.js)linebreak.js(Core Knuth-Plass algorithm)knuth-and-plass.js(Adapter forlinebreak.js)Hyphenopoly_Loader.js&Hyphenopoly.js(Hyphenation library)kokoro-js.js,kokoro-handler.js,tts-handler.js,tts-factory.js(Text-to-Speech components)
Usage
- Include Libraries: Ensure all necessary supporting libraries (
smartypants.js,ink.js,Hyphenopoly, etc.) are loaded in your HTML file before the main application module. - Include Main Module: Load the main application module (
animated-fiction.js) using<script type="module">. - HTML Structure: Ensure your HTML has the necessary container elements (e.g.,
#story,#choices) and UI controls (buttons, sliders) with the expected IDs, or configure theUiControllerwith the correct element references. - Initialization: The
animated-fiction.jsscript will automatically instantiate theAnimatedFictionclass onwindow.onload, load the story specified in its configuration (Herrenhaus.ink.jsonby default), and start the interactive experience.
Refer to public/modular-index.html for a working example of how to structure the HTML and include the necessary scripts.
Original Files
- The original monolithic implementation can be found in
references/game.js. - The documentation detailing the original implementation and the refactoring plan is in
references/Documentation.md.