Put random coins on the map, the player touches the coin and wins gold.
Description
gold coins are randomly placed on the map, and the hero can collect them
Structure
Files
event
import { RpgEvent, EventData, RpgPlayer } from '@rpgjs/server';
@EventData({
name: 'COIN_EVENT',
hitbox: {
width: 16,
height: 16
}
})
export default class CoinEvent extends RpgEvent {
onInit() {
this.setGraphic('coin_gold')
}
onPlayerTouch(player) {
player.gold += 1
this.remove()
}
}
map.ts
import { MapData, RpgMap } from '@rpgjs/server'
import CoinEvent from '../events/coin'
import file from './map.tmx'
@MapData({
id: './main/maps/map',
file
})
export default class MyMap extends RpgMap {
onJoin() {
function getRandomCoordinates(width: number, height: number): { x: number, y: number } {
const x = Math.floor(Math.random() * width);
const y = Math.floor(Math.random() * height);
return { x, y };
}
for (let i=0 ; i < 10 ; i++) {
this.createDynamicEvent({
...getRandomCoordinates(this.widthPx, this.heightPx),
event: CoinEvent
})
}
}
}
player.ts
import { RpgPlayer, type RpgPlayerHooks, Components } from '@rpgjs/server'
const player: RpgPlayerHooks = {
onConnected(player: RpgPlayer) {
player.setComponentsTop(Components.text('{gold} Gold'))
}
}
export default player