Comparing version 0.2.6 to 0.2.7
@@ -1,2 +0,2 @@ | ||
const e={bool:"bool",int8:"int8",uint8:"uint8",uint8clamped:"uint8clamped",int16:"int16",uint16:"uint16",int32:"int32",uint32:"uint32",float32:"float32",float64:"float64"},t={bool:"bool",int8:Int8Array,uint8:Uint8Array,uint8clamped:Uint8ClampedArray,int16:Int16Array,uint16:Uint16Array,int32:Int32Array,uint32:Uint32Array,float32:Float32Array,float64:Float64Array},n={uint8:255,uint16:65535,uint32:4294967295},r=()=>{let e=1,r=0;const o=(s,i={},a=!0)=>{const c={},l={},f={},m=Object.keys(i),p=m.filter((e=>Array.isArray(i[e])&&"object"==typeof i[e][0])),y=Object.keys(t).reduce(((e,t)=>({...e,[t]:0})),{});let u;return m.forEach((e=>{if("bool"===i[e]){const n=t.uint8,r=s*t.uint8.BYTES_PER_ELEMENT,o=new SharedArrayBuffer(r);l[e]=i[e],c[e]=new n(o),c[e]._boolType=!0}else if(Array.isArray(i[e])&&"string"==typeof i[e][0]){const n=t.uint8,r=s*t.uint8.BYTES_PER_ELEMENT,o=new SharedArrayBuffer(r);l[e]=i[e],c[e]=new n(o)}else if(Array.isArray(i[e])&&"object"==typeof i[e][0]){const{index:o,type:a,length:l}=i[e][0];if(!l)throw new Error("❌ Must define a length for component array.");if(!t[a])throw new Error(`❌ Invalid component array property type ${a}.`);if(!t[o])throw new Error(`❌ Invalid component array index type ${o}.`);if(!n[o])throw new Error(`❌ Index type for component array must be unsigned (non-negative), instead was ${o}.`);if(!f[a]){const e=p.filter((e=>i[e][0].type===a)),n=e.reduce(((e,n)=>e+t[a].BYTES_PER_ELEMENT),0),c=e.reduce(((e,t)=>e+l),0),m=new SharedArrayBuffer((r=n*c*s,4*Math.ceil(r/4))),y=new t[a](m);y._indexType=o,y._indexBytes=t[o].BYTES_PER_ELEMENT,f[a]=y}c[e]={};let m=0;for(let t=0;t<s;t++){const n=y[a]+t*l,r=n+l;c[e][t]=f[a].subarray(n,r),m=r}y[a]=m,c[e]._reset=t=>c[e][t].fill(0),c[e]._set=(t,n)=>c[e][t].set(n,0)}else if("object"==typeof i[e])c[e]=o(s,i[e],!1);else if("string"==typeof i[e]){const n=i[e],r=s*t[n].BYTES_PER_ELEMENT,o=new SharedArrayBuffer(r);c[e]=new t[n](o)}else{if("function"!=typeof i[e])throw new Error("ECS Error: invalid property type "+i[e]);{const t=i[e],n=s*t.BYTES_PER_ELEMENT,r=new SharedArrayBuffer(n);c[e]=new t(r)}}var r})),Object.defineProperty(c,"_schema",{value:i}),Object.defineProperty(c,"_props",{value:Object.keys(i)}),Object.defineProperty(c,"_mapping",{value:e=>l[e]}),Object.defineProperty(c,"_reset",{value:e=>{for(const t of c._props)ArrayBuffer.isView(c[t])?ArrayBuffer.isView(c[t][e])?c[t][e].fill(0):c[t][e]=0:c[t]._reset(e)}}),Object.defineProperty(c,"_set",{value:(e,t)=>{for(const n in t){c._mapping(n)&&"string"==typeof t[n]?c.enum(n,e,t[n]):ArrayBuffer.isView(c[n])?c[n][e]=t[n]:Array.isArray(t[n])&&ArrayBuffer.isView(c[n][e])?c[n][e].set(t[n],0):"object"==typeof c[n]&&c[n]._set(e,t[n])}}}),Object.defineProperty(c,"_get",{value:e=>{const t={};for(const n of c._props){c._mapping(n)?t[n]=c.enum(n,e):ArrayBuffer.isView(c[n])?t[n]=c[n][e]:"object"==typeof c[n]&&(ArrayBuffer.isView(c[n][e])?t[n]=Array.from(c[n][e]):t[n]=c[n]._get(e))}return t}}),Object.defineProperty(c,"_flatten",{value:(e=[])=>{if(u)return u;for(const t of c._props)ArrayBuffer.isView(c[t])?e.push(c[t]):"object"==typeof c[t]&&c[t]._flatten(e);return u=e,e}}),Object.defineProperty(c,"_bitflag",{value:e}),Object.defineProperty(c,"_generationId",{value:r}),Object.defineProperty(c,"check",{value:e=>(e&c._bitflag)===c._bitflag}),Object.defineProperty(c,"enum",{value:(e,t,n)=>{const r=c._mapping(e);if(r){if(!n)return r[c[e][t]];{const o=r.indexOf(n);if(-1===o)return void console.warn(`Value '${n}' is not part of enum.`);c[e][t]=o}}else console.warn("Property is not an enum.")}}),a&&(e*=2,e>=Math.pow(2,32)&&(e=1,r+=1)),c};return o};export default(t={})=>{const n=r(),o=(({maxEntities:e=1e4,maxComponentTypes:t=128,tickRate:n=30,devtools:r=!1})=>({maxEntities:e,maxComponentTypes:t,maxGenerations:Math.ceil(t/32),tickRate:n,devtools:r}))(t),s=(({maxEntities:e,maxGenerations:t})=>({entities:Array.from({length:t},(()=>new Uint32Array(e))),components:{},systems:{}}))(o),{entityCount:i,addEntity:a,removeEntity:c,commitEntityRemovals:l,getEntityCursor:f}=((e,t)=>{const{entities:n,systems:r}=t,o=[],s=[],i=new Uint8Array(e.maxEntities);let a=0;return{entityCount:()=>a-o.length,addEntity:()=>{if(a>=e.maxEntities)return void console.warn("❌ Could not add entity, maximum number of entities reached.");const t=o.length>0?o.pop():a++;return i[t]=1,t},removeEntity:e=>{s.push(e)},commitEntityRemovals:()=>{if(0!==s.length){for(let e of s)if(0!==i[e]){for(const t in r)r[t].remove(e);o.push(e),i[e]=0;for(let t of n)t[e]=0}s.length=0}},getEntityCursor:()=>a}})(o,s),{getGeneration:m,registerComponent:p,addComponent:y,removeComponent:u,removeAllComponents:E,hasComponent:d,commitComponentRemovals:_}=((e,t,n)=>{const{entities:r,components:o,systems:s}=t,i=[];let a=0;const c=e=>{if(void 0===o[e])throw new Error(`❌ Component '${e}' is not registered.`);return o[e]},l=(e,t)=>{i.push(e,t)},f=(e,t)=>{const n=o[e],{_generationId:s,_bitflag:i}=n;return(r[s][t]&i)===i};return{getGeneration:()=>a,registerComponent:(t,r)=>{if(Object.keys(o).length+1>e.maxComponentTypes)throw new Error(`❌ Can not register component '${t}'. Max components '${e.maxComponentTypes}' reached.`);return o[t]=n(e.maxEntities,r),a=o[t]._generationId,o[t]},addComponent:(e,t,n={},o=!0)=>{if(f(e,t))return;const i=c(e),{_generationId:a,_bitflag:l}=i;r[a][t]|=l,o&&i._reset(t),i._set(t,n);for(const e in s){const n=s[e];n.components.length&&n.checkComponent(i)&&n.check(t)&&n.add(t)}},removeComponent:l,removeAllComponents:e=>{Object.keys(o).forEach((t=>{l(t,e)}))},hasComponent:f,commitComponentRemovals:()=>{if(0!==i.length){for(let e=0;e<i.length;e+=2){const t=i[e],n=i[e+1],o=c(t),{_generationId:a,_bitflag:l}=o;if(!(r[a][n]&l))return;r[a][n]&=~l;for(const e in s){const t=s[e];t.components.length&&t.checkComponent(o)&&!t.check(n)&&t.remove(n)}}i.length=0}}}})(o,s,n),{enabled:g,registerSystem:h,toggle:b,step:w}=((e,t,n,r)=>{const{entities:o,components:s,systems:i}=t,a=[];let c=!0;const l=()=>{r(),n()};return{registerSystem:({name:t,components:n=[],enter:r,update:c,exit:f})=>{const m=[],p=new Int32Array(e.maxEntities).fill(-1),y={count:0,name:t,enabled:!0,components:n,localEntities:m,update:c,enter:r,exit:f},u=n.map((e=>{if(void 0===s[e])throw new Error(`❌ Cannot register system '${t}', '${e}' is not a registered component.`);return s[e]})),E=Array.from({length:e.maxGenerations},(()=>0));for(const e of u){const{_generationId:t,_bitflag:n}=e;E[t]|=n}y.masks=E,y.check=e=>{for(let t=0;t<E.length;t++){const n=o[t][e],r=E[t];if((n&r)!==r)return!1}return!0},y.checkComponent=e=>(E[e._generationId]&e._bitflag)===e._bitflag;const d=c?c(...u):null,_=r?r(...u):null,g=f?f(...u):null;y.execute=e=>{(e||y.enabled)&&d&&d(m),l()},y.add=e=>{-1===p[e]&&(p[e]=m.push(e)-1,y.count=m.length,_&&_(e))},y.remove=e=>{const t=p[e];if(-1===t)return;const n=m.pop();n!==e&&(m[t]=n,p[n]=t),p[e]=-1,y.count=m.length,g&&g(e)};for(let e=0;e<o[0].length;e++)y.components.length&&y.check(e)&&y.add(e);return i[t]=y,t.includes("query")||a.push(y),y},enabled:e=>e?i[e]?i[e].enabled:void console.warn(`System '${e}' is not registered.`):c,step:(e,t=!1)=>{if("boolean"==typeof e&&(t=e),"string"!=typeof e){if(t||c)for(let e=0;e<a.length;e++)a[e].execute();l()}else i[e].execute(t)},toggle:e=>{if(e)return i[e]?void(i[e].enabled=!i[e].enabled):void console.warn(`System '${e} is not registered.`);c=!c}}})(o,s,l,_),{save:A,load:v}=((e,t,n)=>({save:()=>{const r=n(),o=t(),s=Object.keys(e.components),i=s.map((t=>e.components[t])),a=Object.keys(e.systems),c=a.map((t=>e.systems[t])),l=Math.ceil(s.length/32),f=o*l*Uint32Array.BYTES_PER_ELEMENT,m=i.reduce(((e,t)=>e+t._flatten().reduce(((e,t)=>e+t.BYTES_PER_ELEMENT*o),0)),0),p=c.reduce(((e,t)=>e+t.localEntities.reduce(((e,t)=>e+Uint32Array.BYTES_PER_ELEMENT),0)),0),y=new ArrayBuffer(f+m+p),u=new DataView(y);let E=0;for(let t=0;t<o;t++)for(let n=0;n<l;n++){const r=e.entities[n];u.setUint32(E,r[t]),E+=Uint32Array.BYTES_PER_ELEMENT}const d={_cursorStart:0,_cursorEnd:E};i.forEach((e=>{e._flatten().forEach((e=>{const t=e.constructor.name.split("Array")[0],n=e.BYTES_PER_ELEMENT,o=r;e._cursorStart=E;for(let r=0;r<o;r++)u["set"+t](E,e[r]),E+=n;e._cursorEnd=E}))}));const _={};return a.forEach((t=>{const n=e.systems[t],r=E;n.localEntities.forEach((e=>{u.setUint32(E,e),E+=Uint32Array.BYTES_PER_ELEMENT}));const o=E;_[t]={_cursorStart:r,_cursorEnd:o}})),{bin:u.buffer.slice(0,E),schema:JSON.stringify({entities:d,components:e.components,systems:_},((e,t)=>t.hasOwnProperty("_cursorStart")?[t._cursorStart,t._cursorEnd]:t))}},load:(r,o)=>{"string"==typeof r&&(r=JSON.parse(r));const s=n(),i=t(),a=Object.keys(e.components),c=a.map((t=>e.components[t])),l=Object.keys(e.systems),f=Math.ceil(a.length/32),m=new DataView(o);let p=0;for(let t=0;t<i;t++)for(let n=0;n<f;n++)e.entities[n][t]=m.getUint32(p),p+=Uint32Array.BYTES_PER_ELEMENT;c.forEach((e=>{e._flatten().forEach((e=>{const t=e.constructor.name.split("Array")[0],n=e.BYTES_PER_ELEMENT,r=s;for(let o=0;o<r;o++)e[o]=m["get"+t](p),p+=n}))})),l.forEach((t=>{const n=e.systems[t],[o,s]=r.systems[t],i=[];for(;p<s;)i.push(m.getUint32(p)),p+=Uint32Array.BYTES_PER_ELEMENT;n.localEntities=i}))}}))(s,i,f);let T=0;return{TYPES:e,config:o,registry:s,entityCount:i,addEntity:a,removeEntity:c,registerComponent:p,removeComponent:u,removeAllComponents:E,addComponent:y,hasComponent:d,registerSystem:h,createQuery:e=>h({name:"query-"+T++,components:e}).localEntities,enabled:g,toggle:b,step:w,save:A,load:v}}; | ||
const e={bool:"bool",int8:"int8",uint8:"uint8",uint8clamped:"uint8clamped",int16:"int16",uint16:"uint16",int32:"int32",uint32:"uint32",float32:"float32",float64:"float64"},t={bool:"bool",int8:Int8Array,uint8:Uint8Array,uint8clamped:Uint8ClampedArray,int16:Int16Array,uint16:Uint16Array,int32:Int32Array,uint32:Uint32Array,float32:Float32Array,float64:Float64Array},n={uint8:255,uint16:65535,uint32:4294967295},r=()=>{let e=1,r=0;const o=(i,s={},a=!0)=>{const c={},l={},f={},y=Object.keys(s),E=y.filter((e=>Array.isArray(s[e])&&"object"==typeof s[e][0])),m=Object.keys(t).reduce(((e,t)=>({...e,[t]:0})),{});let p;return y.forEach((e=>{if("bool"===s[e]){const n=t.uint8,r=i*t.uint8.BYTES_PER_ELEMENT,o=new SharedArrayBuffer(r);l[e]=s[e],c[e]=new n(o),c[e]._boolType=!0}else if(Array.isArray(s[e])&&"string"==typeof s[e][0]){const n=t.uint8,r=i*t.uint8.BYTES_PER_ELEMENT,o=new SharedArrayBuffer(r);l[e]=s[e],c[e]=new n(o)}else if(Array.isArray(s[e])&&"object"==typeof s[e][0]){const{index:o,type:a,length:l}=s[e][0];if(!l)throw new Error("❌ Must define a length for component array.");if(!t[a])throw new Error(`❌ Invalid component array property type ${a}.`);if(!t[o])throw new Error(`❌ Invalid component array index type ${o}.`);if(!n[o])throw new Error(`❌ Index type for component array must be unsigned (non-negative), instead was ${o}.`);if(!f[a]){const e=E.filter((e=>s[e][0].type===a)),n=e.reduce(((e,n)=>e+t[a].BYTES_PER_ELEMENT),0),c=e.reduce(((e,t)=>e+l),0),y=new SharedArrayBuffer((r=n*c*i,4*Math.ceil(r/4))),m=new t[a](y);m._indexType=o,m._indexBytes=t[o].BYTES_PER_ELEMENT,f[a]=m}c[e]={};let y=0;for(let t=0;t<i;t++){const n=m[a]+t*l,r=n+l;c[e][t]=f[a].subarray(n,r),y=r}m[a]=y,c[e]._reset=t=>c[e][t].fill(0),c[e]._set=(t,n)=>c[e][t].set(n,0)}else if("object"==typeof s[e])c[e]=o(i,s[e],!1);else if("string"==typeof s[e]){const n=s[e],r=i*t[n].BYTES_PER_ELEMENT,o=new SharedArrayBuffer(r);c[e]=new t[n](o)}else{if("function"!=typeof s[e])throw new Error("ECS Error: invalid property type "+s[e]);{const t=s[e],n=i*t.BYTES_PER_ELEMENT,r=new SharedArrayBuffer(n);c[e]=new t(r)}}var r})),Object.defineProperty(c,"_schema",{value:s}),Object.defineProperty(c,"_props",{value:Object.keys(s)}),Object.defineProperty(c,"_mapping",{value:e=>l[e]}),Object.defineProperty(c,"_reset",{value:e=>{for(const t of c._props)ArrayBuffer.isView(c[t])?ArrayBuffer.isView(c[t][e])?c[t][e].fill(0):c[t][e]=0:c[t]._reset(e)}}),Object.defineProperty(c,"_set",{value:(e,t)=>{for(const n in t){c._mapping(n)&&"string"==typeof t[n]?c.enum(n,e,t[n]):ArrayBuffer.isView(c[n])?c[n][e]=t[n]:Array.isArray(t[n])&&ArrayBuffer.isView(c[n][e])?c[n][e].set(t[n],0):"object"==typeof c[n]&&c[n]._set(e,t[n])}}}),Object.defineProperty(c,"_get",{value:e=>{const t={};for(const n of c._props){c._mapping(n)?t[n]=c.enum(n,e):ArrayBuffer.isView(c[n])?t[n]=c[n][e]:"object"==typeof c[n]&&(ArrayBuffer.isView(c[n][e])?t[n]=Array.from(c[n][e]):t[n]=c[n]._get(e))}return t}}),Object.defineProperty(c,"_flatten",{value:(e=[])=>{if(p)return p;for(const t of c._props)ArrayBuffer.isView(c[t])?e.push(c[t]):"object"==typeof c[t]&&c[t]._flatten(e);return p=e,e}}),Object.defineProperty(c,"_bitflag",{value:e}),Object.defineProperty(c,"_generationId",{value:r}),Object.defineProperty(c,"check",{value:e=>(e&c._bitflag)===c._bitflag}),Object.defineProperty(c,"enum",{value:(e,t,n)=>{const r=c._mapping(e);if(r){if(!n)return r[c[e][t]];{const o=r.indexOf(n);if(-1===o)return void console.warn(`Value '${n}' is not part of enum.`);c[e][t]=o}}else console.warn("Property is not an enum.")}}),a&&(e*=2,e>=Math.pow(2,32)&&(e=1,r+=1)),c};return o};export default(t={})=>{const n=r(),o=(({maxEntities:e=1e4,maxComponentTypes:t=128,tickRate:n=30,devtools:r=!1})=>({maxEntities:e,maxComponentTypes:t,maxGenerations:Math.ceil(t/32),tickRate:n,devtools:r}))(t),i=(({maxEntities:e,maxGenerations:t})=>({entities:Array.from({length:t},(()=>new Uint32Array(e))),components:{},systems:{}}))(o),{entityCount:s,addEntity:a,removeEntity:c,commitEntityRemovals:l,getEntityCursor:f}=((e,t)=>{const{entities:n,systems:r}=t,o=[],i=[],s=new Uint8Array(e.maxEntities);let a=0;return{entityCount:()=>a-o.length,addEntity:()=>{if(a>=e.maxEntities)return void console.warn("❌ Could not add entity, maximum number of entities reached.");const t=o.length>0?o.pop():a++;return s[t]=1,t},removeEntity:e=>{i.push(e)},commitEntityRemovals:()=>{if(0!==i.length){for(let e of i)if(0!==s[e]){for(const t in r)r[t].remove(e);o.push(e),s[e]=0;for(let t of n)t[e]=0}i.length=0}},getEntityCursor:()=>a}})(o,i),{getGeneration:y,registerComponent:E,addComponent:m,removeComponent:p,removeAllComponents:u,hasComponent:d,commitComponentRemovals:_}=((e,t,n)=>{const{entities:r,components:o,systems:i}=t,s=[];let a=0;const c=e=>{if(void 0===o[e])throw new Error(`❌ Component '${e}' is not registered.`);return o[e]},l=(e,t)=>{s.push(e,t)},f=(e,t)=>{const n=o[e],{_generationId:i,_bitflag:s}=n;return(r[i][t]&s)===s};return{getGeneration:()=>a,registerComponent:(t,r)=>{if(Object.keys(o).length+1>e.maxComponentTypes)throw new Error(`❌ Can not register component '${t}'. Max components '${e.maxComponentTypes}' reached.`);return o[t]=n(e.maxEntities,r),a=o[t]._generationId,o[t]},addComponent:(e,t,n={},o=!0)=>{if(f(e,t))return;const s=c(e),{_generationId:a,_bitflag:l}=s;r[a][t]|=l,o&&s._reset(t),s._set(t,n);for(const e in i){const n=i[e];n.components.length&&n.checkComponent(s)&&n.check(t)&&n.add(t)}},removeComponent:l,removeAllComponents:e=>{Object.keys(o).forEach((t=>{l(t,e)}))},hasComponent:f,commitComponentRemovals:()=>{if(0!==s.length){for(let e=0;e<s.length;e+=2){const t=s[e],n=s[e+1],o=c(t),{_generationId:a,_bitflag:l}=o;if(!(r[a][n]&l))return;r[a][n]&=~l;for(const e in i){const t=i[e];t.components.length&&t.checkComponent(o)&&!t.check(n)&&t.remove(n)}}s.length=0}}}})(o,i,n),{enabled:g,registerSystem:h,toggle:b,step:A}=((e,t,n,r)=>{const{entities:o,components:i,systems:s}=t,a=[];let c=!0;const l=()=>{r(),n()};return{registerSystem:({name:t,components:n=[],enter:r,update:c,exit:f})=>{const y=[],E=new Int32Array(e.maxEntities).fill(-1),m={count:0,name:t,enabled:!0,components:n,localEntities:y,entityIndices:E,update:c,enter:r,exit:f},p=n.map((e=>{if(void 0===i[e])throw new Error(`❌ Cannot register system '${t}', '${e}' is not a registered component.`);return i[e]})),u=Array.from({length:e.maxGenerations},(()=>0));for(const e of p){const{_generationId:t,_bitflag:n}=e;u[t]|=n}m.masks=u,m.check=e=>{for(let t=0;t<u.length;t++){const n=o[t][e],r=u[t];if((n&r)!==r)return!1}return!0},m.checkComponent=e=>(u[e._generationId]&e._bitflag)===e._bitflag;const d=c?c(...p):null,_=r?r(...p):null,g=f?f(...p):null;m.execute=e=>{(e||m.enabled)&&d&&d(y),l()},m.add=e=>{-1===E[e]&&(E[e]=y.push(e)-1,m.count=y.length,_&&_(e))},m.remove=e=>{const t=E[e];if(-1===t)return;const n=y.pop();n!==e&&(y[t]=n,E[n]=t),E[e]=-1,m.count=y.length,g&&g(e)};for(let e=0;e<o[0].length;e++)m.components.length&&m.check(e)&&m.add(e);return s[t]=m,t.includes("query")||a.push(m),m},enabled:e=>e?s[e]?s[e].enabled:void console.warn(`System '${e}' is not registered.`):c,step:(e,t=!1)=>{if("boolean"==typeof e&&(t=e),"string"!=typeof e){if(t||c)for(let e=0;e<a.length;e++)a[e].execute();l()}else s[e].execute(t)},toggle:e=>{if(e)return s[e]?void(s[e].enabled=!s[e].enabled):void console.warn(`System '${e} is not registered.`);c=!c}}})(o,i,l,_),{save:w,load:v}=((e,t,n)=>({save:()=>{const r=n(),o=t(),i=Object.keys(e.components),s=i.map((t=>e.components[t])),a=Object.keys(e.systems),c=a.map((t=>e.systems[t])),l=Math.ceil(i.length/32),f=o*l*Uint32Array.BYTES_PER_ELEMENT,y=s.reduce(((e,t)=>e+t._flatten().reduce(((e,t)=>e+t.BYTES_PER_ELEMENT*o),0)),0),E=c.reduce(((e,t)=>e+t.localEntities.reduce(((e,t)=>e+Uint32Array.BYTES_PER_ELEMENT),0)+Uint32Array.BYTES_PER_ELEMENT),0),m=new ArrayBuffer(f+y+E),p=new DataView(m);let u=0;for(let t=0;t<o;t++)for(let n=0;n<l;n++){const r=e.entities[n];p.setUint32(u,r[t]),u+=Uint32Array.BYTES_PER_ELEMENT}return s.forEach((e=>{e._flatten().forEach((e=>{const t=e.constructor.name.split("Array")[0],n=e.BYTES_PER_ELEMENT,o=r;e._cursorStart=u;for(let r=0;r<o;r++)p["set"+t](u,e[r]),u+=n;e._cursorEnd=u}))})),a.forEach((t=>{const n=e.systems[t];p.setUint32(u,n.localEntities.length),u+=Uint32Array.BYTES_PER_ELEMENT,n.localEntities.forEach((e=>{p.setUint32(u,e),u+=Uint32Array.BYTES_PER_ELEMENT}))})),p.buffer.slice(0,u)},load:r=>{const o=n(),i=t(),s=Object.keys(e.components),a=s.map((t=>e.components[t])),c=Object.keys(e.systems),l=Math.ceil(s.length/32),f=new DataView(r);let y=0;for(let t=0;t<i;t++)for(let n=0;n<l;n++)e.entities[n][t]=f.getUint32(y),y+=Uint32Array.BYTES_PER_ELEMENT;a.forEach((e=>{e._flatten().forEach((e=>{const t=e.constructor.name.split("Array")[0],n=e.BYTES_PER_ELEMENT,r=o;for(let o=0;o<r;o++)e[o]=f["get"+t](y),y+=n}))})),c.forEach((t=>{const n=e.systems[t],r=f.getUint32(y);y+=Uint32Array.BYTES_PER_ELEMENT,n.localEntities.length=0,n.count=0,n.entityIndices.fill(-1);for(let e=0;e<r;e++){const e=f.getUint32(y);n.add(e),y+=Uint32Array.BYTES_PER_ELEMENT}}))}}))(i,s,f);let T=0;return{TYPES:e,config:o,registry:i,entityCount:s,addEntity:a,removeEntity:c,registerComponent:E,removeComponent:p,removeAllComponents:u,addComponent:m,hasComponent:d,registerSystem:h,createQuery:e=>h({name:"query-"+T++,components:e}).localEntities,enabled:g,toggle:b,step:A,save:w,load:v}}; | ||
//# sourceMappingURL=index.es.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";const e={bool:"bool",int8:"int8",uint8:"uint8",uint8clamped:"uint8clamped",int16:"int16",uint16:"uint16",int32:"int32",uint32:"uint32",float32:"float32",float64:"float64"},t={bool:"bool",int8:Int8Array,uint8:Uint8Array,uint8clamped:Uint8ClampedArray,int16:Int16Array,uint16:Uint16Array,int32:Int32Array,uint32:Uint32Array,float32:Float32Array,float64:Float64Array},n={uint8:255,uint16:65535,uint32:4294967295},r=()=>{let e=1,r=0;const o=(s,i={},a=!0)=>{const c={},l={},f={},m=Object.keys(i),p=m.filter((e=>Array.isArray(i[e])&&"object"==typeof i[e][0])),y=Object.keys(t).reduce(((e,t)=>({...e,[t]:0})),{});let u;return m.forEach((e=>{if("bool"===i[e]){const n=t.uint8,r=s*t.uint8.BYTES_PER_ELEMENT,o=new SharedArrayBuffer(r);l[e]=i[e],c[e]=new n(o),c[e]._boolType=!0}else if(Array.isArray(i[e])&&"string"==typeof i[e][0]){const n=t.uint8,r=s*t.uint8.BYTES_PER_ELEMENT,o=new SharedArrayBuffer(r);l[e]=i[e],c[e]=new n(o)}else if(Array.isArray(i[e])&&"object"==typeof i[e][0]){const{index:o,type:a,length:l}=i[e][0];if(!l)throw new Error("❌ Must define a length for component array.");if(!t[a])throw new Error(`❌ Invalid component array property type ${a}.`);if(!t[o])throw new Error(`❌ Invalid component array index type ${o}.`);if(!n[o])throw new Error(`❌ Index type for component array must be unsigned (non-negative), instead was ${o}.`);if(!f[a]){const e=p.filter((e=>i[e][0].type===a)),n=e.reduce(((e,n)=>e+t[a].BYTES_PER_ELEMENT),0),c=e.reduce(((e,t)=>e+l),0),m=new SharedArrayBuffer((r=n*c*s,4*Math.ceil(r/4))),y=new t[a](m);y._indexType=o,y._indexBytes=t[o].BYTES_PER_ELEMENT,f[a]=y}c[e]={};let m=0;for(let t=0;t<s;t++){const n=y[a]+t*l,r=n+l;c[e][t]=f[a].subarray(n,r),m=r}y[a]=m,c[e]._reset=t=>c[e][t].fill(0),c[e]._set=(t,n)=>c[e][t].set(n,0)}else if("object"==typeof i[e])c[e]=o(s,i[e],!1);else if("string"==typeof i[e]){const n=i[e],r=s*t[n].BYTES_PER_ELEMENT,o=new SharedArrayBuffer(r);c[e]=new t[n](o)}else{if("function"!=typeof i[e])throw new Error("ECS Error: invalid property type "+i[e]);{const t=i[e],n=s*t.BYTES_PER_ELEMENT,r=new SharedArrayBuffer(n);c[e]=new t(r)}}var r})),Object.defineProperty(c,"_schema",{value:i}),Object.defineProperty(c,"_props",{value:Object.keys(i)}),Object.defineProperty(c,"_mapping",{value:e=>l[e]}),Object.defineProperty(c,"_reset",{value:e=>{for(const t of c._props)ArrayBuffer.isView(c[t])?ArrayBuffer.isView(c[t][e])?c[t][e].fill(0):c[t][e]=0:c[t]._reset(e)}}),Object.defineProperty(c,"_set",{value:(e,t)=>{for(const n in t){c._mapping(n)&&"string"==typeof t[n]?c.enum(n,e,t[n]):ArrayBuffer.isView(c[n])?c[n][e]=t[n]:Array.isArray(t[n])&&ArrayBuffer.isView(c[n][e])?c[n][e].set(t[n],0):"object"==typeof c[n]&&c[n]._set(e,t[n])}}}),Object.defineProperty(c,"_get",{value:e=>{const t={};for(const n of c._props){c._mapping(n)?t[n]=c.enum(n,e):ArrayBuffer.isView(c[n])?t[n]=c[n][e]:"object"==typeof c[n]&&(ArrayBuffer.isView(c[n][e])?t[n]=Array.from(c[n][e]):t[n]=c[n]._get(e))}return t}}),Object.defineProperty(c,"_flatten",{value:(e=[])=>{if(u)return u;for(const t of c._props)ArrayBuffer.isView(c[t])?e.push(c[t]):"object"==typeof c[t]&&c[t]._flatten(e);return u=e,e}}),Object.defineProperty(c,"_bitflag",{value:e}),Object.defineProperty(c,"_generationId",{value:r}),Object.defineProperty(c,"check",{value:e=>(e&c._bitflag)===c._bitflag}),Object.defineProperty(c,"enum",{value:(e,t,n)=>{const r=c._mapping(e);if(r){if(!n)return r[c[e][t]];{const o=r.indexOf(n);if(-1===o)return void console.warn(`Value '${n}' is not part of enum.`);c[e][t]=o}}else console.warn("Property is not an enum.")}}),a&&(e*=2,e>=Math.pow(2,32)&&(e=1,r+=1)),c};return o};module.exports=(t={})=>{const n=r(),o=(({maxEntities:e=1e4,maxComponentTypes:t=128,tickRate:n=30,devtools:r=!1})=>({maxEntities:e,maxComponentTypes:t,maxGenerations:Math.ceil(t/32),tickRate:n,devtools:r}))(t),s=(({maxEntities:e,maxGenerations:t})=>({entities:Array.from({length:t},(()=>new Uint32Array(e))),components:{},systems:{}}))(o),{entityCount:i,addEntity:a,removeEntity:c,commitEntityRemovals:l,getEntityCursor:f}=((e,t)=>{const{entities:n,systems:r}=t,o=[],s=[],i=new Uint8Array(e.maxEntities);let a=0;return{entityCount:()=>a-o.length,addEntity:()=>{if(a>=e.maxEntities)return void console.warn("❌ Could not add entity, maximum number of entities reached.");const t=o.length>0?o.pop():a++;return i[t]=1,t},removeEntity:e=>{s.push(e)},commitEntityRemovals:()=>{if(0!==s.length){for(let e of s)if(0!==i[e]){for(const t in r)r[t].remove(e);o.push(e),i[e]=0;for(let t of n)t[e]=0}s.length=0}},getEntityCursor:()=>a}})(o,s),{getGeneration:m,registerComponent:p,addComponent:y,removeComponent:u,removeAllComponents:E,hasComponent:d,commitComponentRemovals:_}=((e,t,n)=>{const{entities:r,components:o,systems:s}=t,i=[];let a=0;const c=e=>{if(void 0===o[e])throw new Error(`❌ Component '${e}' is not registered.`);return o[e]},l=(e,t)=>{i.push(e,t)},f=(e,t)=>{const n=o[e],{_generationId:s,_bitflag:i}=n;return(r[s][t]&i)===i};return{getGeneration:()=>a,registerComponent:(t,r)=>{if(Object.keys(o).length+1>e.maxComponentTypes)throw new Error(`❌ Can not register component '${t}'. Max components '${e.maxComponentTypes}' reached.`);return o[t]=n(e.maxEntities,r),a=o[t]._generationId,o[t]},addComponent:(e,t,n={},o=!0)=>{if(f(e,t))return;const i=c(e),{_generationId:a,_bitflag:l}=i;r[a][t]|=l,o&&i._reset(t),i._set(t,n);for(const e in s){const n=s[e];n.components.length&&n.checkComponent(i)&&n.check(t)&&n.add(t)}},removeComponent:l,removeAllComponents:e=>{Object.keys(o).forEach((t=>{l(t,e)}))},hasComponent:f,commitComponentRemovals:()=>{if(0!==i.length){for(let e=0;e<i.length;e+=2){const t=i[e],n=i[e+1],o=c(t),{_generationId:a,_bitflag:l}=o;if(!(r[a][n]&l))return;r[a][n]&=~l;for(const e in s){const t=s[e];t.components.length&&t.checkComponent(o)&&!t.check(n)&&t.remove(n)}}i.length=0}}}})(o,s,n),{enabled:g,registerSystem:h,toggle:b,step:w}=((e,t,n,r)=>{const{entities:o,components:s,systems:i}=t,a=[];let c=!0;const l=()=>{r(),n()};return{registerSystem:({name:t,components:n=[],enter:r,update:c,exit:f})=>{const m=[],p=new Int32Array(e.maxEntities).fill(-1),y={count:0,name:t,enabled:!0,components:n,localEntities:m,update:c,enter:r,exit:f},u=n.map((e=>{if(void 0===s[e])throw new Error(`❌ Cannot register system '${t}', '${e}' is not a registered component.`);return s[e]})),E=Array.from({length:e.maxGenerations},(()=>0));for(const e of u){const{_generationId:t,_bitflag:n}=e;E[t]|=n}y.masks=E,y.check=e=>{for(let t=0;t<E.length;t++){const n=o[t][e],r=E[t];if((n&r)!==r)return!1}return!0},y.checkComponent=e=>(E[e._generationId]&e._bitflag)===e._bitflag;const d=c?c(...u):null,_=r?r(...u):null,g=f?f(...u):null;y.execute=e=>{(e||y.enabled)&&d&&d(m),l()},y.add=e=>{-1===p[e]&&(p[e]=m.push(e)-1,y.count=m.length,_&&_(e))},y.remove=e=>{const t=p[e];if(-1===t)return;const n=m.pop();n!==e&&(m[t]=n,p[n]=t),p[e]=-1,y.count=m.length,g&&g(e)};for(let e=0;e<o[0].length;e++)y.components.length&&y.check(e)&&y.add(e);return i[t]=y,t.includes("query")||a.push(y),y},enabled:e=>e?i[e]?i[e].enabled:void console.warn(`System '${e}' is not registered.`):c,step:(e,t=!1)=>{if("boolean"==typeof e&&(t=e),"string"!=typeof e){if(t||c)for(let e=0;e<a.length;e++)a[e].execute();l()}else i[e].execute(t)},toggle:e=>{if(e)return i[e]?void(i[e].enabled=!i[e].enabled):void console.warn(`System '${e} is not registered.`);c=!c}}})(o,s,l,_),{save:A,load:v}=((e,t,n)=>({save:()=>{const r=n(),o=t(),s=Object.keys(e.components),i=s.map((t=>e.components[t])),a=Object.keys(e.systems),c=a.map((t=>e.systems[t])),l=Math.ceil(s.length/32),f=o*l*Uint32Array.BYTES_PER_ELEMENT,m=i.reduce(((e,t)=>e+t._flatten().reduce(((e,t)=>e+t.BYTES_PER_ELEMENT*o),0)),0),p=c.reduce(((e,t)=>e+t.localEntities.reduce(((e,t)=>e+Uint32Array.BYTES_PER_ELEMENT),0)),0),y=new ArrayBuffer(f+m+p),u=new DataView(y);let E=0;for(let t=0;t<o;t++)for(let n=0;n<l;n++){const r=e.entities[n];u.setUint32(E,r[t]),E+=Uint32Array.BYTES_PER_ELEMENT}const d={_cursorStart:0,_cursorEnd:E};i.forEach((e=>{e._flatten().forEach((e=>{const t=e.constructor.name.split("Array")[0],n=e.BYTES_PER_ELEMENT,o=r;e._cursorStart=E;for(let r=0;r<o;r++)u["set"+t](E,e[r]),E+=n;e._cursorEnd=E}))}));const _={};return a.forEach((t=>{const n=e.systems[t],r=E;n.localEntities.forEach((e=>{u.setUint32(E,e),E+=Uint32Array.BYTES_PER_ELEMENT}));const o=E;_[t]={_cursorStart:r,_cursorEnd:o}})),{bin:u.buffer.slice(0,E),schema:JSON.stringify({entities:d,components:e.components,systems:_},((e,t)=>t.hasOwnProperty("_cursorStart")?[t._cursorStart,t._cursorEnd]:t))}},load:(r,o)=>{"string"==typeof r&&(r=JSON.parse(r));const s=n(),i=t(),a=Object.keys(e.components),c=a.map((t=>e.components[t])),l=Object.keys(e.systems),f=Math.ceil(a.length/32),m=new DataView(o);let p=0;for(let t=0;t<i;t++)for(let n=0;n<f;n++)e.entities[n][t]=m.getUint32(p),p+=Uint32Array.BYTES_PER_ELEMENT;c.forEach((e=>{e._flatten().forEach((e=>{const t=e.constructor.name.split("Array")[0],n=e.BYTES_PER_ELEMENT,r=s;for(let o=0;o<r;o++)e[o]=m["get"+t](p),p+=n}))})),l.forEach((t=>{const n=e.systems[t],[o,s]=r.systems[t],i=[];for(;p<s;)i.push(m.getUint32(p)),p+=Uint32Array.BYTES_PER_ELEMENT;n.localEntities=i}))}}))(s,i,f);let T=0;return{TYPES:e,config:o,registry:s,entityCount:i,addEntity:a,removeEntity:c,registerComponent:p,removeComponent:u,removeAllComponents:E,addComponent:y,hasComponent:d,registerSystem:h,createQuery:e=>h({name:"query-"+T++,components:e}).localEntities,enabled:g,toggle:b,step:w,save:A,load:v}}; | ||
"use strict";const e={bool:"bool",int8:"int8",uint8:"uint8",uint8clamped:"uint8clamped",int16:"int16",uint16:"uint16",int32:"int32",uint32:"uint32",float32:"float32",float64:"float64"},t={bool:"bool",int8:Int8Array,uint8:Uint8Array,uint8clamped:Uint8ClampedArray,int16:Int16Array,uint16:Uint16Array,int32:Int32Array,uint32:Uint32Array,float32:Float32Array,float64:Float64Array},n={uint8:255,uint16:65535,uint32:4294967295},r=()=>{let e=1,r=0;const o=(i,s={},a=!0)=>{const c={},l={},f={},y=Object.keys(s),E=y.filter((e=>Array.isArray(s[e])&&"object"==typeof s[e][0])),m=Object.keys(t).reduce(((e,t)=>({...e,[t]:0})),{});let p;return y.forEach((e=>{if("bool"===s[e]){const n=t.uint8,r=i*t.uint8.BYTES_PER_ELEMENT,o=new SharedArrayBuffer(r);l[e]=s[e],c[e]=new n(o),c[e]._boolType=!0}else if(Array.isArray(s[e])&&"string"==typeof s[e][0]){const n=t.uint8,r=i*t.uint8.BYTES_PER_ELEMENT,o=new SharedArrayBuffer(r);l[e]=s[e],c[e]=new n(o)}else if(Array.isArray(s[e])&&"object"==typeof s[e][0]){const{index:o,type:a,length:l}=s[e][0];if(!l)throw new Error("❌ Must define a length for component array.");if(!t[a])throw new Error(`❌ Invalid component array property type ${a}.`);if(!t[o])throw new Error(`❌ Invalid component array index type ${o}.`);if(!n[o])throw new Error(`❌ Index type for component array must be unsigned (non-negative), instead was ${o}.`);if(!f[a]){const e=E.filter((e=>s[e][0].type===a)),n=e.reduce(((e,n)=>e+t[a].BYTES_PER_ELEMENT),0),c=e.reduce(((e,t)=>e+l),0),y=new SharedArrayBuffer((r=n*c*i,4*Math.ceil(r/4))),m=new t[a](y);m._indexType=o,m._indexBytes=t[o].BYTES_PER_ELEMENT,f[a]=m}c[e]={};let y=0;for(let t=0;t<i;t++){const n=m[a]+t*l,r=n+l;c[e][t]=f[a].subarray(n,r),y=r}m[a]=y,c[e]._reset=t=>c[e][t].fill(0),c[e]._set=(t,n)=>c[e][t].set(n,0)}else if("object"==typeof s[e])c[e]=o(i,s[e],!1);else if("string"==typeof s[e]){const n=s[e],r=i*t[n].BYTES_PER_ELEMENT,o=new SharedArrayBuffer(r);c[e]=new t[n](o)}else{if("function"!=typeof s[e])throw new Error("ECS Error: invalid property type "+s[e]);{const t=s[e],n=i*t.BYTES_PER_ELEMENT,r=new SharedArrayBuffer(n);c[e]=new t(r)}}var r})),Object.defineProperty(c,"_schema",{value:s}),Object.defineProperty(c,"_props",{value:Object.keys(s)}),Object.defineProperty(c,"_mapping",{value:e=>l[e]}),Object.defineProperty(c,"_reset",{value:e=>{for(const t of c._props)ArrayBuffer.isView(c[t])?ArrayBuffer.isView(c[t][e])?c[t][e].fill(0):c[t][e]=0:c[t]._reset(e)}}),Object.defineProperty(c,"_set",{value:(e,t)=>{for(const n in t){c._mapping(n)&&"string"==typeof t[n]?c.enum(n,e,t[n]):ArrayBuffer.isView(c[n])?c[n][e]=t[n]:Array.isArray(t[n])&&ArrayBuffer.isView(c[n][e])?c[n][e].set(t[n],0):"object"==typeof c[n]&&c[n]._set(e,t[n])}}}),Object.defineProperty(c,"_get",{value:e=>{const t={};for(const n of c._props){c._mapping(n)?t[n]=c.enum(n,e):ArrayBuffer.isView(c[n])?t[n]=c[n][e]:"object"==typeof c[n]&&(ArrayBuffer.isView(c[n][e])?t[n]=Array.from(c[n][e]):t[n]=c[n]._get(e))}return t}}),Object.defineProperty(c,"_flatten",{value:(e=[])=>{if(p)return p;for(const t of c._props)ArrayBuffer.isView(c[t])?e.push(c[t]):"object"==typeof c[t]&&c[t]._flatten(e);return p=e,e}}),Object.defineProperty(c,"_bitflag",{value:e}),Object.defineProperty(c,"_generationId",{value:r}),Object.defineProperty(c,"check",{value:e=>(e&c._bitflag)===c._bitflag}),Object.defineProperty(c,"enum",{value:(e,t,n)=>{const r=c._mapping(e);if(r){if(!n)return r[c[e][t]];{const o=r.indexOf(n);if(-1===o)return void console.warn(`Value '${n}' is not part of enum.`);c[e][t]=o}}else console.warn("Property is not an enum.")}}),a&&(e*=2,e>=Math.pow(2,32)&&(e=1,r+=1)),c};return o};module.exports=(t={})=>{const n=r(),o=(({maxEntities:e=1e4,maxComponentTypes:t=128,tickRate:n=30,devtools:r=!1})=>({maxEntities:e,maxComponentTypes:t,maxGenerations:Math.ceil(t/32),tickRate:n,devtools:r}))(t),i=(({maxEntities:e,maxGenerations:t})=>({entities:Array.from({length:t},(()=>new Uint32Array(e))),components:{},systems:{}}))(o),{entityCount:s,addEntity:a,removeEntity:c,commitEntityRemovals:l,getEntityCursor:f}=((e,t)=>{const{entities:n,systems:r}=t,o=[],i=[],s=new Uint8Array(e.maxEntities);let a=0;return{entityCount:()=>a-o.length,addEntity:()=>{if(a>=e.maxEntities)return void console.warn("❌ Could not add entity, maximum number of entities reached.");const t=o.length>0?o.pop():a++;return s[t]=1,t},removeEntity:e=>{i.push(e)},commitEntityRemovals:()=>{if(0!==i.length){for(let e of i)if(0!==s[e]){for(const t in r)r[t].remove(e);o.push(e),s[e]=0;for(let t of n)t[e]=0}i.length=0}},getEntityCursor:()=>a}})(o,i),{getGeneration:y,registerComponent:E,addComponent:m,removeComponent:p,removeAllComponents:u,hasComponent:d,commitComponentRemovals:_}=((e,t,n)=>{const{entities:r,components:o,systems:i}=t,s=[];let a=0;const c=e=>{if(void 0===o[e])throw new Error(`❌ Component '${e}' is not registered.`);return o[e]},l=(e,t)=>{s.push(e,t)},f=(e,t)=>{const n=o[e],{_generationId:i,_bitflag:s}=n;return(r[i][t]&s)===s};return{getGeneration:()=>a,registerComponent:(t,r)=>{if(Object.keys(o).length+1>e.maxComponentTypes)throw new Error(`❌ Can not register component '${t}'. Max components '${e.maxComponentTypes}' reached.`);return o[t]=n(e.maxEntities,r),a=o[t]._generationId,o[t]},addComponent:(e,t,n={},o=!0)=>{if(f(e,t))return;const s=c(e),{_generationId:a,_bitflag:l}=s;r[a][t]|=l,o&&s._reset(t),s._set(t,n);for(const e in i){const n=i[e];n.components.length&&n.checkComponent(s)&&n.check(t)&&n.add(t)}},removeComponent:l,removeAllComponents:e=>{Object.keys(o).forEach((t=>{l(t,e)}))},hasComponent:f,commitComponentRemovals:()=>{if(0!==s.length){for(let e=0;e<s.length;e+=2){const t=s[e],n=s[e+1],o=c(t),{_generationId:a,_bitflag:l}=o;if(!(r[a][n]&l))return;r[a][n]&=~l;for(const e in i){const t=i[e];t.components.length&&t.checkComponent(o)&&!t.check(n)&&t.remove(n)}}s.length=0}}}})(o,i,n),{enabled:g,registerSystem:h,toggle:b,step:A}=((e,t,n,r)=>{const{entities:o,components:i,systems:s}=t,a=[];let c=!0;const l=()=>{r(),n()};return{registerSystem:({name:t,components:n=[],enter:r,update:c,exit:f})=>{const y=[],E=new Int32Array(e.maxEntities).fill(-1),m={count:0,name:t,enabled:!0,components:n,localEntities:y,entityIndices:E,update:c,enter:r,exit:f},p=n.map((e=>{if(void 0===i[e])throw new Error(`❌ Cannot register system '${t}', '${e}' is not a registered component.`);return i[e]})),u=Array.from({length:e.maxGenerations},(()=>0));for(const e of p){const{_generationId:t,_bitflag:n}=e;u[t]|=n}m.masks=u,m.check=e=>{for(let t=0;t<u.length;t++){const n=o[t][e],r=u[t];if((n&r)!==r)return!1}return!0},m.checkComponent=e=>(u[e._generationId]&e._bitflag)===e._bitflag;const d=c?c(...p):null,_=r?r(...p):null,g=f?f(...p):null;m.execute=e=>{(e||m.enabled)&&d&&d(y),l()},m.add=e=>{-1===E[e]&&(E[e]=y.push(e)-1,m.count=y.length,_&&_(e))},m.remove=e=>{const t=E[e];if(-1===t)return;const n=y.pop();n!==e&&(y[t]=n,E[n]=t),E[e]=-1,m.count=y.length,g&&g(e)};for(let e=0;e<o[0].length;e++)m.components.length&&m.check(e)&&m.add(e);return s[t]=m,t.includes("query")||a.push(m),m},enabled:e=>e?s[e]?s[e].enabled:void console.warn(`System '${e}' is not registered.`):c,step:(e,t=!1)=>{if("boolean"==typeof e&&(t=e),"string"!=typeof e){if(t||c)for(let e=0;e<a.length;e++)a[e].execute();l()}else s[e].execute(t)},toggle:e=>{if(e)return s[e]?void(s[e].enabled=!s[e].enabled):void console.warn(`System '${e} is not registered.`);c=!c}}})(o,i,l,_),{save:w,load:v}=((e,t,n)=>({save:()=>{const r=n(),o=t(),i=Object.keys(e.components),s=i.map((t=>e.components[t])),a=Object.keys(e.systems),c=a.map((t=>e.systems[t])),l=Math.ceil(i.length/32),f=o*l*Uint32Array.BYTES_PER_ELEMENT,y=s.reduce(((e,t)=>e+t._flatten().reduce(((e,t)=>e+t.BYTES_PER_ELEMENT*o),0)),0),E=c.reduce(((e,t)=>e+t.localEntities.reduce(((e,t)=>e+Uint32Array.BYTES_PER_ELEMENT),0)+Uint32Array.BYTES_PER_ELEMENT),0),m=new ArrayBuffer(f+y+E),p=new DataView(m);let u=0;for(let t=0;t<o;t++)for(let n=0;n<l;n++){const r=e.entities[n];p.setUint32(u,r[t]),u+=Uint32Array.BYTES_PER_ELEMENT}return s.forEach((e=>{e._flatten().forEach((e=>{const t=e.constructor.name.split("Array")[0],n=e.BYTES_PER_ELEMENT,o=r;e._cursorStart=u;for(let r=0;r<o;r++)p["set"+t](u,e[r]),u+=n;e._cursorEnd=u}))})),a.forEach((t=>{const n=e.systems[t];p.setUint32(u,n.localEntities.length),u+=Uint32Array.BYTES_PER_ELEMENT,n.localEntities.forEach((e=>{p.setUint32(u,e),u+=Uint32Array.BYTES_PER_ELEMENT}))})),p.buffer.slice(0,u)},load:r=>{const o=n(),i=t(),s=Object.keys(e.components),a=s.map((t=>e.components[t])),c=Object.keys(e.systems),l=Math.ceil(s.length/32),f=new DataView(r);let y=0;for(let t=0;t<i;t++)for(let n=0;n<l;n++)e.entities[n][t]=f.getUint32(y),y+=Uint32Array.BYTES_PER_ELEMENT;a.forEach((e=>{e._flatten().forEach((e=>{const t=e.constructor.name.split("Array")[0],n=e.BYTES_PER_ELEMENT,r=o;for(let o=0;o<r;o++)e[o]=f["get"+t](y),y+=n}))})),c.forEach((t=>{const n=e.systems[t],r=f.getUint32(y);y+=Uint32Array.BYTES_PER_ELEMENT,n.localEntities.length=0,n.count=0,n.entityIndices.fill(-1);for(let e=0;e<r;e++){const e=f.getUint32(y);n.add(e),y+=Uint32Array.BYTES_PER_ELEMENT}}))}}))(i,s,f);let T=0;return{TYPES:e,config:o,registry:i,entityCount:s,addEntity:a,removeEntity:c,registerComponent:E,removeComponent:p,removeAllComponents:u,addComponent:m,hasComponent:d,registerSystem:h,createQuery:e=>h({name:"query-"+T++,components:e}).localEntities,enabled:g,toggle:b,step:A,save:w,load:v}}; | ||
//# sourceMappingURL=index.min.js.map |
22
DOCS.md
@@ -31,2 +31,4 @@ | ||
* [.removeEntity(eid)](#module_World.removeEntity) | ||
* [.save()](#module_World.save) ⇒ <code>ArrayBuffer</code> | ||
* [.load(bin)](#module_World.load) | ||
* [.enabled(name)](#module_World.enabled) ⇒ <code>boolean</code> | ||
@@ -320,2 +322,22 @@ * [.registerSystem(system)](#module_World.registerSystem) | ||
<br><a name="module_World.save"></a> | ||
### save | ||
#### World.save() ⇒ <code>ArrayBuffer</code> | ||
> Saves a snapshot of the ECS in its current state. | ||
**Returns**: <code>ArrayBuffer</code> - Snapshot of the ECS state. | ||
<br><a name="module_World.load"></a> | ||
### load | ||
#### World.load(bin) | ||
> Loads a snapshot that was saved with the same ECS setup. | ||
| Param | Type | | ||
| --- | --- | | ||
| bin | <code>ArrayBuffer</code> | | ||
<br><a name="module_World.enabled"></a> | ||
@@ -322,0 +344,0 @@ |
{ | ||
"name": "bitecs", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"description": "Tiny, data-driven, high performance ECS library written in Javascript", | ||
@@ -5,0 +5,0 @@ "license": "MPL-2.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
167808