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

cell-engine

v0.1.4

Published

Data-driven ECS Game/Application engine.

Downloads

6

Readme

Build Status Inline docs MIT license

Data-driven ECS (Entity Component System) game/application engine.

NOTE

Very alpha. Although basic functionality is there, it's currently lacking lots of features (such as a good event system :).

Installation

npm

npm install cell-engine

Demo

Check out the clockclock demo for a working example. Code can be found here.

Cell System === Entity Component System

In frontend development the term 'component' is usually associated with a partial reusable piece of view ('prefabs' speaking in Unity terms). Since react is used to create such pieces I decided to maintain the term 'component' for 'prefabs' and use the term 'cell' for 'component'. Entities are solely represented by an id.

~~Entity~~ id

Well okay technically entities are still there but solely represented by an id, nothing more than a bag of cells (pieces of data) that store their own entity id. Adding or removing features/traits or changing an 'entity' completely becomes as easy as adding or removing cells.

Cells (data/model, Component in ECS) .json

Cells are pure data! Represented by flat json files. Each cell stores the entity id (eid) that it is part of. This should make serialization of the whole application as simple as saving all cells to json (future work).

// transform.json
{
  "eid": -1,
  "x": 0,
  "y": 0,
  "rotation": 0,
  "scaleX": 1.0,
  "scaleY": 1.0
}

Systems (behaviour/logic/controller) .ts

Systems provide the behaviour that is exhibited by the entities. Systems are designed to act on entities with a certain combination of cells (which is efficiently determined by matching bitmasks). Trigger or untrigger a system for a specific entity is easily achieved by added or removing the appropriate cells. Note that the order in which systems are executed is important to make sure changed data is caught on by the next system.

import System from './System';

export default class Renderer extends System
{
    /**
     * The cell combination to act on.
     */
    public static cells = ['transform'];

    /**
     * Is ran each tick for every entity that matches the cell mask
     * 
     * @param transform - Transform cell. Each update call the next matching entity transform is injected.
     */
    public update(transform)
    {
        const view = this.store.views[this.eid]; // NOTE views are normally not accessed from systems!!

        view.x        = transform.x;
        view.y        = transform.y;
        view.rotation = transform.rotation;
        view.scale.x  = transform.scaleX;
        view.scale.y  = transform.scaleY;

        const sprite = this.getCell('sprite');

        if(!sprite) {return}

        view.anchor.x = sprite.anchorX;
        view.anchor.y = sprite.anchorY;
    }
}

Components (Prefabs) .tsx

Components represent parts of reusable views and their cell data settings. Cells are represented as 'c-' prefixed attributes. The element itself is interpreted as a 'node' cell. React is solely used to create such components, because it allows a readable way to define structure as well as some validation on cells.

import * as React from 'react';

export default
<clock c-clock={{}} c-sprite={{texture: 'img_clock'}}>
    <dial name='dial1'/>
    <dial name='dial2' c-transform={{rotation: Math.PI/2}}/>
</clock>;

Views

Ultimately cell-engine should be renderer independent. View nodes are kept separate in a special array in the store. The render system (only) should translate the application data to the views.

Cache friendly Store

Because the whole application is represented by a collection of json's the complete application can be represented by a 2 dimensional array: cell x eid. Systems can iterate effeciently over all entities and triggered appropriately by using bitmasks that represent the combination of cells. Because systems are executed in a specific order this provides a predictable way in which cells are looked up and should reduce the number of cache misses.

Future work

  • Proper event system
  • Unit tests
  • Input handling using streams (RxJS)
  • Proper tween cell
  • Renderer independence
  • Physics
  • Particle system
  • Serialization/saving loading
  • Dependency injection (for engine code)
  • View porting
  • Optimum store initialization based on system order
  • cell-skeleton project generator
  • etc. etc.