Odyc.js Documentation
repository·main·Indexed 19 days ago
https://github.com/achtaitaipai/odycA 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.
What's inside Odyc.js
- 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.
Understand the Odyc Examples project structure
mainThe
apps/examplespackage 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.
Run the Odyc Examples development server
mainTo start the development environment for the Odyc examples, run the following command from the
apps/examplesdirectory. This will launch the local development server so you can view and interact with the example applications.npm run devCreate your first game with createGame()
mainYou can initialize a new game instance by importing
createGamefrom theodycpackage and passing a configuration object to it.import { createGame } from 'odyc' const game = createGame({ // Configuration options go here })Install odyc via npm
mainTo use the odyc library in your project, install the
odycpackage using npm.npm install odycCreate a new Odyc example
mainYou 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-nameDefine Tile and Position types
mainWhen working with the game engine, use the following types for spatial and content representation:
Tile: Astringornumberrepresenting 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]Use the Player facade in callbacks
mainWhen using theonTurncallback, the player is passed as afacade. The facade is a restricted interface that allows you to read and write the player'ssprite,position,direction, andvisibleproperties without exposing the full internalPlayerinstance logic.Configure CameraParams
mainThe
CameraParamstype defines the configuration required to initialize aCamerainstance. 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 fromGameStateParams.
Optional Properties:
cameraWidth: A constraint on the camera's width.cameraHeight: A constraint on the camera's height.
If both
cameraWidthandcameraHeightare 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 };Configure the MessageBox via MessageBoxParams
mainTo initialize a
MessageBox, you must provide aMessageBoxParamsobject. 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 acolorsobject fromRendererParamsto 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'] ... */ } };Configure DialogParams for the Dialog class
mainThe
DialogParamsobject 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 internalDIALOG_SPEEDmapping:'SLOW','NORMAL', or'FAST'.colors: An object of typeRendererParams['colors']used to resolve color indices into actual colors.
const params: DialogParams = { dialogBackground: '#000000', dialogColor: '#ffffff', dialogBorder: '#ff0000', dialogSpeed: 'NORMAL', colors: { /* ... RendererParams colors ... */ } };Configure the Odyc.js Game Config object
mainThe
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 todefaultConfigif omitted.Key configuration areas include:
- Map & Templates: Define the game world using a string-based
mapand atemplatesobject to map characters to behaviors and sprites. - Visuals: Set
colors(an array of hex strings),screenWidth,screenHeight,cellWidth,cellHeight, andfiltersettings. - Audio: Control global
volumeand sound player settings. - Controls: Map input keys (e.g.,
KeyA,ArrowLeft) to game actions likeLEFT,RIGHT,UP,DOWN, andACTIONvia thecontrolsobject. - UI: Configure
messageBoxanddialogappearance 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' } };- Map & Templates: Define the game world using a string-based