From 8d854945e1a2b71070eda3dafe29662b08c044e8 Mon Sep 17 00:00:00 2001 From: nolwn Date: Sun, 2 May 2021 14:44:59 -0700 Subject: [PATCH] Created Container parent class. Introduced soem fancy bugs. --- Container.ts | 26 ++++++++++++++++++++++++++ Player.ts | 25 +++++++------------------ Scene.ts | 18 ++++++++---------- data/data.ts | 1 - data/rooms.ts | 2 +- main.ts | 2 +- 6 files changed, 43 insertions(+), 31 deletions(-) create mode 100644 Container.ts diff --git a/Container.ts b/Container.ts new file mode 100644 index 0000000..ed50609 --- /dev/null +++ b/Container.ts @@ -0,0 +1,26 @@ +import type { Item } from "./data/data.ts"; + +export default class Container { + protected items: Item[]; + + constructor(items: Item[]) { + this.items = items; + } + + description(items: Item[]): string { + const vowels = ["a", "e", "i", "o", "u"]; + const description = items + .map(({ name }, i) => { + let anItem = `${vowels.includes(name[0]) ? "an" : "a"} ${name}`; + + if (i + 1 === items.length) { + anItem = `and ${anItem}`; + } + + return anItem; + }) + .join(", "); + + return description; + } +} diff --git a/Player.ts b/Player.ts index ef601d8..408162c 100644 --- a/Player.ts +++ b/Player.ts @@ -1,33 +1,22 @@ import { Item } from "./data/data.ts"; +import Container from "./Container.ts"; import User from "./User.ts"; -export default class Player { - #items: Item[]; +export default class Player extends Container { #user: User; constructor(items?: Item[]) { - this.#items = items || []; + super(items || []); this.#user = new User(); } drop(item: Item) { - this.#items.push(item); + this.items.push(item); } - async inventory() { - const vowels = ["a", "e", "i", "o", "u"]; - const description = this.#items - .map(({ name }, i) => { - let anItem = `${vowels.includes(name[0]) ? "an" : "a"} ${name}`; + look(): void { + const description = super.description(this.items); - if (i + 1 === this.#items.length) { - anItem = `and ${anItem}`; - } - - return anItem; - }) - .join(", "); - - await this.#user.tell(`You have ${description}.`); + this.#user.tell(`You have ${description}`); } } diff --git a/Scene.ts b/Scene.ts index d7f3ecc..7860177 100644 --- a/Scene.ts +++ b/Scene.ts @@ -1,29 +1,27 @@ import { GameData, Item } from "./data/data.ts"; import User from "./User.ts"; +import Container from "./Container.ts"; -export default class Scene { - #items: Item[]; +export default class Scene extends Container { #user: User; constructor(gameData: GameData) { - this.#items = gameData.items; + super(gameData.items); this.#user = new User(); } async look() { - const description = this.#items - .map(({ description }) => description) - .join(" "); + const description = super.description(this.items); - await this.#user.tell(description); + await this.#user.tell(`There is ${description}`); } async take(target: string): Promise { - const idx = this.#items.findIndex(({ name }) => name === target); + const idx = this.items.findIndex(({ name }) => name === target); if (idx >= 0) { - const item = this.#items[idx]; - this.#items.splice(idx, 1); + const item = this.items[idx]; + this.items.splice(idx, 1); await this.#user.tell("Taken."); diff --git a/data/data.ts b/data/data.ts index 5e1be6b..7674d32 100644 --- a/data/data.ts +++ b/data/data.ts @@ -2,7 +2,6 @@ export * from "./rooms.ts"; export interface Item { name: string; - description: string; } export interface GameData { diff --git a/data/rooms.ts b/data/rooms.ts index 724301b..a21b0a4 100644 --- a/data/rooms.ts +++ b/data/rooms.ts @@ -1,3 +1,3 @@ export const hall = { - items: [{ name: "flashlight", description: "There is a flashlight." }], + items: [{ name: "flashlight" }], }; diff --git a/main.ts b/main.ts index 1612b1a..c8aff68 100644 --- a/main.ts +++ b/main.ts @@ -34,7 +34,7 @@ async function main() { break; case "inventory": - await player.inventory(); + await player.look(); break; default: