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

@trixt0r/ecs

v0.5.1

Published

Entity component system for any JS environment written in TypeScript.

Downloads

11

Readme

ECS-TS

A simple to use entity component system library written in TypeScript.

It is meant to be used for any use case. So you will not find any game specific logic in this library.

Install

npm install @trixt0r/ecs

Examples

Checkout the examples.

Check the rectangles example out, if you do not want to checkout the code.

Usage

The main parts of this library are

Component

A Component is defined as an interface with no specific methods or properties. I highly suggest you to implement your components as classes, since the systems will rely on those.

For example, you could define a position like this:

import { Component } from '@trixt0r/ecs';

class Position implements Component {
  constructor(public x = 0, public y = 0) {}
}

Entity

Entities are the elements, your systems will work with and have an unique identifier.

Since this library doesn't want you to tell, how to generate your ids, the base class AbstractEntity is abstract. This means your entity implementation should extend AbstractEntity.

For example, you could do something like this:

import { AbstractEntity } from '@trixt0r/ecs';

class MyEntity extends AbstractEntity {
  constructor() {
    super(makeId());
  }
}

Adding components is just as simple as:

myComponent.components.add(new Position(10, 20));

An entity, is a Dispatcher, which means, you can register an EntityListener on it, to check whether a component has been added, removed, the components have been sorted or cleared.

System

Systems implement the actual behavior of your entities, based on which components they own.

For programming your own systems, you should implement the abstract class System. This base class provides basic functionalities, such as

  • an updating flag, which indicates whether a system is still updating.
  • an active flag, which tells the engine to either run the system in the next update call or not.
  • an engine property, which will be set/unset, as soon as the system gets added/removed to/from an engine.

A system is also a Dispatcher, which means, you can react to any actions happening to a system, by registering a SystemListener.

Here is a minimal example of a system, which obtains a list of entities with the component type Position.

import { System, Aspect } from '@trixt0r/ecs';

class MySystem extends System {
  private aspect: Aspect;

  constructor() {
    super(/* optional priority here */);
  }

  onAddedToEngine(engine: Engine): void {
    // get entities by component 'Position'
    this.aspect = Aspect.for(engine).all(Position);
  }

  async process(): void {
    const entities = this.aspect.entities;
    entities.forEach(entity => {
      const position = entity.components.get(Position);
      //... do your logic here
    });
  }
}

Note that process can be async. If your systems need to do asynchronous tasks, you can implement them as those. Your engine can then run them as such. This might be useful, if you do not have data which needs to be processed every frame.

In order to keep your focus on the actual system and not the boilerplate code around, you can use the AbstractEntitySystem.

The class will help you by providing component types for directly defining an aspect for your system. The above code would become:

import { System, Aspect } from '@trixt0r/ecs';

class MySystem extends AbstractEntitySystem<MyEntity> {

  constructor() {
    super(/* optional priority here */, [Position]);
  }

  processEntity(entity: MyEntity): void {
    const position = entity.components.get(Position);
    //... do your logic here
  }
}

Engine

An Engine ties systems and entities together. It holds collections of both types, to which you can register listeners. But you could also register an EngineListener, to listen for actions happening inside an engine.

Here is a minimal example on how to initialize an engine and add systems and/or entities to it:

import { Engine, EngineMode } from '@trixt0r/ecs';

// Init the engine
const engine = new Engine();

engine.systems.add(new MySystem());
engine.entities.add(new MyEntity());

// anywhere in your business logic or main loop
engine.run(delta);

// if you want to perform your tasks asynchronously
engine.run(delta, EngineMode.SUCCESSIVE); // Wait for a task to finish
// or...
engine.run(delta, EngineMode.PARALLEL); // Run all systems asynchronously in parallel

Support

If you find any odd behavior or other improvements, feel free to create issues. Pull requests are also welcome!

Otherwise you can help me out by buying me a coffee.

paypal