From 204e5bdad6d45b42c13cf1fd6dfadc8e3941527c Mon Sep 17 00:00:00 2001 From: Bronwen Date: Thu, 24 Jun 2021 17:34:05 -0400 Subject: [PATCH] Begin ItemProps and TaskStates --- content/core/items/Items.ts | 12 ++++++++++-- src/registries/Items.ts | 20 ++++++++++++++++++++ src/registries/Tasks.ts | 17 +++++++++++++---- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/content/core/items/Items.ts b/content/core/items/Items.ts index af6aa65..e309666 100644 --- a/content/core/items/Items.ts +++ b/content/core/items/Items.ts @@ -1,4 +1,4 @@ -import { Item } from '@items'; +import { Item, ItemFilter, ItemProperty, ItemState } from '@items'; export const LOG = new Item().setName("Log").setId('core:resources/log'); export const STICK = new Item().setName("Stick").setId('core:resources/stick'); @@ -28,4 +28,12 @@ export const OBSIDIAN_ARROWHEAD = new Item().setName("Obsidian Arrowhead").setId export const OBSIDIAN_SPEAR = new Item().setName("Obsidian Spear").setId('core:obsidian-spear'); // tools: plant fibres = rope, flint hatchet -// shale - igneous. metamorphasis => slate \ No newline at end of file +// shale - igneous. metamorphasis => slate + +export const SHARPNESS = new ItemProperty('core:sharpness'); + +export function minSharpness(min: number) { + return function(itemState: ItemState): boolean { + return itemState.item.getProperty(SHARPNESS) >= min; + } +} \ No newline at end of file diff --git a/src/registries/Items.ts b/src/registries/Items.ts index 471515d..76a764f 100644 --- a/src/registries/Items.ts +++ b/src/registries/Items.ts @@ -11,6 +11,9 @@ export class Item extends Serializable { name = ''; id: ItemID = ''; + props: { + [propName: string]: any + }; setName(name) { this.name = name; @@ -30,6 +33,14 @@ export class Item extends Serializable { items.set(this.id, this); return this; } + + setProperty(prop: ItemProperty, value: any) { + this.props[prop.name] = value; + } + + getProperty(prop: ItemProperty) { + return this.props[prop.name] ?? null; + } } export class ItemState extends Serializable implements Renderable { @@ -55,3 +66,12 @@ export class ItemState extends Serializable implements Renderable { } } +export class ItemProperty { + name: string; + + constructor(name: string) { + this.name = name; + } +} + +export type ItemFilter = (itemState: ItemState) => boolean; diff --git a/src/registries/Tasks.ts b/src/registries/Tasks.ts index 2daf0b4..cc07c36 100644 --- a/src/registries/Tasks.ts +++ b/src/registries/Tasks.ts @@ -8,10 +8,11 @@ import { progressbar, ProgressbarStyle } from '../Progressbar.js'; export const taskClasses: Map = new Map(); -export abstract class Task extends Serializable { +export class Task { progress = 0; worker: Pawn; data: any; + id: string; ctor() { this.worker ??= null; @@ -66,9 +67,17 @@ export abstract class Task extends Serializable { get status() { return chalk.bgRedBright.black('DOING A TASK'); } + + setId(id: string) { + this.id = id; + return this; + } } -export function registerTask(id: string, taskClass: typeof Task) { - console.log('Registered task', id); - taskClasses.set(id, taskClass) +export class TaskState extends Serializable { + + constructor(task: Task) { + super(); + this._taskId = task.id; + } } \ No newline at end of file