Odyc.js Documentation

repository·main·Indexed 19 days ago

https://github.com/achtaitaipai/odyc

A lightweight JavaScript library for creating narrative-driven games using code. Odyc.js allows developers to integrate pixels, sound, text, and logic within a single file. Key features include a canvas-based dialog system with typing animations, a camera system for viewport management, and a unified game state for managing players, maps, and turns.

Tokens
10.6K
Snippets
39
Records
49
Agent score
67%

What's inside Odyc.js

  1. Overview of odyc.js

    main
    Odyc.js is a lightweight JavaScript library used to create narrative games. It allows developers to combine pixels, sounds, text, and logic into a cohesive experience. A key design philosophy is simplicity: the library is designed so that an entire game can be contained within a single file, with everything being built through code rather than complex external assets or editors.
  2. Understand the Odyc Examples project structure

    main

    The apps/examples package is organized as follows:

    • games/: Contains the HTML entry points for each individual example application.
    • src/: Contains the TypeScript source files for the examples.
    • index.html: The main landing page that indexes all available examples.
  3. Run the Odyc Examples development server

    main

    To start the development environment for the Odyc examples, run the following command from the apps/examples directory. This will launch the local development server so you can view and interact with the example applications.

    npm run dev
  4. Create a new Odyc example

    main

    You can scaffold a new example application using the built-in generator. This command automatically creates the necessary file structure and updates the main index page so the new example is discoverable in the examples list.

    npm run create-example my-game-name
  5. Define Tile and Position types

    main

    When working with the game engine, use the following types for spatial and content representation:

    • Tile: A string or number representing the content of a single cell.
    • Position: A tuple of two numbers [number, number] representing [x, y] coordinates.
    export type Tile = string | number
    export type Position = [number, number]
  6. Configure CameraParams

    main

    The CameraParams type defines the configuration required to initialize a Camera instance. It requires screen dimensions and the game map, while allowing optional constraints for the camera's viewable area.

    Required Properties:

    • screenWidth: The width of the renderer's screen.
    • screenHeight: The height of the renderer's screen.
    • map: The map data from GameStateParams.

    Optional Properties:

    • cameraWidth: A constraint on the camera's width.
    • cameraHeight: A constraint on the camera's height.

    If both cameraWidth and cameraHeight are provided, the camera uses a centered rectangle (#rect) to calculate positioning logic.

    import { CameraParams } from './packages/odyc/src/camera.js';
    
    const params: CameraParams = {
      screenWidth: 800,
      screenHeight: 600,
      map: myMapData,
      cameraWidth: 400, // Optional
      cameraHeight: 300 // Optional
    };
  7. Configure the MessageBox via MessageBoxParams

    main

    To initialize a MessageBox, you must provide a MessageBoxParams object. This object defines the visual styling for the message box background and text content, using either a color index (number) or a CSS color string. It also requires a colors object from RendererParams to resolve these values.

    Properties:

    • messageBackground: The background color for the message box (string or number).
    • messageColor: The text color for the message content (string or number).
    • colors: The color palette used for resolving color indices (RendererParams['colors']).
    import { MessageBoxParams } from './messageBox';
    import { RendererParams } from './renderer';
    
    const params: MessageBoxParams = {
      messageBackground: '#000000',
      messageColor: 1,
      colors: { /* ... RendererParams['colors'] ... */ }
    };
  8. Configure DialogParams for the Dialog class

    main

    The DialogParams object defines the visual appearance and typing behavior of the dialog box. It requires color definitions for the background, text, and border, as well as a typing speed setting.

    Properties:

    • dialogBackground: Background color for the dialog box. Can be a color index (number) or a CSS color string.
    • dialogColor: Text color for the dialog content. Can be a color index (number) or a CSS color string.
    • dialogBorder: Border color for the dialog box outline. Can be a color index (number) or a CSS color string.
    • dialogSpeed: The typing animation speed. Must be one of the keys from the internal DIALOG_SPEED mapping: 'SLOW', 'NORMAL', or 'FAST'.
    • colors: An object of type RendererParams['colors'] used to resolve color indices into actual colors.
    const params: DialogParams = {
      dialogBackground: '#000000',
      dialogColor: '#ffffff',
      dialogBorder: '#ff0000',
      dialogSpeed: 'NORMAL',
      colors: { /* ... RendererParams colors ... */ }
    };
  9. Configure the Odyc.js Game Config object

    main

    The Config<T> type defines the complete configuration for an Odyc.js game. It is a composite type that merges parameters for the renderer, input handler, sound player, camera, message box, dialog system, and game state. All properties are optional and will fall back to defaultConfig if omitted.

    Key configuration areas include:

    • Map & Templates: Define the game world using a string-based map and a templates object to map characters to behaviors and sprites.
    • Visuals: Set colors (an array of hex strings), screenWidth, screenHeight, cellWidth, cellHeight, and filter settings.
    • Audio: Control global volume and sound player settings.
    • Controls: Map input keys (e.g., KeyA, ArrowLeft) to game actions like LEFT, RIGHT, UP, DOWN, and ACTION via the controls object.
    • UI: Configure messageBox and dialog appearance and behavior (e.g., dialogSpeed).
    const config = {
      player: { sprite: 0, position: [2, 3] },
      templates: {
        x: { solid: true, sprite: 4, onCollide: (target) => target.remove() }
      },
      map: `
        ........
        ..x.....
        ........
      `,
      colors: ['#000', '#fff', '#f00'],
      filter: { name: 'crt' }
    };