hadean-old/src/registries/Items.ts

78 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-06-22 19:25:41 -04:00
import { Serializable } from 'frigid';
import { getTheme } from './Themes.js';
import { Renderable } from '../ui/UI.js';
export type ItemID = string;
const items = new Map<ItemID, Item>();
// ITEMS SHALL BE SINGULAR
export class Item extends Serializable {
name = '';
id: ItemID = '';
2021-06-24 17:34:05 -04:00
props: {
[propName: string]: any
};
2021-06-22 19:25:41 -04:00
setName(name) {
this.name = name;
this.register(false);
return this;
}
setId(id: ItemID) {
this.id = id;
this.register(false);
return this;
}
register(force = true) {
if((!this.id || !this.name) && !force) return;
console.log('Added item', (this.name ?? "[No Name]").padStart(20, ' '), `| (${this.id})`)
items.set(this.id, this);
return this;
}
2021-06-24 17:34:05 -04:00
setProperty(prop: ItemProperty, value: any) {
this.props[prop.name] = value;
}
getProperty(prop: ItemProperty) {
return this.props[prop.name] ?? null;
}
2021-06-22 19:25:41 -04:00
}
export class ItemState extends Serializable implements Renderable {
qty: number;
itemId: ItemID;
data: any;
get item() {
if(!items.has(this.itemId))
throw new Error('unknown item: ' + this.itemId);
return items.get(this.itemId);
}
constructor(item: Item, amount: number, data: any) {
super();
this.qty = amount;
this.itemId = item.id;
this.data = data;
}
render() {
return getTheme().normal(` ${this.item.name}{|}${this.qty} `);
}
}
2021-06-24 17:34:05 -04:00
export class ItemProperty {
name: string;
constructor(name: string) {
this.name = name;
}
}
export type ItemFilter = (itemState: ItemState) => boolean;