rot.js Roguelike Toolkit

repository·master·Indexed 25 days ago

https://github.com/ondras/rot.js

A roguelike toolkit written in TypeScript for creating roguelike games in JavaScript. Version 2.2.1 provides essential building blocks including procedural map generation (Rooms, Corridors), Field of View (FOV) calculations, pathfinding (A*), noise generation, lighting, and a game loop engine. It includes a comprehensive Color module for RGB/HSL manipulation and a set of architecture addons for entity management, messaging, and coordinate systems. Compatible with modern browsers via ES2015 modules and Node.js using the "term" layout backend.

Tokens
7K
Snippets
7
Records
84
Agent score
82%

What's inside rot.js

  1. Use rot.js addons for game architecture patterns

    master

    The addons directory provides skeleton files and utility modules designed to be reused and adjusted for common game design patterns and architecture. These modules can be integrated into your project to handle messaging, coordinate systems, and entity management.

    Available modules include:

    • pubsub.js: A simple publish-subscribe implementation for cross-component messaging and notifications.
    • xy.js: Utility for handling 2D coordinates.
    • game.js: A sample main namespace structure.
    • level.js: An empty level container that encapsulates entities.
    • entity.js: An abstract class for displayable game entities.
    • being.js: A rudimentary implementation of a game 'being'.
    • player.js: A specialized 'being' implementation with keyboard controls.
    • textbuffer.js: A utility for buffering text output and displaying it within a subset of ROT.Display.
    • sample.html: A complete, working game example featuring a display, level/map, and an interactive player character.
  2. Install rot.js

    master

    You can include rot.js in your project using several methods:

    • npm: Install via the rot-js package.
    • Direct Download: Download the prebuilt dist/rot.js or the minified dist/rot.min.js files.
    • Source: Clone the repository for full source code access.
    npm install rot-js
  3. Use rot.js in a browser

    master

    Rot.js is written in TypeScript and is available in multiple formats for browser environments:

    1. ES2015 Modules: Use the code in the lib/ directory for modern browsers. These can be used directly without transpilation or can be bundled using tools like Rollup or Babel (recommended for production).
    2. Pre-built Bundle: Include dist/rot.js (or the minified dist/rot.min.js) via a traditional <script> tag. This version uses ES5 and places the library into a global ROT namespace.
  4. Use rot.js in Node.js

    master

    Most parts of rot.js are compatible with Node.js. When using the Display class in a server-side environment, you must specify the "term" layout backend to output to the terminal.

    The pre-bundled package can be loaded as a CommonJS module.

    let display = new ROT.Display({width:40, height:9, layout:"term"});
    display.draw(5,  4, "@");
    display.draw(15, 4, "%", "#0f0");          // foreground color
    display.draw(25, 4, "#", "#f00", "#009");  // and background color
  5. Configure Rogue dungeon generation options

    master

    When instantiating the Rogue class, you can provide a partial Options object to customize the layout:

    • cellWidth (number): The number of cells to create horizontally.
    • cellHeight (number): The number of cells to create vertically.
    • roomWidth ([number, number]): An array containing the [min, max] width for rooms. If not provided, it is calculated automatically based on cellWidth.
    • roomHeight ([number, number]): An array containing the [min, max] height for rooms. If not provided, it is calculated automatically based on cellHeight.
  6. Convert colors to RGB or Hex strings

    master

    Convert a Color array [r, g, b] to a standard web string format. Both methods automatically clamp values between 0 and 255.

    • toRGB(color): Returns an rgb(r,g,b) string.
    • toHex(color): Returns a #rrggbb hex string.
  7. Use the Pathfinding module

    master
    The Pathfinding module provides implementations for common pathfinding algorithms, specifically Dijkstra and AStar. You can import these algorithms to find paths through a grid or map based on provided costs and constraints.
  8. Create a Corridor with the Corridor class

    master

    The Corridor class defines a path between two points.

    Manual Construction

    Use new Corridor(startX, startY, endX, endY) to define a corridor between two specific coordinates.

    Randomized Generation

    • Corridor.createRandomAt(x, y, dx, dy, options): Creates a corridor of random length starting at (x, y) and extending in direction (dx, dy).

    Options

    When using randomized methods, provide a CorridorOptions object:

    • corridorLength: [min, max] number range.

    Digging

    Use create(digCallback) to apply the corridor to your map. The digCallback receives (x, y, value) where value is 0 (empty space).

    To ensure the corridor doesn't leave awkward gaps at its end, you can use createPriorityWalls(priorityWallCallback) to place walls at the terminal points of the corridor.

  9. Validate Map Features

    master

    Both Room and Corridor provide an isValid method to check if a feature can be placed without violating map constraints.

    isValid(isWallCallback, canBeDugCallback) requires two callbacks:

    • isWallCallback(x, y): Returns true if the position is a wall.
    • canBeDugCallback(x, y): Returns true if the position can be dug (e.g., is currently empty or a specific type of terrain).
  10. Use the RNG class for pseudorandom number generation

    master

    The RNG class provides various methods for generating different types of random values, including uniform distributions, normal distributions, and weighted selections. It uses the Alea algorithm for high-quality pseudorandom numbers.

    Key methods include:

    • getUniform(): Returns a value in the range [0, 1).
    • getUniformInt(lowerBound, upperBound): Returns an integer between lowerBound and upperBound (inclusive).
    • getNormal(mean, stddev): Returns a normally distributed value.
    • getPercentage(): Returns an integer between 1 and 100 (inclusive).
    • getItem(array): Returns a random item from an array, or null if the array is empty.
    • shuffle(array): Returns a new array containing the same items in a randomized order.
    • getWeightedValue(data): Returns a key from an object where values represent relative weights/probabilities.