Hello,
I have implemented mechanism that mobs drops items after being killed.
static dropItem(mob: KillableMob, drop: DropProbability) {
const map = mob.getCurrentMap();
if (!map) {
return;
}
@EventData({
name: 'dropped-item',
})
class DroppedItem extends RpgEvent {
onInit() {
this.setGraphic(drop.item.graphic);
}
onAction(player: RpgPlayer) {
player.addItem(drop.item as any);
mob.getCurrentMap()?.removeEvent(this.id);
}
}
map.createDynamicEvent({
x: mob.position.x,
y: mob.position.y,
event: DroppedItem,
});
}
shield.ts
declare module '@rpgjs/server' {
export interface ArmorOptions {
graphic: string;
}
}
export function ArmorWithGraphic(options: any) {
return (target) => {
Armor(options)(target);
target.graphic = options.graphic;
};
}
@ArmorWithGraphic({
id: 'shield',
name: 'Shield',
graphic: 'shield',
})
export default class Shield extends Item {
onAdd(player: RpgPlayer) {
}
onEquip(player: RpgPlayer, equip: boolean) {
}
onRemove(player: RpgPlayer) {
}
}
item.ts
export default class Item {
graphic: string;
}
DropProbability.ts
import Item from "../item/Item";
export default interface DropProbability {
item: Item;
probability: number;
}
Currently, I extend the new class Item
in order to have the possibility to set the graphic on @Armor
.
- Is there an easier way to set up a new property (
graphic
) on Decorator?
- Is there a way to extract DroppedItem class? Currently, it is inside
dropItem
method because I need information about what item will be added to the inventory after being picked up
I checked this post: https://community.rpgjs.dev/d/2-how-do-we-create-objects-interactions-at-scale/17
But I think that syntax is outdated