@arancini/core
Advanced tools
Comparing version 6.1.2 to 6.1.3
@@ -1,2 +0,2 @@ | ||
class t{listeners=new Set;add(t){return this.listeners.add(t),()=>this.remove(t)}remove(t){this.listeners.delete(t)}emit(...t){for(const e of this.listeners)e(...t)}clear(){this.listeners.clear()}}class e{entities=[];version=0;onEntityAdded=new t;onEntityRemoved=new t;_entityPositions=new Map;_entitySet=new Set;get first(){return this.entities[0]||void 0}[Symbol.iterator](){let t=this.entities.length;const e={value:void 0,done:!1};return{next:()=>(e.value=this.entities[--t],e.done=t<0,e)}}has(t){return this._entitySet.has(t)}}const s=(t,e)=>{e&&!t.has(e)&&(t.entities.push(e),t._entityPositions.set(e,t.entities.length-1),t._entitySet.add(e),t.version++,t.onEntityAdded.emit(e))},i=(t,e)=>{if(!t.has(e))return;const s=t._entityPositions.get(e);t._entityPositions.delete(e),t._entitySet.delete(e);const i=t.entities[t.entities.length-1];i!==e&&(t.entities[s]=i,t._entityPositions.set(i,s)),t.entities.pop(),t.version++,t.onEntityRemoved.emit(e)};class n{words;constructor(t=[]){this.words=new Uint32Array(1);for(const e of t)this.add(e)}add(...t){const e=Math.max(...t);this.words.length<<5<=e&&this.resize(e,!1);for(let e=0;e<t.length;e++){const s=t[e];this.words[s>>>5]|=1<<s}}remove(...t){for(let e=0;e<t.length;e++){const s=t[e];this.words[s>>>5]&=~(1<<s)}}has(t){return 0!=(this.words[t>>>5]&1<<t)}resize(t,e=!0){if(e&&this.words.length<<5>t)return;const s=new Uint32Array(t+32>>>5<<1);s.set(this.words),this.words=s}reset(){for(let t=0;t<this.words.length;t++)this.words[t]=0}copy(t){const e=new Uint32Array(t.words.length);e.set(t.words),this.words=e}clone(){const t=new Uint32Array(this.words.length);t.set(this.words);const e=new n;return e.words=t,e}containsAll(t){for(let e=0;e<this.words.length;e++)if(0!=(~this.words[e]&t.words[e]))return!1;return!0}containsAny(t){for(let e=0;e<this.words.length;e++)if(0!=(this.words[e]&t.words[e]))return!0;return!1}}const o=Symbol("__arancini");class r extends e{world;key;conditions;bitSets;constructor(t,e,s,i){super(),this.world=t,this.key=e,this.conditions=s,this.bitSets=i}destroy(){this.world.destroyQuery(this)}}const h=(t,e)=>{const s=[];for(const i of e)a(t,i)&&s.push(i);return s},a=(t,e)=>{const{bitset:s}=e[o];for(const e of t){if("all"===e.type&&!s.containsAll(e.bitset))return!1;if("any"===e.type&&!s.containsAny(e.bitset))return!1;if("not"===e.type&&s.containsAny(e.bitset))return!1}return!0},c=t=>{const e=new u;t(e);const s=e.conditions;if(s.length<=0)throw new Error("Query must have at least one condition");if(s.some((t=>t.components.length<=0)))throw new Error("Query conditions must have at least one component");const i={type:"all",components:[]},n=[];for(const t of s)"all"===t.type?i.components.push(...t.components):n.push(t);return[i,...n]},l=(t,e)=>e.map((({type:e,components:s})=>"all"===e?s.map((e=>t[e])).sort().join(","):[`${e}:${s.map((e=>t[e])).sort().join(",")}`])).sort().join("&"),d=(t,e)=>e.map((e=>({type:e.type,bitset:new n(e.components.map((e=>t[e])))})));class u{T;conditions=[];all=(...t)=>(this.conditions.push({type:"all",components:t}),this);any=(...t)=>(this.conditions.push({type:"any",components:t}),this);not=(...t)=>(this.conditions.push({type:"not",components:t}),this);with=this.all;have=this.all;has=this.all;every=this.all;is=this.all;some=this.any;one=this.any;none=this.not;without=this.not;get and(){return this}get but(){return this}get where(){return this}get are(){return this}}class y{availableObjects=[];factory;get available(){return this.availableObjects.length}get used(){return this.size-this.availableObjects.length}size=0;constructor(t,e){this.factory=t,void 0!==e&&this.grow(e)}grow(t){for(let e=0;e<t;e++)this.availableObjects.push(this.factory());this.size+=t}free(t){for(let e=0;e<t&&this.availableObjects.pop();e++)this.size--}request(){return this.availableObjects.length<=0&&this.grow(Math.round(.2*this.size)+1),this.availableObjects.pop()}recycle(t){this.availableObjects.push(t),this.availableObjects.length>Math.round(.2*this.size)&&this.free(this.availableObjects.length-Math.round(.2*this.size))}}const p="standalone";class f extends e{queries=new Map;queryUsages=new Map;componentIndexCounter=-1;componentRegistry={};idToEntity=new Map;entityIdCounter=0;bulkUpdateInProgress=!1;bulkUpdateEntities=new Set;entityMetadataPool=new y((()=>({bitset:new n,id:void 0})));initialised=!1;constructor(t){super(),t?.components&&this.registerComponents(t.components),this.initialised=!0}reset(){this.entities.forEach((t=>this.destroy(t))),this.entityIdCounter=0,this.idToEntity.clear(),this._entityPositions.clear()}id(t){if(!this.has(t))return;const e=t[o];let s=e.id;return void 0===s&&(s=this.entityIdCounter++,e.id=s,this.idToEntity.set(s,t)),s}entity(t){return this.idToEntity.get(t)}create(t){if(this.has(t))return t;const e=[];for(const s of Object.keys(t)){let t=this.componentRegistry[s];t||([t]=this.registerComponents([s])),e.push(t)}const i=this.entityMetadataPool.request();return i.bitset.add(...e),t[o]=i,s(this,t),this.index(t),t}destroy(t){i(this,t),this.queries.forEach((e=>i(e,t)));const e=t[o];if(!e)return;delete t[o];const s=e.id;void 0!==s&&(this.idToEntity.delete(s),e.id=void 0),e.bitset.reset(),this.entityMetadataPool.recycle(e)}add(t,e,s){if(void 0!==t[e])return this;t[e]=s;const i=t[o];let n=this.componentRegistry[e];return void 0===n&&([n]=this.registerComponents([e])),i.bitset.add(n),this.index(t),this}remove(t,e){if(void 0!==t[e]){if(this.has(t)){t[o].bitset.remove(this.componentRegistry[e]),this.index(t)}delete t[e]}}update(t,e){if("function"==typeof e){const s=e,i=new Proxy(t,{set:(e,s,i)=>{const n=s,o=n in t;return o&&void 0===i?this.remove(t,n):o?Reflect.set(t,s,i):this.add(t,n,i),!0},deleteProperty:(e,s)=>(this.remove(t,s),!0)});this.bulk((()=>{s(i)}))}else{const s=e;this.bulk((()=>{for(const e in s){const i=s[e];void 0!==i?this.add(t,e,i):this.remove(t,e)}}))}}bulk(t){this.bulkUpdateInProgress=!0,t(),this.bulkUpdateInProgress=!1;for(const t of this.bulkUpdateEntities)this.index(t);this.bulkUpdateEntities.clear()}query(t,e){const i=c(t);this.registerQueryDescriptionComponents(i);const n=l(this.componentRegistry,i),o=e?.handle??p,a=this.queryUsages.get(n);a?a.push(o):this.queryUsages.set(n,[o]);let u=this.queries.get(n);if(u)return u;u=new r(this,n,i,d(this.componentRegistry,i));const y=h(u.bitSets,this.entities.values());for(const t of y)s(u,t);return this.queries.set(n,u),u}destroyQuery(t,e){if(!this.queries.has(t.key))return;const s=e?.handle??p;let i=this.queryUsages.get(t.key)??[];i=i.filter((t=>t!==s)),i.length>0?this.queryUsages.set(t.key,i):(this.queries.delete(t.key),this.queryUsages.delete(t.key),t.onEntityAdded.clear(),t.onEntityRemoved.clear())}filter(t){const e=c(t);this.registerQueryDescriptionComponents(e);const s=l(this.componentRegistry,e),i=this.queries.get(s);if(i)return i.entities;const n=d(this.componentRegistry,e);return h(n,this.entities)}find(t){const e=c(t);this.registerQueryDescriptionComponents(e);const s=l(this.componentRegistry,e),i=this.queries.get(s);if(i)return i.first;return((t,e)=>{for(const s of e)if(a(t,s))return s})(d(this.componentRegistry,e),this.entities)}registerComponents(t){const e=[];for(const s of t)void 0===this.componentRegistry[s]&&(this.componentIndexCounter++,e.push(this.componentIndexCounter),this.componentRegistry[s]=this.componentIndexCounter);if(this.initialised)for(const t of this.entities.values()){t[o].bitset.resize(this.componentIndexCounter)}return e}registerQueryDescriptionComponents(t){const e=new Set(t.flatMap((t=>t.components))),s=[];for(const t of e)void 0===this.componentRegistry[t]&&s.push(t);s.length>0&&this.registerComponents(s)}index(t){if(this.has(t))if(this.bulkUpdateInProgress)this.bulkUpdateEntities.add(t);else for(const e of this.queries.values()){const n=a(e.bitSets,t),o=e.has(t);n&&!o?s(e,t):!n&&o&&i(e,t)}}}export{e as EntityContainer,r as Query,u as QueryBuilder,f as World}; | ||
class t{listeners=new Set;add(t){return this.listeners.add(t),()=>this.remove(t)}remove(t){this.listeners.delete(t)}emit(...t){for(const e of this.listeners)e(...t)}clear(){this.listeners.clear()}}class e{entities=[];version=0;onEntityAdded=new t;onEntityRemoved=new t;_entityPositions=new Map;_entitySet=new Set;get first(){return this.entities[0]||void 0}[Symbol.iterator](){let t=this.entities.length;const e={value:void 0,done:!1};return{next:()=>(e.value=this.entities[--t],e.done=t<0,e)}}has(t){return this._entitySet.has(t)}}const s=(t,e)=>{e&&!t.has(e)&&(t.entities.push(e),t._entityPositions.set(e,t.entities.length-1),t._entitySet.add(e),t.version++,t.onEntityAdded.emit(e))},i=(t,e)=>{if(!t.has(e))return;const s=t._entityPositions.get(e);t._entityPositions.delete(e),t._entitySet.delete(e);const i=t.entities[t.entities.length-1];i!==e&&(t.entities[s]=i,t._entityPositions.set(i,s)),t.entities.pop(),t.version++,t.onEntityRemoved.emit(e)};class n{words;constructor(t=[]){this.words=new Uint32Array(1);for(const e of t)this.add(e)}add(...t){const e=Math.max(...t);this.words.length<<5<=e&&this.resize(e,!1);for(let e=0;e<t.length;e++){const s=t[e];this.words[s>>>5]|=1<<s}}remove(...t){for(let e=0;e<t.length;e++){const s=t[e];this.words[s>>>5]&=~(1<<s)}}has(t){return 0!=(this.words[t>>>5]&1<<t)}resize(t,e=!0){if(e&&this.words.length<<5>t)return;const s=new Uint32Array(t+32>>>5<<1);s.set(this.words),this.words=s}reset(){for(let t=0;t<this.words.length;t++)this.words[t]=0}copy(t){const e=new Uint32Array(t.words.length);e.set(t.words),this.words=e}clone(){const t=new Uint32Array(this.words.length);t.set(this.words);const e=new n;return e.words=t,e}containsAll(t){for(let e=0;e<this.words.length;e++)if(0!=(~this.words[e]&t.words[e]))return!1;return!0}containsAny(t){for(let e=0;e<this.words.length;e++)if(0!=(this.words[e]&t.words[e]))return!0;return!1}}const o=Symbol("__arancini");class r extends e{world;key;conditions;bitSets;constructor(t,e,s,i){super(),this.world=t,this.key=e,this.conditions=s,this.bitSets=i}destroy(){this.world.destroyQuery(this)}}const h=(t,e)=>{const s=[];for(const i of e)a(t,i)&&s.push(i);return s},a=(t,e)=>{const{bitset:s}=e[o];for(const e of t){if("all"===e.type&&!s.containsAll(e.bitset))return!1;if("any"===e.type&&!s.containsAny(e.bitset))return!1;if("not"===e.type&&s.containsAny(e.bitset))return!1}return!0},d=t=>{const e=new u;t(e);const s=e.conditions;if(s.length<=0)throw new Error("Query must have at least one condition");if(s.some((t=>t.components.length<=0)))throw new Error("Query conditions must have at least one component");const i={type:"all",components:[]},n=[];for(const t of s)"all"===t.type?i.components.push(...t.components):n.push(t);return[i,...n]},l=(t,e)=>e.map((({type:e,components:s})=>"all"===e?s.map((e=>t[e])).sort().join(","):[`${e}:${s.map((e=>t[e])).sort().join(",")}`])).sort().join("&"),c=(t,e)=>e.map((e=>({type:e.type,bitset:new n(e.components.map((e=>t[e])))})));class u{T;conditions=[];all=(...t)=>(this.conditions.push({type:"all",components:t}),this);any=(...t)=>(this.conditions.push({type:"any",components:t}),this);not=(...t)=>(this.conditions.push({type:"not",components:t}),this);with=this.all;have=this.all;has=this.all;every=this.all;is=this.all;some=this.any;one=this.any;none=this.not;without=this.not;get and(){return this}get but(){return this}get where(){return this}get are(){return this}}class y{availableObjects=[];factory;get available(){return this.availableObjects.length}get used(){return this.size-this.availableObjects.length}size=0;constructor(t,e){this.factory=t,void 0!==e&&this.grow(e)}grow(t){for(let e=0;e<t;e++)this.availableObjects.push(this.factory());this.size+=t}free(t){for(let e=0;e<t&&this.availableObjects.pop();e++)this.size--}request(){return this.availableObjects.length<=0&&this.grow(Math.round(.2*this.size)+1),this.availableObjects.pop()}recycle(t){this.availableObjects.push(t),this.availableObjects.length>Math.round(.2*this.size)&&this.free(this.availableObjects.length-Math.round(.2*this.size))}}const p="standalone";class f extends e{queries=new Map;queryUsages=new Map;componentIndexCounter=-1;componentRegistry={};idToEntity=new Map;entityIdCounter=0;bulkUpdateInProgress=!1;bulkUpdateEntities=new Set;entityMetadataPool=new y((()=>({bitset:new n,id:void 0})));initialised=!1;constructor(t){super(),t?.components&&this.registerComponents(t.components),this.initialised=!0}reset(){this.entities.forEach((t=>this.destroy(t))),this.entityIdCounter=0,this.idToEntity.clear(),this._entityPositions.clear()}id(t){if(!this.has(t))return;const e=t[o];let s=e.id;return void 0===s&&(s=this.entityIdCounter++,e.id=s,this.idToEntity.set(s,t)),s}entity(t){return this.idToEntity.get(t)}create(t){if(this.has(t))return t;const e=[];for(const s of Object.keys(t)){let t=this.componentRegistry[s];t||([t]=this.registerComponents([s])),e.push(t)}const i=this.entityMetadataPool.request();return i.bitset.add(...e),t[o]=i,s(this,t),this.index(t),t}destroy(t){i(this,t),this.queries.forEach((e=>i(e,t)));const e=t[o];if(!e)return;delete t[o];const s=e.id;void 0!==s&&(this.idToEntity.delete(s),e.id=void 0),e.bitset.reset(),this.entityMetadataPool.recycle(e)}add(t,e,s){if(void 0!==t[e])return this;t[e]=s;const i=t[o];let n=this.componentRegistry[e];return void 0===n&&([n]=this.registerComponents([e])),i.bitset.add(n),this.index(t),this}remove(t,e){if(void 0!==t[e]){if(this.has(t)){t[o].bitset.remove(this.componentRegistry[e]),this.index(t)}delete t[e]}}update(t,e){if("function"==typeof e){const s=e,i=new Proxy(t,{set:(e,s,i)=>{const n=s,o=n in t;return o&&void 0===i?this.remove(t,n):o?Reflect.set(t,s,i):this.add(t,n,i),!0},deleteProperty:(e,s)=>(this.remove(t,s),!0)});this.bulk((()=>{s(i)}))}else{const s=e;this.bulk((()=>{for(const e in s){const i=s[e];void 0!==i?this.add(t,e,i):this.remove(t,e)}}))}}bulk(t){this.bulkUpdateInProgress=!0,t(),this.bulkUpdateInProgress=!1;for(const t of this.bulkUpdateEntities)this.index(t);this.bulkUpdateEntities.clear()}query(t,e){const i=d(t);this.registerQueryConditionComponents(i);const n=l(this.componentRegistry,i),o=e?.handle??p,a=this.queryUsages.get(n);a?a.push(o):this.queryUsages.set(n,[o]);let u=this.queries.get(n);if(u)return u;u=new r(this,n,i,c(this.componentRegistry,i));const y=h(u.bitSets,this.entities.values());for(const t of y)s(u,t);return this.queries.set(n,u),u}destroyQuery(t,e){if(!this.queries.has(t.key))return;const s=e?.handle??p;let i=this.queryUsages.get(t.key)??[];i=i.filter((t=>t!==s)),i.length>0?this.queryUsages.set(t.key,i):(this.queries.delete(t.key),this.queryUsages.delete(t.key),t.onEntityAdded.clear(),t.onEntityRemoved.clear())}filter(t){const e=d(t);this.registerQueryConditionComponents(e);const s=l(this.componentRegistry,e),i=this.queries.get(s);if(i)return i.entities;const n=c(this.componentRegistry,e);return h(n,this.entities)}find(t){const e=d(t);this.registerQueryConditionComponents(e);const s=l(this.componentRegistry,e),i=this.queries.get(s);if(i)return i.first;return((t,e)=>{for(const s of e)if(a(t,s))return s})(c(this.componentRegistry,e),this.entities)}registerComponents(t){const e=[];for(const s of t)void 0===this.componentRegistry[s]&&(this.componentIndexCounter++,e.push(this.componentIndexCounter),this.componentRegistry[s]=this.componentIndexCounter);if(this.initialised)for(const t of this.entities.values()){t[o].bitset.resize(this.componentIndexCounter)}return e}registerQueryConditionComponents(t){const e=new Set(t.flatMap((t=>t.components))),s=[];for(const t of e)void 0===this.componentRegistry[t]&&s.push(t);s.length>0&&this.registerComponents(s)}index(t){if(this.has(t))if(this.bulkUpdateInProgress)this.bulkUpdateEntities.add(t);else for(const e of this.queries.values()){const n=a(e.bitSets,t),o=e.has(t);n&&!o?s(e,t):!n&&o&&i(e,t)}}}export{e as EntityContainer,r as Query,u as QueryBuilder,f as World}; | ||
//# sourceMappingURL=index.es.js.map |
@@ -185,4 +185,4 @@ import { EntityContainer } from './entity-container'; | ||
registerComponents(components: (keyof E)[]): number[]; | ||
private registerQueryDescriptionComponents; | ||
private registerQueryConditionComponents; | ||
private index; | ||
} |
@@ -12,3 +12,3 @@ { | ||
"license": "MIT", | ||
"version": "6.1.2", | ||
"version": "6.1.3", | ||
"homepage": "https://github.com/isaac-mason/arancini", | ||
@@ -26,4 +26,4 @@ "bugs": { | ||
"dependencies": { | ||
"@arancini/events": "6.1.2", | ||
"@arancini/pool": "6.1.2" | ||
"@arancini/events": "6.1.3", | ||
"@arancini/pool": "6.1.3" | ||
}, | ||
@@ -30,0 +30,0 @@ "devDependencies": { |
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
57839
+ Added@arancini/events@6.1.3(transitive)
+ Added@arancini/pool@6.1.3(transitive)
- Removed@arancini/events@6.1.2(transitive)
- Removed@arancini/pool@6.1.2(transitive)
Updated@arancini/events@6.1.3
Updated@arancini/pool@6.1.3