Comparing version 0.1.1 to 0.1.2
@@ -154,11 +154,2 @@ <!-- Generated by documentation.js. Update this documentation by updating the source code. --> | ||
## createComponent | ||
Create a component class from a schema | ||
### Parameters | ||
- `schema` | ||
- `name` | ||
## createType | ||
@@ -176,2 +167,11 @@ | ||
## createComponentClass | ||
Create a component class from a schema | ||
### Parameters | ||
- `schema` | ||
- `name` | ||
[1]: #system | ||
@@ -178,0 +178,0 @@ |
@@ -164,3 +164,3 @@ # ECSY Architecture | ||
Please notice that creating a component using the `createComponent` helper (**Link**) will include a `reset` implementation. | ||
Please notice that creating a component using the `createComponentClass` helper (**Link**) will include a `reset` implementation. | ||
@@ -167,0 +167,0 @@ ### Create component helper |
{ | ||
"name": "ecsy", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Entity Component System in JS", | ||
@@ -5,0 +5,0 @@ "main": "build/ecsy.js", |
@@ -7,3 +7,2 @@ import Query from "./Query.js"; | ||
// @todo reset it by world? | ||
var nextId = 0; | ||
@@ -39,2 +38,4 @@ | ||
this._ComponentTypesToRemove = []; | ||
this.alive = false; | ||
} | ||
@@ -41,0 +42,0 @@ |
@@ -6,2 +6,3 @@ import Entity from "./Entity.js"; | ||
import { componentPropertyName, getName } from "./Utils.js"; | ||
import { SystemStateComponent } from "./SystemStateComponent.js"; | ||
@@ -27,2 +28,4 @@ /** | ||
this.entitiesToRemove = []; | ||
this.numStateComponents = 0; | ||
} | ||
@@ -35,2 +38,3 @@ | ||
var entity = this._entityPool.aquire(); | ||
entity.alive = true; | ||
entity._world = this; | ||
@@ -55,2 +59,6 @@ this._entities.push(entity); | ||
if (Component.__proto__ === SystemStateComponent) { | ||
this.numStateComponents++; | ||
} | ||
var componentPool = this.world.componentsManager.getComponentsPool( | ||
@@ -83,5 +91,5 @@ Component | ||
* @param {*} Component Component to remove from the entity | ||
* @param {Bool} forceRemove If you want to remove the component immediately instead of deferred (Default is false) | ||
* @param {Bool} immediately If you want to remove the component immediately instead of deferred (Default is false) | ||
*/ | ||
entityRemoveComponent(entity, Component, forceRemove) { | ||
entityRemoveComponent(entity, Component, immediately) { | ||
var index = entity._ComponentTypes.indexOf(Component); | ||
@@ -92,3 +100,3 @@ if (!~index) return; | ||
if (forceRemove) { | ||
if (immediately) { | ||
this._entityRemoveComponentSync(entity, Component, index); | ||
@@ -110,2 +118,11 @@ } else { | ||
this._queryManager.onEntityComponentRemoved(entity, Component); | ||
if (Component.__proto__ === SystemStateComponent) { | ||
this.numStateComponents--; | ||
// Check if the entity was a ghost waiting for the last system state component to be removed | ||
if (this.numStateComponents === 0 && !entity.alive) { | ||
entity.remove(); | ||
} | ||
} | ||
} | ||
@@ -128,7 +145,8 @@ | ||
*/ | ||
entityRemoveAllComponents(entity, forceRemove) { | ||
entityRemoveAllComponents(entity, immediately) { | ||
let Components = entity._ComponentTypes; | ||
for (let j = Components.length - 1; j >= 0; j--) { | ||
this.entityRemoveComponent(entity, Components[j], forceRemove); | ||
if (Components[j].__proto__ !== SystemStateComponent) | ||
this.entityRemoveComponent(entity, Components[j], immediately); | ||
} | ||
@@ -140,5 +158,5 @@ } | ||
* @param {Entity} entity Entity to remove from the manager | ||
* @param {Bool} forceRemove If you want to remove the component immediately instead of deferred (Default is false) | ||
* @param {Bool} immediately If you want to remove the component immediately instead of deferred (Default is false) | ||
*/ | ||
removeEntity(entity, forceRemove) { | ||
removeEntity(entity, immediately) { | ||
var index = this._entities.indexOf(entity); | ||
@@ -148,21 +166,21 @@ | ||
// Remove from entity list | ||
this.eventDispatcher.dispatchEvent(ENTITY_REMOVED, entity); | ||
this._queryManager.onEntityRemoved(entity); | ||
entity.alive = false; | ||
if (forceRemove === true) { | ||
this._removeEntitySync(entity, index, true); | ||
} else { | ||
this.entityRemoveAllComponents(entity); | ||
this.entitiesToRemove.push(entity); | ||
if (this.numStateComponents === 0) { | ||
// Remove from entity list | ||
this.eventDispatcher.dispatchEvent(ENTITY_REMOVED, entity); | ||
this._queryManager.onEntityRemoved(entity); | ||
if (immediately === true) { | ||
this._releaseEntity(entity, index); | ||
} else { | ||
this.entitiesToRemove.push(entity); | ||
} | ||
} | ||
this.entityRemoveAllComponents(entity, immediately); | ||
} | ||
_removeEntitySync(entity, index, removeAllComponents) { | ||
_releaseEntity(entity, index) { | ||
this._entities.splice(index, 1); | ||
if (removeAllComponents) { | ||
this.entityRemoveAllComponents(entity, true); | ||
} | ||
// Prevent any access and free | ||
@@ -178,3 +196,3 @@ entity._world = null; | ||
for (var i = this._entities.length - 1; i >= 0; i--) { | ||
this._entities[i].remove(); | ||
this.removeEntity(this._entities[i]); | ||
} | ||
@@ -187,3 +205,3 @@ } | ||
let index = this._entities.indexOf(entity); | ||
this._removeEntitySync(entity, index, false); | ||
this._releaseEntity(entity, index); | ||
} | ||
@@ -190,0 +208,0 @@ this.entitiesToRemove.length = 0; |
export { World } from "./World.js"; | ||
export { System, Not } from "./System.js"; | ||
export { Component } from "./Component.js"; | ||
export { SystemStateComponent } from "./SystemStateComponent.js"; | ||
export { TagComponent } from "./TagComponent.js"; | ||
export { createComponent } from "./CreateComponent.js"; | ||
export { createComponentClass } from "./CreateComponentClass.js"; | ||
export { createType } from "./CreateType.js"; | ||
export { Types } from "./StandardTypes.js"; |
import Query from "./Query.js"; | ||
import { componentPropertyName } from "./Utils.js"; | ||
@@ -90,3 +89,18 @@ /** | ||
); | ||
} else if (Array.isArray(event)) { | ||
let eventList = (this.queries[queryName][eventName] = []); | ||
query.eventDispatcher.addEventListener( | ||
Query.prototype.COMPONENT_CHANGED, | ||
(entity, changedComponent) => { | ||
// Avoid duplicates | ||
if ( | ||
event.indexOf(changedComponent.constructor) !== -1 && | ||
eventList.indexOf(entity) === -1 | ||
) { | ||
eventList.push(entity); | ||
} | ||
} | ||
); | ||
} else { | ||
/* | ||
// Checking just specific components | ||
@@ -110,2 +124,3 @@ let changedList = (this.queries[queryName][eventName] = {}); | ||
}); | ||
*/ | ||
} | ||
@@ -112,0 +127,0 @@ } else { |
@@ -8,2 +8,3 @@ /** | ||
this._systems = []; | ||
this._executeSystems = []; // Systems that have `execute` method | ||
this.world = world; | ||
@@ -21,2 +22,3 @@ } | ||
this._systems.push(system); | ||
if (system.execute) this._executeSystems.push(system); | ||
this.sortSystems(); | ||
@@ -27,3 +29,3 @@ return this; | ||
sortSystems() { | ||
this._systems.sort((a, b) => { | ||
this._executeSystems.sort((a, b) => { | ||
return a.priority - b.priority || a.order - b.order; | ||
@@ -65,5 +67,5 @@ }); | ||
execute(delta, time) { | ||
this._systems.forEach(system => { | ||
this._executeSystems.forEach(system => { | ||
if (system.enabled && system.initialized) { | ||
if (system.execute && system.canExecute()) { | ||
if (system.canExecute()) { | ||
let startTime = performance.now(); | ||
@@ -70,0 +72,0 @@ system.execute(delta, time); |
@@ -599,3 +599,5 @@ global.performance = | ||
class ReactiveSystem extends System {} | ||
class ReactiveSystem extends System { | ||
execute() {} | ||
} | ||
@@ -650,5 +652,5 @@ ReactiveSystem.queries = { | ||
entity0.getMutableComponent(FooComponent); | ||
//t.is(query.changed.length, 1); | ||
t.is(query.changed.fooComponent.length, 1); | ||
t.is(query.changed.barComponent.length, 0); | ||
t.is(query.changed.length, 1); | ||
//t.is(query.changed.fooComponent.length, 1); | ||
//t.is(query.changed.barComponent.length, 0); | ||
world.execute(); // After execute, events should be cleared | ||
@@ -658,20 +660,20 @@ // t.is(query.changed.length, 0); | ||
entity0.getMutableComponent(BarComponent); | ||
//t.is(query.changed.length, 1); | ||
t.is(query.changed.fooComponent.length, 0); | ||
t.is(query.changed.barComponent.length, 1); | ||
t.is(query.changed.length, 1); | ||
//t.is(query.changed.fooComponent.length, 0); | ||
//t.is(query.changed.barComponent.length, 1); | ||
world.execute(); // After execute, events should be cleared | ||
//t.is(query.changed.length, 0); | ||
t.is(query.changed.barComponent.length, 0); | ||
t.is(query.changed.length, 0); | ||
//t.is(query.changed.barComponent.length, 0); | ||
// Check if the entity is already on the list? | ||
entity0.getMutableComponent(FooComponent); | ||
entity0.getMutableComponent(BarComponent); | ||
//t.is(query.changed.length, 1); | ||
t.is(query.changed.fooComponent.length, 1); | ||
t.is(query.changed.barComponent.length, 1); | ||
t.is(query.changed.length, 1); | ||
//t.is(query.changed.fooComponent.length, 1); | ||
//t.is(query.changed.barComponent.length, 1); | ||
world.execute(); // After execute, events should be cleared | ||
//t.is(query.changed.length, 0); | ||
t.is(query.changed.fooComponent.length, 0); | ||
t.is(query.changed.barComponent.length, 0); | ||
t.is(query.changed.length, 0); | ||
//t.is(query.changed.fooComponent.length, 0); | ||
//t.is(query.changed.barComponent.length, 0); | ||
@@ -678,0 +680,0 @@ // remove an entity |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
483956
73
7638