UNPKG

ecspresso

Version:

A minimal Entity-Component-System library for typescript and javascript.

5 lines (3 loc) 3.56 kB
var X=((j)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(j,{get:(B,D)=>(typeof require<"u"?require:B)[D]}):j)(function(j){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+j+'" is not supported')});import{definePlugin as I}from"ecspresso";function G(j,B){return j*73856093^B*19349663}function H(j){return{cellSize:j,invCellSize:1/j,cells:new Map,entries:[],_aliveGen:0,_queryGen:0}}function W(j){j._aliveGen++}function z(j,B,D,K,O,V){let _=j._aliveGen,Z=j.entries[B],$;if(Z)Z.x=D,Z.y=K,Z.halfW=O,Z.halfH=V,Z._aliveGen=_,$=Z;else $={entityId:B,x:D,y:K,halfW:O,halfH:V,_lastSeenGen:0,_aliveGen:_},j.entries[B]=$;let F=j.invCellSize,L=Math.floor((D-O)*F),T=Math.floor((D+O)*F),U=Math.floor((K-V)*F),w=Math.floor((K+V)*F);for(let J=L;J<=T;J++)for(let N=U;N<=w;N++){let A=G(J,N),M=j.cells.get(A);if(M&&M._gen===_)M.push($);else if(M)M.length=0,M._gen=_,M.push($);else{let P=[$];P._gen=_,j.cells.set(A,P)}}}function q(j,B,D,K,O,V,_=-1){let Z=j.invCellSize,$=Math.floor(B*Z),F=Math.floor(K*Z),L=Math.floor(D*Z),T=Math.floor(O*Z),U=++j._queryGen,w=j._aliveGen;for(let J=$;J<=F;J++)for(let N=L;N<=T;N++){let A=j.cells.get(G(J,N));if(!A||A._gen!==w)continue;for(let M of A){if(M.entityId<=_||M._lastSeenGen===U)continue;M._lastSeenGen=U,V.push(M.entityId)}}}function S(j,B,D,K,O){let V=K*K,_=j.invCellSize,Z=Math.floor((B-K)*_),$=Math.floor((B+K)*_),F=Math.floor((D-K)*_),L=Math.floor((D+K)*_),T=++j._queryGen,U=j._aliveGen;for(let w=Z;w<=$;w++)for(let J=F;J<=L;J++){let N=j.cells.get(G(w,J));if(!N||N._gen!==U)continue;for(let A of N){if(A._lastSeenGen===T)continue;A._lastSeenGen=T;let M=Math.max(A.x-A.halfW,Math.min(B,A.x+A.halfW)),P=Math.max(A.y-A.halfH,Math.min(D,A.y+A.halfH)),E=B-M,Q=D-P;if(E*E+Q*Q<=V)O.push(A.entityId)}}}function R(j,B){let D=j.entries[B];if(!D||D._aliveGen!==j._aliveGen)return;return D}function v(j){return{grid:j,queryRect(B,D,K,O){let V=[];return q(j,B,D,K,O,V),V},queryRectInto(B,D,K,O,V,_){q(j,B,D,K,O,V,_)},queryRadius(B,D,K){let O=[];return S(j,B,D,K,O),O},queryRadiusInto(B,D,K,O){S(j,B,D,K,O)},getEntry(B){return R(j,B)}}}function h(j){let{cellSize:B=64,systemGroup:D="spatialIndex",priority:K=2000,phases:O=["fixedUpdate","postUpdate"]}=j??{},V=H(B),_=v(V);return I("spatialIndex").withComponentTypes().withResourceTypes().withLabels().withGroups().install((Z)=>{Z.addResource("spatialIndex",_);let $=!1;for(let F of O){let L=F==="fixedUpdate"?"localTransform":"worldTransform",T=F==="postUpdate";Z.addSystem(`spatial-index-rebuild-${F}`).setPriority(K).inPhase(F).inGroup(D).addQuery("aabbOnly",{with:[L,"aabbCollider"],without:["circleCollider"]}).addQuery("circleOnly",{with:[L,"circleCollider"],without:["aabbCollider"]}).addQuery("both",{with:[L,"aabbCollider","circleCollider"]}).setProcess(({queries:U,ecs:w})=>{if(T){let J=$;if($=!1,J&&!w.entityManager.hasHierarchy)return}W(V);for(let J of U.aabbOnly){let N=J.components[L],{aabbCollider:A}=J.components,M=N.x+(A.offsetX??0),P=N.y+(A.offsetY??0);z(V,J.id,M,P,A.width/2,A.height/2)}for(let J of U.circleOnly){let N=J.components[L],{circleCollider:A}=J.components,M=N.x+(A.offsetX??0),P=N.y+(A.offsetY??0);z(V,J.id,M,P,A.radius,A.radius)}for(let J of U.both){let N=J.components[L],{aabbCollider:A,circleCollider:M}=J.components,P=N.x+(A.offsetX??0)+(M.offsetX??0),E=N.y+(A.offsetY??0)+(M.offsetY??0),Q=Math.max(A.width/2,M.radius),k=Math.max(A.height/2,M.radius);z(V,J.id,P,E,Q,k)}if(!T)$=!0})}})}export{h as createSpatialIndexPlugin}; //# debugId=31D57D221FCA9D0D64756E2164756E21 //# sourceMappingURL=spatial-index.js.map