npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tilelib-2d

v0.2.1

Published

Lightweight 2D Tile Manipulation Library, useful for map building, maze generation, dungeon crafting etc.

Downloads

17

Readme

Readme

tilelib-2d is a quick and dirty collection of utility classes and models I used to make this Auto Tiling Component. Feel free to use it however you'd like, you'll find it most useful though in 2D Tile Based Map contexts.

Contents

The Package is organized in three groups Models, Commands, and Utilities.

Commands

Commands contains the extendable Command base class used to implement the namesake "command" pattern, which is really just a fancy way of making a function call into an object with a generic .execute() call to run, and a generic .undo(), to undo said command. Why use a command? Read this. They're very useful when you want something done on an input, you want to customize the input source regardless of platform, and you want to possibly take it back in the future.

export abstract class Command {
  // ...some fields to store previous state to undo into.
  execute(): void {}
  undo(): void {}
}

There are a few predefined commands concerning setting tile types on a 2d grid, updating their bitmask based on surrounding tile-types, and a bundled command called "draw" which executes these sub-commands on a user interaction with the tile grid.

Models

Models contain essential classes needed for basic 2D tile grids including the generic 2D array Grid class, TileGrid class, Position class, and of course the Tile class.

their interfaces best describe their usage.

interface GridBase<T> {
  /** Gets the object located at the (X,Y) position of the grid.
   * e.g.
   * ```ts
   * TileGrid.get(new Position(0, 0)) => HoleTile(...)
   * ```
   */
  get(p: Position): T | undefined;
  /** Sets the (X,Y) position of the grid to a specified object
   * e.g.
   * ```ts
   * TileGrid.set(new Position(0, 0), new GroundTile(...));
   * TileGrid.get(new Position(0, 0)) => GroundTile(...);
   * ```
   */
  set(p: Position, value: T): void;
  /** The actual grid which is a 2D array of specified generic type */
  grid(): T[][];
  /** The horizontal length of the grid, including empty spots */
  get width(): number;
  /** The horizontal length of the grid, including empty spots */
  get height(): number;
  /**
   * Determines if the provided (X, Y) position is valid and within the grid. Used for boundary detection
   * e.g.
   * ```ts
   * Grid.width() => 3;
   * Grid.height() => 3;
   * Grid.contains(new Position(5, 5)) => false;
   * Grid.contains(new Position(2, 2)) => true;
   * ```
   */
  contains(p: Position): boolean;
}
interface TileBase {
  /**
   * Whether the tile exists on the ground layer or not.
   * A hole or wall tile would have collision set to true,
   * while a ground tile would have collision set to false.
   * Used to determine whether this tile is considered "adjacent"
   * to another tile while calculating the bitmask.
   */
  collision: boolean;

  /**
   * Binary number representing whether the four surrounding tiles are traversable or not.
   */
  bitmask: number;

  /**
   * The type of tile, currently: `Ground`, `Hole`, `Default`
   */
  tileType: Tiles;
}
interface PositionBase {
  /** Horizontal zero-indexed position in a 2D array grid */
  get x(): number;

  /** Vertical zero-indexed position in a 2D array grid */
  get y(): number;

  /** Position located one decreasing y value above the current position e.g.
   * ```ts
   * new Position(1, 1).north => Position(1, 0)
   * ```
   */
  get north(): Position;

  /** Position located one increasing x value to the right of the current position e.g.
   * ```ts
   * new Position(1, 1).east => Position(2, 1)
   * ```
   */
  get east(): Position;

  /** Position located one increasing y value below the current position e.g.
   * ```ts
   * new Position(1, 1).south => Position(1, 2)
   * ```
   */
  get south(): Position;

  /** Position located one decreasing x value to the left of the current position e.g.
   * ```ts
   * new Position(1, 1).north => Position(0, 1)
   * ```
   */
  get west(): Position;

  /**
   * An array of all the positions surrounding the current position. in North -> East -> South -> West order.
   * e.g.
   * ```ts
   * new Position(1, 1).surrounding => [Position(1, 0), Position(2, 1), Position(1, 2), Position(0, 1)]
   * ```
   */
  get surrounding(): Position[];

  /**
   * Adds the x and y values of another position to the current position and returns a new Position,
   * used for distance calculations or movement.
   * @param other Position to compare to
   */
  add(other: Position): Position;

  /**
   * Subtracts the x and y values of another position from the current position and returns a new Position,
   * used for distance calculations or movement.
   * @param other Position to compare to
   */
  subtract(other: Position): Position;

  /**
   * Compares the x and y values of another position to the current position and returns true if both are equal
   * @param other Position to compare to
   */
  equals(other: Position): boolean;
}

Utilities

Contains the clone utility to generically duplicate a copy of an object, the calcBitmask utility to generate a 4-bit binary bitmask indicating what sides a tile has another non-colliding tile next to it, and calculateEventPosition which converts a mouse coordinate to a grid coordinate Position.