@antv/g-ecs
Advanced tools
Comparing version 1.0.0-alpha.6 to 1.0.0-alpha.7
@@ -1,45 +0,37 @@ | ||
var _dec, _class; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
import { __decorate } from "tslib"; | ||
import { injectable } from 'inversify'; | ||
var counter = 0; | ||
export var Component = (_dec = injectable(), _dec(_class = /*#__PURE__*/function () { | ||
var Component = function () { | ||
function Component() { | ||
_classCallCheck(this, Component); | ||
// eslint-disable-next-line no-plusplus | ||
this.pid = counter++; | ||
} | ||
_createClass(Component, [{ | ||
key: "getId", | ||
value: function getId() { | ||
return this.pid; | ||
} | ||
}, { | ||
key: "copy", | ||
value: function copy(props) { | ||
Object.assign(this, props); | ||
return this; | ||
} | ||
}, { | ||
key: "clone", | ||
value: function clone() { | ||
return new Component().copy(this); | ||
} | ||
}, { | ||
key: "reset", | ||
value: function reset() {// | ||
} | ||
}, { | ||
key: "destroy", | ||
value: function destroy() {// | ||
} | ||
}]); | ||
Component_1 = Component; | ||
Component.prototype.getId = function () { | ||
return this.pid; | ||
}; | ||
Component.prototype.copy = function (props) { | ||
Object.assign(this, props); | ||
return this; | ||
}; | ||
Component.prototype.clone = function () { | ||
return new Component_1().copy(this); | ||
}; | ||
Component.prototype.reset = function () {// | ||
}; | ||
Component.prototype.destroy = function () {// | ||
}; | ||
var Component_1; | ||
Component = Component_1 = __decorate([injectable()], Component); | ||
return Component; | ||
}()) || _class); | ||
}(); | ||
export { Component }; |
@@ -1,37 +0,18 @@ | ||
var _dec, _dec2, _class, _class2, _descriptor; | ||
function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; } | ||
function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'proposal-class-properties is enabled and runs after the decorators transform.'); } | ||
import { __decorate, __metadata } from "tslib"; | ||
import { inject, injectable } from 'inversify'; | ||
import { IDENTIFIER } from './identifier'; | ||
export var ComponentManager = (_dec = injectable(), _dec2 = inject(IDENTIFIER.ComponentRegistry), _dec(_class = (_class2 = /*#__PURE__*/function () { | ||
function ComponentManager() { | ||
_classCallCheck(this, ComponentManager); | ||
_initializerDefineProperty(this, "registry", _descriptor, this); | ||
} | ||
var ComponentManager = function () { | ||
function ComponentManager() {} | ||
_createClass(ComponentManager, [{ | ||
key: "registerComponent", | ||
value: function registerComponent(clazz) { | ||
this.registry(clazz); | ||
} | ||
}]); | ||
ComponentManager.prototype.registerComponent = function (clazz) { | ||
this.registry(clazz); | ||
}; | ||
__decorate([inject(IDENTIFIER.ComponentRegistry), __metadata("design:type", Function)], ComponentManager.prototype, "registry", void 0); | ||
ComponentManager = __decorate([injectable()], ComponentManager); | ||
return ComponentManager; | ||
}(), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "registry", [_dec2], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: null | ||
})), _class2)) || _class); | ||
}(); | ||
export { ComponentManager }; |
179
es/Entity.js
@@ -1,15 +0,2 @@ | ||
var _dec, _dec2, _class, _class2, _descriptor; | ||
function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; } | ||
function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'proposal-class-properties is enabled and runs after the decorators transform.'); } | ||
import { __decorate, __metadata } from "tslib"; | ||
import { inject, injectable } from 'inversify'; | ||
@@ -23,102 +10,88 @@ import { EntityManager } from './EntityManager'; | ||
export var Entity = (_dec = injectable(), _dec2 = inject(EntityManager), _dec(_class = (_class2 = /*#__PURE__*/function () { | ||
var Entity = function () { | ||
function Entity() { | ||
_classCallCheck(this, Entity); | ||
/** | ||
* A unique ID for this entity. | ||
*/ | ||
this.id = counter++; | ||
this.name = void 0; | ||
this.components = {}; | ||
_initializerDefineProperty(this, "entityManager", _descriptor, this); | ||
} | ||
_createClass(Entity, [{ | ||
key: "getId", | ||
value: function getId() { | ||
return this.id; | ||
} | ||
}, { | ||
key: "getComponents", | ||
value: function getComponents() { | ||
return this.components; | ||
} | ||
}, { | ||
key: "setName", | ||
value: function setName(name) { | ||
this.name = name; | ||
} | ||
}, { | ||
key: "getName", | ||
value: function getName() { | ||
return this.name || "".concat(this.id); | ||
} | ||
}, { | ||
key: "getComponent", | ||
value: function getComponent(clazz) { | ||
return this.components[clazz.tag]; | ||
} | ||
}, { | ||
key: "hasComponent", | ||
value: function hasComponent(clazz) { | ||
return !!~Object.keys(this.components).indexOf(clazz.tag); | ||
} | ||
}, { | ||
key: "hasAllComponents", | ||
value: function hasAllComponents(clazzes) { | ||
var _this = this; | ||
Entity.prototype.getId = function () { | ||
return this.id; | ||
}; | ||
return clazzes.every(function (clazz) { | ||
return _this.hasComponent(clazz); | ||
}); | ||
Entity.prototype.getComponents = function () { | ||
return this.components; | ||
}; | ||
Entity.prototype.setName = function (name) { | ||
this.name = name; | ||
}; | ||
Entity.prototype.getName = function () { | ||
return this.name || "" + this.id; | ||
}; | ||
Entity.prototype.getComponent = function (clazz) { | ||
return this.components[clazz.tag]; | ||
}; | ||
Entity.prototype.hasComponent = function (clazz) { | ||
return !!~Object.keys(this.components).indexOf(clazz.tag); | ||
}; | ||
Entity.prototype.hasAllComponents = function (clazzes) { | ||
var _this = this; | ||
return clazzes.every(function (clazz) { | ||
return _this.hasComponent(clazz); | ||
}); | ||
}; | ||
Entity.prototype.hasAnyComponents = function (clazzes) { | ||
var _this = this; | ||
return clazzes.some(function (clazz) { | ||
return _this.hasComponent(clazz); | ||
}); | ||
}; | ||
Entity.prototype.addComponent = function (clazz, values) { | ||
return this.entityManager.addComponentToEntity(this, clazz, values); | ||
}; | ||
Entity.prototype.removeComponent = function (clazz, forceImmediate) { | ||
if (forceImmediate === void 0) { | ||
forceImmediate = false; | ||
} | ||
}, { | ||
key: "hasAnyComponents", | ||
value: function hasAnyComponents(clazzes) { | ||
var _this2 = this; | ||
return clazzes.some(function (clazz) { | ||
return _this2.hasComponent(clazz); | ||
}); | ||
} | ||
}, { | ||
key: "addComponent", | ||
value: function addComponent(clazz, values) { | ||
return this.entityManager.addComponentToEntity(this, clazz, values); | ||
} | ||
}, { | ||
key: "removeComponent", | ||
value: function removeComponent(clazz) { | ||
var forceImmediate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
this.entityManager.removeComponentFromEntity(this, clazz, forceImmediate); | ||
return this; | ||
} // public removeAllComponents() { | ||
// return this; | ||
this.entityManager.removeComponentFromEntity(this, clazz, forceImmediate); | ||
return this; | ||
}; // public removeAllComponents() { | ||
// return this; | ||
// } | ||
Entity.prototype.reset = function () {// | ||
}; | ||
Entity.prototype.destroy = function () { | ||
// TODO: remove components first | ||
// for (const name in this.components) { | ||
// this.entityManager.removeComponentFromEntity(this, this.components[name]); | ||
// } | ||
this.entityManager.destroyEntity(this); | ||
}; | ||
}, { | ||
key: "reset", | ||
value: function reset() {// | ||
} | ||
}, { | ||
key: "destroy", | ||
value: function destroy() { | ||
// TODO: remove components first | ||
// for (const name in this.components) { | ||
// this.entityManager.removeComponentFromEntity(this, this.components[name]); | ||
// } | ||
this.entityManager.destroyEntity(this); | ||
} | ||
}, { | ||
key: "cast", | ||
value: function cast(component, componentClass) { | ||
return !!(component && component instanceof componentClass); | ||
} | ||
}]); | ||
Entity.prototype.cast = function (component, componentClass) { | ||
return !!(component && component instanceof componentClass); | ||
}; | ||
__decorate([inject(EntityManager), __metadata("design:type", EntityManager)], Entity.prototype, "entityManager", void 0); | ||
Entity = __decorate([injectable()], Entity); | ||
return Entity; | ||
}(), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "entityManager", [_dec2], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: null | ||
})), _class2)) || _class); | ||
}(); | ||
export { Entity }; |
@@ -1,31 +0,2 @@ | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
var _dec, _dec2, _class, _class2, _descriptor; | ||
function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } | ||
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } | ||
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } | ||
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } | ||
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } | ||
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; } | ||
function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'proposal-class-properties is enabled and runs after the decorators transform.'); } | ||
import { __decorate, __extends, __metadata } from "tslib"; | ||
import { EventEmitter } from 'eventemitter3'; | ||
@@ -41,22 +12,15 @@ import { inject, injectable } from 'inversify'; | ||
}; | ||
export var EntityManager = (_dec = injectable(), _dec2 = inject(IDENTIFIER.ComponentPoolFactory), _dec(_class = (_class2 = /*#__PURE__*/function (_EventEmitter) { | ||
_inherits(EntityManager, _EventEmitter); | ||
var _super = _createSuper(EntityManager); | ||
var EntityManager = function (_super) { | ||
__extends(EntityManager, _super); | ||
function EntityManager() { | ||
var _this; | ||
var _this = _super !== null && _super.apply(this, arguments) || this; | ||
_classCallCheck(this, EntityManager); | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
_this = _super.call.apply(_super, [this].concat(args)); | ||
_initializerDefineProperty(_this, "componentPoolFactory", _descriptor, _assertThisInitialized(_this)); | ||
_this.entities = []; | ||
_this.entitiesByNames = {}; | ||
/** | ||
* use cache when query a matcher | ||
*/ | ||
_this.matcherCache = {}; | ||
@@ -66,140 +30,131 @@ return _this; | ||
_createClass(EntityManager, [{ | ||
key: "getEntityByName", | ||
value: function getEntityByName(name) { | ||
return this.entitiesByNames[name]; | ||
} | ||
}, { | ||
key: "getAllEntities", | ||
value: function getAllEntities() { | ||
return this.entities; | ||
} | ||
}, { | ||
key: "queryByMatcher", | ||
value: function queryByMatcher(matcher) { | ||
var hash = matcher.hash(); | ||
EntityManager.prototype.getEntityByName = function (name) { | ||
return this.entitiesByNames[name]; | ||
}; | ||
if (!this.matcherCache[hash]) { | ||
this.matcherCache[hash] = this.entities.filter(function (entity) { | ||
return matcher.matches(entity); | ||
}); | ||
} | ||
EntityManager.prototype.getAllEntities = function () { | ||
return this.entities; | ||
}; | ||
return this.matcherCache[hash]; | ||
EntityManager.prototype.queryByMatcher = function (matcher) { | ||
var hash = matcher.hash(); | ||
if (!this.matcherCache[hash]) { | ||
this.matcherCache[hash] = this.entities.filter(function (entity) { | ||
return matcher.matches(entity); | ||
}); | ||
} | ||
}, { | ||
key: "createEntity", | ||
value: function createEntity(entity) { | ||
var name = entity.getName(); | ||
if (name) { | ||
if (this.entitiesByNames[name]) { | ||
throw new Error("Entity name '".concat(name, "' already exist")); | ||
} else { | ||
this.entitiesByNames[name] = entity; | ||
} | ||
return this.matcherCache[hash]; | ||
}; | ||
EntityManager.prototype.createEntity = function (entity) { | ||
var name = entity.getName(); | ||
if (name) { | ||
if (this.entitiesByNames[name]) { | ||
throw new Error("Entity name '" + name + "' already exist"); | ||
} else { | ||
this.entitiesByNames[name] = entity; | ||
} | ||
} | ||
this.entities.push(entity); | ||
this.emit(ENTITY_EVENT.Created, entity); | ||
return entity; | ||
this.entities.push(entity); | ||
this.emit(ENTITY_EVENT.Created, entity); | ||
return entity; | ||
}; | ||
EntityManager.prototype.destroyEntity = function (entity) { | ||
if (entity.getName()) { | ||
delete this.entitiesByNames[entity.getName()]; | ||
} | ||
}, { | ||
key: "destroyEntity", | ||
value: function destroyEntity(entity) { | ||
if (entity.getName()) { | ||
delete this.entitiesByNames[entity.getName()]; | ||
} | ||
var index = this.entities.indexOf(entity); | ||
var index = this.entities.indexOf(entity); | ||
if (index > -1) { | ||
this.entities.splice(index, 1); | ||
} | ||
this.matcherCache = {}; | ||
if (index > -1) { | ||
this.entities.splice(index, 1); | ||
} | ||
}, { | ||
key: "addComponentToEntity", | ||
value: function addComponentToEntity(entity, clazz, values) { | ||
var tag = clazz.tag; | ||
var components = entity.getComponents(); | ||
var component = components[tag]; | ||
if (component) { | ||
if (!entity.cast(component, clazz)) { | ||
throw new Error("There are multiple classes with the same tag or name \"".concat(tag, "\".\nAdd a different property \"tag\" to one of them.")); | ||
} | ||
this.matcherCache = {}; | ||
}; | ||
delete components[tag]; | ||
} | ||
EntityManager.prototype.addComponentToEntity = function (entity, clazz, values) { | ||
var tag = clazz.tag; | ||
var components = entity.getComponents(); | ||
var component = components[tag]; | ||
component = this.componentPoolFactory(clazz).acquire(); | ||
if (values) { | ||
component.copy(values); | ||
if (component) { | ||
if (!entity.cast(component, clazz)) { | ||
throw new Error("There are multiple classes with the same tag or name \"" + tag + "\".\nAdd a different property \"tag\" to one of them."); | ||
} | ||
components[tag] = component; | ||
this.emit(COMPONENT_EVENT.Added, entity, component); | ||
this.matcherCache = {}; | ||
return component; | ||
delete components[tag]; | ||
} | ||
}, { | ||
key: "removeComponentFromEntity", | ||
value: function removeComponentFromEntity(entity, clazz) { | ||
var immediately = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
if (!entity.hasComponent(clazz)) { | ||
return; | ||
} | ||
component = this.componentPoolFactory(clazz).acquire(); | ||
this.emit(COMPONENT_EVENT.Remove, entity, clazz); | ||
if (values) { | ||
component.copy(values); | ||
} | ||
if (immediately) { | ||
this.removeComponentFromEntitySync(entity, clazz); | ||
} else {// if (entity._ComponentTypesToRemove.length === 0) | ||
// this.entitiesWithComponentsToRemove.push(entity); | ||
// entity._ComponentTypes.splice(index, 1); | ||
// entity._ComponentTypesToRemove.push(Component); | ||
// entity._componentsToRemove[Component._typeId] = | ||
// entity._components[Component._typeId]; | ||
// delete entity._components[Component._typeId]; | ||
// } | ||
// Check each indexed query to see if we need to remove it | ||
// this._queryManager.onEntityComponentRemoved(entity, Component); | ||
// if (Component.__proto__ === SystemStateComponent) { | ||
// entity.numStateComponents--; | ||
// // Check if the entity was a ghost waiting for the last system state component to be removed | ||
// if (entity.numStateComponents === 0 && !entity.alive) { | ||
// entity.remove(); | ||
// } | ||
} | ||
components[tag] = component; | ||
this.emit(COMPONENT_EVENT.Added, entity, component); | ||
this.matcherCache = {}; | ||
return component; | ||
}; | ||
this.matcherCache = {}; | ||
EntityManager.prototype.removeComponentFromEntity = function (entity, clazz, immediately) { | ||
if (immediately === void 0) { | ||
immediately = false; | ||
} | ||
}, { | ||
key: "destroy", | ||
value: function destroy() { | ||
this.entities = []; | ||
this.entitiesByNames = {}; | ||
if (!entity.hasComponent(clazz)) { | ||
return; | ||
} | ||
}, { | ||
key: "removeComponentFromEntitySync", | ||
value: function removeComponentFromEntitySync(entity, clazz) { | ||
// Remove T listing on entity and property ref, then free the component. | ||
// entity._ComponentTypes.splice(index, 1); | ||
var component = entity.getComponent(clazz); | ||
var components = entity.getComponents(); | ||
delete components[clazz.tag]; | ||
component.destroy(); // this.world.componentsManager.componentRemovedFromEntity(Component); | ||
this.emit(COMPONENT_EVENT.Remove, entity, clazz); | ||
if (immediately) { | ||
this.removeComponentFromEntitySync(entity, clazz); | ||
} else {// if (entity._ComponentTypesToRemove.length === 0) | ||
// this.entitiesWithComponentsToRemove.push(entity); | ||
// entity._ComponentTypes.splice(index, 1); | ||
// entity._ComponentTypesToRemove.push(Component); | ||
// entity._componentsToRemove[Component._typeId] = | ||
// entity._components[Component._typeId]; | ||
// delete entity._components[Component._typeId]; | ||
// } | ||
// Check each indexed query to see if we need to remove it | ||
// this._queryManager.onEntityComponentRemoved(entity, Component); | ||
// if (Component.__proto__ === SystemStateComponent) { | ||
// entity.numStateComponents--; | ||
// // Check if the entity was a ghost waiting for the last system state component to be removed | ||
// if (entity.numStateComponents === 0 && !entity.alive) { | ||
// entity.remove(); | ||
// } | ||
} | ||
}]); | ||
this.matcherCache = {}; | ||
}; | ||
EntityManager.prototype.destroy = function () { | ||
this.entities = []; | ||
this.entitiesByNames = {}; | ||
}; | ||
EntityManager.prototype.removeComponentFromEntitySync = function (entity, clazz) { | ||
// Remove T listing on entity and property ref, then free the component. | ||
// entity._ComponentTypes.splice(index, 1); | ||
var component = entity.getComponent(clazz); | ||
var components = entity.getComponents(); | ||
delete components[clazz.tag]; | ||
component.destroy(); // this.world.componentsManager.componentRemovedFromEntity(Component); | ||
}; | ||
__decorate([inject(IDENTIFIER.ComponentPoolFactory), __metadata("design:type", Function)], EntityManager.prototype, "componentPoolFactory", void 0); | ||
EntityManager = __decorate([injectable()], EntityManager); | ||
return EntityManager; | ||
}(EventEmitter), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "componentPoolFactory", [_dec2], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: null | ||
})), _class2)) || _class); | ||
}(EventEmitter); | ||
export { EntityManager }; |
@@ -56,3 +56,3 @@ import 'reflect-metadata'; | ||
if (!isBound) { | ||
throw new Error("Component \"".concat(clazz.tag, "\" is not registered, please call registerComponent() first.")); | ||
throw new Error("Component \"" + clazz.tag + "\" is not registered, please call registerComponent() first."); | ||
} | ||
@@ -59,0 +59,0 @@ |
@@ -1,19 +0,2 @@ | ||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | ||
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | ||
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
import { __read, __spreadArray } from "tslib"; | ||
/** | ||
@@ -27,6 +10,5 @@ * Query language of ECS | ||
*/ | ||
export var Matcher = /*#__PURE__*/function () { | ||
var Matcher = function () { | ||
function Matcher() { | ||
_classCallCheck(this, Matcher); | ||
this.allOfComponentCtors = []; | ||
@@ -37,50 +19,51 @@ this.anyOfComponentCtors = []; | ||
_createClass(Matcher, [{ | ||
key: "allOf", | ||
value: function allOf() { | ||
for (var _len = arguments.length, clazzes = new Array(_len), _key = 0; _key < _len; _key++) { | ||
clazzes[_key] = arguments[_key]; | ||
} | ||
Matcher.prototype.allOf = function () { | ||
var clazzes = []; | ||
this.allOfComponentCtors = clazzes; | ||
return this; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
clazzes[_i] = arguments[_i]; | ||
} | ||
}, { | ||
key: "anyOf", | ||
value: function anyOf() { | ||
for (var _len2 = arguments.length, clazzes = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
clazzes[_key2] = arguments[_key2]; | ||
} | ||
this.anyOfComponentCtors = clazzes; | ||
return this; | ||
this.allOfComponentCtors = clazzes; | ||
return this; | ||
}; | ||
Matcher.prototype.anyOf = function () { | ||
var clazzes = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
clazzes[_i] = arguments[_i]; | ||
} | ||
}, { | ||
key: "noneOf", | ||
value: function noneOf() { | ||
for (var _len3 = arguments.length, clazzes = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
clazzes[_key3] = arguments[_key3]; | ||
} | ||
this.noneOfComponentCtors = clazzes; | ||
return this; | ||
this.anyOfComponentCtors = clazzes; | ||
return this; | ||
}; | ||
Matcher.prototype.noneOf = function () { | ||
var clazzes = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
clazzes[_i] = arguments[_i]; | ||
} | ||
}, { | ||
key: "matches", | ||
value: function matches(entity) { | ||
var matchesAllOf = !this.allOfComponentCtors.length ? true : entity.hasAllComponents(this.allOfComponentCtors); | ||
var matchesAnyOf = !this.anyOfComponentCtors.length ? true : entity.hasAnyComponents(this.anyOfComponentCtors); | ||
var matchesNoneOf = !this.noneOfComponentCtors.length ? true : !entity.hasAnyComponents(this.noneOfComponentCtors); | ||
return matchesAllOf && matchesAnyOf && matchesNoneOf; | ||
} | ||
}, { | ||
key: "hash", | ||
value: function hash() { | ||
return [].concat(_toConsumableArray(this.allOfComponentCtors), _toConsumableArray(this.anyOfComponentCtors), _toConsumableArray(this.noneOfComponentCtors)).map(function (c) { | ||
return c.tag; | ||
}).join(); | ||
} | ||
}]); | ||
this.noneOfComponentCtors = clazzes; | ||
return this; | ||
}; | ||
Matcher.prototype.matches = function (entity) { | ||
var matchesAllOf = !this.allOfComponentCtors.length ? true : entity.hasAllComponents(this.allOfComponentCtors); | ||
var matchesAnyOf = !this.anyOfComponentCtors.length ? true : entity.hasAnyComponents(this.anyOfComponentCtors); | ||
var matchesNoneOf = !this.noneOfComponentCtors.length ? true : !entity.hasAnyComponents(this.noneOfComponentCtors); | ||
return matchesAllOf && matchesAnyOf && matchesNoneOf; | ||
}; | ||
Matcher.prototype.hash = function () { | ||
return __spreadArray(__spreadArray(__spreadArray([], __read(this.allOfComponentCtors)), __read(this.anyOfComponentCtors)), __read(this.noneOfComponentCtors)).map(function (c) { | ||
return c.tag; | ||
}).join(); | ||
}; | ||
return Matcher; | ||
}(); | ||
}(); | ||
export { Matcher }; |
@@ -1,56 +0,44 @@ | ||
var _dec, _class; | ||
import { __decorate } from "tslib"; | ||
import { injectable } from 'inversify'; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
import { injectable } from 'inversify'; | ||
export var ObjectPool = (_dec = injectable(), _dec(_class = /*#__PURE__*/function () { | ||
var ObjectPool = function () { | ||
function ObjectPool() { | ||
_classCallCheck(this, ObjectPool); | ||
this.count = 0; | ||
this.freeList = []; | ||
this.objectFactory = void 0; | ||
} | ||
_createClass(ObjectPool, [{ | ||
key: "init", | ||
value: function init(T, initialSize) { | ||
this.objectFactory = T; | ||
ObjectPool.prototype.init = function (T, initialSize) { | ||
this.objectFactory = T; | ||
if (typeof initialSize !== 'undefined') { | ||
this.expand(initialSize); | ||
} | ||
if (typeof initialSize !== 'undefined') { | ||
this.expand(initialSize); | ||
} | ||
}, { | ||
key: "acquire", | ||
value: function acquire() { | ||
if (this.freeList.length <= 0) { | ||
this.expand(Math.round(this.count * 0.2) + 1); | ||
} | ||
}; | ||
return this.freeList.pop(); | ||
ObjectPool.prototype.acquire = function () { | ||
if (this.freeList.length <= 0) { | ||
this.expand(Math.round(this.count * 0.2) + 1); | ||
} | ||
}, { | ||
key: "release", | ||
value: function release(item) { | ||
item.reset(); | ||
this.freeList.push(item); | ||
} | ||
}, { | ||
key: "expand", | ||
value: function expand(count) { | ||
for (var n = 0; n < count; n++) { | ||
var clone = this.objectFactory(); | ||
this.freeList.push(clone); | ||
} | ||
this.count += count; | ||
return this.freeList.pop(); | ||
}; | ||
ObjectPool.prototype.release = function (item) { | ||
item.reset(); | ||
this.freeList.push(item); | ||
}; | ||
ObjectPool.prototype.expand = function (count) { | ||
for (var n = 0; n < count; n++) { | ||
var clone = this.objectFactory(); | ||
this.freeList.push(clone); | ||
} | ||
}]); | ||
this.count += count; | ||
}; | ||
ObjectPool = __decorate([injectable()], ObjectPool); | ||
return ObjectPool; | ||
}()) || _class); | ||
}(); | ||
export { ObjectPool }; |
@@ -1,5 +0,1 @@ | ||
export var System = 'System'; | ||
/** | ||
* inspired by Entitas' Systems | ||
* @see https://github.com/sschmid/Entitas-CSharp/wiki/Systems | ||
*/ | ||
export var System = 'System'; |
@@ -1,117 +0,89 @@ | ||
var _dec, _dec2, _dec3, _class, _class2, _descriptor, _descriptor2; | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; } | ||
function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'proposal-class-properties is enabled and runs after the decorators transform.'); } | ||
import { __decorate, __metadata, __values } from "tslib"; | ||
import { inject, injectable } from 'inversify'; | ||
import { COMPONENT_EVENT, EntityManager } from './EntityManager'; | ||
import { IDENTIFIER } from './identifier'; | ||
export var SystemManager = (_dec = injectable(), _dec2 = inject(IDENTIFIER.SystemRegistry), _dec3 = inject(EntityManager), _dec(_class = (_class2 = /*#__PURE__*/function () { | ||
var SystemManager = function () { | ||
function SystemManager() { | ||
_classCallCheck(this, SystemManager); | ||
_initializerDefineProperty(this, "registry", _descriptor, this); | ||
_initializerDefineProperty(this, "entityManager", _descriptor2, this); | ||
this.systems = []; | ||
} | ||
_createClass(SystemManager, [{ | ||
key: "registerSystem", | ||
value: function registerSystem(clazz) { | ||
if (!this.registry.has(clazz)) { | ||
this.registry.register(clazz); | ||
var system = this.registry.get(clazz); | ||
var systemCtor = system.constructor; | ||
SystemManager.prototype.registerSystem = function (clazz) { | ||
if (!this.registry.has(clazz)) { | ||
this.registry.register(clazz); | ||
var system_1 = this.registry.get(clazz); | ||
var systemCtor_1 = system_1.constructor; | ||
if (system.initialize) { | ||
// TODO: support async init @see https://github.com/ecsyjs/ecsy/issues/20 | ||
system.initialize(); | ||
} else { | ||
system.initialized = true; | ||
if (system_1.initialize) { | ||
// TODO: support async init @see https://github.com/ecsyjs/ecsy/issues/20 | ||
system_1.initialize(); | ||
} else { | ||
system_1.initialized = true; | ||
} | ||
this.entityManager.on(COMPONENT_EVENT.Added, function (entity) { | ||
if (system_1.onEntityAdded && systemCtor_1.trigger && systemCtor_1.trigger.matches(entity)) { | ||
system_1.onEntityAdded(entity); | ||
} | ||
this.entityManager.on(COMPONENT_EVENT.Added, function (entity) { | ||
if (system.onEntityAdded && systemCtor.trigger && systemCtor.trigger.matches(entity)) { | ||
system.onEntityAdded(entity); | ||
} | ||
}); | ||
this.entityManager.on(COMPONENT_EVENT.Remove, function (entity) { | ||
if (system.onEntityRemoved && systemCtor.trigger && systemCtor.trigger.matches(entity)) { | ||
system.onEntityRemoved(entity); | ||
} | ||
}); | ||
this.systems.push(system); | ||
this.systems.sort(function (a, b) { | ||
return (a.constructor.priority || 0) - (b.constructor.priority || 0); | ||
}); | ||
} | ||
}); | ||
this.entityManager.on(COMPONENT_EVENT.Remove, function (entity) { | ||
if (system_1.onEntityRemoved && systemCtor_1.trigger && systemCtor_1.trigger.matches(entity)) { | ||
system_1.onEntityRemoved(entity); | ||
} | ||
}); | ||
this.systems.push(system_1); | ||
this.systems.sort(function (a, b) { | ||
return (a.constructor.priority || 0) - (b.constructor.priority || 0); | ||
}); | ||
} | ||
}, { | ||
key: "execute", | ||
value: function execute(delta, millis) { | ||
var _iterator = _createForOfIteratorHelper(this.systems), | ||
_step; | ||
}; | ||
try { | ||
for (_iterator.s(); !(_step = _iterator.n()).done;) { | ||
var system = _step.value; | ||
SystemManager.prototype.execute = function (delta, millis) { | ||
var e_1, _a; | ||
if (system.initialized && system.execute) { | ||
// const t1 = performance.now(); | ||
system.execute(this.getEntities(system), delta, millis); // console.log((system.constructor as SystemConstructor<System>).tag, performance.now() - t1); | ||
} | ||
try { | ||
for (var _b = __values(this.systems), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var system = _c.value; | ||
if (system.initialized && system.execute) { | ||
// const t1 = performance.now(); | ||
system.execute(this.getEntities(system), delta, millis); // console.log((system.constructor as SystemConstructor<System>).tag, performance.now() - t1); | ||
} | ||
} catch (err) { | ||
_iterator.e(err); | ||
} | ||
} catch (e_1_1) { | ||
e_1 = { | ||
error: e_1_1 | ||
}; | ||
} finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} finally { | ||
_iterator.f(); | ||
if (e_1) throw e_1.error; | ||
} | ||
} | ||
}, { | ||
key: "destroy", | ||
value: function destroy() { | ||
var _this = this; | ||
}; | ||
this.systems.forEach(function (system) { | ||
if (system.initialized && system.tearDown) { | ||
system.tearDown(_this.getEntities(system)); | ||
} | ||
}); | ||
} | ||
}, { | ||
key: "getEntities", | ||
value: function getEntities(system) { | ||
var systemCtor = system.constructor; | ||
return systemCtor.trigger ? this.entityManager.queryByMatcher(systemCtor.trigger) : this.entityManager.getAllEntities(); | ||
} | ||
}]); | ||
SystemManager.prototype.destroy = function () { | ||
var _this = this; | ||
this.systems.forEach(function (system) { | ||
if (system.initialized && system.tearDown) { | ||
system.tearDown(_this.getEntities(system)); | ||
} | ||
}); | ||
}; | ||
SystemManager.prototype.getEntities = function (system) { | ||
var systemCtor = system.constructor; | ||
return systemCtor.trigger ? this.entityManager.queryByMatcher(systemCtor.trigger) : this.entityManager.getAllEntities(); | ||
}; | ||
__decorate([inject(IDENTIFIER.SystemRegistry), __metadata("design:type", Object)], SystemManager.prototype, "registry", void 0); | ||
__decorate([inject(EntityManager), __metadata("design:type", EntityManager)], SystemManager.prototype, "entityManager", void 0); | ||
SystemManager = __decorate([injectable()], SystemManager); | ||
return SystemManager; | ||
}(), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "registry", [_dec2], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: null | ||
}), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "entityManager", [_dec3], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: null | ||
})), _class2)) || _class); | ||
}(); | ||
export { SystemManager }; |
155
es/World.js
@@ -1,15 +0,2 @@ | ||
var _dec, _dec2, _dec3, _dec4, _dec5, _class, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4; | ||
function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; } | ||
function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'proposal-class-properties is enabled and runs after the decorators transform.'); } | ||
import { __decorate, __metadata } from "tslib"; | ||
import { inject, injectable } from 'inversify'; | ||
@@ -20,3 +7,2 @@ import { ComponentManager } from './ComponentManager'; | ||
import { SystemManager } from './SystemManager'; | ||
export | ||
/** | ||
@@ -36,95 +22,70 @@ * the context of ECS | ||
*/ | ||
var World = (_dec = injectable(), _dec2 = inject(IDENTIFIER.EntityPoolFactory), _dec3 = inject(EntityManager), _dec4 = inject(ComponentManager), _dec5 = inject(SystemManager), _dec(_class = (_class2 = /*#__PURE__*/function () { | ||
var World = function () { | ||
function World() { | ||
_classCallCheck(this, World); | ||
this.lastMillis = new Date().getTime(); | ||
this.enabled = true; | ||
} | ||
_initializerDefineProperty(this, "entityPoolFactory", _descriptor, this); | ||
World.prototype.createEntity = function (name) { | ||
if (name === void 0) { | ||
name = ''; | ||
} | ||
_initializerDefineProperty(this, "entityManager", _descriptor2, this); | ||
var entity = this.entityPoolFactory().acquire(); | ||
entity.setName(name); | ||
return this.entityManager.createEntity(entity); | ||
}; | ||
_initializerDefineProperty(this, "componentManager", _descriptor3, this); | ||
World.prototype.getEntityByName = function (name) { | ||
return this.entityManager.getEntityByName(name); | ||
}; | ||
_initializerDefineProperty(this, "systemManager", _descriptor4, this); | ||
World.prototype.registerComponent = function (clazz) { | ||
this.componentManager.registerComponent(clazz); | ||
return this; | ||
}; | ||
this.lastMillis = new Date().getTime(); | ||
this.enabled = true; | ||
} | ||
World.prototype.registerSystem = function (clazz) { | ||
this.systemManager.registerSystem(clazz); | ||
return this; | ||
}; | ||
_createClass(World, [{ | ||
key: "createEntity", | ||
value: function createEntity() { | ||
var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; | ||
var entity = this.entityPoolFactory().acquire(); | ||
entity.setName(name); | ||
return this.entityManager.createEntity(entity); | ||
World.prototype.execute = function (delta, millis) { | ||
if (!delta) { | ||
millis = new Date().getTime(); | ||
delta = millis - this.lastMillis; | ||
this.lastMillis = millis; | ||
} | ||
}, { | ||
key: "getEntityByName", | ||
value: function getEntityByName(name) { | ||
return this.entityManager.getEntityByName(name); | ||
} | ||
}, { | ||
key: "registerComponent", | ||
value: function registerComponent(clazz) { | ||
this.componentManager.registerComponent(clazz); | ||
return this; | ||
} | ||
}, { | ||
key: "registerSystem", | ||
value: function registerSystem(clazz) { | ||
this.systemManager.registerSystem(clazz); | ||
return this; | ||
} | ||
}, { | ||
key: "execute", | ||
value: function execute(delta, millis) { | ||
if (!delta) { | ||
millis = new Date().getTime(); | ||
delta = millis - this.lastMillis; | ||
this.lastMillis = millis; | ||
} | ||
if (this.enabled) { | ||
this.systemManager.execute(delta, millis); | ||
} | ||
if (this.enabled) { | ||
this.systemManager.execute(delta, millis); | ||
} | ||
}, { | ||
key: "stop", | ||
value: function stop() { | ||
this.enabled = false; | ||
} | ||
}, { | ||
key: "resume", | ||
value: function resume() { | ||
this.enabled = true; | ||
} | ||
}, { | ||
key: "destroy", | ||
value: function destroy() { | ||
this.systemManager.destroy(); | ||
this.entityManager.destroy(); // this.componentManager.destroy(); | ||
} | ||
}]); | ||
}; | ||
World.prototype.stop = function () { | ||
this.enabled = false; | ||
}; | ||
World.prototype.resume = function () { | ||
this.enabled = true; | ||
}; | ||
World.prototype.destroy = function () { | ||
this.systemManager.destroy(); | ||
this.entityManager.destroy(); // this.componentManager.destroy(); | ||
}; | ||
__decorate([inject(IDENTIFIER.EntityPoolFactory), __metadata("design:type", Function)], World.prototype, "entityPoolFactory", void 0); | ||
__decorate([inject(EntityManager), __metadata("design:type", EntityManager)], World.prototype, "entityManager", void 0); | ||
__decorate([inject(ComponentManager), __metadata("design:type", ComponentManager)], World.prototype, "componentManager", void 0); | ||
__decorate([inject(SystemManager), __metadata("design:type", SystemManager)], World.prototype, "systemManager", void 0); | ||
World = __decorate([injectable()], World); | ||
return World; | ||
}(), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "entityPoolFactory", [_dec2], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: null | ||
}), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "entityManager", [_dec3], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: null | ||
}), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "componentManager", [_dec4], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: null | ||
}), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, "systemManager", [_dec5], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: null | ||
})), _class2)) || _class); | ||
}(); | ||
export { World }; |
@@ -8,48 +8,40 @@ "use strict"; | ||
var _tslib = require("tslib"); | ||
var _inversify = require("inversify"); | ||
var _dec, _class; | ||
var counter = 0; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var Component = function () { | ||
function Component() { | ||
// eslint-disable-next-line no-plusplus | ||
this.pid = counter++; | ||
} | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
Component_1 = Component; | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
Component.prototype.getId = function () { | ||
return this.pid; | ||
}; | ||
var counter = 0; | ||
var Component = (_dec = (0, _inversify.injectable)(), _dec(_class = /*#__PURE__*/function () { | ||
function Component() { | ||
_classCallCheck(this, Component); | ||
Component.prototype.copy = function (props) { | ||
Object.assign(this, props); | ||
return this; | ||
}; | ||
this.pid = counter++; | ||
} | ||
Component.prototype.clone = function () { | ||
return new Component_1().copy(this); | ||
}; | ||
_createClass(Component, [{ | ||
key: "getId", | ||
value: function getId() { | ||
return this.pid; | ||
} | ||
}, { | ||
key: "copy", | ||
value: function copy(props) { | ||
Object.assign(this, props); | ||
return this; | ||
} | ||
}, { | ||
key: "clone", | ||
value: function clone() { | ||
return new Component().copy(this); | ||
} | ||
}, { | ||
key: "reset", | ||
value: function reset() {// | ||
} | ||
}, { | ||
key: "destroy", | ||
value: function destroy() {// | ||
} | ||
}]); | ||
Component.prototype.reset = function () {// | ||
}; | ||
Component.prototype.destroy = function () {// | ||
}; | ||
var Component_1; | ||
Component = Component_1 = (0, _tslib.__decorate)([(0, _inversify.injectable)()], Component); | ||
return Component; | ||
}()) || _class); | ||
}(); | ||
exports.Component = Component; |
@@ -8,2 +8,4 @@ "use strict"; | ||
var _tslib = require("tslib"); | ||
var _inversify = require("inversify"); | ||
@@ -13,37 +15,14 @@ | ||
var _dec, _dec2, _class, _class2, _descriptor; | ||
var ComponentManager = function () { | ||
function ComponentManager() {} | ||
function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); } | ||
ComponentManager.prototype.registerComponent = function (clazz) { | ||
this.registry(clazz); | ||
}; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
(0, _tslib.__decorate)([(0, _inversify.inject)(_identifier.IDENTIFIER.ComponentRegistry), (0, _tslib.__metadata)("design:type", Function)], ComponentManager.prototype, "registry", void 0); | ||
ComponentManager = (0, _tslib.__decorate)([(0, _inversify.injectable)()], ComponentManager); | ||
return ComponentManager; | ||
}(); | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; } | ||
function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'proposal-class-properties is enabled and runs after the decorators transform.'); } | ||
var ComponentManager = (_dec = (0, _inversify.injectable)(), _dec2 = (0, _inversify.inject)(_identifier.IDENTIFIER.ComponentRegistry), _dec(_class = (_class2 = /*#__PURE__*/function () { | ||
function ComponentManager() { | ||
_classCallCheck(this, ComponentManager); | ||
_initializerDefineProperty(this, "registry", _descriptor, this); | ||
} | ||
_createClass(ComponentManager, [{ | ||
key: "registerComponent", | ||
value: function registerComponent(clazz) { | ||
this.registry(clazz); | ||
} | ||
}]); | ||
return ComponentManager; | ||
}(), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "registry", [_dec2], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: null | ||
})), _class2)) || _class); | ||
exports.ComponentManager = ComponentManager; |
@@ -8,2 +8,4 @@ "use strict"; | ||
var _tslib = require("tslib"); | ||
var _inversify = require("inversify"); | ||
@@ -13,16 +15,2 @@ | ||
var _dec, _dec2, _class, _class2, _descriptor; | ||
function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; } | ||
function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'proposal-class-properties is enabled and runs after the decorators transform.'); } | ||
var counter = 0; | ||
@@ -34,103 +22,87 @@ /** | ||
var Entity = (_dec = (0, _inversify.injectable)(), _dec2 = (0, _inversify.inject)(_EntityManager.EntityManager), _dec(_class = (_class2 = /*#__PURE__*/function () { | ||
var Entity = function () { | ||
function Entity() { | ||
_classCallCheck(this, Entity); | ||
/** | ||
* A unique ID for this entity. | ||
*/ | ||
this.id = counter++; | ||
this.name = void 0; | ||
this.components = {}; | ||
_initializerDefineProperty(this, "entityManager", _descriptor, this); | ||
} | ||
_createClass(Entity, [{ | ||
key: "getId", | ||
value: function getId() { | ||
return this.id; | ||
} | ||
}, { | ||
key: "getComponents", | ||
value: function getComponents() { | ||
return this.components; | ||
} | ||
}, { | ||
key: "setName", | ||
value: function setName(name) { | ||
this.name = name; | ||
} | ||
}, { | ||
key: "getName", | ||
value: function getName() { | ||
return this.name || "".concat(this.id); | ||
} | ||
}, { | ||
key: "getComponent", | ||
value: function getComponent(clazz) { | ||
return this.components[clazz.tag]; | ||
} | ||
}, { | ||
key: "hasComponent", | ||
value: function hasComponent(clazz) { | ||
return !!~Object.keys(this.components).indexOf(clazz.tag); | ||
} | ||
}, { | ||
key: "hasAllComponents", | ||
value: function hasAllComponents(clazzes) { | ||
var _this = this; | ||
Entity.prototype.getId = function () { | ||
return this.id; | ||
}; | ||
return clazzes.every(function (clazz) { | ||
return _this.hasComponent(clazz); | ||
}); | ||
Entity.prototype.getComponents = function () { | ||
return this.components; | ||
}; | ||
Entity.prototype.setName = function (name) { | ||
this.name = name; | ||
}; | ||
Entity.prototype.getName = function () { | ||
return this.name || "" + this.id; | ||
}; | ||
Entity.prototype.getComponent = function (clazz) { | ||
return this.components[clazz.tag]; | ||
}; | ||
Entity.prototype.hasComponent = function (clazz) { | ||
return !!~Object.keys(this.components).indexOf(clazz.tag); | ||
}; | ||
Entity.prototype.hasAllComponents = function (clazzes) { | ||
var _this = this; | ||
return clazzes.every(function (clazz) { | ||
return _this.hasComponent(clazz); | ||
}); | ||
}; | ||
Entity.prototype.hasAnyComponents = function (clazzes) { | ||
var _this = this; | ||
return clazzes.some(function (clazz) { | ||
return _this.hasComponent(clazz); | ||
}); | ||
}; | ||
Entity.prototype.addComponent = function (clazz, values) { | ||
return this.entityManager.addComponentToEntity(this, clazz, values); | ||
}; | ||
Entity.prototype.removeComponent = function (clazz, forceImmediate) { | ||
if (forceImmediate === void 0) { | ||
forceImmediate = false; | ||
} | ||
}, { | ||
key: "hasAnyComponents", | ||
value: function hasAnyComponents(clazzes) { | ||
var _this2 = this; | ||
return clazzes.some(function (clazz) { | ||
return _this2.hasComponent(clazz); | ||
}); | ||
} | ||
}, { | ||
key: "addComponent", | ||
value: function addComponent(clazz, values) { | ||
return this.entityManager.addComponentToEntity(this, clazz, values); | ||
} | ||
}, { | ||
key: "removeComponent", | ||
value: function removeComponent(clazz) { | ||
var forceImmediate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
this.entityManager.removeComponentFromEntity(this, clazz, forceImmediate); | ||
return this; | ||
} // public removeAllComponents() { | ||
// return this; | ||
this.entityManager.removeComponentFromEntity(this, clazz, forceImmediate); | ||
return this; | ||
}; // public removeAllComponents() { | ||
// return this; | ||
// } | ||
Entity.prototype.reset = function () {// | ||
}; | ||
Entity.prototype.destroy = function () { | ||
// TODO: remove components first | ||
// for (const name in this.components) { | ||
// this.entityManager.removeComponentFromEntity(this, this.components[name]); | ||
// } | ||
this.entityManager.destroyEntity(this); | ||
}; | ||
}, { | ||
key: "reset", | ||
value: function reset() {// | ||
} | ||
}, { | ||
key: "destroy", | ||
value: function destroy() { | ||
// TODO: remove components first | ||
// for (const name in this.components) { | ||
// this.entityManager.removeComponentFromEntity(this, this.components[name]); | ||
// } | ||
this.entityManager.destroyEntity(this); | ||
} | ||
}, { | ||
key: "cast", | ||
value: function cast(component, componentClass) { | ||
return !!(component && component instanceof componentClass); | ||
} | ||
}]); | ||
Entity.prototype.cast = function (component, componentClass) { | ||
return !!(component && component instanceof componentClass); | ||
}; | ||
(0, _tslib.__decorate)([(0, _inversify.inject)(_EntityManager.EntityManager), (0, _tslib.__metadata)("design:type", _EntityManager.EntityManager)], Entity.prototype, "entityManager", void 0); | ||
Entity = (0, _tslib.__decorate)([(0, _inversify.injectable)()], Entity); | ||
return Entity; | ||
}(), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "entityManager", [_dec2], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: null | ||
})), _class2)) || _class); | ||
}(); | ||
exports.Entity = Entity; |
"use strict"; | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -10,2 +8,4 @@ value: true | ||
var _tslib = require("tslib"); | ||
var _eventemitter = require("eventemitter3"); | ||
@@ -17,30 +17,2 @@ | ||
var _dec, _dec2, _class, _class2, _descriptor; | ||
function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } | ||
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } | ||
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } | ||
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } | ||
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } | ||
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; } | ||
function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'proposal-class-properties is enabled and runs after the decorators transform.'); } | ||
var ENTITY_EVENT = { | ||
@@ -55,22 +27,15 @@ Created: 'Created' | ||
exports.COMPONENT_EVENT = COMPONENT_EVENT; | ||
var EntityManager = (_dec = (0, _inversify.injectable)(), _dec2 = (0, _inversify.inject)(_identifier.IDENTIFIER.ComponentPoolFactory), _dec(_class = (_class2 = /*#__PURE__*/function (_EventEmitter) { | ||
_inherits(EntityManager, _EventEmitter); | ||
var _super = _createSuper(EntityManager); | ||
var EntityManager = function (_super) { | ||
(0, _tslib.__extends)(EntityManager, _super); | ||
function EntityManager() { | ||
var _this; | ||
var _this = _super !== null && _super.apply(this, arguments) || this; | ||
_classCallCheck(this, EntityManager); | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
_this = _super.call.apply(_super, [this].concat(args)); | ||
_initializerDefineProperty(_this, "componentPoolFactory", _descriptor, _assertThisInitialized(_this)); | ||
_this.entities = []; | ||
_this.entitiesByNames = {}; | ||
/** | ||
* use cache when query a matcher | ||
*/ | ||
_this.matcherCache = {}; | ||
@@ -80,141 +45,130 @@ return _this; | ||
_createClass(EntityManager, [{ | ||
key: "getEntityByName", | ||
value: function getEntityByName(name) { | ||
return this.entitiesByNames[name]; | ||
} | ||
}, { | ||
key: "getAllEntities", | ||
value: function getAllEntities() { | ||
return this.entities; | ||
} | ||
}, { | ||
key: "queryByMatcher", | ||
value: function queryByMatcher(matcher) { | ||
var hash = matcher.hash(); | ||
EntityManager.prototype.getEntityByName = function (name) { | ||
return this.entitiesByNames[name]; | ||
}; | ||
if (!this.matcherCache[hash]) { | ||
this.matcherCache[hash] = this.entities.filter(function (entity) { | ||
return matcher.matches(entity); | ||
}); | ||
} | ||
EntityManager.prototype.getAllEntities = function () { | ||
return this.entities; | ||
}; | ||
return this.matcherCache[hash]; | ||
EntityManager.prototype.queryByMatcher = function (matcher) { | ||
var hash = matcher.hash(); | ||
if (!this.matcherCache[hash]) { | ||
this.matcherCache[hash] = this.entities.filter(function (entity) { | ||
return matcher.matches(entity); | ||
}); | ||
} | ||
}, { | ||
key: "createEntity", | ||
value: function createEntity(entity) { | ||
var name = entity.getName(); | ||
if (name) { | ||
if (this.entitiesByNames[name]) { | ||
throw new Error("Entity name '".concat(name, "' already exist")); | ||
} else { | ||
this.entitiesByNames[name] = entity; | ||
} | ||
return this.matcherCache[hash]; | ||
}; | ||
EntityManager.prototype.createEntity = function (entity) { | ||
var name = entity.getName(); | ||
if (name) { | ||
if (this.entitiesByNames[name]) { | ||
throw new Error("Entity name '" + name + "' already exist"); | ||
} else { | ||
this.entitiesByNames[name] = entity; | ||
} | ||
} | ||
this.entities.push(entity); | ||
this.emit(ENTITY_EVENT.Created, entity); | ||
return entity; | ||
this.entities.push(entity); | ||
this.emit(ENTITY_EVENT.Created, entity); | ||
return entity; | ||
}; | ||
EntityManager.prototype.destroyEntity = function (entity) { | ||
if (entity.getName()) { | ||
delete this.entitiesByNames[entity.getName()]; | ||
} | ||
}, { | ||
key: "destroyEntity", | ||
value: function destroyEntity(entity) { | ||
if (entity.getName()) { | ||
delete this.entitiesByNames[entity.getName()]; | ||
} | ||
var index = this.entities.indexOf(entity); | ||
var index = this.entities.indexOf(entity); | ||
if (index > -1) { | ||
this.entities.splice(index, 1); | ||
} | ||
this.matcherCache = {}; | ||
if (index > -1) { | ||
this.entities.splice(index, 1); | ||
} | ||
}, { | ||
key: "addComponentToEntity", | ||
value: function addComponentToEntity(entity, clazz, values) { | ||
var tag = clazz.tag; | ||
var components = entity.getComponents(); | ||
var component = components[tag]; | ||
if (component) { | ||
if (!entity.cast(component, clazz)) { | ||
throw new Error("There are multiple classes with the same tag or name \"".concat(tag, "\".\nAdd a different property \"tag\" to one of them.")); | ||
} | ||
this.matcherCache = {}; | ||
}; | ||
delete components[tag]; | ||
} | ||
EntityManager.prototype.addComponentToEntity = function (entity, clazz, values) { | ||
var tag = clazz.tag; | ||
var components = entity.getComponents(); | ||
var component = components[tag]; | ||
component = this.componentPoolFactory(clazz).acquire(); | ||
if (values) { | ||
component.copy(values); | ||
if (component) { | ||
if (!entity.cast(component, clazz)) { | ||
throw new Error("There are multiple classes with the same tag or name \"" + tag + "\".\nAdd a different property \"tag\" to one of them."); | ||
} | ||
components[tag] = component; | ||
this.emit(COMPONENT_EVENT.Added, entity, component); | ||
this.matcherCache = {}; | ||
return component; | ||
delete components[tag]; | ||
} | ||
}, { | ||
key: "removeComponentFromEntity", | ||
value: function removeComponentFromEntity(entity, clazz) { | ||
var immediately = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
if (!entity.hasComponent(clazz)) { | ||
return; | ||
} | ||
component = this.componentPoolFactory(clazz).acquire(); | ||
this.emit(COMPONENT_EVENT.Remove, entity, clazz); | ||
if (values) { | ||
component.copy(values); | ||
} | ||
if (immediately) { | ||
this.removeComponentFromEntitySync(entity, clazz); | ||
} else {// if (entity._ComponentTypesToRemove.length === 0) | ||
// this.entitiesWithComponentsToRemove.push(entity); | ||
// entity._ComponentTypes.splice(index, 1); | ||
// entity._ComponentTypesToRemove.push(Component); | ||
// entity._componentsToRemove[Component._typeId] = | ||
// entity._components[Component._typeId]; | ||
// delete entity._components[Component._typeId]; | ||
// } | ||
// Check each indexed query to see if we need to remove it | ||
// this._queryManager.onEntityComponentRemoved(entity, Component); | ||
// if (Component.__proto__ === SystemStateComponent) { | ||
// entity.numStateComponents--; | ||
// // Check if the entity was a ghost waiting for the last system state component to be removed | ||
// if (entity.numStateComponents === 0 && !entity.alive) { | ||
// entity.remove(); | ||
// } | ||
} | ||
components[tag] = component; | ||
this.emit(COMPONENT_EVENT.Added, entity, component); | ||
this.matcherCache = {}; | ||
return component; | ||
}; | ||
this.matcherCache = {}; | ||
EntityManager.prototype.removeComponentFromEntity = function (entity, clazz, immediately) { | ||
if (immediately === void 0) { | ||
immediately = false; | ||
} | ||
}, { | ||
key: "destroy", | ||
value: function destroy() { | ||
this.entities = []; | ||
this.entitiesByNames = {}; | ||
if (!entity.hasComponent(clazz)) { | ||
return; | ||
} | ||
}, { | ||
key: "removeComponentFromEntitySync", | ||
value: function removeComponentFromEntitySync(entity, clazz) { | ||
// Remove T listing on entity and property ref, then free the component. | ||
// entity._ComponentTypes.splice(index, 1); | ||
var component = entity.getComponent(clazz); | ||
var components = entity.getComponents(); | ||
delete components[clazz.tag]; | ||
component.destroy(); // this.world.componentsManager.componentRemovedFromEntity(Component); | ||
this.emit(COMPONENT_EVENT.Remove, entity, clazz); | ||
if (immediately) { | ||
this.removeComponentFromEntitySync(entity, clazz); | ||
} else {// if (entity._ComponentTypesToRemove.length === 0) | ||
// this.entitiesWithComponentsToRemove.push(entity); | ||
// entity._ComponentTypes.splice(index, 1); | ||
// entity._ComponentTypesToRemove.push(Component); | ||
// entity._componentsToRemove[Component._typeId] = | ||
// entity._components[Component._typeId]; | ||
// delete entity._components[Component._typeId]; | ||
// } | ||
// Check each indexed query to see if we need to remove it | ||
// this._queryManager.onEntityComponentRemoved(entity, Component); | ||
// if (Component.__proto__ === SystemStateComponent) { | ||
// entity.numStateComponents--; | ||
// // Check if the entity was a ghost waiting for the last system state component to be removed | ||
// if (entity.numStateComponents === 0 && !entity.alive) { | ||
// entity.remove(); | ||
// } | ||
} | ||
}]); | ||
this.matcherCache = {}; | ||
}; | ||
EntityManager.prototype.destroy = function () { | ||
this.entities = []; | ||
this.entitiesByNames = {}; | ||
}; | ||
EntityManager.prototype.removeComponentFromEntitySync = function (entity, clazz) { | ||
// Remove T listing on entity and property ref, then free the component. | ||
// entity._ComponentTypes.splice(index, 1); | ||
var component = entity.getComponent(clazz); | ||
var components = entity.getComponents(); | ||
delete components[clazz.tag]; | ||
component.destroy(); // this.world.componentsManager.componentRemovedFromEntity(Component); | ||
}; | ||
(0, _tslib.__decorate)([(0, _inversify.inject)(_identifier.IDENTIFIER.ComponentPoolFactory), (0, _tslib.__metadata)("design:type", Function)], EntityManager.prototype, "componentPoolFactory", void 0); | ||
EntityManager = (0, _tslib.__decorate)([(0, _inversify.injectable)()], EntityManager); | ||
return EntityManager; | ||
}(_eventemitter.EventEmitter), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "componentPoolFactory", [_dec2], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: null | ||
})), _class2)) || _class); | ||
}(_eventemitter.EventEmitter); | ||
exports.EntityManager = EntityManager; |
@@ -126,3 +126,3 @@ "use strict"; | ||
if (!isBound) { | ||
throw new Error("Component \"".concat(clazz.tag, "\" is not registered, please call registerComponent() first.")); | ||
throw new Error("Component \"" + clazz.tag + "\" is not registered, please call registerComponent() first."); | ||
} | ||
@@ -129,0 +129,0 @@ |
@@ -8,20 +8,4 @@ "use strict"; | ||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | ||
var _tslib = require("tslib"); | ||
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | ||
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
/** | ||
@@ -35,6 +19,4 @@ * Query language of ECS | ||
*/ | ||
var Matcher = /*#__PURE__*/function () { | ||
var Matcher = function () { | ||
function Matcher() { | ||
_classCallCheck(this, Matcher); | ||
this.allOfComponentCtors = []; | ||
@@ -45,49 +27,48 @@ this.anyOfComponentCtors = []; | ||
_createClass(Matcher, [{ | ||
key: "allOf", | ||
value: function allOf() { | ||
for (var _len = arguments.length, clazzes = new Array(_len), _key = 0; _key < _len; _key++) { | ||
clazzes[_key] = arguments[_key]; | ||
} | ||
Matcher.prototype.allOf = function () { | ||
var clazzes = []; | ||
this.allOfComponentCtors = clazzes; | ||
return this; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
clazzes[_i] = arguments[_i]; | ||
} | ||
}, { | ||
key: "anyOf", | ||
value: function anyOf() { | ||
for (var _len2 = arguments.length, clazzes = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
clazzes[_key2] = arguments[_key2]; | ||
} | ||
this.anyOfComponentCtors = clazzes; | ||
return this; | ||
this.allOfComponentCtors = clazzes; | ||
return this; | ||
}; | ||
Matcher.prototype.anyOf = function () { | ||
var clazzes = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
clazzes[_i] = arguments[_i]; | ||
} | ||
}, { | ||
key: "noneOf", | ||
value: function noneOf() { | ||
for (var _len3 = arguments.length, clazzes = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
clazzes[_key3] = arguments[_key3]; | ||
} | ||
this.noneOfComponentCtors = clazzes; | ||
return this; | ||
this.anyOfComponentCtors = clazzes; | ||
return this; | ||
}; | ||
Matcher.prototype.noneOf = function () { | ||
var clazzes = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
clazzes[_i] = arguments[_i]; | ||
} | ||
}, { | ||
key: "matches", | ||
value: function matches(entity) { | ||
var matchesAllOf = !this.allOfComponentCtors.length ? true : entity.hasAllComponents(this.allOfComponentCtors); | ||
var matchesAnyOf = !this.anyOfComponentCtors.length ? true : entity.hasAnyComponents(this.anyOfComponentCtors); | ||
var matchesNoneOf = !this.noneOfComponentCtors.length ? true : !entity.hasAnyComponents(this.noneOfComponentCtors); | ||
return matchesAllOf && matchesAnyOf && matchesNoneOf; | ||
} | ||
}, { | ||
key: "hash", | ||
value: function hash() { | ||
return [].concat(_toConsumableArray(this.allOfComponentCtors), _toConsumableArray(this.anyOfComponentCtors), _toConsumableArray(this.noneOfComponentCtors)).map(function (c) { | ||
return c.tag; | ||
}).join(); | ||
} | ||
}]); | ||
this.noneOfComponentCtors = clazzes; | ||
return this; | ||
}; | ||
Matcher.prototype.matches = function (entity) { | ||
var matchesAllOf = !this.allOfComponentCtors.length ? true : entity.hasAllComponents(this.allOfComponentCtors); | ||
var matchesAnyOf = !this.anyOfComponentCtors.length ? true : entity.hasAnyComponents(this.anyOfComponentCtors); | ||
var matchesNoneOf = !this.noneOfComponentCtors.length ? true : !entity.hasAnyComponents(this.noneOfComponentCtors); | ||
return matchesAllOf && matchesAnyOf && matchesNoneOf; | ||
}; | ||
Matcher.prototype.hash = function () { | ||
return (0, _tslib.__spreadArray)((0, _tslib.__spreadArray)((0, _tslib.__spreadArray)([], (0, _tslib.__read)(this.allOfComponentCtors)), (0, _tslib.__read)(this.anyOfComponentCtors)), (0, _tslib.__read)(this.noneOfComponentCtors)).map(function (c) { | ||
return c.tag; | ||
}).join(); | ||
}; | ||
return Matcher; | ||
@@ -94,0 +75,0 @@ }(); |
@@ -8,59 +8,46 @@ "use strict"; | ||
var _tslib = require("tslib"); | ||
var _inversify = require("inversify"); | ||
var _dec, _class; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
var ObjectPool = (_dec = (0, _inversify.injectable)(), _dec(_class = /*#__PURE__*/function () { | ||
var ObjectPool = function () { | ||
function ObjectPool() { | ||
_classCallCheck(this, ObjectPool); | ||
this.count = 0; | ||
this.freeList = []; | ||
this.objectFactory = void 0; | ||
} | ||
_createClass(ObjectPool, [{ | ||
key: "init", | ||
value: function init(T, initialSize) { | ||
this.objectFactory = T; | ||
ObjectPool.prototype.init = function (T, initialSize) { | ||
this.objectFactory = T; | ||
if (typeof initialSize !== 'undefined') { | ||
this.expand(initialSize); | ||
} | ||
if (typeof initialSize !== 'undefined') { | ||
this.expand(initialSize); | ||
} | ||
}, { | ||
key: "acquire", | ||
value: function acquire() { | ||
if (this.freeList.length <= 0) { | ||
this.expand(Math.round(this.count * 0.2) + 1); | ||
} | ||
}; | ||
return this.freeList.pop(); | ||
ObjectPool.prototype.acquire = function () { | ||
if (this.freeList.length <= 0) { | ||
this.expand(Math.round(this.count * 0.2) + 1); | ||
} | ||
}, { | ||
key: "release", | ||
value: function release(item) { | ||
item.reset(); | ||
this.freeList.push(item); | ||
} | ||
}, { | ||
key: "expand", | ||
value: function expand(count) { | ||
for (var n = 0; n < count; n++) { | ||
var clone = this.objectFactory(); | ||
this.freeList.push(clone); | ||
} | ||
this.count += count; | ||
return this.freeList.pop(); | ||
}; | ||
ObjectPool.prototype.release = function (item) { | ||
item.reset(); | ||
this.freeList.push(item); | ||
}; | ||
ObjectPool.prototype.expand = function (count) { | ||
for (var n = 0; n < count; n++) { | ||
var clone = this.objectFactory(); | ||
this.freeList.push(clone); | ||
} | ||
}]); | ||
this.count += count; | ||
}; | ||
ObjectPool = (0, _tslib.__decorate)([(0, _inversify.injectable)()], ObjectPool); | ||
return ObjectPool; | ||
}()) || _class); | ||
}(); | ||
exports.ObjectPool = ObjectPool; |
@@ -8,7 +8,2 @@ "use strict"; | ||
var System = 'System'; | ||
/** | ||
* inspired by Entitas' Systems | ||
* @see https://github.com/sschmid/Entitas-CSharp/wiki/Systems | ||
*/ | ||
exports.System = System; |
@@ -8,2 +8,4 @@ "use strict"; | ||
var _tslib = require("tslib"); | ||
var _inversify = require("inversify"); | ||
@@ -15,116 +17,83 @@ | ||
var _dec, _dec2, _dec3, _class, _class2, _descriptor, _descriptor2; | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; } | ||
function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'proposal-class-properties is enabled and runs after the decorators transform.'); } | ||
var SystemManager = (_dec = (0, _inversify.injectable)(), _dec2 = (0, _inversify.inject)(_identifier.IDENTIFIER.SystemRegistry), _dec3 = (0, _inversify.inject)(_EntityManager.EntityManager), _dec(_class = (_class2 = /*#__PURE__*/function () { | ||
var SystemManager = function () { | ||
function SystemManager() { | ||
_classCallCheck(this, SystemManager); | ||
_initializerDefineProperty(this, "registry", _descriptor, this); | ||
_initializerDefineProperty(this, "entityManager", _descriptor2, this); | ||
this.systems = []; | ||
} | ||
_createClass(SystemManager, [{ | ||
key: "registerSystem", | ||
value: function registerSystem(clazz) { | ||
if (!this.registry.has(clazz)) { | ||
this.registry.register(clazz); | ||
var system = this.registry.get(clazz); | ||
var systemCtor = system.constructor; | ||
SystemManager.prototype.registerSystem = function (clazz) { | ||
if (!this.registry.has(clazz)) { | ||
this.registry.register(clazz); | ||
var system_1 = this.registry.get(clazz); | ||
var systemCtor_1 = system_1.constructor; | ||
if (system.initialize) { | ||
// TODO: support async init @see https://github.com/ecsyjs/ecsy/issues/20 | ||
system.initialize(); | ||
} else { | ||
system.initialized = true; | ||
if (system_1.initialize) { | ||
// TODO: support async init @see https://github.com/ecsyjs/ecsy/issues/20 | ||
system_1.initialize(); | ||
} else { | ||
system_1.initialized = true; | ||
} | ||
this.entityManager.on(_EntityManager.COMPONENT_EVENT.Added, function (entity) { | ||
if (system_1.onEntityAdded && systemCtor_1.trigger && systemCtor_1.trigger.matches(entity)) { | ||
system_1.onEntityAdded(entity); | ||
} | ||
this.entityManager.on(_EntityManager.COMPONENT_EVENT.Added, function (entity) { | ||
if (system.onEntityAdded && systemCtor.trigger && systemCtor.trigger.matches(entity)) { | ||
system.onEntityAdded(entity); | ||
} | ||
}); | ||
this.entityManager.on(_EntityManager.COMPONENT_EVENT.Remove, function (entity) { | ||
if (system.onEntityRemoved && systemCtor.trigger && systemCtor.trigger.matches(entity)) { | ||
system.onEntityRemoved(entity); | ||
} | ||
}); | ||
this.systems.push(system); | ||
this.systems.sort(function (a, b) { | ||
return (a.constructor.priority || 0) - (b.constructor.priority || 0); | ||
}); | ||
} | ||
}); | ||
this.entityManager.on(_EntityManager.COMPONENT_EVENT.Remove, function (entity) { | ||
if (system_1.onEntityRemoved && systemCtor_1.trigger && systemCtor_1.trigger.matches(entity)) { | ||
system_1.onEntityRemoved(entity); | ||
} | ||
}); | ||
this.systems.push(system_1); | ||
this.systems.sort(function (a, b) { | ||
return (a.constructor.priority || 0) - (b.constructor.priority || 0); | ||
}); | ||
} | ||
}, { | ||
key: "execute", | ||
value: function execute(delta, millis) { | ||
var _iterator = _createForOfIteratorHelper(this.systems), | ||
_step; | ||
}; | ||
try { | ||
for (_iterator.s(); !(_step = _iterator.n()).done;) { | ||
var system = _step.value; | ||
SystemManager.prototype.execute = function (delta, millis) { | ||
var e_1, _a; | ||
if (system.initialized && system.execute) { | ||
// const t1 = performance.now(); | ||
system.execute(this.getEntities(system), delta, millis); // console.log((system.constructor as SystemConstructor<System>).tag, performance.now() - t1); | ||
} | ||
try { | ||
for (var _b = (0, _tslib.__values)(this.systems), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var system = _c.value; | ||
if (system.initialized && system.execute) { | ||
// const t1 = performance.now(); | ||
system.execute(this.getEntities(system), delta, millis); // console.log((system.constructor as SystemConstructor<System>).tag, performance.now() - t1); | ||
} | ||
} catch (err) { | ||
_iterator.e(err); | ||
} | ||
} catch (e_1_1) { | ||
e_1 = { | ||
error: e_1_1 | ||
}; | ||
} finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} finally { | ||
_iterator.f(); | ||
if (e_1) throw e_1.error; | ||
} | ||
} | ||
}, { | ||
key: "destroy", | ||
value: function destroy() { | ||
var _this = this; | ||
}; | ||
this.systems.forEach(function (system) { | ||
if (system.initialized && system.tearDown) { | ||
system.tearDown(_this.getEntities(system)); | ||
} | ||
}); | ||
} | ||
}, { | ||
key: "getEntities", | ||
value: function getEntities(system) { | ||
var systemCtor = system.constructor; | ||
return systemCtor.trigger ? this.entityManager.queryByMatcher(systemCtor.trigger) : this.entityManager.getAllEntities(); | ||
} | ||
}]); | ||
SystemManager.prototype.destroy = function () { | ||
var _this = this; | ||
this.systems.forEach(function (system) { | ||
if (system.initialized && system.tearDown) { | ||
system.tearDown(_this.getEntities(system)); | ||
} | ||
}); | ||
}; | ||
SystemManager.prototype.getEntities = function (system) { | ||
var systemCtor = system.constructor; | ||
return systemCtor.trigger ? this.entityManager.queryByMatcher(systemCtor.trigger) : this.entityManager.getAllEntities(); | ||
}; | ||
(0, _tslib.__decorate)([(0, _inversify.inject)(_identifier.IDENTIFIER.SystemRegistry), (0, _tslib.__metadata)("design:type", Object)], SystemManager.prototype, "registry", void 0); | ||
(0, _tslib.__decorate)([(0, _inversify.inject)(_EntityManager.EntityManager), (0, _tslib.__metadata)("design:type", _EntityManager.EntityManager)], SystemManager.prototype, "entityManager", void 0); | ||
SystemManager = (0, _tslib.__decorate)([(0, _inversify.injectable)()], SystemManager); | ||
return SystemManager; | ||
}(), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "registry", [_dec2], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: null | ||
}), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "entityManager", [_dec3], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: null | ||
})), _class2)) || _class); | ||
}(); | ||
exports.SystemManager = SystemManager; |
149
lib/World.js
@@ -8,2 +8,4 @@ "use strict"; | ||
var _tslib = require("tslib"); | ||
var _inversify = require("inversify"); | ||
@@ -19,16 +21,2 @@ | ||
var _dec, _dec2, _dec3, _dec4, _dec5, _class, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4; | ||
function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; } | ||
function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'proposal-class-properties is enabled and runs after the decorators transform.'); } | ||
/** | ||
@@ -48,96 +36,65 @@ * the context of ECS | ||
*/ | ||
var World = (_dec = (0, _inversify.injectable)(), _dec2 = (0, _inversify.inject)(_identifier.IDENTIFIER.EntityPoolFactory), _dec3 = (0, _inversify.inject)(_EntityManager.EntityManager), _dec4 = (0, _inversify.inject)(_ComponentManager.ComponentManager), _dec5 = (0, _inversify.inject)(_SystemManager.SystemManager), _dec(_class = (_class2 = /*#__PURE__*/function () { | ||
var World = function () { | ||
function World() { | ||
_classCallCheck(this, World); | ||
this.lastMillis = new Date().getTime(); | ||
this.enabled = true; | ||
} | ||
_initializerDefineProperty(this, "entityPoolFactory", _descriptor, this); | ||
World.prototype.createEntity = function (name) { | ||
if (name === void 0) { | ||
name = ''; | ||
} | ||
_initializerDefineProperty(this, "entityManager", _descriptor2, this); | ||
var entity = this.entityPoolFactory().acquire(); | ||
entity.setName(name); | ||
return this.entityManager.createEntity(entity); | ||
}; | ||
_initializerDefineProperty(this, "componentManager", _descriptor3, this); | ||
World.prototype.getEntityByName = function (name) { | ||
return this.entityManager.getEntityByName(name); | ||
}; | ||
_initializerDefineProperty(this, "systemManager", _descriptor4, this); | ||
World.prototype.registerComponent = function (clazz) { | ||
this.componentManager.registerComponent(clazz); | ||
return this; | ||
}; | ||
this.lastMillis = new Date().getTime(); | ||
this.enabled = true; | ||
} | ||
World.prototype.registerSystem = function (clazz) { | ||
this.systemManager.registerSystem(clazz); | ||
return this; | ||
}; | ||
_createClass(World, [{ | ||
key: "createEntity", | ||
value: function createEntity() { | ||
var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; | ||
var entity = this.entityPoolFactory().acquire(); | ||
entity.setName(name); | ||
return this.entityManager.createEntity(entity); | ||
World.prototype.execute = function (delta, millis) { | ||
if (!delta) { | ||
millis = new Date().getTime(); | ||
delta = millis - this.lastMillis; | ||
this.lastMillis = millis; | ||
} | ||
}, { | ||
key: "getEntityByName", | ||
value: function getEntityByName(name) { | ||
return this.entityManager.getEntityByName(name); | ||
} | ||
}, { | ||
key: "registerComponent", | ||
value: function registerComponent(clazz) { | ||
this.componentManager.registerComponent(clazz); | ||
return this; | ||
} | ||
}, { | ||
key: "registerSystem", | ||
value: function registerSystem(clazz) { | ||
this.systemManager.registerSystem(clazz); | ||
return this; | ||
} | ||
}, { | ||
key: "execute", | ||
value: function execute(delta, millis) { | ||
if (!delta) { | ||
millis = new Date().getTime(); | ||
delta = millis - this.lastMillis; | ||
this.lastMillis = millis; | ||
} | ||
if (this.enabled) { | ||
this.systemManager.execute(delta, millis); | ||
} | ||
if (this.enabled) { | ||
this.systemManager.execute(delta, millis); | ||
} | ||
}, { | ||
key: "stop", | ||
value: function stop() { | ||
this.enabled = false; | ||
} | ||
}, { | ||
key: "resume", | ||
value: function resume() { | ||
this.enabled = true; | ||
} | ||
}, { | ||
key: "destroy", | ||
value: function destroy() { | ||
this.systemManager.destroy(); | ||
this.entityManager.destroy(); // this.componentManager.destroy(); | ||
} | ||
}]); | ||
}; | ||
World.prototype.stop = function () { | ||
this.enabled = false; | ||
}; | ||
World.prototype.resume = function () { | ||
this.enabled = true; | ||
}; | ||
World.prototype.destroy = function () { | ||
this.systemManager.destroy(); | ||
this.entityManager.destroy(); // this.componentManager.destroy(); | ||
}; | ||
(0, _tslib.__decorate)([(0, _inversify.inject)(_identifier.IDENTIFIER.EntityPoolFactory), (0, _tslib.__metadata)("design:type", Function)], World.prototype, "entityPoolFactory", void 0); | ||
(0, _tslib.__decorate)([(0, _inversify.inject)(_EntityManager.EntityManager), (0, _tslib.__metadata)("design:type", _EntityManager.EntityManager)], World.prototype, "entityManager", void 0); | ||
(0, _tslib.__decorate)([(0, _inversify.inject)(_ComponentManager.ComponentManager), (0, _tslib.__metadata)("design:type", _ComponentManager.ComponentManager)], World.prototype, "componentManager", void 0); | ||
(0, _tslib.__decorate)([(0, _inversify.inject)(_SystemManager.SystemManager), (0, _tslib.__metadata)("design:type", _SystemManager.SystemManager)], World.prototype, "systemManager", void 0); | ||
World = (0, _tslib.__decorate)([(0, _inversify.injectable)()], World); | ||
return World; | ||
}(), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "entityPoolFactory", [_dec2], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: null | ||
}), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "entityManager", [_dec3], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: null | ||
}), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "componentManager", [_dec4], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: null | ||
}), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, "systemManager", [_dec5], { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
initializer: null | ||
})), _class2)) || _class); | ||
}(); | ||
exports.World = World; |
{ | ||
"name": "@antv/g-ecs", | ||
"version": "1.0.0-alpha.6", | ||
"version": "1.0.0-alpha.7", | ||
"description": "A simple ECS implement", | ||
@@ -46,3 +46,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "bfcb87639cfb109825e1d4515ffef6ea48e2b65f" | ||
"gitHead": "9dd75b27ba6bf0c42256b1971cde602a765e7f23" | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
46
1696
64954
1