Created Container parent class. Introduced soem fancy bugs.

This commit is contained in:
2021-05-02 14:44:59 -07:00
parent fbf841b551
commit 8d854945e1
6 changed files with 43 additions and 31 deletions

26
Container.ts Normal file
View File

@ -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;
}
}

View File

@ -1,33 +1,22 @@
import { Item } from "./data/data.ts"; import { Item } from "./data/data.ts";
import Container from "./Container.ts";
import User from "./User.ts"; import User from "./User.ts";
export default class Player { export default class Player extends Container {
#items: Item[];
#user: User; #user: User;
constructor(items?: Item[]) { constructor(items?: Item[]) {
this.#items = items || []; super(items || []);
this.#user = new User(); this.#user = new User();
} }
drop(item: Item) { drop(item: Item) {
this.#items.push(item); this.items.push(item);
} }
async inventory() { look(): void {
const vowels = ["a", "e", "i", "o", "u"]; const description = super.description(this.items);
const description = this.#items
.map(({ name }, i) => {
let anItem = `${vowels.includes(name[0]) ? "an" : "a"} ${name}`;
if (i + 1 === this.#items.length) { this.#user.tell(`You have ${description}`);
anItem = `and ${anItem}`;
}
return anItem;
})
.join(", ");
await this.#user.tell(`You have ${description}.`);
} }
} }

View File

@ -1,29 +1,27 @@
import { GameData, Item } from "./data/data.ts"; import { GameData, Item } from "./data/data.ts";
import User from "./User.ts"; import User from "./User.ts";
import Container from "./Container.ts";
export default class Scene { export default class Scene extends Container {
#items: Item[];
#user: User; #user: User;
constructor(gameData: GameData) { constructor(gameData: GameData) {
this.#items = gameData.items; super(gameData.items);
this.#user = new User(); this.#user = new User();
} }
async look() { async look() {
const description = this.#items const description = super.description(this.items);
.map(({ description }) => description)
.join(" ");
await this.#user.tell(description); await this.#user.tell(`There is ${description}`);
} }
async take(target: string): Promise<Item | null> { async take(target: string): Promise<Item | null> {
const idx = this.#items.findIndex(({ name }) => name === target); const idx = this.items.findIndex(({ name }) => name === target);
if (idx >= 0) { if (idx >= 0) {
const item = this.#items[idx]; const item = this.items[idx];
this.#items.splice(idx, 1); this.items.splice(idx, 1);
await this.#user.tell("Taken."); await this.#user.tell("Taken.");

View File

@ -2,7 +2,6 @@ export * from "./rooms.ts";
export interface Item { export interface Item {
name: string; name: string;
description: string;
} }
export interface GameData { export interface GameData {

View File

@ -1,3 +1,3 @@
export const hall = { export const hall = {
items: [{ name: "flashlight", description: "There is a flashlight." }], items: [{ name: "flashlight" }],
}; };

View File

@ -34,7 +34,7 @@ async function main() {
break; break;
case "inventory": case "inventory":
await player.inventory(); await player.look();
break; break;
default: default: