Hello,
I have problems with removing event from map.
I have implemented something like this:
import { RpgMap, RpgPlayer } from "@rpgjs/server";
export default class TakeDamage {
static onHit(victim: RpgPlayer, attacker: RpgPlayer) {
var dmg = 50;
victim.hp -= dmg;
if (victim.hp <= 0) {
const map = victim.getCurrentMap() as RpgMap;
victim.breakRoutes(true)
map.removeEvent(victim.id);
}
}
}
It is called after detecting collission with physical attack:
this.player.getCurrentMap()?.createMovingHitbox(
[
{ x: this.getPositionX(), y: this.getPositionY(), width: HITBOX.x, height: HITBOX.y },
]
).subscribe({
next(hitbox) {
var otherPlayersCollision = that.excludePlayer(hitbox.otherPlayersCollision as RpgPlayer[]);
otherPlayersCollision.forEach((player: RpgPlayer) => {
FollowPlayer.onHit(player, that.player);
TakeDamage.onHit(player, that.player);
})
},
})
The issue only when I trigger in other class FollowPlayer::onHit
:
victim.breakRoutes(true);
victim.moveTo(attacker).subscribe();
It is triggered after every single attack. Maybe I should detect if moveTo is already called?
Here is how it looks (the event has the last possible hp which is 10 and I can go through it)
Thank you in advance!
EDIT: I also tried to add victim.stopMoveTo();
Furthermore if I add setTimeout to removing event:
setTimeout(() => {
const map = victim.getCurrentMap() as RpgMap;
map.removeEvent(victim.id);
}, 1000)
Then the event is properly removed after 1 second, so I suppose something blocks it from removal, but I do not know what 🙁