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 1.0.0 to 1.0.1

4

dist/entity.d.ts

@@ -127,7 +127,7 @@ import type { ComponentClass } from './component';

* Adds a handler for entity events
* @param eventName the event name
* @param topic the event topic
* @param handler the handler function
* @returns the subscription
*/
on<E extends Event | Event>(eventName: string, handler: EventHandler<E>): EventSubscription;
on<E extends Event | Event>(topic: E['topic'], handler: EventHandler<E>): EventSubscription;
/**

@@ -134,0 +134,0 @@ * Broadcasts an event to the Entity EventSystem

@@ -56,13 +56,13 @@ /**

* Adds a handler to the event system
* @param eventName the event name
* @param topic the event topic
* @param handler the handler function
* @returns the id of the new handler
*/
on<E extends Event | Event>(eventName: string, handler: EventHandler<E>): EventSubscription;
on<E extends Event | Event>(topic: string, handler: EventHandler<E>): EventSubscription;
/**
* Removes an event handler by handler id
* @param eventName the name of the event
* @param topic the name of the event
* @param handler the event handler
*/
unsubscribe(eventName: string, handler: EventHandler<never>): void;
unsubscribe(topic: string, handler: EventHandler<never>): void;
/**

@@ -69,0 +69,0 @@ * Emits an event for handling by the event system

@@ -128,15 +128,15 @@ var __defProp = Object.defineProperty;

}
on(eventName, handler) {
let eventDispatcher = this.dispatchers.get(eventName);
on(topic, handler) {
let eventDispatcher = this.dispatchers.get(topic);
if (eventDispatcher === void 0) {
eventDispatcher = new EventDispatcher();
this.dispatchers.set(eventName, eventDispatcher);
this.dispatchers.set(topic, eventDispatcher);
}
eventDispatcher.add(handler);
return {
unsubscribe: () => this.unsubscribe(eventName, handler)
unsubscribe: () => this.unsubscribe(topic, handler)
};
}
unsubscribe(eventName, handler) {
const eventHandlers = this.dispatchers.get(eventName);
unsubscribe(topic, handler) {
const eventHandlers = this.dispatchers.get(topic);
if (eventHandlers !== void 0) {

@@ -217,4 +217,4 @@ eventHandlers.remove(handler);

}
on(eventName, handler) {
return this.events.on(eventName, handler);
on(topic, handler) {
return this.events.on(topic, handler);
}

@@ -282,11 +282,9 @@ emit(event) {

__publicField(this, "world");
this.world = world;
this.id = (params == null ? void 0 : params.id) || uniqueId();
}
get create() {
return {
__publicField(this, "create", {
entity: (components) => {
return this.world.spaceManager.createEntity(this, components);
}
};
});
this.world = world;
this.id = (params == null ? void 0 : params.id) || uniqueId();
}

@@ -296,4 +294,4 @@ emit(event) {

}
on(eventName, handler) {
return this.events.on(eventName, handler);
on(topic, handler) {
return this.events.on(topic, handler);
}

@@ -761,11 +759,3 @@ destroy() {

__publicField(this, "componentRegistry");
this.events = new EventSystem();
this.componentRegistry = new ComponentRegistry(this);
this.spaceManager = new SpaceManager(this);
this.queryManager = new QueryManager(this);
this.systemManager = new SystemManager(this);
this.defaultSpace = this.create.space({ id: WORLD_DEFAULT_SPACE_ID });
}
get create() {
return {
__publicField(this, "create", {
entity: (components) => {

@@ -780,3 +770,9 @@ return this.spaceManager.createEntity(this.defaultSpace, components);

}
};
});
this.events = new EventSystem();
this.componentRegistry = new ComponentRegistry(this);
this.spaceManager = new SpaceManager(this);
this.queryManager = new QueryManager(this);
this.systemManager = new SystemManager(this);
this.defaultSpace = this.create.space({ id: WORLD_DEFAULT_SPACE_ID });
}

@@ -799,4 +795,4 @@ init() {

}
on(eventName, handler) {
return this.events.on(eventName, handler);
on(topic, handler) {
return this.events.on(topic, handler);
}

@@ -803,0 +799,0 @@ query(queryDescription) {

@@ -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 x=Object.defineProperty;var O=(o,r,l)=>r in o?x(o,r,{enumerable:!0,configurable:!0,writable:!0,value:l}):o[r]=l;var n=(o,r,l)=>(O(o,typeof r!="symbol"?r+"":r,l),l);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 l=(d,e)=>Object.getOwnPropertyNames(d.prototype).includes(e);let f=0;const h=()=>(f++,f.toString());class p{constructor(){n(this,"id",h());n(this,"entity");n(this,"_class")}get space(){return this.entity.space}get world(){return this.entity.world}construct(...e){}onDestroy(){}onInit(){}}n(p,"componentIndex");class y{constructor(){n(this,"listeners",new Set)}add(e){return this.listeners.add(e),this}remove(e){return this.listeners.delete(e),this}emit(e){for(const t of this.listeners.values())t(e)}clear(){this.listeners.clear()}}class m{constructor(e){n(this,"queue",[]);n(this,"dispatchers",new Map);n(this,"queued");this.queued=(e==null?void 0:e.queued)||!1}tick(){this.queue.splice(0,this.queue.length).forEach(e=>this.process(e))}on(e,t){let s=this.dispatchers.get(e);return s===void 0&&(s=new y,this.dispatchers.set(e,s)),s.add(t),{unsubscribe:()=>this.unsubscribe(e,t)}}unsubscribe(e,t){const s=this.dispatchers.get(e);s!==void 0&&s.remove(t)}emit(e){this.queued?this.queue.push(e):this.process(e)}reset(){this.dispatchers.clear(),this.queue=[]}process(e){const t=this.dispatchers.get(e.topic);t!==void 0&&t.emit(e)}}class g{constructor(){n(this,"id",h());n(this,"events",new m);n(this,"initialised",!1);n(this,"space");n(this,"_componentsBitSet",new r);n(this,"_components",{})}get world(){return this.space.world}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 p){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),this.world.queryManager.onEntityComponentChange(this),this}on(e,t){return this.events.on(e,t)}emit(e){this.events.emit(e)}destroy(){this.world.spaceManager.destroyEntity(this,this.space)}}const w={ALL:"all",ANY:"any",NOT:"not"};class u{constructor(e,t){n(this,"key");n(this,"entities",[]);n(this,"onEntityAdded",new y);n(this,"onEntityRemoved",new y);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 v{constructor(e,t){n(this,"id");n(this,"entities",new Map);n(this,"events",new m);n(this,"initialised",!1);n(this,"world");this.world=e,this.id=(t==null?void 0:t.id)||h()}get create(){return{entity:e=>this.world.spaceManager.createEntity(this,e)}}emit(e){this.events.emit(e)}on(e,t){return this.events.on(e,t)}destroy(){this.world.spaceManager.destroySpace(this)}}class E{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 b{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 c=this.getQueryResults(s.bitSets);for(const M of c.values())s.entities.push(M),s.entitySet.add(M);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 c of t.instances)c.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(c=>c.componentIndex)):void 0,a.any=s?new r(s.map(c=>c.componentIndex)):void 0,a.not=i?new r(i.map(c=>c.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 C{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 I{constructor(){n(this,"objectPool",new S(()=>new g))}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 j{constructor(e){n(this,"spaces",new Map);n(this,"componentPool");n(this,"entityPool");n(this,"world");this.world=e,this.componentPool=new C,this.entityPool=new I}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 v(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.space=e,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,e.initialised=!1,t.entities.delete(e.id);for(const s of Object.values(e._components))this.removeComponentFromEntity(e,s);this.world.queryManager.onEntityRemoved(e),e.id=h(),e.events.reset(),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){t.onDestroy(),delete e._components[t._class.componentIndex],e._componentsBitSet.remove(t._class.componentIndex),t.id=h(),t.entity=void 0,this.componentPool.release(t)}initialiseComponent(e){e.onInit()}}class P{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=l(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 A{constructor(){n(this,"id",h());n(this,"initialised",!1);n(this,"time",0);n(this,"defaultSpace");n(this,"events");n(this,"spaceManager");n(this,"queryManager");n(this,"systemManager");n(this,"componentRegistry");this.events=new m,this.componentRegistry=new b(this),this.spaceManager=new j(this),this.queryManager=new Q(this),this.systemManager=new P(this),this.defaultSpace=this.create.space({id:_})}get create(){return{entity:e=>this.spaceManager.createEntity(this.defaultSpace,e),space:e=>this.spaceManager.createSpace(e),query:e=>this.queryManager.createQuery(e)}}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()}emit(e){this.events.emit(e)}on(e,t){return this.events.on(e,t)}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=p,o.Entity=g,o.Query=u,o.QueryConditionType=w,o.Space=v,o.System=E,o.WORLD_DEFAULT_SPACE_ID=_,o.World=A,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 x=Object.defineProperty;var O=(o,r,l)=>r in o?x(o,r,{enumerable:!0,configurable:!0,writable:!0,value:l}):o[r]=l;var n=(o,r,l)=>(O(o,typeof r!="symbol"?r+"":r,l),l);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 l=(d,e)=>Object.getOwnPropertyNames(d.prototype).includes(e);let f=0;const h=()=>(f++,f.toString());class y{constructor(){n(this,"id",h());n(this,"entity");n(this,"_class")}get space(){return this.entity.space}get world(){return this.entity.world}construct(...e){}onDestroy(){}onInit(){}}n(y,"componentIndex");class p{constructor(){n(this,"listeners",new Set)}add(e){return this.listeners.add(e),this}remove(e){return this.listeners.delete(e),this}emit(e){for(const t of this.listeners.values())t(e)}clear(){this.listeners.clear()}}class m{constructor(e){n(this,"queue",[]);n(this,"dispatchers",new Map);n(this,"queued");this.queued=(e==null?void 0:e.queued)||!1}tick(){this.queue.splice(0,this.queue.length).forEach(e=>this.process(e))}on(e,t){let s=this.dispatchers.get(e);return s===void 0&&(s=new p,this.dispatchers.set(e,s)),s.add(t),{unsubscribe:()=>this.unsubscribe(e,t)}}unsubscribe(e,t){const s=this.dispatchers.get(e);s!==void 0&&s.remove(t)}emit(e){this.queued?this.queue.push(e):this.process(e)}reset(){this.dispatchers.clear(),this.queue=[]}process(e){const t=this.dispatchers.get(e.topic);t!==void 0&&t.emit(e)}}class g{constructor(){n(this,"id",h());n(this,"events",new m);n(this,"initialised",!1);n(this,"space");n(this,"_componentsBitSet",new r);n(this,"_components",{})}get world(){return this.space.world}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),this.world.queryManager.onEntityComponentChange(this),this}on(e,t){return this.events.on(e,t)}emit(e){this.events.emit(e)}destroy(){this.world.spaceManager.destroyEntity(this,this.space)}}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 v{constructor(e,t){n(this,"id");n(this,"entities",new Map);n(this,"events",new m);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()}emit(e){this.events.emit(e)}on(e,t){return this.events.on(e,t)}destroy(){this.world.spaceManager.destroySpace(this)}}class E{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 b{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 c=this.getQueryResults(s.bitSets);for(const M of c.values())s.entities.push(M),s.entitySet.add(M);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 c of t.instances)c.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(c=>c.componentIndex)):void 0,a.any=s?new r(s.map(c=>c.componentIndex)):void 0,a.not=i?new r(i.map(c=>c.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 C{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 I{constructor(){n(this,"objectPool",new S(()=>new g))}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 j{constructor(e){n(this,"spaces",new Map);n(this,"componentPool");n(this,"entityPool");n(this,"world");this.world=e,this.componentPool=new C,this.entityPool=new I}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 v(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.space=e,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,e.initialised=!1,t.entities.delete(e.id);for(const s of Object.values(e._components))this.removeComponentFromEntity(e,s);this.world.queryManager.onEntityRemoved(e),e.id=h(),e.events.reset(),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){t.onDestroy(),delete e._components[t._class.componentIndex],e._componentsBitSet.remove(t._class.componentIndex),t.id=h(),t.entity=void 0,this.componentPool.release(t)}initialiseComponent(e){e.onInit()}}class P{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=l(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 A{constructor(){n(this,"id",h());n(this,"initialised",!1);n(this,"time",0);n(this,"defaultSpace");n(this,"events");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.events=new m,this.componentRegistry=new b(this),this.spaceManager=new j(this),this.queryManager=new Q(this),this.systemManager=new P(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()}emit(e){this.events.emit(e)}on(e,t){return this.events.on(e,t)}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=g,o.Query=u,o.QueryConditionType=w,o.Space=v,o.System=E,o.WORLD_DEFAULT_SPACE_ID=_,o.World=A,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});

@@ -82,3 +82,3 @@ import type { ComponentDetails } from './component';

*/
get create(): {
create: {
/**

@@ -97,7 +97,7 @@ * Creates a new entity in the space

* Adds a handler for Space events
* @param eventName the event name
* @param topic the topic name
* @param handler the handler function
* @returns the id of the new handler
*/
on<E extends Event | Event>(eventName: string, handler: EventHandler<E>): EventSubscription;
on<E extends Event | Event>(topic: E['topic'], handler: EventHandler<E>): EventSubscription;
/**

@@ -104,0 +104,0 @@ * Destroys the space and removes it from the World

@@ -92,3 +92,3 @@ import type { ComponentClass, ComponentDetails } from './component';

*/
get create(): {
create: {
/**

@@ -133,7 +133,7 @@ * Creates a new Entity in the default World Space

* Adds a handler for World events
* @param eventName the event name
* @param topic the topic name
* @param handler the handler function
* @returns the id of the new handler
*/
on<E extends Event | Event>(eventName: string, handler: EventHandler<E>): EventSubscription;
on<E extends Event | Event>(topic: E['topic'], handler: EventHandler<E>): EventSubscription;
/**

@@ -140,0 +140,0 @@ * Retrieves entities that match a given query description.

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

"license": "MIT",
"version": "1.0.0",
"version": "1.0.1",
"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