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

@skyboxgg/bjs-ecs

v0.4.0

Published

A simple ECS for Babylon.js

Downloads

47

Readme

bjs-esc

ECS for Babylon.js

Objectives

Provide an easy way to help structure your game logic for Babylon.js based projects using ECS. This is (currently) more about the functionality of ECS than the performance benefits. In short if you are looking for an ECS implementation to handle 10Ks of entities, you may want to look elsewhere.

  • ✅ (current) Focus on developer ergonomics, e.g. being TypeScript idiomatic
  • ✅ (current) Tight integration with Babylon.js
  • ✅ (current) Optimize query performance
  • ❓ (not currently in scope) Optimized data storage for Entities/Components

Sample

To run the sample:

git clone https://github.com/Skybox-Technologies/bjs-ecs.git
cd bjs-ecs
npm install
npx nx run sample:serve

See also CodeSandbox

Installation

npm install @skyboxgg/bjs-ecs

Usage

Core ECS:

import { addEntity, createComponent, queryEntities, removeEntity } from '@skyboxgg/bjs-ecs';

// add entity with tags
const player = addEntity(['actor', 'player']);

// define components
const color = createComponent('color', (hex: string) => hex);
const door = createComponent('door', (isLocked: boolean) => ({isLocked}));

// Subscribe to events when entities are added to the world.
entityEvents.on('add', [door], (entity) => {
  // This callback is invoked whenever an entity, which includes the components specified in the second argument, is added to the world.
  console.log('Entity added: ', entity.door);
});

// Subscribe to events when entities are removed from the world.
entityEvents.on('remove', [door], (entity) => {
  // This callback is invoked whenever an entity, which includes the components specified in the second argument, is removed from the world.
  console.log('Entity removed: ', entity.door);
});

// add entity with components (and tag)
// component properties propagate to entity with typing
const redDoor = addEntity([door(true), color('#ff0000'), 'static']);
console.log('redDoor color:', redDoor.color);
console.log('redDoor lock status:', redDoor.locked);

const greenDoor = addEntity([door(false), color('#00ff00'), 'static']);
console.log('greenDoor color:', greenDoor.color);
console.log('greenDoor lock status:', greenDoor.locked);

// query entities by component
// result is typed
const entities = queryEntities([door, color]);
for (const entity of entities) {
  console.log('entity color:', entity.color);
  console.log('entity door lock status:', entity.door.locked);
}

// remove entity from world
removeEntity(entities[0]);

With Babylon.js

Babylon Nodes can be added as entities, and subtypes (TransformNode, Mesh) as well as PhysicsBody are automatically detected as additional components.

Nodes are automatically disposed from the Babylon scenegraph when removed from the ECS world and vice versa.

There is also initial support to show entity ID and list of components / tags, for Nodes in the Babylon inspector

Inspector Support

import { Scene } from '@babylonjs/core/scene';
import { addNodeEntity, queryXforms } from '@skyboxgg/bjs-ecs';

function setupScene(scene: Scene) {
  // other scene setup: camera, lights, etc

  // create and add entities
  const sphere1 = MeshBuilder.CreateSphere('player1', { diameter: 1, segments: 32 }, scene);
  addNodeEntity(sphere1, ['player']);

  const sphere2 = MeshBuilder.CreateSphere('player2', { diameter: 1, segments: 32 }, scene);
  addNodeEntity(sphere2, ['player']);

  const ground = MeshBuilder.CreateGround('ground', { width: 8, height: 6 }, scene);
  addNodeEntity(ground, ['ground']);

  // query transform entities (with typing)
  for(const player of queryXforms(['player'])) {
    player.xform.position.y += 2;
  }
}

Acknowledgements

  • The interface and TypeScript Fu for adding entities with components, is inspired by Kaboom.js