@arancini/core
Advanced tools
Comparing version 6.2.0 to 6.3.0
@@ -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 extends e{world;key;conditions;constructor(t,e,s){super(),this.world=t,this.key=e,this.conditions=s}destroy(){this.world.destroyQuery(this)}}const o=(t,e)=>{const s=[];for(const i of e)r(t,i)&&s.push(i);return s},r=(t,e)=>{for(const s of t){if("all"===s.type&&!s.components.every((t=>e[t])))return!1;if("any"===s.type&&!s.components.some((t=>e[t])))return!1;if("not"===s.type&&s.components.some((t=>e[t])))return!1}return!0},h=t=>{const e=new a;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]},d=t=>t.map((({type:t,components:e})=>"all"===t?e.sort().join(","):[`${t}:${e.sort().join(",")}`])).sort().join("&");class a{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}}const l="standalone";class u extends e{queries=new Map;queryUsages=new Map;idToEntity=new Map;entityToId=new WeakMap;entityIdCounter=0;bulkUpdateInProgress=!1;bulkUpdateEntities=new Set;reset(){this.entities.forEach((t=>this.destroy(t))),this.entityIdCounter=0,this.idToEntity.clear(),this._entityPositions.clear()}id(t){if(!this.has(t))return;let e=this.entityToId.get(t);return void 0===e&&(e=this.entityIdCounter++,this.entityToId.set(t,e),this.idToEntity.set(e,t)),e}entity(t){return this.idToEntity.get(t)}create(t){return this.has(t)||(s(this,t),this.index(t)),t}destroy(t){i(this,t),this.queries.forEach((e=>i(e,t)))}add(t,e,s){return void 0!==t[e]||(t[e]=s,this.index(t)),this}remove(t,e){if(void 0!==t[e]){if(this.has(t)){const s={...t};delete s[e],this.index(t,s)}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=h(t),r=d(i),a=e?.handle??l,u=this.queryUsages.get(r);u?u.push(a):this.queryUsages.set(r,[a]);let c=this.queries.get(r);if(c)return c;c=new n(this,r,i);const y=o(c.conditions,this.entities.values());for(const t of y)s(c,t);return this.queries.set(r,c),c}destroyQuery(t,e){if(!this.queries.has(t.key))return;const s=e?.handle??l;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=h(t),s=d(e),i=this.queries.get(s);return i?i.entities:o(e,this.entities)}find(t){const e=h(t),s=d(e),i=this.queries.get(s);return i?i.first:((t,e)=>{for(const s of e)if(r(t,s))return s})(e,this.entities)}index(t,e=t){if(this.has(t))if(this.bulkUpdateInProgress)this.bulkUpdateEntities.add(t);else for(const n of this.queries.values()){const o=r(n.conditions,e),h=n.has(t);o&&!h?s(n,t):!o&&h&&i(n,t)}}}export{e as EntityContainer,n as Query,a as QueryBuilder,u 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 extends e{world;key;conditions;constructor(t,e,s){super(),this.world=t,this.key=e,this.conditions=s}destroy(){this.world.destroyQuery(this)}}const o=(t,e)=>{const s=[];for(const i of e)r(t,i)&&s.push(i);return s},r=(t,e)=>{for(const s of t){if("all"===s.type&&!s.components.every((t=>void 0!==e[t])))return!1;if("any"===s.type&&!s.components.some((t=>void 0!==e[t])))return!1;if("not"===s.type&&s.components.some((t=>void 0!==e[t])))return!1}return!0},h=t=>{const e=new a;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]},d=t=>t.map((({type:t,components:e})=>"all"===t?e.sort().join(","):[`${t}:${e.sort().join(",")}`])).sort().join("&");class a{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}}const l=Symbol("standalone");class u extends e{queries=new Map;queryUsages=new Map;idToEntity=new Map;entityToId=new WeakMap;entityIdCounter=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;let e=this.entityToId.get(t);return void 0===e&&(e=this.entityIdCounter++,this.entityToId.set(t,e),this.idToEntity.set(e,t)),e}entity(t){return this.idToEntity.get(t)}create(t){return this.has(t)||(s(this,t),this.index(t)),t}destroy(t){i(this,t),this.queries.forEach((e=>i(e,t)))}add(t,e,s){return void 0!==t[e]||(t[e]=s,this.index(t)),this}remove(t,e){if(void 0!==t[e]){if(this.has(t)){const s={...t};delete s[e],this.index(t,s)}delete t[e]}}update(t,e){const s={...t};if("function"==typeof e){e(s)}else Object.assign(s,e);this.index(t,s),Object.assign(t,s)}query(t,e){const i=h(t),r=d(i),a=e?.handle??l,u=this.queryUsages.get(r);u?u.push(a):this.queryUsages.set(r,[a]);let y=this.queries.get(r);if(y)return y;y=new n(this,r,i);const c=o(y.conditions,this.entities.values());for(const t of c)s(y,t);return this.queries.set(r,y),y}destroyQuery(t,e){if(!this.queries.has(t.key))return;const s=e?.handle??l;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=h(t),s=d(e),i=this.queries.get(s);return i?i.entities:o(e,this.entities)}find(t){const e=h(t),s=d(e),i=this.queries.get(s);return i?i.first:((t,e)=>{for(const s of e)if(r(t,s))return s})(e,this.entities)}index(t,e=t){if(this.has(t))for(const n of this.queries.values()){const o=r(n.conditions,e),h=n.has(t);o&&!h?s(n,t):!o&&h&&i(n,t)}}}export{e as EntityContainer,n as Query,a as QueryBuilder,u as World}; | ||
//# sourceMappingURL=index.es.js.map |
@@ -13,4 +13,2 @@ import { EntityContainer } from './entity-container'; | ||
private entityIdCounter; | ||
private bulkUpdateInProgress; | ||
private bulkUpdateEntities; | ||
/** | ||
@@ -117,17 +115,2 @@ * Removes all entities from the world. Components remain registered, and queries are not destroyed. | ||
/** | ||
* Utility method for adding and removing components from entities in bulk. | ||
* @param updateFn callback to update entities in the World | ||
* | ||
* @example | ||
* ```ts | ||
* const entity = world.create({ health: 10, poisioned: true }) | ||
* | ||
* world.bulk(() => { | ||
* world.add(entity, 'position', { x: 0, y: 0 }) | ||
* world.remove(entity, 'poisioned') | ||
* }) | ||
* ``` | ||
*/ | ||
bulk(updateFn: () => void): void; | ||
/** | ||
* Creates a query that updates with entity composition changes. | ||
@@ -134,0 +117,0 @@ * @param queryDescription the query description |
@@ -12,3 +12,3 @@ { | ||
"license": "MIT", | ||
"version": "6.2.0", | ||
"version": "6.3.0", | ||
"homepage": "https://github.com/isaac-mason/arancini", | ||
@@ -26,3 +26,3 @@ "bugs": { | ||
"dependencies": { | ||
"@arancini/events": "6.2.0" | ||
"@arancini/events": "6.3.0" | ||
}, | ||
@@ -29,0 +29,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
37196
248
+ Added@arancini/events@6.3.0(transitive)
- Removed@arancini/events@6.2.0(transitive)
Updated@arancini/events@6.3.0