Player takes items and lists inventory.

This commit is contained in:
2021-04-29 22:51:52 -07:00
parent e037190c7f
commit fbf841b551
2 changed files with 48 additions and 1 deletions

33
Player.ts Normal file
View File

@ -0,0 +1,33 @@
import { Item } from "./data/data.ts";
import User from "./User.ts";
export default class Player {
#items: Item[];
#user: User;
constructor(items?: Item[]) {
this.#items = items || [];
this.#user = new User();
}
drop(item: 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}`;
if (i + 1 === this.#items.length) {
anItem = `and ${anItem}`;
}
return anItem;
})
.join(", ");
await this.#user.tell(`You have ${description}.`);
}
}

16
main.ts
View File

@ -1,11 +1,13 @@
import User from "./User.ts"; import User from "./User.ts";
import Scene from "./Scene.ts"; import Scene from "./Scene.ts";
import Player from "./Player.ts";
import { hall } from "./data/data.ts"; import { hall } from "./data/data.ts";
import parseCommand from "./parseCommand.ts"; import parseCommand from "./parseCommand.ts";
async function main() { async function main() {
const user = new User(); const user = new User();
const scene = new Scene(hall); const scene = new Scene(hall);
const player = new Player();
const question = ""; const question = "";
let running = true; let running = true;
let statement = ""; let statement = "";
@ -28,7 +30,11 @@ async function main() {
break; break;
case "take": case "take":
await scene.take(target); await pickUpItem(scene, player, target);
break;
case "inventory":
await player.inventory();
break; break;
default: default:
@ -49,4 +55,12 @@ function quit(user: User): boolean {
return true; return true;
} }
async function pickUpItem(scene: Scene, player: Player, target: string) {
const item = await scene.take(target);
if (item !== null) {
player.drop(item);
}
}
main(); main();