Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@arancini/core

Package Overview
Dependencies
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@arancini/core - npm Package Compare versions

Comparing version 2.0.0 to 2.1.0

16

dist/index.es.js

@@ -239,6 +239,7 @@ var __defProp = Object.defineProperty;

__publicField(this, "__internal", {
class: null,
queries: /* @__PURE__ */ new Set(),
priority: 0,
order: 0,
class: null
requiredQueries: []
});

@@ -256,5 +257,8 @@ this.world = world;

}
query(queryDescription) {
query(queryDescription, options) {
const query = this.world.queryManager.createQuery(queryDescription);
this.__internal.queries.add(query);
if (options == null ? void 0 : options.required) {
this.__internal.requiredQueries.push(query);
}
return query;

@@ -637,5 +641,9 @@ }

for (const system of this.sortedSystems.values()) {
if (system.enabled) {
system.onUpdate(delta, time);
if (!system.enabled) {
continue;
}
if (system.__internal.requiredQueries.length > 0 && system.__internal.requiredQueries.some((q) => q.entities.length === 0)) {
continue;
}
system.onUpdate(delta, time);
}

@@ -642,0 +650,0 @@ }

@@ -1,1 +0,1 @@

(function(o,r){typeof exports=="object"&&typeof module!="undefined"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(o=typeof globalThis!="undefined"?globalThis:o||self,r(o.index={}))})(this,function(o){"use strict";var A=Object.defineProperty;var O=(o,r,c)=>r in o?A(o,r,{enumerable:!0,configurable:!0,writable:!0,value:c}):o[r]=c;var n=(o,r,c)=>(O(o,typeof r!="symbol"?r+"":r,c),c);class r{constructor(e=[]){n(this,"words");this.words=new Uint32Array(8);for(const t of e)this.add(t)}add(e){this.resize(e),this.words[e>>>5]|=1<<e}remove(e){this.resize(e),this.words[e>>>5]&=~(1<<e)}has(e){return(this.words[e>>>5]&1<<e)!==0}resize(e){if(this.words.length<<5>e)return;const t=e+32>>>5,s=new Uint32Array(t<<1);s.set(this.words),this.words=s}reset(){for(let e=0;e<this.words.length;e++)this.words[e]=0}copy(e){const t=new Uint32Array(e.words.length);t.set(e.words),this.words=t}clone(){const e=new Uint32Array(this.words.length);e.set(this.words);const t=new r;return t.words=e,t}containsAll(e){for(let t=0;t<this.words.length;t++)if((~this.words[t]&e.words[t])!==0)return!1;return!0}containsAny(e){for(let t=0;t<this.words.length;t++)if((this.words[t]&e.words[t])!==0)return!0;return!1}}const c=(d,e)=>Object.getOwnPropertyNames(d.prototype).includes(e);let m=0;const h=()=>(m++,m.toString());class y{constructor(){n(this,"id",h());n(this,"entity");n(this,"_class")}construct(...e){}onDestroy(){}onInit(){}}n(y,"componentIndex");class f{constructor(){n(this,"id",h());n(this,"initialised",!1);n(this,"space");n(this,"world");n(this,"_componentsBitSet",new r);n(this,"_components",{})}add(e,...t){if(this._components[e.componentIndex])throw new Error(`Cannot add component ${e.name}, entity with id ${this.id} already has this component`);const s=this.world.spaceManager.addComponentToEntity(this,e,t);return this.world.queryManager.onEntityComponentChange(this),s}get(e){const t=this._components[e.componentIndex];if(t!==void 0)return t;throw new Error(`Component ${e}} not in entity ${this.id}`)}getAll(){return Object.values(this._components)}find(e){return this._components[e.componentIndex]}has(e){return this._components[e.componentIndex]!==void 0}remove(e){let t;if(e instanceof y){if(!this._components[e._class.componentIndex])throw new Error("Component instance does not exist in Entity");t=e}else if(t=this.find(e),t===void 0)throw new Error("Component does not exist in Entity");return this.world.spaceManager.removeComponentFromEntity(this,t,!0),this.world.queryManager.onEntityComponentChange(this),this}destroy(){!this.space||!this.world||this.space.world.spaceManager.destroyEntity(this,this.space)}}class p{constructor(){n(this,"listeners",new Set)}add(e){return this.listeners.add(e),()=>this.remove(e)}remove(e){this.listeners.delete(e)}emit(...e){for(const t of this.listeners.values())t(...e)}clear(){this.listeners.clear()}}const w={ALL:"all",ANY:"any",NOT:"not"};class u{constructor(e,t){n(this,"key");n(this,"entities",[]);n(this,"onEntityAdded",new p);n(this,"onEntityRemoved",new p);n(this,"world");this.world=e,this.key=t}get first(){return this.entities[0]||void 0}[Symbol.iterator](){let e=this.entities.length;const t={value:void 0,done:!1};return{next:()=>(t.value=this.entities[--e],t.done=e<0,t)}}destroy(){this.world.queryManager.removeQuery(this)}static getDescriptionDedupeString(e){return Array.isArray(e)?e.map(t=>`${t.name}`).join("&"):Object.entries(e).flatMap(([t,s])=>t===w.ALL?s.map(i=>`${i.name}`).sort():[`${t}:${s.sort().map(i=>i.name)}`]).sort().join("&")}}class g{constructor(e,t){n(this,"id");n(this,"entities",new Map);n(this,"initialised",!1);n(this,"world");n(this,"create",{entity:e=>this.world.spaceManager.createEntity(this,e)});this.world=e,this.id=(t==null?void 0:t.id)||h()}destroy(){!this.world||this.world.spaceManager.destroySpace(this)}}class M{constructor(e){n(this,"enabled",!0);n(this,"world");n(this,"__internal",{queries:new Set,priority:0,order:0,class:null});this.world=e}onDestroy(){}onInit(){}onUpdate(e,t){}destroy(){this.world.unregisterSystem(this.__internal.class)}query(e){const t=this.world.queryManager.createQuery(e);return this.__internal.queries.add(t),t}}class E{constructor(e){n(this,"components",new Map);n(this,"currentComponentIndex",-1);n(this,"world");this.world=e}registerComponent(e){let t=this.components.get(e);if(t!==void 0)return t;if(this.currentComponentIndex++,t=this.currentComponentIndex,e.componentIndex=t,this.components.set(e,t),this.world.initialised)for(const s of this.world.spaceManager.spaces.values())for(const i of s.entities.values())i._componentsBitSet.resize(this.currentComponentIndex);return t}}class b{constructor(e){n(this,"dedupedQueries",new Map);n(this,"world");this.world=e}createQuery(e){const t=u.getDescriptionDedupeString(e);let s=this.dedupedQueries.get(t);if(s===void 0){const a=Array.isArray(e);if(a&&e.length===0||!a&&(!e.all&&!e.any&&!e.not||e.all&&e.all.length===0||e.any&&e.any.length===0||e.not&&e.not.length===0))throw new Error("Query must have at least one condition");s={dedupeString:t,instances:new Set,description:e,bitSets:this.getQueryBitSets(e),entities:[],entitySet:new Set};const l=this.getQueryResults(s.bitSets);for(const v of l.values())s.entities.push(v),s.entitySet.add(v);this.dedupedQueries.set(t,s)}const i=new u(this.world,t);return i.entities=s.entities,s.instances.add(i),i}hasQuery(e){const t=u.getDescriptionDedupeString(e);return this.dedupedQueries.has(t)}onEntityComponentChange(e){for(const t of this.dedupedQueries.values()){const s=t.entitySet.has(e),i=this.matchesQueryConditions(t.bitSets,e);if(i&&!s){t.entities.push(e),t.entitySet.add(e);for(const a of t.instances)a.onEntityAdded.emit(e)}else if(!i&&s){const a=t.entities.indexOf(e,0);a!==-1&&t.entities.splice(a,1),t.entitySet.delete(e);for(const l of t.instances)l.onEntityRemoved.emit(e)}}}onEntityRemoved(e){for(const t of this.dedupedQueries.values()){const s=t.entities.indexOf(e,0);s!==-1&&t.entities.splice(s,1),t.entitySet.delete(e);for(const i of t.instances)i.onEntityRemoved.emit(e)}}query(e){const t=u.getDescriptionDedupeString(e),s=this.dedupedQueries.get(t);return s?s.entities:this.getQueryResults(this.getQueryBitSets(e))}removeQuery(e){const t=this.dedupedQueries.get(e.key);t===void 0||!t.instances.has(e)||(t.instances.delete(e),e.onEntityAdded.clear(),e.onEntityRemoved.clear(),t.instances.size===0&&this.dedupedQueries.delete(t.dedupeString))}matchesQueryConditions(e,t){return!(e.all&&!t._componentsBitSet.containsAll(e.all)||e.any&&!t._componentsBitSet.containsAny(e.any)||e.not&&t._componentsBitSet.containsAny(e.not))}getQueryResults(e){const t=[];for(const s of this.world.spaceManager.spaces.values())for(const i of s.entities.values())this.matchesQueryConditions(e,i)&&t.push(i);return t}getQueryBitSets(e){const{all:t,any:s,not:i}=Array.isArray(e)?{all:e,any:void 0,not:void 0}:e,a={};return a.all=t?new r(t.map(l=>l.componentIndex)):void 0,a.any=s?new r(s.map(l=>l.componentIndex)):void 0,a.not=i?new r(i.map(l=>l.componentIndex)):void 0,a}}class S{constructor(e,t){n(this,"availableObjects",[]);n(this,"factory");n(this,"size",0);this.factory=e,t!==void 0&&this.expand(t)}get free(){return this.availableObjects.length}get used(){return this.size-this.availableObjects.length}expand(e){for(let t=0;t<e;t++)this.availableObjects.push(this.factory());this.size+=e}request(){return this.availableObjects.length<=0&&this.expand(Math.round(this.size*.2)+1),this.availableObjects.pop()}release(e){this.availableObjects.push(e)}}class Q{constructor(){n(this,"objectPools",new Map)}get totalPools(){return this.objectPools.size}get size(){let e=0;for(const t of this.objectPools.values())e+=t.size;return e}get free(){let e=0;for(const t of this.objectPools.values())e+=t.free;return e}get used(){let e=0;for(const t of this.objectPools.values())e+=t.used;return e}request(e){let t=this.objectPools.get(e);return t===void 0&&(t=new S(()=>{const s=new e;return s._class=e,s}),this.objectPools.set(e,t)),t.request()}release(e){const t=this.objectPools.get(e._class);t!==void 0&&t.release(e)}}class C{constructor(e){n(this,"objectPool",new S(()=>{const e=new f;return e.world=this.world,e}));n(this,"world");this.world=e}get size(){return this.objectPool.size}get free(){return this.objectPool.free}get used(){return this.objectPool.used}request(){return this.objectPool.request()}release(e){this.objectPool.release(e)}}class I{constructor(e){n(this,"spaces",new Map);n(this,"componentPool");n(this,"entityPool");n(this,"world");this.world=e,this.componentPool=new Q,this.entityPool=new C(this.world)}init(){for(const e of this.spaces.values())this.initialiseSpace(e)}destroy(){for(const e of this.spaces.values())this.destroySpace(e)}createSpace(e){if((e==null?void 0:e.id)&&this.spaces.has(e.id))throw new Error("A space with the provided id already exists");const t=new g(this.world,e);return this.spaces.set(t.id,t),this.world.initialised&&this.initialiseSpace(t),t}initialiseSpace(e){e.initialised=!0;for(const t of e.entities.values())this.initialiseEntity(t)}destroySpace(e){this.spaces.delete(e.id);for(const t of e.entities.values())this.destroyEntity(t,e)}createEntity(e,t=[]){var i;const s=this.entityPool.request();s.id=h(),s.space=e,s.initialised=!1,e.entities.set(s.id,s);for(const a of t)this.world.spaceManager.addComponentToEntity(s,a.type,(i=a.args)!=null?i:[]);return this.world.queryManager.onEntityComponentChange(s),e.initialised&&this.world.spaceManager.initialiseEntity(s),s}initialiseEntity(e){e.initialised=!0,e._componentsBitSet.resize(this.world.componentRegistry.currentComponentIndex);for(const t of Object.values(e._components))this.initialiseComponent(t)}destroyEntity(e,t){e.space=null,t.entities.delete(e.id);for(const s of Object.values(e._components))this.removeComponentFromEntity(e,s,!1);this.world.queryManager.onEntityRemoved(e),e._componentsBitSet.reset(),this.entityPool.release(e)}addComponentToEntity(e,t,s){const i=this.componentPool.request(t);return i.entity=e,i.construct(...s),e._components[t.componentIndex]=i,e._componentsBitSet.add(i._class.componentIndex),e.initialised&&this.initialiseComponent(i),i}removeComponentFromEntity(e,t,s){t.onDestroy(),delete e._components[t._class.componentIndex],s&&e._componentsBitSet.remove(t._class.componentIndex),t.id=h(),t.entity=void 0,this.componentPool.release(t)}initialiseComponent(e){e.onInit()}}class j{constructor(e){n(this,"systems",new Map);n(this,"sortedSystems",[]);n(this,"systemCounter",0);n(this,"initialised",!1);n(this,"world");this.world=e}init(){this.initialised=!0;for(const e of this.systems.values())e.onInit();this.sortSystems()}update(e,t){for(const s of this.sortedSystems.values())s.enabled&&s.onUpdate(e,t)}destroy(){for(const e of this.systems.values())e.onDestroy(),this.systems.delete(e.__internal.class)}registerSystem(e,t){var a;if(this.systems.has(e))throw new Error(`System "${e.name}" has already been registered`);this.systemCounter++;const s=new e(this.world);s.__internal.class=e,s.__internal.priority=(a=t==null?void 0:t.priority)!=null?a:0,s.__internal.order=this.systemCounter,this.systems.set(e,s);const i=c(e,"onUpdate");i&&this.sortedSystems.push(s),this.initialised&&(s.onInit(),i&&this.sortSystems())}unregisterSystem(e){const t=this.systems.get(e);!t||(this.systems.delete(e),this.sortedSystems=this.sortedSystems.filter(s=>s.__internal.class!==e),t.__internal.queries.forEach(s=>{this.world.queryManager.removeQuery(s)}),t.onDestroy())}sortSystems(){this.sortedSystems.sort((e,t)=>t.__internal.priority-e.__internal.priority||e.__internal.order-t.__internal.order)}}const _="__arancini_default_world_space";class P{constructor(){n(this,"id",h());n(this,"initialised",!1);n(this,"time",0);n(this,"defaultSpace");n(this,"spaceManager");n(this,"queryManager");n(this,"systemManager");n(this,"componentRegistry");n(this,"create",{entity:e=>this.spaceManager.createEntity(this.defaultSpace,e),space:e=>this.spaceManager.createSpace(e),query:e=>this.queryManager.createQuery(e)});this.componentRegistry=new E(this),this.spaceManager=new I(this),this.queryManager=new b(this),this.systemManager=new j(this),this.defaultSpace=this.create.space({id:_})}init(){this.initialised=!0,this.spaceManager.init(),this.systemManager.init()}update(e=0){this.time+=e,this.systemManager.update(e,this.time)}destroy(){this.systemManager.destroy(),this.spaceManager.destroy()}query(e){return this.queryManager.query(e)}registerComponent(e){return this.componentRegistry.registerComponent(e),this}registerSystem(e,t){return this.systemManager.registerSystem(e,t),this}unregisterSystem(e){return this.systemManager.unregisterSystem(e),this}getSystem(e){return this.systemManager.systems.get(e)}getSystems(){return Array.from(this.systemManager.systems.values())}getSpace(e){return this.spaceManager.spaces.get(e)}}o.Component=y,o.Entity=f,o.Query=u,o.QueryConditionType=w,o.Space=g,o.System=M,o.Topic=p,o.WORLD_DEFAULT_SPACE_ID=_,o.World=P,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
(function(o,r){typeof exports=="object"&&typeof module!="undefined"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(o=typeof globalThis!="undefined"?globalThis:o||self,r(o.index={}))})(this,function(o){"use strict";var A=Object.defineProperty;var O=(o,r,c)=>r in o?A(o,r,{enumerable:!0,configurable:!0,writable:!0,value:c}):o[r]=c;var n=(o,r,c)=>(O(o,typeof r!="symbol"?r+"":r,c),c);class r{constructor(e=[]){n(this,"words");this.words=new Uint32Array(8);for(const t of e)this.add(t)}add(e){this.resize(e),this.words[e>>>5]|=1<<e}remove(e){this.resize(e),this.words[e>>>5]&=~(1<<e)}has(e){return(this.words[e>>>5]&1<<e)!==0}resize(e){if(this.words.length<<5>e)return;const t=e+32>>>5,s=new Uint32Array(t<<1);s.set(this.words),this.words=s}reset(){for(let e=0;e<this.words.length;e++)this.words[e]=0}copy(e){const t=new Uint32Array(e.words.length);t.set(e.words),this.words=t}clone(){const e=new Uint32Array(this.words.length);e.set(this.words);const t=new r;return t.words=e,t}containsAll(e){for(let t=0;t<this.words.length;t++)if((~this.words[t]&e.words[t])!==0)return!1;return!0}containsAny(e){for(let t=0;t<this.words.length;t++)if((this.words[t]&e.words[t])!==0)return!0;return!1}}const c=(d,e)=>Object.getOwnPropertyNames(d.prototype).includes(e);let m=0;const h=()=>(m++,m.toString());class y{constructor(){n(this,"id",h());n(this,"entity");n(this,"_class")}construct(...e){}onDestroy(){}onInit(){}}n(y,"componentIndex");class f{constructor(){n(this,"id",h());n(this,"initialised",!1);n(this,"space");n(this,"world");n(this,"_componentsBitSet",new r);n(this,"_components",{})}add(e,...t){if(this._components[e.componentIndex])throw new Error(`Cannot add component ${e.name}, entity with id ${this.id} already has this component`);const s=this.world.spaceManager.addComponentToEntity(this,e,t);return this.world.queryManager.onEntityComponentChange(this),s}get(e){const t=this._components[e.componentIndex];if(t!==void 0)return t;throw new Error(`Component ${e}} not in entity ${this.id}`)}getAll(){return Object.values(this._components)}find(e){return this._components[e.componentIndex]}has(e){return this._components[e.componentIndex]!==void 0}remove(e){let t;if(e instanceof y){if(!this._components[e._class.componentIndex])throw new Error("Component instance does not exist in Entity");t=e}else if(t=this.find(e),t===void 0)throw new Error("Component does not exist in Entity");return this.world.spaceManager.removeComponentFromEntity(this,t,!0),this.world.queryManager.onEntityComponentChange(this),this}destroy(){!this.space||!this.world||this.space.world.spaceManager.destroyEntity(this,this.space)}}class p{constructor(){n(this,"listeners",new Set)}add(e){return this.listeners.add(e),()=>this.remove(e)}remove(e){this.listeners.delete(e)}emit(...e){for(const t of this.listeners.values())t(...e)}clear(){this.listeners.clear()}}const g={ALL:"all",ANY:"any",NOT:"not"};class u{constructor(e,t){n(this,"key");n(this,"entities",[]);n(this,"onEntityAdded",new p);n(this,"onEntityRemoved",new p);n(this,"world");this.world=e,this.key=t}get first(){return this.entities[0]||void 0}[Symbol.iterator](){let e=this.entities.length;const t={value:void 0,done:!1};return{next:()=>(t.value=this.entities[--e],t.done=e<0,t)}}destroy(){this.world.queryManager.removeQuery(this)}static getDescriptionDedupeString(e){return Array.isArray(e)?e.map(t=>`${t.name}`).join("&"):Object.entries(e).flatMap(([t,s])=>t===g.ALL?s.map(i=>`${i.name}`).sort():[`${t}:${s.sort().map(i=>i.name)}`]).sort().join("&")}}class w{constructor(e,t){n(this,"id");n(this,"entities",new Map);n(this,"initialised",!1);n(this,"world");n(this,"create",{entity:e=>this.world.spaceManager.createEntity(this,e)});this.world=e,this.id=(t==null?void 0:t.id)||h()}destroy(){!this.world||this.world.spaceManager.destroySpace(this)}}class M{constructor(e){n(this,"enabled",!0);n(this,"world");n(this,"__internal",{class:null,queries:new Set,priority:0,order:0,requiredQueries:[]});this.world=e}onDestroy(){}onInit(){}onUpdate(e,t){}destroy(){this.world.unregisterSystem(this.__internal.class)}query(e,t){const s=this.world.queryManager.createQuery(e);return this.__internal.queries.add(s),t!=null&&t.required&&this.__internal.requiredQueries.push(s),s}}class E{constructor(e){n(this,"components",new Map);n(this,"currentComponentIndex",-1);n(this,"world");this.world=e}registerComponent(e){let t=this.components.get(e);if(t!==void 0)return t;if(this.currentComponentIndex++,t=this.currentComponentIndex,e.componentIndex=t,this.components.set(e,t),this.world.initialised)for(const s of this.world.spaceManager.spaces.values())for(const i of s.entities.values())i._componentsBitSet.resize(this.currentComponentIndex);return t}}class Q{constructor(e){n(this,"dedupedQueries",new Map);n(this,"world");this.world=e}createQuery(e){const t=u.getDescriptionDedupeString(e);let s=this.dedupedQueries.get(t);if(s===void 0){const a=Array.isArray(e);if(a&&e.length===0||!a&&(!e.all&&!e.any&&!e.not||e.all&&e.all.length===0||e.any&&e.any.length===0||e.not&&e.not.length===0))throw new Error("Query must have at least one condition");s={dedupeString:t,instances:new Set,description:e,bitSets:this.getQueryBitSets(e),entities:[],entitySet:new Set};const l=this.getQueryResults(s.bitSets);for(const v of l.values())s.entities.push(v),s.entitySet.add(v);this.dedupedQueries.set(t,s)}const i=new u(this.world,t);return i.entities=s.entities,s.instances.add(i),i}hasQuery(e){const t=u.getDescriptionDedupeString(e);return this.dedupedQueries.has(t)}onEntityComponentChange(e){for(const t of this.dedupedQueries.values()){const s=t.entitySet.has(e),i=this.matchesQueryConditions(t.bitSets,e);if(i&&!s){t.entities.push(e),t.entitySet.add(e);for(const a of t.instances)a.onEntityAdded.emit(e)}else if(!i&&s){const a=t.entities.indexOf(e,0);a!==-1&&t.entities.splice(a,1),t.entitySet.delete(e);for(const l of t.instances)l.onEntityRemoved.emit(e)}}}onEntityRemoved(e){for(const t of this.dedupedQueries.values()){const s=t.entities.indexOf(e,0);s!==-1&&t.entities.splice(s,1),t.entitySet.delete(e);for(const i of t.instances)i.onEntityRemoved.emit(e)}}query(e){const t=u.getDescriptionDedupeString(e),s=this.dedupedQueries.get(t);return s?s.entities:this.getQueryResults(this.getQueryBitSets(e))}removeQuery(e){const t=this.dedupedQueries.get(e.key);t===void 0||!t.instances.has(e)||(t.instances.delete(e),e.onEntityAdded.clear(),e.onEntityRemoved.clear(),t.instances.size===0&&this.dedupedQueries.delete(t.dedupeString))}matchesQueryConditions(e,t){return!(e.all&&!t._componentsBitSet.containsAll(e.all)||e.any&&!t._componentsBitSet.containsAny(e.any)||e.not&&t._componentsBitSet.containsAny(e.not))}getQueryResults(e){const t=[];for(const s of this.world.spaceManager.spaces.values())for(const i of s.entities.values())this.matchesQueryConditions(e,i)&&t.push(i);return t}getQueryBitSets(e){const{all:t,any:s,not:i}=Array.isArray(e)?{all:e,any:void 0,not:void 0}:e,a={};return a.all=t?new r(t.map(l=>l.componentIndex)):void 0,a.any=s?new r(s.map(l=>l.componentIndex)):void 0,a.not=i?new r(i.map(l=>l.componentIndex)):void 0,a}}class _{constructor(e,t){n(this,"availableObjects",[]);n(this,"factory");n(this,"size",0);this.factory=e,t!==void 0&&this.expand(t)}get free(){return this.availableObjects.length}get used(){return this.size-this.availableObjects.length}expand(e){for(let t=0;t<e;t++)this.availableObjects.push(this.factory());this.size+=e}request(){return this.availableObjects.length<=0&&this.expand(Math.round(this.size*.2)+1),this.availableObjects.pop()}release(e){this.availableObjects.push(e)}}class b{constructor(){n(this,"objectPools",new Map)}get totalPools(){return this.objectPools.size}get size(){let e=0;for(const t of this.objectPools.values())e+=t.size;return e}get free(){let e=0;for(const t of this.objectPools.values())e+=t.free;return e}get used(){let e=0;for(const t of this.objectPools.values())e+=t.used;return e}request(e){let t=this.objectPools.get(e);return t===void 0&&(t=new _(()=>{const s=new e;return s._class=e,s}),this.objectPools.set(e,t)),t.request()}release(e){const t=this.objectPools.get(e._class);t!==void 0&&t.release(e)}}class C{constructor(e){n(this,"objectPool",new _(()=>{const e=new f;return e.world=this.world,e}));n(this,"world");this.world=e}get size(){return this.objectPool.size}get free(){return this.objectPool.free}get used(){return this.objectPool.used}request(){return this.objectPool.request()}release(e){this.objectPool.release(e)}}class I{constructor(e){n(this,"spaces",new Map);n(this,"componentPool");n(this,"entityPool");n(this,"world");this.world=e,this.componentPool=new b,this.entityPool=new C(this.world)}init(){for(const e of this.spaces.values())this.initialiseSpace(e)}destroy(){for(const e of this.spaces.values())this.destroySpace(e)}createSpace(e){if((e==null?void 0:e.id)&&this.spaces.has(e.id))throw new Error("A space with the provided id already exists");const t=new w(this.world,e);return this.spaces.set(t.id,t),this.world.initialised&&this.initialiseSpace(t),t}initialiseSpace(e){e.initialised=!0;for(const t of e.entities.values())this.initialiseEntity(t)}destroySpace(e){this.spaces.delete(e.id);for(const t of e.entities.values())this.destroyEntity(t,e)}createEntity(e,t=[]){var i;const s=this.entityPool.request();s.id=h(),s.space=e,s.initialised=!1,e.entities.set(s.id,s);for(const a of t)this.world.spaceManager.addComponentToEntity(s,a.type,(i=a.args)!=null?i:[]);return this.world.queryManager.onEntityComponentChange(s),e.initialised&&this.world.spaceManager.initialiseEntity(s),s}initialiseEntity(e){e.initialised=!0,e._componentsBitSet.resize(this.world.componentRegistry.currentComponentIndex);for(const t of Object.values(e._components))this.initialiseComponent(t)}destroyEntity(e,t){e.space=null,t.entities.delete(e.id);for(const s of Object.values(e._components))this.removeComponentFromEntity(e,s,!1);this.world.queryManager.onEntityRemoved(e),e._componentsBitSet.reset(),this.entityPool.release(e)}addComponentToEntity(e,t,s){const i=this.componentPool.request(t);return i.entity=e,i.construct(...s),e._components[t.componentIndex]=i,e._componentsBitSet.add(i._class.componentIndex),e.initialised&&this.initialiseComponent(i),i}removeComponentFromEntity(e,t,s){t.onDestroy(),delete e._components[t._class.componentIndex],s&&e._componentsBitSet.remove(t._class.componentIndex),t.id=h(),t.entity=void 0,this.componentPool.release(t)}initialiseComponent(e){e.onInit()}}class j{constructor(e){n(this,"systems",new Map);n(this,"sortedSystems",[]);n(this,"systemCounter",0);n(this,"initialised",!1);n(this,"world");this.world=e}init(){this.initialised=!0;for(const e of this.systems.values())e.onInit();this.sortSystems()}update(e,t){for(const s of this.sortedSystems.values())!s.enabled||s.__internal.requiredQueries.length>0&&s.__internal.requiredQueries.some(i=>i.entities.length===0)||s.onUpdate(e,t)}destroy(){for(const e of this.systems.values())e.onDestroy(),this.systems.delete(e.__internal.class)}registerSystem(e,t){var a;if(this.systems.has(e))throw new Error(`System "${e.name}" has already been registered`);this.systemCounter++;const s=new e(this.world);s.__internal.class=e,s.__internal.priority=(a=t==null?void 0:t.priority)!=null?a:0,s.__internal.order=this.systemCounter,this.systems.set(e,s);const i=c(e,"onUpdate");i&&this.sortedSystems.push(s),this.initialised&&(s.onInit(),i&&this.sortSystems())}unregisterSystem(e){const t=this.systems.get(e);!t||(this.systems.delete(e),this.sortedSystems=this.sortedSystems.filter(s=>s.__internal.class!==e),t.__internal.queries.forEach(s=>{this.world.queryManager.removeQuery(s)}),t.onDestroy())}sortSystems(){this.sortedSystems.sort((e,t)=>t.__internal.priority-e.__internal.priority||e.__internal.order-t.__internal.order)}}const S="__arancini_default_world_space";class P{constructor(){n(this,"id",h());n(this,"initialised",!1);n(this,"time",0);n(this,"defaultSpace");n(this,"spaceManager");n(this,"queryManager");n(this,"systemManager");n(this,"componentRegistry");n(this,"create",{entity:e=>this.spaceManager.createEntity(this.defaultSpace,e),space:e=>this.spaceManager.createSpace(e),query:e=>this.queryManager.createQuery(e)});this.componentRegistry=new E(this),this.spaceManager=new I(this),this.queryManager=new Q(this),this.systemManager=new j(this),this.defaultSpace=this.create.space({id:S})}init(){this.initialised=!0,this.spaceManager.init(),this.systemManager.init()}update(e=0){this.time+=e,this.systemManager.update(e,this.time)}destroy(){this.systemManager.destroy(),this.spaceManager.destroy()}query(e){return this.queryManager.query(e)}registerComponent(e){return this.componentRegistry.registerComponent(e),this}registerSystem(e,t){return this.systemManager.registerSystem(e,t),this}unregisterSystem(e){return this.systemManager.unregisterSystem(e),this}getSystem(e){return this.systemManager.systems.get(e)}getSystems(){return Array.from(this.systemManager.systems.values())}getSpace(e){return this.spaceManager.spaces.get(e)}}o.Component=y,o.Entity=f,o.Query=u,o.QueryConditionType=g,o.Space=w,o.System=M,o.Topic=p,o.WORLD_DEFAULT_SPACE_ID=S,o.World=P,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});

@@ -71,2 +71,6 @@ import type { QueryDescription } from './query';

order: number;
/**
* Queries that must have at least one result for onUpdate to be called
*/
requiredQueries: Query[];
};

@@ -95,5 +99,8 @@ constructor(world: World);

* @param queryDescription the query description
* @param options optional options for the system query
* @returns the query
*/
protected query(queryDescription: QueryDescription): Query;
protected query(queryDescription: QueryDescription, options?: {
required: boolean;
}): Query;
}

@@ -13,3 +13,3 @@ {

"license": "MIT",
"version": "2.0.0",
"version": "2.1.0",
"homepage": "https://github.com/isaac-mason/arancini",

@@ -16,0 +16,0 @@ "bugs": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc