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

tiny-ecs

v2.0.0

Published

A mean lean Entity-Component-System library.

Downloads

67

Readme

TinyECS

Build Status NPM version

A mean lean Entity-Component-System library.

browser support

Installation

Works on the server or the browser (via Browserify):

npm install tiny-ecs

Usage

Manage entities via an EntityManager instance:

var EntityManager = require('tiny-ecs').EntityManager;

var entities = new EntityManager();

Creating Entities

Create an entity:

var hero = entities.createEntity();

Working with Entities

Components are added via providing any generic constructor function:

function Position()
{
  this.x = 0;
  this.y = 0;
}
function Sprite()
{
  this.image = 'hero.png';
}

Add the components:

hero.addComponent(Position).addComponent(Sprite);

We now have new data members on our entity for the components. TinyECS will add an instance member that is the name of the component constructor, lowercased.

hero.position.x = 10;
hero.sprite.image === 'hero.png'; // true

Add arbitrary text tags to an entity:

hero.addTag('player');

You can also remove components and tags in much the same way:

hero.removeComponent(Sprite);
hero.removeTag('player');

To determine if an entity has a specific component:

if (hero.hasComponent(Position)) { ... }

And to check if an entity has ALL of a set of components:

if (hero.hasAllComponents([Position, Sprite])) { ... }

Querying Entities

The entity manager is setup with indexed queries, allowing extremely fast querying of the current entities. Querying entities returns an array of entities.

Get all entities that have a specific set of components:

var toDraw = entities.queryComponents([Position, Sprite]);

Get all entities with a certain tag:

var enemies = entities.queryTag('enemy');

Removing Entities

Directly:

hero.remove();

Via the manager:

entities.remove(hero);

Creating Components

Any object constructor can be used as a component, nothing special required. Components should be lean, primarily data containers, leaving all the heavy lifting for the systems.

function Position()
{
  this.x = 0;
  this.y = 0;
}

Creating Systems

In TinyECS, there is no formal notion of a system. A system is considered any context in which entities and their components are updated. As to how this occurs will vary depending on your use.

In the example of a game, mainting a list of systems that are instantiated with some sort of IoC container that request a list of entities seems like a good idea.

function PhysicsSystem(entities)
{
  // Dependency inject -- reference to our EntityManager
  this.entities = entities;
}

PhysicsSystem.prototype.update = function(dt, time)
{
  var toUpdate = this.entities.queryComponents([Position, Physics]);

  toUpdate.forEach(function(entity) { ... });
  ...
}

Tern Support

The source files are all decorated with JSDoc3-style annotations that work great with the Tern code inference system. Combined with the Node plugin (see this project's .tern-project file), you can have intelligent autocomplete for methods in this library.

Testing

Testing is done with Tape and can be run with the command npm test.

Automated CI cross-browser testing is provided by Testling.

License

Copyright 2014 Brandon Valosek

TinyECS is released under the MIT license.