Hello,
After mob dies I create new RPGEvent with animation of vaporization like this:
onDead() {
this.dead = true;
this.stopMoveTo();
this.breakRoutes(true)
MobDestroyer.destroy(this);
process.nextTick(() => {
this.getCurrentMap()?.removeEvent(this.id);
DeathAnimation.play({
map: this.getCurrentMap(),
position: this.position,
graphic: this.deathGraphic
});
})
}
DeathAnimation.ts
import { EventData, RpgEvent, RpgMap } from "@rpgjs/server";
import { PositionXY } from "@rpgjs/types";
interface DeathAnimationPlayer {
map?: RpgMap | null;
position: PositionXY;
graphic: string;
}
export default class DeathAnimation {
static async play({ map, position, graphic }: DeathAnimationPlayer) {
try {
if (!map) {
return;
}
/** @ts-ignore */
@EventData({
name: 'death-animation',
hitbox: {
width: 64,
height: 64,
}
})
class DeathAnimationEvent extends RpgEvent {
onInit() {
this.setGraphic(graphic);
this.through = true;
this.throughOtherPlayer = true;
}
}
const events = Object.values(map.createDynamicEvent({
x: position.x,
y: position.y,
event: DeathAnimationEvent,
})) as RpgEvent[];
const event = events[0];
setTimeout(() => {
event.showAnimation(graphic, 'death');
setTimeout(() => {
map.removeEvent(event.id);
}, 700);
}, 75);
} catch (e) {
console.error('Failed to play death animation', e);
return;
}
}
}
I had to add setTimeout for 75 miliseconds until calling showAnimation because without that animation was not played.
I tried to createDynamicEvent with await
but it does not have impact, because createDynamicEvent
is not async.
So there is a delay between creating an event and the possibility to call animation for it.
Furthermore, I think 75ms is still not enough because sometimes the animation is not called.
- Is there a proper workaround for this? Is it a bug?
- Is there a possibility to remove the event just after finishing animation? I used setTimeout(..., 700); and in callback, I remove the event. But I am curious if there is a better way for this.
Video as always: https://imgur.com/z2qdz21
Thanks in advance.