1
0
Fork 0
tsgames/src/games/text-dungeon/player.ts

48 lines
1.1 KiB
TypeScript

import { INVENTORY_X, INVENTORY_Y } from "./const";
import { getItemsCount, Item } from "./item";
import { Sprite } from "./sprite";
export class Player extends Sprite {
private inventory: Item[] = [];
addItem(item: Item) {
for (const i of this.inventory) {
if (i.id === item.id) {
i.count += item.count;
return;
}
}
this.inventory.push(new Item(
this.display,
item.id,
item.count,
INVENTORY_X + 1 + this.inventory.length,
INVENTORY_Y + 1,
));
}
hasItem(item: Item) {
for (const i of this.inventory) {
if (i.id === item.id) {
return true;
}
}
return false;
}
get foundItems() {
return this.inventory.length;
}
override doDraw() {
super.doDraw();
this.display.drawBox(INVENTORY_X, INVENTORY_Y, getItemsCount(), 1, { fill: [' '], title: 'Inv' });
this.inventory.forEach((item) => {
if (item.count > 0) {
item.doDraw();
}
});
}
}