Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
A high performance lightweight ECS with a plugin ecosystem.
See TECHNICAL.md for details on performance and the internals of this library.
Install via npm/yarn:
yarn add hecs
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()
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]
})
world.update()
const basketball = world.entities.create()
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
basketball.add(Model, { path: 'basketball.glb' })
const model = basketball.get(Model)
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()
basketball.remove(Model)
Entities are inactive by default and are not simulated in world queries until they are activated:
basketball.activate()
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()
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()
import { Component, Types } from 'hecs'
export class Model extends Component {
static props = {
path: Types.Text
}
}
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 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
}
}
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 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.
FAQs
A high performance ECS with a plugin ecosystem
The npm package hecs receives a total of 0 weekly downloads. As such, hecs popularity was classified as not popular.
We found that hecs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.