
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
@tamb/gamegrid
Advanced tools
**A 2D HTML Grid for Creating Web Games** <small>or other things that could use a 2D Matrix</small>
A 2D HTML Grid for Creating Web Games
or other things that could use a 2D Matrix
GameGrid is a class that you instantiate to create a 2D grid in memory, with optional rendering of the grid.
// All code examples are written in Typescript
const grid : GameGrid = new GameGrid(config: IConfig, element: HTMLElement);
config : IConfigexport interface IConfig {
options?: IOptions;
matrix: ICell[][];
state?: IState;
}
options : IOptionsexport interface IOptions {
id?: string;
arrowControls?: boolean;
wasdControls?: boolean;
infiniteX?: boolean;
infiniteY?: boolean;
clickable?: boolean;
rewindLimit?: number;
middlewares?: {
pre: ((gamegridInstance: IGameGrid, newState: any) => void)[];
post: ((gamegridInstance: IGameGrid, newState: any) => void)[];
};
// TODO: Utilize these options to add additional supported cell types
blockOnType?: string[];
collideOnType?: string[];
moveOnType?: string[];
// TODO: Add support for this
// render options
activeClass?: string;
containerClass?: string;
rowClass?: string;
}
The default options are as follows:
{
activeClass: classesEnum.ACTIVE_CELL,
arrowControls: true,
wasdControls: true,
infiniteX: true,
infiniteY: true,
clickable: true,
rewindLimit: 20,
blockOnType: [cellTypeEnum.BARRIER],
collideOnType: [cellTypeEnum.INTERACTIVE],
moveOnType: [cellTypeEnum.OPEN],
}
matrix : ICell[][]The Matrix is a great movie. It's also a 2D representation of the game grid you're making.
ICell[][]cell : ICellThe Cell is a really inferior movie.* It's also an object representing a single point in the grid.
state : IStateThe State is a really funny TV show. It's also an object representing the current state of the grid.
export interface IState {
activeCoords: number[];
prevCoords: number[];
nextCoords: number[];
moves: number[][];
currentDirection?: string;
rendered?: boolean;
}
State has to have preceeding attributes to be valid. But when you setStateSync you can add whatever else you want. You can also do a partial state update and it will work.
The default state is
export const INITIAL_STATE: IState = {
activeCoords: [0, 0],
prevCoords: [0, 0],
nextCoords: [],
currentDirection: '',
rendered: false,
moves: [[0, 0]],
};
The pre middleware is called everytime before the state is updated. It receives the gamegridInstance and the newState as arguments. You can modify the newState object and it will be used to update the state. You can also access the gamegridInstance to get the current state.
These functions execute in the order in which they are registered.
The post middleware is called everytime after the state is updated. It receives the gamegridInstance and the newState as arguments. These functions execute in the order in which they are registered.
Refs are... I'm not gonna continue with the bit.
The GameGrid instance has a refs object that contains references to the optional HTML elements of the grid
export interface IRefs {
container: HTMLElement | null;
rows: HTMLDivElement[];
cells: HTMLDivElement[][];
}
export interface IGameGrid {
refs: IRefs;
options: IOptions;
root?: HTMLElement;
renderGrid(container: HTMLElement): void;
getOptions(): IOptions;
setOptions(newOptions: IOptions): void;
destroy(): void;
getState(): IState;
moveLeft(): void;
moveUp(): void;
moveRight(): void;
moveDown(): void;
setMatrix(m: ICell[][]): void;
getMatrix(): ICell[][];
setStateSync(obj: IState): void;
getActiveCell(): IRenderedCell;
getPreviousCell(): IRenderedCell;
}
GameGrid emits the following custom events from the window object. Each event has a detail object with the gameGridInstance containing all of the above.
gamegrid:grid:renderedgamegrid:move:leftgamegrid:move:rightgamegrid:move:upgamegrid:move:downgamegrid:move:blockedgamegrid:move:collidegamegrid:move:dettachgamegrid:move:landgamegrid:limitgamegrid:limit:xgamegrid:limit:ygamegrid:wrapgamegrid:wrap:xgamegrid:wrap:y... coming soon ...
import GameGrid from "@tamb/gamegrid";
const config = // my config settings
const gg : GameGrid = new GameGrid(config, rootElement);
// optionally render the grid
gg.render();
// create moves like so
gg.moveLeft();
gg.moveRight();
The following enums are available for use in your code and are named exports of the package.
export enum classesEnum {
GRID = 'gamegrid',
ROW = 'gamegrid__row',
CELL = 'gamegrid__cell',
ACTIVE_CELL = 'gamegrid__cell--active',
}
export enum cellTypeEnum {
OPEN = 'open',
BARRIER = 'barrier',
INTERACTIVE = 'interactive',
}
export const gridEventsEnum = {
RENDERED: 'gamegrid:grid:rendered',
CREATED: 'gamegrid:grid:created',
DESTROYED: 'gamegrid:grid:destroyed',
MOVE_LEFT: 'gamegrid:move:left',
MOVE_RIGHT: 'gamegrid:move:right',
MOVE_UP: 'gamegrid:move:up',
MOVE_DOWN: 'gamegrid:move:down',
MOVE_BLOCKED: 'gamegrid:move:blocked', // hits a wall
MOVE_COLLISION: 'gamegrid:move:collide', // overlaps another entity
MOVE_DETTACH: 'gamegrid:move:dettach', // leaves overlapping an entity
MOVE_LAND: 'gamegrid:move:land', // move finished
LIMIT: 'gamegrid:move:limit',
LIMIT_X: 'gamegrid:move:limit:x',
LIMIT_Y: 'gamegrid:move:limit:y',
WRAP: 'gamegrid:move:wrap',
WRAP_X: 'gamegrid:move:wrap:x',
WRAP_Y: 'gamegrid:move:wrap:y',
};
FAQs
**A 2D HTML Grid for Creating Web Games** <small>or other things that could use a 2D Matrix</small>
We found that @tamb/gamegrid demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.