ecspresso
Version:
A minimal Entity-Component-System library for typescript and javascript.
5 lines (3 loc) • 2.7 kB
JavaScript
var R=((j)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(j,{get:(I,F)=>(typeof require<"u"?require:I)[F]}):j)(function(j){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+j+'" is not supported')});import{definePlugin as K}from"ecspresso";function h(j,I,F,S){let V={width:j,height:I};if(F!==void 0)V.x=F;if(S!==void 0)V.y=S;return V}function T(j){return{destroyOutOfBounds:j!==void 0?{padding:j}:{}}}function b(j){return{clampToBounds:j!==void 0?{margin:j}:{}}}function w(j){return{wrapAtBounds:j!==void 0?{padding:j}:{}}}function y(j){let{systemGroup:I="physics",priority:F=50,boundsResourceKey:S="bounds",autoRemove:V=!0,phase:U="postUpdate"}=j??{};return K("bounds").withComponentTypes().withEventTypes().withResourceTypes().withLabels().withGroups().requires().install((G)=>{G.addSystem("bounds-destroy").setPriority(F).inPhase(U).inGroup(I).addQuery("entities",{with:["worldTransform","destroyOutOfBounds"]}).setProcess(({queries:D,ecs:J})=>{let C=J.getResource(S),L=C.x??0,N=C.y??0,v=L+C.width,A=N+C.height;for(let Q of D.entities){let{worldTransform:Z,destroyOutOfBounds:z}=Q.components,P=z.padding??0,k=E(Z,L,N,v,A,P);if(!k)continue;if(J.eventBus.publish("entityOutOfBounds",{entityId:Q.id,exitEdge:k}),V)J.commands.removeEntity(Q.id)}}),G.addSystem("bounds-clamp").setPriority(F-1).inPhase(U).inGroup(I).addQuery("entities",{with:["localTransform","worldTransform","clampToBounds"]}).setProcess(({queries:D,ecs:J})=>{let C=J.getResource(S),L=C.x??0,N=C.y??0,v=L+C.width,A=N+C.height;for(let Q of D.entities){let{localTransform:Z,worldTransform:z,clampToBounds:P}=Q.components,k=P.margin??0,_=L+k,$=N+k,H=v-k,O=A-k,W=0,B=0;if(z.x<_)W=_-z.x;if(z.x>H)W=H-z.x;if(z.y<$)B=$-z.y;if(z.y>O)B=O-z.y;if(W!==0||B!==0)Z.x+=W,Z.y+=B,J.markChanged(Q.id,"localTransform")}}),G.addSystem("bounds-wrap").setPriority(F-2).inPhase(U).inGroup(I).addQuery("entities",{with:["localTransform","worldTransform","wrapAtBounds"]}).setProcess(({queries:D,ecs:J})=>{let C=J.getResource(S),L=C.x??0,N=C.y??0,v=L+C.width,A=N+C.height;for(let Q of D.entities){let{localTransform:Z,worldTransform:z,wrapAtBounds:P}=Q.components,k=P.padding??0,_=0,$=0,H=v-L,O=A-N;if(z.x>v+k)_=-(H+2*k);else if(z.x<L-k)_=H+2*k;if(z.y>A+k)$=-(O+2*k);else if(z.y<N-k)$=O+2*k;if(_!==0||$!==0)Z.x+=_,Z.y+=$,J.markChanged(Q.id,"localTransform")}})})}function E(j,I,F,S,V,U){if(j.x>S+U)return"right";if(j.x<I-U)return"left";if(j.y>V+U)return"bottom";if(j.y<F-U)return"top";return null}export{w as createWrapAtBounds,T as createDestroyOutOfBounds,b as createClampToBounds,y as createBoundsPlugin,h as createBounds};
//# debugId=C4210D075EB1DBA264756E2164756E21
//# sourceMappingURL=bounds.js.map