Skip to content

Instantly share code, notes, and snippets.

@MattRoelle
Created April 7, 2022 23:15
Show Gist options
  • Save MattRoelle/9347d6beb73140977020b095383846ae to your computer and use it in GitHub Desktop.
Save MattRoelle/9347d6beb73140977020b095383846ae to your computer and use it in GitHub Desktop.
import { GRID_TILE_HEIGHT, GRID_TILE_WIDTH } from "../../constants";
import { addxy, left, midpoint, xy } from "../../utils";
import { BattleActorObject } from "../BattleActorObject";
import { damageNumbers } from "../effects/damageNumbers";
import { Sequence } from "./sequence";
export const respawnSequence: Sequence<{
actor: BattleActorObject,
x: number,
y: number
}> = function* (scene, input) {
input.actor.playWalkAnim();
const s = Math.abs(input.actor.spr.scaleX || 1)
input.actor.spr.scale = 5;
input.actor.container.x = input.x * GRID_TILE_WIDTH;
input.actor.container.y = input.y * GRID_TILE_HEIGHT;
yield {
type: 'tweens',
tweens: [
{
targets: [input.actor.container],
alpha: 1,
duration: 1000,
ease: Phaser.Math.Easing.Quadratic.InOut
},
{
targets: [input.actor.spr],
scale: s,
duration: 2000,
ease: Phaser.Math.Easing.Quadratic.InOut
},
]
}
input.actor.playIdleAnim();
}
export type SequenceResult = {
type: 'wait',
duration: number
} | {
type: 'tween',
tween: Phaser.Types.Tweens.TweenBuilderConfig
} | {
type: 'tweens',
tweens: Phaser.Types.Tweens.TweenBuilderConfig[]
} | {
type: 'delayed-call',
duration: number,
fn: () => void
};
export type SequenceGenerator = Generator<SequenceResult, void, unknown>;
export type Sequence<TParams> = (scene: Phaser.Scene, input: TParams) => SequenceGenerator;
function processSequenceResult(scene: Phaser.Scene, result: SequenceResult, onDone: () => void) {
switch (result.type) {
case 'delayed-call':
scene.time.delayedCall(result.duration, result.fn);
onDone();
break;
case 'wait':
scene.time.delayedCall(result.duration, onDone);
break;
case 'tween':
scene.tweens.add({
...result.tween,
onComplete: onDone
});
break;
case 'tweens':
let numDone = 0;
const completeTween = () => {
numDone++;
if (result.tweens.length <= numDone) {
onDone();
}
}
for (let t of result.tweens) {
scene.tweens.add({
...t,
onComplete: completeTween
});
}
break;
}
}
export async function playSequence(scene: Phaser.Scene, sequence: SequenceGenerator) {
return new Promise<void>((resolve, reject) => {
function next() {
const result = sequence.next();
if (result && result.value && !result.done) {
processSequenceResult(scene, result.value, next);
} else {
resolve();
}
}
next();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment