excaliburjs / Excalibur

🎮 Your friendly TypeScript 2D game engine for the web 🗡️
https://excaliburjs.com
BSD 2-Clause "Simplified" License
1.82k stars 189 forks source link

New API actions.parallel #3084

Open spustlik opened 5 months ago

spustlik commented 5 months ago

Context

using paralel actions is different than using standard actions

Proposal

please consider to add new method to ActionContext probably .parallel() with inner context parameter like .repeat(), where it will be possible to use fluent Action API something like

myactor.actions
.paralel(ctx=>[
  ctx.moveTo(...),
  ctx.scaleTo(...),
]);

maybe it is possible to use generators with yield.

also usable should be shortcut for

myActor.actions.runAction(new ex.ParallelActions([ 
  new ex.MoveTo(...),
  new ex.ScaleTo(...)
])

to

myActor.actions.para(
  new ex.MoveTo(...),
  new ex.ScaleTo(...)
)
-- with para(...args:ex.Action[]) declaration
eonarheim commented 5 months ago

Hi @spustlik, I agree the current parallel actions is a bit clunky to use. I definitely think there is a better api here. Your proposal is interesting I'd like to play with it more 👍

myactor.actions
.paralel(ctx=>[
  ctx.moveTo(...),
  ctx.scaleTo(...),
]);

In the near term coroutines are a nice way to do more complicated animation simultaneously https://excaliburjs.com/docs/coroutines/

Here is an example doing some scaling on different axes at the same time

var newScaleBy = (actor: ex.Actor, scaleChange: ex.Vector, durationSeconds: number) => {
  // coroutines start automatically
  ex.coroutine(function* () {
    let duration = durationSeconds * 1000; // milliseconds
    let xScaleChangeRate = scaleChange.x / duration;
    let yScaleChangeRate = scaleChange.y / duration;
    let targetScale = actor.scale.add(scaleChange);
    while (duration > 0) {
      const elapsed = yield;
      duration -= elapsed;
      actor.scale.x += xScaleChangeRate * elapsed;
      actor.scale.y += yScaleChangeRate * elapsed;
    }
    actor.scale = targetScale;
  });
};

actor.onInitialize = () => {
  newScaleBy(actor, ex.vec(-0.5, -0.25), 2);
};
github-actions[bot] commented 3 months ago

This issue hasn't had any recent activity lately and is being marked as stale automatically.