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

naive-ts-ecs

v1.4.0-rc1

Published

A naive implementation of ECS pattern

Downloads

30

Readme

Ts-ECS

A naive implementation of the ECS pattern in typescript!

npm package can be found here

quick guide

  • Be sure to checkout the wave shooter demo for a practical example
  • Make sure to install the package, preferably through npm, but you could use the master branch if you want to, although expect breaking changes.

This is a WIP project and things are bound to change. I strive to make it as user friendly as possible, but there is a lot to reach that goal. That being said. This is how you can use 1.4:

// create some types that represent components
interface Circle {
  radius: number;
}

interface Position {
  x: number;
  y: number;
}

interface Colored {
  color: string;
}


// create a manager
const manager = new ECSManager();

// register your types in the manager with default values
registerComponentType<Circle>(manager, { radius: 10 });
registerComponentType<Position>(manager, { x: 0, y: 0 });
registerComponentType<Colored>(manager, { color: '#ff0000' });

// create an entity and attach components to them
const { entityId } = manager.createEntity();
const radius = Math.random() * circleMaxSize;
addComponent<Circle>(manager, entityId, { radius: radius });
addComponent<Position>(manager, entityId, { x: Math.random() * canvas.width, y: Math.random() * canvas.height });
addComponent<Colored>(manager, entityId, { color: '#'+(Math.random()*0xFFFFFF<<0).toString(16) });

// create systems that will run each frame 
// all systems take in delta time as their first argument (the time since last frame)
// all other arguments needs to be wrapped in a Component<T>
const drawCircleSystem = (dt: number, circle: Component<Circle>, pos: Component<Position>, color: Component<Colored>) => {
    ctx.beginPath();
    ctx.strokeStyle = color.data.color;
    ctx.arc(pos.data.x, pos.data.y, circle.data.radius, 0, 2 * Math.PI);
    ctx.stroke();
}

// For simple query requirements ts-ecs can generate the query for you base on your system
// Simple meaning only "AND" so in this case we ask for all entities with circle and position and color
registerSystem(manager, drawCircleSystem);

manager.dispatch();

See the circle demo for a better overview of a simple application.

Using this project

This project utilizes typescript transformers. In order for it to work in your project you will have to enable the transformers. How you do this depends on the project facilities:

In Webpack

const managerEndpointsTransformer = require('naive-ts-ecs/manager-endpoints.transformer').default; // <--

module.exports = ['ts-loader'].map(loader => ({
   // ... omitted
    module: {
        rules: [
            {
                // ... omitted
                options: {
                    // make sure not to set `transpileOnly: true` here, otherwise it will not work
                    getCustomTransformers: program => ({
                        before: [
                            managerEndpointsTransformer(program)
                        ]
                    })
                }
            },
        ],
    },
}));

read more about webpack here

in ttypescript

Remeber to build using ttsc not tsc

{
    ...
    "compilerOptions": {
        ...
        "plugins": [
            { "transform": "naive-ts-ecs/manager-endpoints.transformer" },
        ]
    }
    ...
}

read more about ttypescript here

Documentation

Open index.html from the docs folder in a browser

You can also build the documentation using npm run doc

How to build

Make sure you have node and npm

Install dependencies:

npm install

To build:

npm run build

To run tests:

npm test

Examples

You can check out my simple wave shooter implementation here