Comparing version 0.2.12 to 0.2.13
{ | ||
"name": "bitecs", | ||
"version": "0.2.12", | ||
"version": "0.2.13", | ||
"description": "Tiny, data-driven, high performance ECS library written in Javascript", | ||
@@ -5,0 +5,0 @@ "license": "MPL-2.0", |
122
README.md
@@ -20,37 +20,87 @@ # 👾 bitECS 👾 | ||
```js | ||
import { | ||
// this is the entire API | ||
import { | ||
createWorld, | ||
addEntity, | ||
removeEntity, | ||
registerComponent, | ||
registerComponents, | ||
defineComponent, | ||
addComponent, | ||
removeComponent, | ||
defineQuery, | ||
Changed, | ||
Not, | ||
enterQuery, | ||
exitQuery, | ||
defineSystem, | ||
addComponent, | ||
removeComponent, | ||
addEntity, | ||
removeEntity, | ||
pipe, | ||
Types, | ||
defineSerializer, | ||
defineDeserializer, | ||
} from 'bitecs' | ||
// create a world | ||
/** createWorld | ||
* | ||
* Creates a world which represents a set of entities and what components they possess. | ||
* Does NOT store actual component data. | ||
* Create as many worlds as you want. | ||
**/ | ||
// returns an empty object which can be used to store miscellaneous state about the world | ||
const world = createWorld() | ||
const world2 = createWorld() | ||
// define component data stores | ||
const { f32 } = Types | ||
/** defineComponent | ||
* | ||
* Returns a SoA (Structure of Arrays). | ||
* Store of component data. | ||
**/ | ||
// available types | ||
const { bool, i8, ui8, ui8c, i16, ui16, i32, ui32, f32, f64 } = Types | ||
// schema for a component | ||
const Vector2 = { x: f32, y: f32 } | ||
// define components, which creates SoA data stores | ||
const Position = defineComponent(Vector2) | ||
const Velocity = defineComponent(Vector2) | ||
const Health = defineComponent(f32) | ||
const Health = defineComponent(ui16) | ||
const Alive = defineComponent() // "tag" component | ||
const Mapping = defineComponent(new Map()) // can pass any data structure to use | ||
const SetOfStuff = defineComponent(new Set()) | ||
const RegularObject = defineComponent({}) | ||
// register components on world | ||
// register components with worlds | ||
registerComponents(world, [Position, Velocity, Health]) // in groups | ||
registerComponent(world, Alive) // or individually | ||
/** defineQuery | ||
* | ||
* Returns a query function which returns set of entities from a world that match the given components. | ||
**/ | ||
// define a query using components | ||
const movementQuery = defineQuery([Position, Velocity]) | ||
// use the query on a world | ||
const ents = movementQuery(world) | ||
// wrapping a component with the Change modifier creates a query which | ||
// returns entities whose component's state has changed since last call of the function | ||
const changedPositionQuery = defineQuery([ Changed(Position) ]) | ||
// wrapping a component with the Not modifier creates a query which | ||
// returns entities who explicitly do not have the component | ||
const notVelocityQuery = defineQuery([ Position, Not(Velocity) ]) | ||
// enter-query hook, called when an entity's components matches the query | ||
@@ -62,4 +112,14 @@ enterQuery(world, movementQuery, eid => {}) | ||
// define a system using the query | ||
const movementSystem = defineSystem(movementQuery, ents => { | ||
/** defineSystem | ||
* | ||
* Creates a function which can be processed against a given world. | ||
* Use queries to access relevant entities for the system. | ||
* | ||
* Note: Entity and component removals are deferred until the system has finished running. | ||
**/ | ||
// define a system | ||
const movementSystem = defineSystem(world => { | ||
const ents = movementQuery(world) | ||
for (let i = 0; i < ents.length; i++) { | ||
@@ -72,14 +132,34 @@ const eid = ents[i]; | ||
// add an entity to the world | ||
/** Entity | ||
* | ||
* An entity is a single ID which components can be associated with. | ||
* Entities are accessed via queries, components of whom are mutated with systems. | ||
**/ | ||
// add entities to the world | ||
const eid = addEntity(world) | ||
const eid2 = addEntity(world) | ||
// add components to the new entity in the world | ||
// remove entities from the world (deferred until any system runs) | ||
removeEntity(world, eid2) | ||
// add components to the new entities in the world | ||
addComponent(world, Position, eid) | ||
addComponent(world, Velocity, eid) | ||
// remove components from entities in the world (deferred until any system runs) | ||
removeComponent(world, Velocity, eid) | ||
// there are no component getters or setters | ||
// data is accessed directly by entity ID | ||
Velocity.x[eid] = 1 | ||
Velocity.y[eid] = 2 | ||
Position.x[eid] = 1 | ||
Position.y[eid] = 2 | ||
/** Pipe | ||
* | ||
* Creates a sequence of systems which are executed in serial. | ||
**/ | ||
const pipeline = pipe( | ||
@@ -91,6 +171,12 @@ movementSystem, | ||
/** | ||
* Update worlds with systems or pipelines of systems. | ||
**/ | ||
movementSystem(world) // executes movement system on world | ||
pipeline(world) // executes a pipeline of systems on world | ||
``` | ||
Full documentation and feature rich examples can be found [here](DOCS.md). |
37
test.js
@@ -30,11 +30,11 @@ import { performance} from'perf_hooks' | ||
const Velocity = defineComponent(Vector2) | ||
const a = defineComponent(new Map()) | ||
registerComponents(world, [Position, Velocity, a]) | ||
registerComponents(world, [Position, Velocity]) | ||
const query = defineQuery([Changed(Position),Velocity]) | ||
const queryA = defineQuery([a]) | ||
const query = defineQuery([Position,Velocity]) | ||
const serialize = defineSerializer([Changed(Position)]) | ||
const deserialize = defineDeserializer([Position,Velocity]) | ||
const n = 100_000 | ||
const notPosition = defineQuery([Not(Position), Velocity]) | ||
const n = 1 | ||
for (let i = 0; i < n; i++) { | ||
@@ -44,3 +44,2 @@ const eid = addEntity(world) | ||
addComponent(world, Velocity, eid) | ||
addComponent(world, a, eid) | ||
Position.x[eid]++ | ||
@@ -50,19 +49,21 @@ Position.y[eid]++ | ||
console.log(queryA(world)) | ||
const eid = addEntity(world) | ||
addComponent(world, Velocity, eid) | ||
console.log(notPosition(world)) | ||
let then | ||
// let then | ||
console.log(`n = ${n}`) | ||
// console.log(`n = ${n}`) | ||
then = now() | ||
const ents = query(world) | ||
console.log('query', (now()-then).toFixed(2)) | ||
// then = now() | ||
// const ents = query(world) | ||
// console.log('query', (now()-then).toFixed(2)) | ||
then = now() | ||
const packet = serialize(ents) | ||
console.log('serialize', (now()-then).toFixed(2)) | ||
// then = now() | ||
// const packet = serialize(ents) | ||
// console.log('serialize', (now()-then).toFixed(2)) | ||
then = now() | ||
deserialize(packet) | ||
console.log('deserialize', (now()-then).toFixed(2)) | ||
// then = now() | ||
// deserialize(packet) | ||
// console.log('deserialize', (now()-then).toFixed(2)) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
225523
179