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

phanx-ecs

v0.0.13

Published

Simple lightweight ECS system for TypeScript.

Downloads

30

Readme

Phanx ECS

A simple implementation of a Entity Component System written in TypeScript.

Install

npm install phanx-ecs

Setting up your Entities

You may either use this library by extending the BaseEntity class or by implementing IEntity directly.

1) Extend BaseEntity

import {BaseEntity} from "phanx-ecs/BaseEntity";

export class Person extends BaseEntity {
    ...
}

The BaseEntity class has implemented all methods for you, and you now have access to them.

You will need to optionally call both updateComponents() and disposeComponents() methods if you wish to update (or tick) your components or clean up the components.

2) Implement IEntity

import {IEntity} from "phanx-ecs/IEntity";

export class Person implements IEntity {
    ...
}

To assist in the implementation of the required methods you may use the UtilECS class which exposes the implementations as static methods.

Example:

...
    addComponent(component: IComponent): IComponent {
        return UtilECS.addComponent(this.components, component);
    }
...

See the source code of the BaseEntity class for further examples.

Setting up your Components

Implement IComponent

In the following example we create a Position component.

import {IComponent} from "phanx-ecs/IComponent";

export class Position implements IComponent {

    componentName:string = "Position";

    //properties
    public x:number;
    public y:number;
}

Notice, we had to give the component a name by setting the componentName property, this is a requirement of the interface IComponent.

Optional onAdded() and onRemoved()

You may optionally use the onAdded() and onRemoved() methods to add functionality when the component is added or removed from its parent entity. These methods will be automatically called by the Entity.

Example:

...
   onAdded():void {
      addListener("on_move", this.moved);
   }

   onRemoved():void {
      removeListener("on_move",this.moved);
   }
...

Example

See the example directory for a basic example of an Entity and two Components.

API

IEntity

components:Dictionary<ComponentName, IComponent>

Property.

The Dictionary that stores all components of this entity. Stored by ComponentName:string as the key.

entityChildren:Set<IEntity>

Property.

The Set that stores all entity children of this entity.

addEntityChild(entity:IEntity):IEntity

Adds a child entity to this entity.

Returns: IEntity inserted, useful for chaining.

Parameters:

  • entity:IEntity

removeEntityChild(entity:IEntity):boolean

Removes a child entity from this entity.

Returns: Boolean - if the entity was removed.

Parameters:

  • entity:IEntity

hasEntityChild(entity:IEntity):boolean

Check whether this entity has the child entity.

Returns: Boolean - if the child entity was found.

Parameters:

  • entity:IEntity

getEntityChildren():Array

Returns all child entities on this entity.

Returns: the array of entities

Parameters:

  • None

addComponent(component:IComponent):IComponent

Adds a component to the entity.

Returns: IComponent inserted, useful for chaining.

Parameters:

  • component:IComponent

removeComponent(component:IComponent|ComponentName):boolean

Removes a component from the entity.

Returns: Boolean - if the component was removed.

Parameters:

  • component:IComponent|ComponentName - The component by reference or by name.

hasComponent(component:IComponent|ComponentName):boolean

Check whether the entity has a component.

Returns: Boolean - if the component was found.

Parameters:

  • component:IComponent|ComponentName - The component by reference or by name.

getComponent(component:ComponentName):IComponent

Returns component by name.

Returns: the component or null

Parameters:

  • component:ComponentName - The component by by name.

getComponents():Dictionary<ComponentName, IComponent>

Returns all components on the entity.

Returns: the dictionary of components

Parameters:

  • None

Optional - update(t:number):void

Included on BaseEntity.

Call when you wish to update all components. Calls comp.update(t) method on each component. Calls entity.update(t) method on each child entity.

Optional - dispose():void

Included on BaseEntity.

Call when you wish to dispose all components. Calls comp.dispose() method on each component. Calls entity.dispose() method on each child entity.

IComponent

componentName:string

Give your component a unique name. Recommended to use the Class Name.

Optional - onAdded():void

Called when the component was added to the Entity.

Optional - onRemoved():void

Called when the component was removed from the Entity.

Optional - update():void

Called when from the entity automatically from the entity.update(t) method.

Optional - dispose():void

Called when from the entity automatically from the entity.dispose() method.

Dependencies