UNPKG

super-ecs

Version:
3 lines (2 loc) 4.7 kB
import{Subject as h}from"rxjs";var u=0,m=class{id;_componentAddedSubject$=new h;_componentRemovedSubject$=new h;_componentMap=new Map;constructor(){this.id=u++}hasComponent(t){return this._componentMap.has(t)}getComponent(t){let e=this._componentMap.get(t);if(typeof e<"u")return e}addComponent(t){return this._componentMap.set(t.name,t),this._componentAddedSubject$.next(t),this}removeComponent(t){let e=this._componentMap.get(t);return typeof e<"u"&&(this._componentMap.delete(t),this._componentRemovedSubject$.next(e)),this}get componentAdded$(){return this._componentAddedSubject$.asObservable()}get componentRemoved$(){return this._componentRemovedSubject$.asObservable()}};var l=class{_world;addedToWorld(t){this._world=t}removedFromWorld(t){this._world=void 0}update(t){}get world(){if(!this._world)throw Error("wait till the system is added to the world");return this._world}};import{Subject as v,takeUntil as f}from"rxjs";var d=class{entity;prev=null;next=null;constructor(t){this.entity=t}};var r=class{length=0;head=null;tail=null;_entityMap=new Map;constructor(){}add(t){let e=new d(t);this.head===null?this.head=this.tail=e:(e.prev=this.tail,this.tail&&(this.tail.next=e),this.tail=e),this.length+=1,this._entityMap.set(t.id,e)}remove(t){let e=t.id,i=this._entityMap.get(e);typeof i>"u"||(i.prev===null?this.head=i.next:i.prev.next=i.next,i.next===null?this.tail=i.prev:i.next.prev=i.prev,this.length-=1,this._entityMap.delete(e))}has(t){return this._entityMap.has(t.id)}clear(){this.head=this.tail=null,this.length=0,this._entityMap.clear()}toArray(){let t=[],e;for(e=this.head;e;e=e.next)t.push(e.entity);return t}};import{Subject as c}from"rxjs";var a=class{_componentNames;_entityAddedSubject$=new c;_entityRemovedSubject$=new c;_entities=new r;constructor(t){this._componentNames=t}getEntities(){return this._entities.toArray()}addEntityIfMatch(t){!this._entities.has(t)&&this.matchEntity(t)&&(this._entities.add(t),this._entityAddedSubject$.next(t))}removeEntity(t){this._entities.has(t)&&(this._entities.remove(t),this._entityRemovedSubject$.next(t))}onComponentAdded(t,e){this.addEntityIfMatch(t)}onComponentRemoved(t,e){if(!this._entities.has(t))return;let i=this._componentNames,n=i.length;for(let o=0;o<n;++o)i[o]===e.name&&(this._entities.remove(t),this._entityRemovedSubject$.next(t))}get entityAdded$(){return this._entityAddedSubject$.asObservable()}get entityRemoved$(){return this._entityRemovedSubject$.asObservable()}matchEntity(t){let e=this._componentNames,i=e.length;for(let n=0;n<i;++n)if(!t.hasComponent(e[n]))return!1;return!0}};var p=class{_systems=[];_entities=new r;_families=new Map;_disposeEntityMap=new Map;constructor(){}addSystem(t){return this._systems.push(t),t.addedToWorld(this),this}removeSystem(t){let e=this._systems,i=e.length;for(let n=0;n<i;++n)e[n]===t&&(e.splice(n,1),t.removedFromWorld(this));return this}removeAllSystems(){return this._systems.forEach(t=>t.removedFromWorld(this)),this._systems.length=0,this}addEntity(t){this._families.forEach(o=>o.addEntityIfMatch(t));let e=new v;this._disposeEntityMap.set(t,e);let{componentAdded$:i,componentRemoved$:n}=t;return i.pipe(f(e)).subscribe(o=>this.onComponentAdded(t,o)),n.pipe(f(e)).subscribe(o=>this.onComponentRemoved(t,o)),this._entities.add(t),this}removeEntity(t){this._families.forEach(i=>i.removeEntity(t)),this._entities.remove(t);let e=this._disposeEntityMap.get(t);return typeof e<"u"&&(e.next(),this._disposeEntityMap.delete(t)),this}removeAllEntities(){return this._entities.toArray().forEach(t=>this.removeEntity(t)),this}getEntities(t){let e=this.generateFamilyId(t);this.ensureFamilyExists(t,e);let i=this._families.get(e);if(typeof i<"u")return i.getEntities();throw Error(`unable to getEntities, ${t}`)}update(t){let e=this._systems,i=e.length;for(let n=0;n<i;++n)e[n].update(t)}entityAdded$(t){let e=this.generateFamilyId(t);this.ensureFamilyExists(t,e);let i=this._families.get(e);if(typeof i<"u")return i.entityAdded$;throw Error(`unable to perform entityAdded, ${t}`)}entityRemoved$(t){let e=this.generateFamilyId(t);this.ensureFamilyExists(t,e);let i=this._families.get(e);if(typeof i<"u")return i.entityRemoved$;throw Error(`unable to perform entityRemoved, ${t}`)}generateFamilyId(t){return`$-${t.map(i=>i.toString()).join(",")}`}ensureFamilyExists(t,e){let i=this._families;if(i.has(e))return;let n=new a([...t]);i.set(e,n);for(let o=this._entities.head;o;o=o.next){let y=i.get(e);typeof y<"u"&&y.addEntityIfMatch(o.entity)}}onComponentAdded(t,e){this._families.forEach(i=>i.onComponentAdded(t,e))}onComponentRemoved(t,e){this._families.forEach(i=>i.onComponentRemoved(t,e))}};export{m as Entity,l as System,p as World}; //# sourceMappingURL=super-ecs.min.js.map