UNPKG

ecsjs

Version:

An entity component system library for JavaScript

3 lines (2 loc) 4.14 kB
var C="ComponentMap",c="ComponentClassesMap";var i=class extends Map{firstEntry(){return this.size===0?void 0:this.entries().next().value}firstKey(){return this.size===0?void 0:this.keys().next().value}firstValue(){return this.size===0?void 0:this.values().next().value}toJSON(){return{[C]:1,iterable:[...this.entries()]}}toTable(n=!1){let e=[];for(let[t,o]of this.entries()){let s=o,r={};n==!1&&(r["entity.key"]=t),r["entity.type"]=s.constructor.name,e.push({...r,...s})}return e}printTable(n=!1){console.table(this.toTable(n))}static get[Symbol.species](){return Map}},m=class extends Map{toJSON(){return{[c]:1,iterable:[...this.entries()]}}static get[Symbol.species](){return Map}};var y=class extends Error{constructor(){super("Component type is missing the 'name' parameter. i.e. a constructor name")}},p=class extends Error{constructor(e){super(`Component map does not exist for '${e}'`);this.componentName=e}};var l=class{constructor(n,e,...t){this.ecs=n;this.key=e;this.keyMap=this.ecs.getMap(e),this.entries=this.keyMap.entries(),this.components=t.map(o=>this.ecs.getMap(o))}keyMap;components;entries;next(){let n=this.entries.next(),{value:e,done:t}=n;if(t)return{value:e,done:t};let[o,s]=e,r=[o,s];for(let d of this.components)r.push(d.get(o));return{value:r,done:!1}}reset(){this.entries=this.keyMap.entries()}[Symbol.iterator](){return this}};var u=class a{components=new m;nextId=0;register(...n){for(let e of n){let t=e.name;if(t===void 0)throw new y;let o=new i;this.components.set(t,o)}return this}getMap(n){let e=this.components.get(n.name);if(e===void 0)throw new p(n.name);return e}firstEntry(n){return this.getMap(n)?.firstEntry()}firstKey(n,...e){let t=this.getMap(n)?.firstKey();if(arguments.length===1)return t;if(t!==void 0)return[t,...e.map(o=>this.getEntity(t,o))]}firstValue(n,...e){if(arguments.length===1)return this.getMap(n)?.firstValue();let[t,o]=this.getMap(n)?.firstEntry()??[];if(t!==void 0)return[o,...e.map(s=>this.getEntity(t,s))]}getEntity(n,e){let t=this.components.get(e.name);if(t===void 0)throw new p(e.name);return t.get(n)}get(n,...e){return e.length>1?e.map(t=>this.getEntity(n,t)):this.getEntity(n,e[0])}has(n,e){let t=this.components.get(e.name);return t===void 0?!1:t.has(n)}hasAll(n,...e){for(let t=0;t<e.length;t++){let o=e[t],s=this.components.get(o.name);if(s===void 0||s.has(n)===!1)return!1}return!0}hasAny(n,...e){for(let t=0;t<e.length;t++){let o=e[t],s=this.components.get(o.name);if(s!==void 0&&s.has(n))return!0}return!1}setEntity(n,e){let t=this.components.get(e.constructor.name);if(t===void 0)throw new p(e.constructor.name);return t.set(n,e),e}set(n,...e){return e.length>1?e.map(t=>this.setEntity(n,t)):this.setEntity(n,e[0])}remove(n,...e){for(let t of e)this.removeByKey(n,t.name)}removeByKey(n,e){let t=this.components.get(e);if(t===void 0)throw new p(e);return t.get(n)===void 0?!1:t.delete(n)}destroyEntity(...n){for(let e=0;e<n.length;e++){let t=n[e];for(let o of this.components.values())o.has(t)&&o.delete(t)}}getNextId(){return this.nextId++,this.nextId}clear(){return this.components.clear(),this}clearComponents(){return this.components.forEach(n=>n.clear()),this}iterator(n,...e){return new l(this,n,...e)}printTable(){return this.components.forEach(n=>console.table(n.toTable(!0))),this}printEntity(n){for(let e of this.components.values()){if(e.has(n)===!1)continue;let t=e.get(n),o={name:t.constructor.name,...t};console.table({[n]:o})}return this}static parse(n){return JSON.parse(n,function(t,o){return o.hasOwnProperty("components")?(Reflect.setPrototypeOf(o,a.prototype),o):o.hasOwnProperty(C)?new i(o.iterable):o.hasOwnProperty(c)?new m(o.iterable):this[t]})}static createWithTracing(n){let e={get(t,o){let s=t[o];return typeof s=="function"&&(n.length===0||n.includes(o))?function(...r){return console.groupCollapsed("ecs trace",o,r),console.trace(),console.groupEnd(),s.apply(this,r)}:s}};return new Proxy(new a,e)}};var f=new u;typeof window<"u"?window.ecs=f:typeof module<"u"&&module!==null&&(module.exports={ecs:f});export{m as ComponentClassesMap,l as ComponentIterator,i as ComponentMap,u as EntityMap,f as ecs}; //# sourceMappingURL=ecs.js.map