Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
gg-entities
Advanced tools
easy-to-use Entity-Component System for browsers and Node.js
import { EntityManager } from 'gg-entities'
const entityManager = new EntityManager()
// 1. Register Components
const pos = entityManager.registerComponent('position', {
x : 10.0,
y : 10.0
})
const vel = entityManager.registerComponent('velocity', 2.0)
// 2. Register Systems
function movementSystem(entities, { delta }) {
for (const {entity} of entities) {
entity[pos].x += entity[vel] * delta
}
}
entityManager.registerLogicSystem([ pos, vel ], movementSystem)
function logSystem(entities) {
for (const {entity} of entities) {
console.log(entity[pos].x, entity[pos].y)
}
}
entityManager.registerRenderSystem([ pos ], logSystem)
// 3. Add an entity
entityManager
.build()
.withComponent(pos)
.withComponent(vel)
.create(1)
// 4. Run the systems
entityManager.onLogic({ delta: 16 }) // invokes all logic systems (movementSystem)
entityManager.onRender({ delta: 16 }) // invokes all render systems (logSystem)
http://ggalansmithee.github.io/Entities/
There are no official typings (yet), but @ForsakenHarmony has provided a gist for TypeScript users.
EntityManager
as its context, a system must be a regular function (not an es6 arrow function)Accessing an entity's components in a system usually looks like this
function movementSystem(entities) {
for (const { entity } of entities) {
entity[POS_COMPONENT].x += entity[VEL_COMPONENT].x * delta
entity[POS_COMPONENT].y += entity[VEL_COMPONENT].y * delta
}
}
which can be a bit ugly, especially if your entity has a lot of components which are accessed multiple times. Using some ES6 (computed property keys) and ES7 (object spread) magic, we can make it a bit more concise:
function movementSystem(entities) {
for (const { entity: { [POS_COMPONENT]: pos, [VEL_COMPONENT]: vel } } of entities) {
pos.x += vel.x * delta
pos.y += vel.y * delta
}
}
Got questions or want to leave feedback? create an issue
Got improvements? Feel free to send a PR to the dev
branch (don't forget to add tests)
npm run build
builds the projectnpm run test
runs the test suitev3.0.0-beta.2 (2018-09-11)
Renamed entityManager.iterateEntities
to entityManager.getEntitiesByComponents
SystemManager.registerSystem
now prevents registering a system
with a key
that's already used
Added gotchas.md
doc
FAQs
easy-to-use Entity-Component System in JavaScript
The npm package gg-entities receives a total of 2 weekly downloads. As such, gg-entities popularity was classified as not popular.
We found that gg-entities 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.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.