ecspresso
Version:
A minimal Entity-Component-System library for typescript and javascript.
5 lines (3 loc) • 2.43 kB
JavaScript
var V=((j)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(j,{get:(z,x)=>(typeof require<"u"?require:z)[x]}):j)(function(j){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+j+'" is not supported')});import{definePlugin as R}from"ecspresso";function S(j,z){return{coroutine:{generator:j,onComplete:z?.onComplete}}}function*Y(j){if(j<=0)return;let z=0;while(z<j){let x=yield;z+=x}}function*Z(j){for(let z=0;z<j;z++)yield}function*_(j){while(!j())yield}function*$(...j){if(j.length===0)return;let z=j.map((x)=>{return x.next(0),{gen:x,done:!1}});while(z.some((x)=>!x.done)){let x=yield;for(let D of z){if(D.done)continue;if(D.gen.next(x).done)D.done=!0}}}function*A(...j){if(j.length===0)return;let z=j.map((x)=>{return x.next(0),{gen:x,done:!1}});try{while(!0){let x=yield;for(let D of z){if(D.done)continue;if(D.gen.next(x).done){D.done=!0;for(let K of z)if(!K.done)K.gen.return(),K.done=!0;return}}}}finally{for(let x of z)if(!x.done)x.gen.return(),x.done=!0}}function*U(j,z,x){let D=!1,H=j.subscribe(z,(K)=>{if(!x||x(K))D=!0});try{while(!D)yield}finally{H()}}function C(j,z){let x=j.getComponent(z,"coroutine");if(!x)return!1;return x.generator.return(),j.commands.removeComponent(z,"coroutine"),!0}function E(j){return{createCoroutine:S,waitForEvent:U}}function F(j){let{systemGroup:z="coroutines",priority:x=0,phase:D="update"}=j??{},H=new Set;return R("coroutines").withComponentTypes().withLabels().withGroups().install((K)=>{K.registerDispose("coroutine",({value:L,entityId:M})=>{L.generator.return(),H.delete(M)}),K.addSystem("coroutine-update").setPriority(x).inPhase(D).inGroup(z).addQuery("coroutines",{with:["coroutine"]}).setOnEntityEnter("coroutines",({entity:L})=>{L.components.coroutine.generator.next(0)}).setProcess(({queries:L,dt:M,ecs:N})=>{for(let J of L.coroutines){if(H.has(J.id)){H.delete(J.id);continue}let O=J.components.coroutine;try{if(O.generator.next(M).done)H.add(J.id),O.onComplete?.({entityId:J.id}),N.commands.removeComponent(J.id,"coroutine")}catch(Q){console.warn(`Coroutine error on entity ${J.id}:`,Q),H.add(J.id),N.commands.removeComponent(J.id,"coroutine")}}})})}export{_ as waitUntil,Y as waitSeconds,Z as waitFrames,U as waitForEvent,A as race,$ as parallel,F as createCoroutinePlugin,E as createCoroutineHelpers,S as createCoroutine,C as cancelCoroutine};
//# debugId=DF25F3CC4BAA535D64756E2164756E21
//# sourceMappingURL=coroutine.js.map