Comparing version 0.2.5 to 0.2.6
@@ -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=(i,s={},a=!0)=>{const l={},f={},c={},p=Object.keys(s),m=p.filter((e=>Array.isArray(s[e])&&"object"==typeof s[e][0])),y=Object.keys(t).reduce(((e,t)=>({...e,[t]:0})),{});let u;return p.forEach((e=>{if("bool"===s[e]){const n=t.uint8,r=i*t.uint8.BYTES_PER_ELEMENT,o=new SharedArrayBuffer(r);f[e]=s[e],l[e]=new n(o),l[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);f[e]=s[e],l[e]=new n(o)}else if(Array.isArray(s[e])&&"object"==typeof s[e][0]){const{index:o,type:a,length:f}=s[e][0];if(!f)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(!c[a]){const e=m.filter((e=>s[e][0].type===a)),n=e.reduce(((e,n)=>e+t[a].BYTES_PER_ELEMENT),0),l=e.reduce(((e,t)=>e+f),0),p=new SharedArrayBuffer((r=n*l*i,4*Math.ceil(r/4))),y=new t[a](p);y._indexType=o,y._indexBytes=t[o].BYTES_PER_ELEMENT,c[a]=y}l[e]={};let p=0;for(let t=0;t<i;t++){const n=y[a]+t*f,r=n+f;l[e][t]=c[a].subarray(n,r),p=r}y[a]=p,l[e]._reset=t=>l[e][t].fill(0),l[e]._set=(t,n)=>l[e][t].set(n,0)}else if("object"==typeof s[e])l[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);l[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);l[e]=new t(r)}}var r})),Object.defineProperty(l,"_schema",{value:s}),Object.defineProperty(l,"_props",{value:Object.keys(s)}),Object.defineProperty(l,"_mapping",{value:e=>f[e]}),Object.defineProperty(l,"_reset",{value:e=>{for(const t of l._props)ArrayBuffer.isView(l[t])?ArrayBuffer.isView(l[t][e])?l[t][e].fill(0):l[t][e]=0:l[t]._reset(e)}}),Object.defineProperty(l,"_set",{value:(e,t)=>{for(const n in t){l._mapping(n)&&"string"==typeof t[n]?l.enum(n,e,t[n]):ArrayBuffer.isView(l[n])?l[n][e]=t[n]:Array.isArray(t[n])&&ArrayBuffer.isView(l[n][e])?l[n][e].set(t[n],0):"object"==typeof l[n]&&l[n]._set(e,t[n])}}}),Object.defineProperty(l,"_get",{value:e=>{const t={};for(const n of l._props){l._mapping(n)?t[n]=l.enum(n,e):ArrayBuffer.isView(l[n])?t[n]=l[n][e]:"object"==typeof l[n]&&(ArrayBuffer.isView(l[n][e])?t[n]=Array.from(l[n][e]):t[n]=l[n]._get(e))}return t}}),Object.defineProperty(l,"_flatten",{value:(e=[])=>{if(u)return u;for(const t of l._props)ArrayBuffer.isView(l[t])?e.push(l[t]):"object"==typeof l[t]&&l[t]._flatten(e);return u=e,e}}),Object.defineProperty(l,"_bitflag",{value:e}),Object.defineProperty(l,"_generationId",{value:r}),Object.defineProperty(l,"check",{value:e=>(e&l._bitflag)===l._bitflag}),Object.defineProperty(l,"enum",{value:(e,t,n)=>{const r=l._mapping(e);if(r){if(!n)return r[l[e][t]];{const o=r.indexOf(n);if(-1===o)return void console.warn(`Value '${n}' is not part of enum.`);l[e][t]=o}}else console.warn("Property is not an enum.")}}),a&&(e*=2,e>=Math.pow(2,32)&&(e=1,r+=1)),l};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:l,commitEntityRemovals: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}}}})(o,i),{registerComponent:c,addComponent:p,removeComponent:m,removeAllComponents:y,hasComponent:u,commitComponentRemovals:d}=((e,t,n)=>{const{entities:r,components:o,systems:i}=t,s=[],a=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{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),o[t].name=t,o[t]},addComponent:(e,t,n={},o=!0)=>{if(f(e,t))return;const s=a(e),{_generationId:l,_bitflag:c}=s;r[l][t]|=c,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=a(t),{_generationId:l,_bitflag:f}=o;if(!(r[l][n]&f))return;r[l][n]&=~f;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:E,toggle:h,step:_}=((e,t,n,r)=>{const{entities:o,components:i,systems:s}=t,a=[];let l=!0;const f=()=>{r(),n()};return{registerSystem:({name:t,components:n=[],enter:r,update:l,exit:c})=>{const p=[],m=new Int32Array(e.maxEntities).fill(-1),y={count:0,name:t,enabled:!0,components:n,localEntities:p,update:l,enter:r,exit:c},u=n.map((e=>{if(void 0===i[e])throw new Error(`❌ Cannot register system '${t}', '${e}' is not a registered component.`);return i[e]})),d=Array.from({length:e.maxGenerations},(()=>0));for(const e of u){const{_generationId:t,_bitflag:n}=e;d[t]|=n}y.masks=d,y.check=e=>{for(let t=0;t<d.length;t++){const n=o[t][e],r=d[t];if((n&r)!==r)return!1}return!0},y.checkComponent=e=>(d[e._generationId]&e._bitflag)===e._bitflag;const g=l?l(...u):null,E=r?r(...u):null,h=c?c(...u):null;y.execute=e=>{(e||y.enabled)&&g&&g(p),f()},y.add=e=>{-1===m[e]&&(m[e]=p.push(e)-1,y.count=p.length,E&&E(e))},y.remove=e=>{const t=m[e];if(-1===t)return;const n=p.pop();n!==e&&(p[t]=n,m[n]=t),m[e]=-1,y.count=p.length,h&&h(e)};for(let e=0;e<o[0].length;e++)y.components.length&&y.check(e)&&y.add(e);return s[t]=y,t.includes("query")||a.push(y),y},enabled:e=>e?s[e]?s[e].enabled:void console.warn(`System '${e}' is not registered.`):l,step:(e,t=!1)=>{if("boolean"==typeof e&&(t=e),"string"!=typeof e){if(t||l)for(let e=0;e<a.length;e++)a[e].execute();f()}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.`);l=!l}}})(o,i,f,d);let b=0;return{TYPES:e,config:o,registry:i,entityCount:s,addEntity:a,removeEntity:l,registerComponent:c,removeComponent:m,removeAllComponents:y,addComponent:p,hasComponent:u,registerSystem:E,createQuery:e=>E({name:"query-"+b++,components:e}).localEntities,enabled:g,toggle:h,step:_}}; | ||
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}}; | ||
//# 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=(i,s={},a=!0)=>{const l={},f={},c={},p=Object.keys(s),m=p.filter((e=>Array.isArray(s[e])&&"object"==typeof s[e][0])),y=Object.keys(t).reduce(((e,t)=>({...e,[t]:0})),{});let u;return p.forEach((e=>{if("bool"===s[e]){const n=t.uint8,r=i*t.uint8.BYTES_PER_ELEMENT,o=new SharedArrayBuffer(r);f[e]=s[e],l[e]=new n(o),l[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);f[e]=s[e],l[e]=new n(o)}else if(Array.isArray(s[e])&&"object"==typeof s[e][0]){const{index:o,type:a,length:f}=s[e][0];if(!f)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(!c[a]){const e=m.filter((e=>s[e][0].type===a)),n=e.reduce(((e,n)=>e+t[a].BYTES_PER_ELEMENT),0),l=e.reduce(((e,t)=>e+f),0),p=new SharedArrayBuffer((r=n*l*i,4*Math.ceil(r/4))),y=new t[a](p);y._indexType=o,y._indexBytes=t[o].BYTES_PER_ELEMENT,c[a]=y}l[e]={};let p=0;for(let t=0;t<i;t++){const n=y[a]+t*f,r=n+f;l[e][t]=c[a].subarray(n,r),p=r}y[a]=p,l[e]._reset=t=>l[e][t].fill(0),l[e]._set=(t,n)=>l[e][t].set(n,0)}else if("object"==typeof s[e])l[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);l[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);l[e]=new t(r)}}var r})),Object.defineProperty(l,"_schema",{value:s}),Object.defineProperty(l,"_props",{value:Object.keys(s)}),Object.defineProperty(l,"_mapping",{value:e=>f[e]}),Object.defineProperty(l,"_reset",{value:e=>{for(const t of l._props)ArrayBuffer.isView(l[t])?ArrayBuffer.isView(l[t][e])?l[t][e].fill(0):l[t][e]=0:l[t]._reset(e)}}),Object.defineProperty(l,"_set",{value:(e,t)=>{for(const n in t){l._mapping(n)&&"string"==typeof t[n]?l.enum(n,e,t[n]):ArrayBuffer.isView(l[n])?l[n][e]=t[n]:Array.isArray(t[n])&&ArrayBuffer.isView(l[n][e])?l[n][e].set(t[n],0):"object"==typeof l[n]&&l[n]._set(e,t[n])}}}),Object.defineProperty(l,"_get",{value:e=>{const t={};for(const n of l._props){l._mapping(n)?t[n]=l.enum(n,e):ArrayBuffer.isView(l[n])?t[n]=l[n][e]:"object"==typeof l[n]&&(ArrayBuffer.isView(l[n][e])?t[n]=Array.from(l[n][e]):t[n]=l[n]._get(e))}return t}}),Object.defineProperty(l,"_flatten",{value:(e=[])=>{if(u)return u;for(const t of l._props)ArrayBuffer.isView(l[t])?e.push(l[t]):"object"==typeof l[t]&&l[t]._flatten(e);return u=e,e}}),Object.defineProperty(l,"_bitflag",{value:e}),Object.defineProperty(l,"_generationId",{value:r}),Object.defineProperty(l,"check",{value:e=>(e&l._bitflag)===l._bitflag}),Object.defineProperty(l,"enum",{value:(e,t,n)=>{const r=l._mapping(e);if(r){if(!n)return r[l[e][t]];{const o=r.indexOf(n);if(-1===o)return void console.warn(`Value '${n}' is not part of enum.`);l[e][t]=o}}else console.warn("Property is not an enum.")}}),a&&(e*=2,e>=Math.pow(2,32)&&(e=1,r+=1)),l};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:l,commitEntityRemovals: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}}}})(o,i),{registerComponent:c,addComponent:p,removeComponent:m,removeAllComponents:y,hasComponent:u,commitComponentRemovals:d}=((e,t,n)=>{const{entities:r,components:o,systems:i}=t,s=[],a=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{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),o[t].name=t,o[t]},addComponent:(e,t,n={},o=!0)=>{if(f(e,t))return;const s=a(e),{_generationId:l,_bitflag:c}=s;r[l][t]|=c,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=a(t),{_generationId:l,_bitflag:f}=o;if(!(r[l][n]&f))return;r[l][n]&=~f;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:E,toggle:h,step:_}=((e,t,n,r)=>{const{entities:o,components:i,systems:s}=t,a=[];let l=!0;const f=()=>{r(),n()};return{registerSystem:({name:t,components:n=[],enter:r,update:l,exit:c})=>{const p=[],m=new Int32Array(e.maxEntities).fill(-1),y={count:0,name:t,enabled:!0,components:n,localEntities:p,update:l,enter:r,exit:c},u=n.map((e=>{if(void 0===i[e])throw new Error(`❌ Cannot register system '${t}', '${e}' is not a registered component.`);return i[e]})),d=Array.from({length:e.maxGenerations},(()=>0));for(const e of u){const{_generationId:t,_bitflag:n}=e;d[t]|=n}y.masks=d,y.check=e=>{for(let t=0;t<d.length;t++){const n=o[t][e],r=d[t];if((n&r)!==r)return!1}return!0},y.checkComponent=e=>(d[e._generationId]&e._bitflag)===e._bitflag;const g=l?l(...u):null,E=r?r(...u):null,h=c?c(...u):null;y.execute=e=>{(e||y.enabled)&&g&&g(p),f()},y.add=e=>{-1===m[e]&&(m[e]=p.push(e)-1,y.count=p.length,E&&E(e))},y.remove=e=>{const t=m[e];if(-1===t)return;const n=p.pop();n!==e&&(p[t]=n,m[n]=t),m[e]=-1,y.count=p.length,h&&h(e)};for(let e=0;e<o[0].length;e++)y.components.length&&y.check(e)&&y.add(e);return s[t]=y,t.includes("query")||a.push(y),y},enabled:e=>e?s[e]?s[e].enabled:void console.warn(`System '${e}' is not registered.`):l,step:(e,t=!1)=>{if("boolean"==typeof e&&(t=e),"string"!=typeof e){if(t||l)for(let e=0;e<a.length;e++)a[e].execute();f()}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.`);l=!l}}})(o,i,f,d);let b=0;return{TYPES:e,config:o,registry:i,entityCount:s,addEntity:a,removeEntity:l,registerComponent:c,removeComponent:m,removeAllComponents:y,addComponent:p,hasComponent:u,registerSystem:E,createQuery:e=>E({name:"query-"+b++,components:e}).localEntities,enabled:g,toggle:h,step:_}}; | ||
"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}}; | ||
//# sourceMappingURL=index.min.js.map |
{ | ||
"name": "bitecs", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"description": "Tiny, data-driven, high performance ECS library written in Javascript", | ||
@@ -5,0 +5,0 @@ "license": "MPL-2.0", |
@@ -41,2 +41,5 @@ # 👾 bitECS 👾 | ||
// Create a query | ||
const positions = world.createQuery(['POSITION']) // array will continuously update, no need to recreate | ||
// Create an entity | ||
@@ -43,0 +46,0 @@ const eid = world.addEntity() |
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
168242
74
65