ecsy
Version:
Entity Component System in JS
96 lines (75 loc) • 2.14 kB
JavaScript
import { SystemManager } from "./SystemManager.js";
import { EntityManager } from "./EntityManager.js";
import { ComponentManager } from "./ComponentManager.js";
import { Version } from "./Version.js";
import { hasWindow, now } from "./Utils.js";
import { Entity } from "./Entity.js";
const DEFAULT_OPTIONS = {
entityPoolSize: 0,
entityClass: Entity,
};
export class World {
constructor(options = {}) {
this.options = Object.assign({}, DEFAULT_OPTIONS, options);
this.componentsManager = new ComponentManager(this);
this.entityManager = new EntityManager(this);
this.systemManager = new SystemManager(this);
this.enabled = true;
this.eventQueues = {};
if (hasWindow && typeof CustomEvent !== "undefined") {
var event = new CustomEvent("ecsy-world-created", {
detail: { world: this, version: Version },
});
window.dispatchEvent(event);
}
this.lastTime = now() / 1000;
}
registerComponent(Component, objectPool) {
this.componentsManager.registerComponent(Component, objectPool);
return this;
}
registerSystem(System, attributes) {
this.systemManager.registerSystem(System, attributes);
return this;
}
hasRegisteredComponent(Component) {
return this.componentsManager.hasComponent(Component);
}
unregisterSystem(System) {
this.systemManager.unregisterSystem(System);
return this;
}
getSystem(SystemClass) {
return this.systemManager.getSystem(SystemClass);
}
getSystems() {
return this.systemManager.getSystems();
}
execute(delta, time) {
if (!delta) {
time = now() / 1000;
delta = time - this.lastTime;
this.lastTime = time;
}
if (this.enabled) {
this.systemManager.execute(delta, time);
this.entityManager.processDeferredRemoval();
}
}
stop() {
this.enabled = false;
}
play() {
this.enabled = true;
}
createEntity(name) {
return this.entityManager.createEntity(name);
}
stats() {
var stats = {
entities: this.entityManager.stats(),
system: this.systemManager.stats(),
};
return stats;
}
}