ecspresso
Version:
A minimal Entity-Component-System library for typescript and javascript.
5 lines (3 loc) • 1.31 kB
JavaScript
var $=((j)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(j,{get:(B,I)=>(typeof require<"u"?require:B)[I]}):j)(function(j){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+j+'" is not supported')});import{definePlugin as _}from"ecspresso";function G(j,B){return{moveTarget:{x:j,y:B}}}function O(j){return{moveSpeed:j}}function b(j){let{systemGroup:B="steering",priority:I=100,phase:U="update",arrivalThreshold:N=2}=j??{},V=N*N;return _("steering").withComponentTypes().withEventTypes().withLabels().withGroups().requires().install((W)=>{W.addSystem("move-to-target").setPriority(I).inPhase(U).inGroup(B).addQuery("movers",{with:["localTransform","moveTarget","moveSpeed"]}).setProcess(({queries:X,ecs:F,dt:Y})=>{for(let D of X.movers){let{localTransform:k,moveTarget:H,moveSpeed:Z}=D.components,J=H.x-k.x,K=H.y-k.y,Q=J*J+K*K;if(Q<=V){k.x=H.x,k.y=H.y,F.markChanged(D.id,"localTransform"),F.commands.removeComponent(D.id,"moveTarget"),F.eventBus.publish("arriveAtTarget",{entityId:D.id});continue}let L=Math.sqrt(Q),R=Math.min(Z*Y,L);k.x+=J/L*R,k.y+=K/L*R,F.markChanged(D.id,"localTransform")}})})}export{b as createSteeringPlugin,G as createMoveTarget,O as createMoveSpeed};
//# debugId=A245A409F4202B3864756E2164756E21
//# sourceMappingURL=steering.js.map