@curbl/ecs
Version:
Small Entity Component System
3 lines (2 loc) • 10.7 kB
JavaScript
var t=require("uuid");function e(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,i(t,e)}function i(t,e){return i=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},i(t,e)}function n(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function s(t,e){(null==e||e>t.length)&&(e=t.length);for(var i=0,n=new Array(e);i<e;i++)n[i]=t[i];return n}function o(t,e){var i="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(i)return(i=i.call(t)).next.bind(i);if(Array.isArray(t)||(i=function(t,e){if(t){if("string"==typeof t)return s(t,e);var i=Object.prototype.toString.call(t).slice(8,-1);return"Object"===i&&t.constructor&&(i=t.constructor.name),"Map"===i||"Set"===i?Array.from(t):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?s(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){i&&(t=i);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}}}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 r=/*#__PURE__*/function(){function t(t,e){this.mask=void 0,this.mask=new Uint32Array(Math.ceil(t/32)),void 0!==e&&this.mask.set(e)}var e=t.prototype;return e.set=function(t,e){var i=this.mask.length-(1+(t>>>5));this.mask[i]=0===e?(this.mask[i]|1<<t)^1<<t:this.mask[i]|1<<t},e.compareAnd=function(t){this.balance(t);for(var e=0;e<this.mask.length;e++)if(this.mask[e]!==(this.mask[e]&t.mask[e])>>>0)return!1;return!0},e.isEqual=function(t){this.balance(t);for(var e=0;e<this.mask.length;e++)if(this.mask[e]!==t.mask[e])return!1;return!0},e.balance=function(t){this.mask.length!==t.mask.length&&(this.mask.length>t.mask.length?t.grow(this.mask.length):this.grow(t.mask.length))},e.grow=function(t){var e=new Uint32Array(t);e.set(this.mask,(t||this.mask.length)-this.mask.length),this.mask=e},e.clone=function(){return new t(32*this.mask.length,this.mask)},e.clear=function(){for(var t=0;t<this.mask.length;t++)this.mask[t]=0},e.toString=function(){for(var t="",e=0;e<this.mask.length;e++){var i=this.mask[e].toString(2);t+="00000000000000000000000000000000".substr(0,32-i.length)+i}return t},t}(),a=/*#__PURE__*/function(){function t(){this.components=void 0,this.size=void 0,this.components=Object.create(null),this.size=0}var e=t.prototype;return e.clear=function(){this.components=Object.create(null),this.size=0},e.register=function(t){return void 0===this.components[t]&&(this.components[t]=this.size,this.size++),this.components[t]},e.remove=function(t){void 0!==this.components[t]&&(delete this.components[t],this.size=Object.keys(this.components).length)},e.buildMask=function(t){for(var e,i=new r(this.size),n=0;e=t[n];n++)i.set("string"==typeof e?this.components[e]:this.components[e.__id],1);return i},t}(),d=/*#__PURE__*/function(){function t(t,e){this.__id=void 0,this.__bitmask=void 0,this.dead=void 0,this.dirty=void 0,this.paused=void 0,this.store=void 0,this.components=void 0,this.updates=void 0,this.removedComponents=void 0,this.__id=t,this.__bitmask=new r(32),this.store=e,this.components=new Map,this.updates=new Map,this.removedComponents=[],this.dirty=!1,this.dead=!1,this.paused=!1}var e=t.prototype;return e.addComponent=function(t){this.updates.set(t.constructor.__id,{component:t,added:!0}),this.markDirty()},e.add=function(t){var e=this;if(!this.dead){var i=t;i.load?i.load().then(function(){return e.addComponent(i)}):this.addComponent(i)}},e.get=function(t){return this.components.get("string"==typeof t?t:t.__id)},e.has=function(t){return this.components.has("string"==typeof t?t:t.__id)},e.removeComponent=function(t){this.components.has(t)?(this.updates.set(t,{component:this.components.get(t),added:!1}),this.markDirty()):this.updates.delete(t)},e.remove=function(t){var e=this,i="string"==typeof t?t:t.__id;if(!this.dead&&(this.components.has(i)||this.updates.has(i))){var n=this.components.get(i);n&&n.unload?n.unload().then(function(){return e.removeComponent(i)}):this.removeComponent(i)}},e.dispose=function(){this.dead=!0,this.markDirty()},e.active=function(){return this.store.active(this)},e.pause=function(){this.paused||(this.markDirty(),this.paused=!0)},e.unpause=function(){this.paused&&(this.paused=!1,this.store.add(this),this.markDirty())},e.__updateRemoved=function(){if(this.dead)return this.__clear(),void this.store.delete(this);if(this.paused)this.store.remove(this);else{for(var t,e=0;t=this.removedComponents[e];e++)this.components.delete(t.constructor.__id);this.removedComponents.length=0}},e.__updateMaskAndNew=function(){if(this.dirty){if(this.dead||this.paused)return this.__bitmask.clear(),void(this.dirty=!1);for(var t,e=o(this.updates.values());!(t=e()).done;){var i=t.value;i.added?this.__add(i.component):i.component&&(this.__bitmask.set(i.component.constructor.__bit,0),this.removedComponents.push(i.component))}this.updates.clear(),this.dirty=!1}},e.markDirty=function(){this.dirty||this.paused||(this.dirty=!0,this.store.markModified(this))},e.__add=function(t){this.__bitmask.set(t.constructor.__bit,1),this.components.set(t.constructor.__id,t)},e.__clear=function(){this.__bitmask.clear(),this.components.clear(),this.updates.clear(),this.removedComponents.length=0,this.dirty=!1,this.dead=!1,this.paused=!1},t}(),h=/*#__PURE__*/function(){function t(){this.queries=new Map}var e=t.prototype;return e.registerQuery=function(t,e){for(var i,n=o(this.queries.keys());!(i=n()).done;){var s=i.value;if(s.isEqual(e))return[this.queries.get(s).list,s]}for(var r,a={set:new Set,list:[],onEntityAdded:[],onEntityRemoved:[]},d=o(t);!(r=d()).done;){var h=r.value;e.compareAnd(h.__bitmask)&&(a.set.add(h),a.list.push(h))}return this.queries.set(e,a),[a.list,e]},e.addQueryOnAdded=function(t,e){var i;null==(i=this.queries.get(t))||i.onEntityAdded.push(e)},e.addQueryOnRemoved=function(t,e){var i;null==(i=this.queries.get(t))||i.onEntityRemoved.push(e)},t.callEntityAdded=function(t,e){for(var i,n=0;i=e.onEntityAdded[n];n++)i.onEntityAdded(t)},t.callEntityRemoved=function(t,e){for(var i,n=0;i=e.onEntityRemoved[n];n++)i.onEntityRemoved(t)},t.updateQuery=function(e,i,n){var s=i.compareAnd(e.__bitmask),o=n.set.has(e);return s&&!o?(n.set.add(e),t.callEntityAdded(e,n),!0):!(s||!o||(n.set.delete(e),t.callEntityRemoved(e,n),0))},e.update=function(e){for(var i,n=o(this.queries.entries());!(i=n()).done;){for(var s,r=i.value,a=r[0],d=r[1],h=!1,u=0;s=e[u];u++)s.__updateMaskAndNew(),h=t.updateQuery(s,a,d)||h;if(h){d.list.length=0;for(var c,m=o(d.set.values());!(c=m()).done;)d.list.push(c.value)}}for(var p,f=0;p=e[f];f++)p.__updateRemoved()},e.clear=function(){for(var t,e=o(this.queries.values());!(t=e()).done;){var i=t.value;i.set.clear(),i.list.length=0}},t}(),u=/*#__PURE__*/function(){function e(){this.queryStore=new h,this.entities=new Map,this.pool=[],this.maxPoolSize=1e4,this.modified=[]}var i=e.prototype;return i.setMaxPoolSize=function(t){this.maxPoolSize=t,this.pool.length>this.maxPoolSize&&(this.pool.length=this.maxPoolSize)},i.registerQuery=function(t){return this.queryStore.registerQuery(this.entities.values(),t)},i.addQueryOnAdded=function(t,e){this.queryStore.addQueryOnAdded(t,e)},i.addQueryOnRemoved=function(t,e){this.queryStore.addQueryOnRemoved(t,e)},i.create=function(e){var i=this.pool.pop()||new d(t.v4(),this);this.entities.set(i.__id,i);for(var n,s=0;n=e[s];s++)i.add(n);return i},i.active=function(t){return this.entities.has(t.__id)},i.add=function(t){this.entities.set(t.__id,t)},i.delete=function(t){this.entities.delete(t.__id),this.pool.length<this.maxPoolSize&&this.pool.push(t)},i.remove=function(t){this.entities.delete(t.__id)},i.markModified=function(t){this.modified.push(t)},i.update=function(){if(this.modified.length){var t=[].concat(this.modified);this.queryStore.update(t),this.modified=this.modified.slice(t.length,this.modified.length)}},i.clear=function(){this.modified.length=0,this.entities.clear(),this.queryStore.clear()},e}();function c(){}var m=/*#__PURE__*/function(){function t(){}var e=t.prototype;return e.setUp=function(){},e.tearDown=function(){},e.entities=function(){return this.constructor.__entities},t}();function p(t,e){return function(t,e){for(var i,n=0;i=e[n];n++)t.constructor&&t.constructor.prototype?null==t.constructor.prototype[i]&&(t.constructor.prototype[i]=c):null==t[i]&&(t[i]=c)}(t,e),t}var f=/*#__PURE__*/function(){function t(){this.updateMethods=["update"],this.systems=[]}var e=t.prototype;return e.setUpdateMethods=function(t){this.updateMethods=t},e.addSystem=function(t){this.hasSystem(t)||(this.systems.push(t),p(t,this.updateMethods),t.setUp())},e.removeSystem=function(t){var e=this.systems.indexOf(t);-1!==e&&(this.systems.splice(e,1),t.tearDown())},e.hasSystem=function(t){return this.systems.includes(t)},e.clear=function(){this.systems.length=0,this.updateMethods=["update"]},e.callMethodOnSystems=function(t,e,i,n,s,o,r,a,d,h){for(var u,c=0;u=this.systems[c];c++)u[t](e,i,n,s,o,r,a,d,h)},e.update=function(t,e,i,n,s,o,r,a,d){for(var h,u=0;h=this.updateMethods[u];u++)this.callMethodOnSystems(h,t,e,i,n,s,o,r,a,d)},t}();exports.ECS=/*#__PURE__*/function(){function t(){this.entityStore=void 0,this.systemStore=void 0,this.componentBitMask=void 0,this.entityStore=new u,this.systemStore=new f,this.componentBitMask=new a}var i=t.prototype;return i.setMaxEntityPoolSize=function(t){this.entityStore.setMaxPoolSize(t)},i.setUpdateMethods=function(t){this.systemStore.setUpdateMethods(t)},i.reset=function(){this.systemStore.clear(),this.entityStore.clear()},i.addEntity=function(){return this.entityStore.create([].slice.call(arguments))},i.addSystem=function(t){this.systemStore.addSystem(t)},i.active=function(t){return this.entityStore.active(t)},i.removeSystem=function(t){this.systemStore.removeSystem(t)},i.hasSystem=function(t){return this.systemStore.hasSystem(t)},i.Component=function(t){var e=this.componentBitMask.register(t);return function(i){return i.__id=t,i.__bit=e,i}},i.System=function(){for(var t,i=[].slice.call(arguments),s=0;t=i[s];s++)this.componentBitMask.register("string"==typeof t?t:t.__id);var o=this.componentBitMask.buildMask(i),r=this.entityStore.registerQuery(o),a=r[0],d=r[1],h=this.entityStore;return function(t){return t.__entities=a,/*#__PURE__*/function(t){function i(){var e;return(e=t.call.apply(t,[this].concat([].slice.call(arguments)))||this).onEntityAdded&&h.addQueryOnAdded(d,n(e)),e.onEntityRemoved&&h.addQueryOnRemoved(d,n(e)),e}return e(i,t),i}(t)}},i.update=function(t,e,i,n,s,o,r,a,d){this.entityStore.update(),this.systemStore.update(t,e,i,n,s,o,r,a,d)},t}(),exports.EntityHandle=d,exports.System=m,exports.injectSystem=p;
//# sourceMappingURL=ecs.js.map