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

tilelogic

v3.0.0

Published

Logic for tilemaps

Downloads

14

Readme

tilelogic

Provides logic for tilemaps. There is no actual drawing involved, that is something you need to do for yourself.

Features

  • Small and simple
  • Provides only the logic for tilemaps
  • No dependencies

Installing

npm install tilelogic

(or if you prefer Yarn it would be yarn add tilelogic)

Constructing a tilemap

import { TileLogic } from 'tilelogic';

var tileLogic = new TileLogic(10, 10);
// Creates a 10 by 10 tilemap

By default all tiles will be filled with null. In order to fill the tile map with the values you would like you would need to provide a third parameter (a so-called reviver) which is a function that provides the x and y coordinate and needs to return the data

const tileLogic = new TileLogic(4, 4, (x, y) => {
  if (x === 0 && y === 0) {
    return 'Hello';
  }

  return 'World';
});

If you are using TypeScript, TileLogic is using generics to set the type of the tiles

const tileLogic = new TileLogic<string>(4, 4, () => 'Hello World');

Accessing a tile

Tiles can be accessed through a convienent get method.

tilelogic.get({ x: 0, y: 0 }); // < The content at position x: 0 y: 0

Setting content in a tile

tileLogic.set({ x: 0, y: 0, content: 'Hello!' }); // < The content of the tile at position x: 0 y: 0 will now be "Hello"

Helper functions

#forEach

Iterates through the tilemap without modifying it. Starts at the top-left tile and goes through it row by row.

tilemap.forEach(function(x: number, y: number, tile: T) {
  // Iterate through the tilemap
  // x and y are the current position
  // tile is the content of each tile
});

#map

Iterates through the tilemap and returning

tilemap.map(function(x, y, tile) {
  // Iterate through the tilemap
  // x and y are the current position
  // tile is the content of each tile
  return x;
});
// We now get an array with all the x-values

#toArray

Returns a two-dimensional array with the data stored in the tile map.

.fromArray

Takes in a two-dimensional array and returns a TileLogic instance

const tiles = [
  [2, 2],
  [1, 1],
];

const tileLogic = TileLogic.fromArray<number>(tiles);

#equals

Compare two tilemaps with each other and returns true if they match.

tilemap.equals(new TileMap(10, 10));

#flatten

Flattens the tile map into a one-dimensional array to make it easier to draw either on the DOM or canvas.

tilemap.flatten(); // Returns an array with the content of each tile

Immutable TileLogic

This library also exports an immutable version of TileLogic where any change to its data returns a new instance. ImmutableTileLogic includes all functionality from TileLogic as well as some extra helpful tools

import { ImmutableTileLogic } from 'tilelogic';

const tileLogic = new ImmutableTileLogic<string>(4, 4, () => 'Hello');

const newTileLogic = tileLogic.set({ x: 2, y: 2, data: 'World' }); //< Returns a new instance and leaves the original untouched

const otherTileLogic = newTileLogic.swap({ x: 2, y: 2 }, { x: 0, y: 0 }); //< Returns a new instance where x: 2 y: 2 and x: 0 y: 0 have been swapped and 'World' is now at position x: 0 y: 0

Using a cursor to navigate

TileLogic also provides a cursor to navigate inside the tilemap. It is read-only and is not able to modify the tile map directly.

const tileLogic = new TileLogic<string>(4, 4, () => 'Hello');

const cursor = tileLogic.cursor({ x: 0, y: 0 });

cursor.content(); // < Returns 'Hello'

The cursor provides up, down, right, left and move methods to move in any direction.

License

Tilelogic is public domain. If this does not work for you, you can alternatively use the MIT license.