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

itay-ecs

v1.0.2

Published

A generic entity component system in typescript.

Downloads

8

Readme

itay-ecs

A generic entity component system in typescript.

The goal of this library is to allow combining multiple independent libraries to create games.
For example: Use a rendering library like "pixi", "threejs", "babylon" etc. With a physics system like "Box2D" or "p2.js".

Install

npm install --save itay-ecs

Concept

Component

A piece of information (of an entity) on a specific topic.

For example, A person entity might have a "HeightComponent" that holds a number to represent the person's height.

Components can be used to mark an entity, without having any additional properties.

For example, A person entity can hold a "TeacherComponent" to be marked as a teacher.

Entity

A logical representatin of an object. Its data is contained in the components it holds.

System

Contains logic that acts on entities, using the entities components.

In this library

Every class can be a component, from a simple number to a complex class.

A component is usualy identified by its class. To be more exact, his constructor function (well thats javascript).

A component can also be referenced by other and multiple constructors. See the advanced section.

The library allows you to choose what to use and how.
The library provides:

  • ComponentsCollection class - to hold the entity's components.
  • EntitiesCollection class - to hold entities (objects that implements Entity).

When it comes to systems, implement as you like.
Check the usage sections to learn about classes and functionality that will make your life very simple when implementin a system.

Basic Usage

ComponentsCollection

let collection = new ComponentsCollection();
let component = 1; // Component of type Number.

collection.add(component);

let myNumber: number = collection.get(Number);

EntitiesCollection

// Some example entity
class TankEntity implements Entity {
    public components: ComponentsCollection = new ComponentsCollection();

    constructor() {
        this.components.add(new GunsComponent(7));
    }
}

// Some example component
class GunsComponent {
    public power: number = 1;

    constructor(power?: number) {
        if (power) {
            this.power = power;
        }
    }
}

// Using EntitiesCollection
let entities = new EntitiesCollection();
entities.add(new TankEntity());

let myShootingEntities: Entity[] = collection.getByComponent(GunsComponent);
console.log(myShootingEntities.length); // 1

Advanced Usage

Shared Components

Usualy a component is tailor made for a specific system.
That raises some common problems:

  • Sharing information between systems is hard, and might cause coupling.
  • Mixing third-party libraries is hard.

For example, a "PositionComponent" is used by the physics-system and by the render-system.

This library offers a simple solution: Multiple component keys.

Given the classes:

class StaticBodyComponent { // A physics system component.
    public x: number;
    public y: number;
}

class RenderablePositionComponent { // A render system component.
    public posX: number;
    public posY: number;
}

class MyPositionComponent { // My game component.
    public x: number;
    public y: number;

    public get posX() {
        return this.x;
    }

    public get posY() {
        return this.y;
    }
}

We can:

let collection = new ComponentsCollection();
let myComponent = new MyPositionComponent();
myComponent.x = 1;
myComponent.y = 2;

collection.add(myComponent, StaticBodyComponent, RenderablePositionComponent);
// Or: collection.add(myComponent, [StaticBodyComponent, RenderablePositionComponent]);

let staticBody = collection.get(StaticBodyComponent); // x: 1, y: 2
let renderable = collection.get(RenderablePositionComponent); // posX: 1, posY: 2

Observe entities

Observing entities collection is a simple and usefull way to keep track of a set of entities.
One use case is using observation to keep track of entities for a system.

let collection = new EntitiesCollection();

let addedTriggered = false;

let observation = new EntitiesObservation();
observation.added = entity => addedTriggered = true;
observation.filter = EntitiesFilter.componentsContainsAny([GunsComponent, SomeOtherComponent])

collection.addObservation(observation);

collection.add(new TankEntity()); // TankEntity has GunsComponent

console.log(addedTriggered); // true

EntitiesSearchCache

EntitiesSearchCache class holds a list of filtered entities from an EntitiesCollection.

Its implemented using the observation feature.

You can use this class to hold entities for a system. This will be the simplest approach.

let collection = new EntitiesCollection();

let entityToRemove = new NumberBooleanEntity();
collection.add(entityToRemove); // Will be selected by the search.
collection.add(new NumberStringEntity()); // Will NOT be selected by the search.

let cache = EntitiesSearchCache.from(collection).componentsContainsAll([Number, Boolean]); // Cache contains 1 entity.

collection.add(new NumberBooleanEntity()); // Selected by the search. Cache contains 2 entities.
collection.remove(entityToRemove); // Cache contains 1 entity.

for (let entity of cache.entities){
    console.log(entity);
}