const world = new World();
function mySystem() { }
class MyComponent() {}
/* old busted */
const systemFunc = createSystem(world, [MyComponent], mySystem);
function run() {
systemFunc();
}
run();
/* new hotness */
world.addSystem([MyComponent], mySystem);
world.systems.run(); // calls all systems.
Added trackComponent()
function which allows Components to be decorated with a few lifecycle methods (onAdd, onUpdate, onRemove)
import { trackComponent } from '@brochington/ecstatic';
class MyComponent {
something = 'here';
}
const TrackedMyComponent = trackComponent(MyComponent, {
onAdd({ world, component, entity, entities }) {
// Triggers when component is added to an entity.
},
onUpdate({ world, component, property, previousVal, entities }) {
// Triggers when a property on the component is updated.
},
onRemove({ world, component, entity, entities }) {
// Triggers when component is removed from an entity.
}
});
const trackedComp = new TrackedMyComponent();
const entity = world.createEntity().add(trackedComp);
// trackedComp.onAdd() is called.
entity.get(MyComponent /* or TrackedMyComponent */).something = 'there';
// trackedComp.onUpdate() is called.
entity.remove(MyComponent);
// trackedComp.onRemove() is called.
class EntityWithLifecycle extends Entity<CompTypes> {
onCreate(): void {}
onTrackedComponentUpdate(): void {}
onDestroy(): void {}
}
// Note that you must pass in the world, instead of just using world.createEntity().
const firstLCComp = new EntityWithLifecycle(world);
-
entities now have a state that can be check via
entity.state
.- possible states:
creating
created
destroying
destroyed
error
creating
anddestroying
are particularly useful in systems:
function mySystem({ entity }) { if (entity.state === 'creating') { // do initialization logic here. } if (entity.state === 'destroying') { // do any cleanup that is needed. Remove event handlers, unmount elements, etc. } }
- possible states:
-
If needed, entities can be destroyed by calling
entity.destroyImmediately()
.entity.state
will immediately be set todestroyed
. -
this will be bypassed if no systems are added.
/* old busted */
const entity = createEntity(world);
/* new hotness */
const entity = world.createEntity();
// OR
const entity = new Entity(world);
Typescript: Component type unions that are passed into World are no longer "typeof Component" unions, and just "Union".
/* old busted */
const compTypes = typeof Component1 | typeof Component2;
/* new hotness */
const compTypes = Component1 | Component2;
this makes internal types much easier