It may take a while to explain. It is a system that can take a little time to realize
But the idea,
Solution 1
1) You can create an event
2)
import { RpgEvent, EventData, RpgPlayer } from '@rpgjs/server'
@EventData({
name: 'Monster'
})
export class CharaEvent extends RpgEvent {
onInit() {
// add graphic here
}
onAction(player: RpgPlayer) {
this.hp -= 10
if (this.hp <= 0) {
const map = player.getCurrentMap()
map.removeEvent(this.id)
}
}
}
Of course, this is the server side interaction, but it's up to you to display the animation, to put the fluidity of the attack on the client side
This solution does not do PvP combat
Solution 2
In src/modules/main/server/player.ts
:
import { RpgPlayer, RpgPlayerHooks, Control } from '@rpgjs/server'
export const player: RpgPlayerHooks = {
onInput(player: RpgPlayer, { input }) {
if (input == Control.Action) {
const otherPlayers = player.otherPlayersCollision
for (let otherPlayer of otherPlayers) {
otherPlayer.hp -= 10
if (otherPlayer.hp <= 0) {
const map = player.getCurrentMap()
map.removeEvent(otherPlayer.id)
}
}
}
}
}
It's almost the same idea, except that other players and events are involved. The disadvantage is that it takes all the events (for example a chest).
You can combine solution 1 and 2 as well. In the example above you can find out the type of player with player.type