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

bahagi

v0.6.1

Published

A type-based Entity Component System. Implemented in TypeScript

Downloads

22

Readme

Bahagi

Bahagi is a type-based EntityComponentSystem, written in TypeScript. It's purpose is to provide a stable core framework which implements the composition over inheritance pattern.

Bahagi is Filipino for "Component".

Installation

npm install bahagi --save

Running Tests

Tests require Chai, Mocha and ts-node.

npm test

Concept

Entity

The simplest form of an entity is an object with a single key, an identifier to identify the entity. Ultimately, it is an Object that holds components. In this implementation, an EntityCollection is provided to manage entities. The EntityCollection assumes anything is an entity if it has a key of 'components' which is of type ComponentCollection.

Component

A single chunk of data for an entity. Components can be used to add additional functionality to entities. The entity alone is simply an object which has no function. By adding components, you add functionality to the entity. By following the inheritance over composition pattern, your components are chunks of data and identifies a specific function on the entity.

In this implementation, any type can be added as a component. From a class, a number, a boolean or anything else. The ComponentCollection will track and cache components by their type for fast look-up in the Systems. This makes components very flexible and makes it easy to integrate other libraries. If you're using PIXI.js for example, you can use the PIXI.Sprite class as a component on an entity and handle drawing through that component. Logic on the PIXI.Sprite (Such as transformation updates), will be handled through a specific system (Or series of systems).

System

Contains logic which acts on entities, using the components on the entity to identify the components which need logic applied. Typically, the system is built against a specific type of component and is responsible for applying logic to that specific component type.

Usage

Basic

Using the ComponentCollection:

let components = new ComponentCollection();
let stringComponent: string = "My string Comp!";
components.add(stringComponent);
console.log(components.getByType(String));
// ^ Will return an array with 1 item, the value of our stringComponent

Simple entity example:

let myEntity = {
	components: new ComponentCollection()
};
myEntity.components.add(1);
myEntity.components.add(2);
myEntity.components.add(3, 4, 5); // Or you can do it like this
console.log(myEntity.components.getByType(String)); // Returns null
console.log(myEntity.components.getByType(Number)); // Return an array with length of 5: [1, 2, 3, 4, 5]

EntityCollection example:

let entityCollection: EntityCollection = new EntityCollection();
entityCollection.add(myEntity);
console.log(entityCollection.getByComponent(String)); // Returns an empty array
console.log(entityCollection.getByComponent(Number)); // Returns an array where each item is an entity that has the provided component

Tagging Entities and Components:

let myTaggedComponent = {
	tag: "myThing"
};
myEntity.components.add(myTaggedComponent);
console.log(myEntity.components.getByTag("myThing")); // Returns an array with all components tagged "myThing"

Simple CountingSystem example:

class CountingSystem extends AbstractSystem {
	constructor(collection: EntityCollection){
		super(collection);

		setInterval(() => {
			let entitiesWithNumber = collection.getByComponent(Number);

			entitiesWithNumber.forEach((entity: IEntity) => {
				let numberComponents = entity.components.getByType<Number>(Number);
				numberComponents.forEach((num: number, idx) => {
					numberComponents[idx]++;
				});

				console.log("Numbers!", numberComponents); // Increases the count by 1 each second for every component
			})
		}, 1000)
	}
}

new CountingSystem(entityCollection);