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

hecs

v0.17.0

Published

A high performance ECS with a plugin ecosystem

Downloads

9

Readme

Hecs

A high performance lightweight ECS with a plugin ecosystem.

See TECHNICAL.md for details on performance and the internals of this library.

Install

Install via npm/yarn:

yarn add hecs

Quickstart

import { World, System, Component, StateComponent, Types, Not, Modified } from 'hecs'
import { Object3D } from './types'

class Model extends Component {
  static props = {
    type: Types.Text,
  }
}

class Mesh extends StateComponent {
  static props = {
    value: Object3D,
  }
}

class ModelSystem extends System {
  static queries = {
    added: [Model, Not(Mesh)],
    modified: [Modified(Model), Mesh],
    removed: [Not(Model), Mesh],
  }

  update() {
    this.queries.added.forEach(entity => {
      const model = entity.get(Model)
      const object3d = getModel(model.type)
      entity.add(Mesh, { value: object3d })
    })
    this.queries.modified.forEach(entity => {
      entity.remove(Mesh)
      const model = entity.get(Model)
      const object3d = getModel(model.type)
      entity.add(Mesh, { value: object3d })
    })
    this.queries.removed.forEach(entity => {
      const object3d = entity.get(Mesh).value
      object3d.dispose()
      entity.remove(Mesh)
    })
  }
}

const world = new World({
  systems: [ModelSystem],
  components: [Model, Mesh]
})

world.entities.create().add(Model, { type: 'basketball' }).activate()

function update() {
  world.update()
  requestAnimationFrame(update)
}
update()

Worlds

Creating a world

import { World } from 'hecs'
import CorePlugin from 'hecs-plugin-core'
import MySystem from './MySystem'
import MyComponent from './MyComponent'

const world = new World({
  plugins: [CorePlugin],
  systems: [MySystem],
  components: [MyComponent]
})

Updating the world

world.update()

Entities

Creating an entity

const basketball = world.entities.create()

Entity Hierarchy

const parent = world.entities.create()
const child = world.entities.create()

child.setParent(parent) // use `null` to unset
parent.traverse(entity => {
  // called for this and all descendants
})
child.traverseAscendants(entity => {
  // called for all ascendants
})
parent.getChildren() // [child]
child.getParent() // parent

Adding components

basketball.add(Model, { path: 'basketball.glb' })

Retrieving components

const model = basketball.get(Model)

Marking a component as modified

If you use queries that need to know when components are modified you will need to mark them as modified.

const model = basketball.get(Model)
model.path = 'basketball-2.glb'
model.modified()

Removing components

basketball.remove(Model)

Activating an entity

Entities are inactive by default and are not simulated in world queries until they are activated:

basketball.activate()

Deactivating an entity

If you want to remove an entity from the world but still keep it around, use deactivate. When deactivated, all Components are temporarily removed except StateComponents, so that System queries can still process and deallocate any resources. Once all StateComponents are removed the enitity is completely removed from all queries.

basketball.deactivate()

Destroying an entity

If you want to completely remove an entity, use destroy. All Components are removed except StateComponents so that System queries can still process and deallocate resources. Once all StateComponents are removed the entity is completely destroyed.

basketball.destroy()

Components

Defining components

import { Component, Types } from 'hecs'

export class Model extends Component {
  static props = {
    path: Types.Text
  }
}

LocalComponents

LocalComponents work exactly the same as Components except they are ignored when calling world.toJSON() or entity.toJSON(). LocalComponents should be used in a network environment when the data doesn't need to be synced between other servers or clients.

import { LocalComponent } from 'hecs'
import { Axis } from './types'

export class Input extends LocalComponent {
  static props = {
    axis: Types.Axis2D
  }
}

StateComponents

StateComponents work the same as regular Components but are kept around after an entity is deactivated or destroyed, so that Systems can process or deallocate resources.

import { StateComponent } from 'hecs'
import { Object3D } from './types'

export class Mesh extends StateComponent {
  static props = {
    value: Types.Object3D
  }
}

Systems

Creating systems

import { System, Not, Changed } from 'hecs'
import { Mesh } from './components'

export class ModelSystem extends System {
  static queries = {
    added: [Model, Not(Mesh)],
    modified: [Modified(Model), Mesh],
    removed: [Not(Model), Mesh],
  }

  update() {
    this.queries.added.forEach(entity => {
      const model = entity.get(Model)
      const object3d = getModel(model.type)
      entity.add(Mesh, { value: object3d })
    })
    this.queries.modified.forEach(entity => {
      entity.remove(Mesh)
      const model = entity.get(Model)
      const object3d = getModel(model.type)
      entity.add(Mesh, { value: object3d })
    })
    this.queries.removed.forEach(entity => {
      const object3d = entity.get(Mesh).value
      object3d.dispose()
      entity.remove(Mesh)
    })
  }
}

Plugins

Creating a plugin

Plugins are a collection of things such as Systems and Components that you can use in your world, or compose with other plugins.

The Plugin options are exactly the same as when instantiating a new World:

import { createPlugin } from 'hecs'

export default createPlugin({
  name: 'my-plugin',
  plugins: [],
  systems: [],
  components: [],
  decorate(world) {}
})

name: The name of your plugin, used for debugging purposes.

plugins: A plugin can depend on other plugins. If two plugins depend on the same plugin it will only be registered once.

systems: An array of Systems to add to the world.

components: An array of Components to register.

decorate: A function that provides the world instance. Useful for constructing shared services, eg scenes.