diff --git a/Scene.ts b/Scene.ts index d06e734..d7f3ecc 100644 --- a/Scene.ts +++ b/Scene.ts @@ -1,18 +1,31 @@ import { GameData, Item } from "./data/data.ts"; +import User from "./User.ts"; export default class Scene { #items: Item[]; + #user: User; constructor(gameData: GameData) { this.#items = gameData.items; + this.#user = new User(); } - take(target: string): Item | null { + async look() { + const description = this.#items + .map(({ description }) => description) + .join(" "); + + await this.#user.tell(description); + } + + async take(target: string): Promise { const idx = this.#items.findIndex(({ name }) => name === target); if (idx >= 0) { const item = this.#items[idx]; - this.#items = this.#items.splice(idx, 1); + this.#items.splice(idx, 1); + + await this.#user.tell("Taken."); return item; } diff --git a/User.ts b/User.ts new file mode 100644 index 0000000..cf2930a --- /dev/null +++ b/User.ts @@ -0,0 +1,28 @@ +const DEFAULT_PROMPT = ">"; + +export default class User { + #prompt: string; + #out: (text: string) => Promise; + + constructor() { + this.#prompt = DEFAULT_PROMPT; + this.#out = async (text: string) => { + const data = new TextEncoder().encode(text); + await Deno.stdout.write(data); + }; + } + + ask(question: string): string { + const answer = prompt(`${question}\n${this.#prompt} `); + + if (!answer) { + return ""; + } + + return answer; + } + + async tell(statement: string): Promise { + await this.#out(`\n${statement}\n`); + } +} diff --git a/console.ts b/console.ts deleted file mode 100644 index 904cc14..0000000 --- a/console.ts +++ /dev/null @@ -1,19 +0,0 @@ -const DEFAULT_PROMPT = ">"; - -export default class Console { - #prompt: string; - - constructor() { - this.#prompt = DEFAULT_PROMPT; - } - - ask(question: string): string { - const answer = prompt(`${question}\n${this.#prompt} `); - - if (!answer) { - return ""; - } - - return answer; - } -} diff --git a/main.ts b/main.ts index 2898bd8..7a92255 100644 --- a/main.ts +++ b/main.ts @@ -1,26 +1,34 @@ -import Console from "./console.ts"; +import User from "./User.ts"; +import Scene from "./Scene.ts"; +import { hall } from "./data/data.ts"; import parseCommand from "./parseCommand.ts"; async function main() { - const c = new Console(); - const question = "Are you a butthole?"; + const user = new User(); + const scene = new Scene(hall); + const question = ""; let running = true; let statement = ""; while (running) { const prompts = `${statement}${question}`; - const answer = await c.ask(prompts); + const answer = await user.ask(prompts); - const { action } = parseCommand(answer); + const { action, target } = parseCommand(answer); statement = ""; switch (action) { case "quit": - running = quit(c); + running = quit(user); + break; + + case "look": + await scene.look(); break; case "take": + await scene.take(target); break; default: @@ -31,8 +39,8 @@ async function main() { } } -function quit(c: Console): boolean { - const confirmQuit = c.ask("Are you sure you want to quit?"); +function quit(user: User): boolean { + const confirmQuit = user.ask("Are you sure you want to quit?\n"); if (confirmQuit[0] === "y") { return false;