ecspresso
Version:
A minimal Entity-Component-System library for typescript and javascript.
5 lines (3 loc) • 3.62 kB
JavaScript
var f=((J)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(J,{get:(N,K)=>(typeof require<"u"?require:N)[K]}):J)(function(J){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+J+'" is not supported')});import{definePlugin as z}from"ecspresso";function R(J,N,K){return J*73856093^N*19349663^K*83492791}function b(J){return{cellSize:J,invCellSize:1/J,cells:new Map,entries:[],_aliveGen:0,_queryGen:0}}function p(J){J._aliveGen++}function k(J,N,K,j,O,$,U,A){let M=J._aliveGen,V=J.entries[N],B;if(V)V.x=K,V.y=j,V.z=O,V.halfW=$,V.halfH=U,V.halfD=A,V._aliveGen=M,B=V;else B={entityId:N,x:K,y:j,z:O,halfW:$,halfH:U,halfD:A,_lastSeenGen:0,_aliveGen:M},J.entries[N]=B;let P=J.invCellSize,q=Math.floor((K-$)*P),W=Math.floor((K+$)*P),L=Math.floor((j-U)*P),Q=Math.floor((j+U)*P),F=Math.floor((O-A)*P),T=Math.floor((O+A)*P);for(let G=q;G<=W;G++)for(let w=L;w<=Q;w++)for(let _=F;_<=T;_++){let S=R(G,w,_),E=J.cells.get(S);if(E&&E._gen===M)E.push(B);else if(E)E.length=0,E._gen=M,E.push(B);else{let H=[B];H._gen=M,J.cells.set(S,H)}}}function I(J,N,K,j,O,$,U,A,M=-1){let V=J.invCellSize,B=Math.floor(N*V),P=Math.floor(O*V),q=Math.floor(K*V),W=Math.floor($*V),L=Math.floor(j*V),Q=Math.floor(U*V),F=++J._queryGen,T=J._aliveGen;for(let G=B;G<=P;G++)for(let w=q;w<=W;w++)for(let _=L;_<=Q;_++){let S=J.cells.get(R(G,w,_));if(!S||S._gen!==T)continue;for(let E of S){if(E.entityId<=M||E._lastSeenGen===F)continue;E._lastSeenGen=F,A.push(E.entityId)}}}function X(J,N,K,j,O,$){let U=O*O,A=J.invCellSize,M=Math.floor((N-O)*A),V=Math.floor((N+O)*A),B=Math.floor((K-O)*A),P=Math.floor((K+O)*A),q=Math.floor((j-O)*A),W=Math.floor((j+O)*A),L=++J._queryGen,Q=J._aliveGen;for(let F=M;F<=V;F++)for(let T=B;T<=P;T++)for(let G=q;G<=W;G++){let w=J.cells.get(R(F,T,G));if(!w||w._gen!==Q)continue;for(let _ of w){if(_._lastSeenGen===L)continue;_._lastSeenGen=L;let S=Math.max(_.x-_.halfW,Math.min(N,_.x+_.halfW)),E=Math.max(_.y-_.halfH,Math.min(K,_.y+_.halfH)),H=Math.max(_.z-_.halfD,Math.min(j,_.z+_.halfD)),Y=N-S,Z=K-E,v=j-H;if(Y*Y+Z*Z+v*v<=U)$.push(_.entityId)}}}function D(J,N){let K=J.entries[N];if(!K||K._aliveGen!==J._aliveGen)return;return K}function C(J){return{grid:J,queryBox(N,K,j,O,$,U){let A=[];return I(J,N,K,j,O,$,U,A),A},queryBoxInto(N,K,j,O,$,U,A,M){I(J,N,K,j,O,$,U,A,M)},queryRadius(N,K,j,O){let $=[];return X(J,N,K,j,O,$),$},queryRadiusInto(N,K,j,O,$){X(J,N,K,j,O,$)},getEntry(N){return D(J,N)}}}function x(J){let{cellSize:N=64,systemGroup:K="spatialIndex3D",priority:j=2000,phases:O=["fixedUpdate","postUpdate"]}=J??{},$=b(N),U=C($);return z("spatialIndex3D").withComponentTypes().withResourceTypes().withLabels().withGroups().install((A)=>{A.addResource("spatialIndex3D",U);let M=!1;for(let V of O){let B=V==="fixedUpdate"?"localTransform3D":"worldTransform3D",P=V==="postUpdate";A.addSystem(`spatial-index3D-rebuild-${V}`).setPriority(j).inPhase(V).inGroup(K).addQuery("aabbWith",{with:[B,"aabb3DCollider"]}).addQuery("sphereOnly",{with:[B,"sphereCollider"],without:["aabb3DCollider"]}).runWhenEmpty().setProcess(({queries:q,ecs:W})=>{if(P){let L=M;if(M=!1,L&&!W.entityManager.hasHierarchy)return}p($);for(let L of q.aabbWith){let Q=L.components[B],{aabb3DCollider:F}=L.components;k($,L.id,Q.x+(F.offsetX??0),Q.y+(F.offsetY??0),Q.z+(F.offsetZ??0),F.width/2,F.height/2,F.depth/2)}for(let L of q.sphereOnly){let Q=L.components[B],{sphereCollider:F}=L.components,T=F.radius;k($,L.id,Q.x+(F.offsetX??0),Q.y+(F.offsetY??0),Q.z+(F.offsetZ??0),T,T,T)}if(!P)M=!0})}})}export{x as createSpatialIndex3DPlugin};
//# debugId=C91D70E729A54E7764756E2164756E21
//# sourceMappingURL=spatial-index3D.js.map