From a968f89bb3181212b3fafeae358e04207c6dccd8 Mon Sep 17 00:00:00 2001 From: Talon Date: Thu, 4 Nov 2021 22:19:50 +0100 Subject: [PATCH] Add command enable/disable --- src/engine/commands.js | 31 +++++++++++++++++++++++++++++-- src/engine/commands/look.js | 2 +- src/engine/index.js | 4 ++++ src/game/rooms/start.js | 2 ++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/engine/commands.js b/src/engine/commands.js index 0f23e32..8a26e51 100644 --- a/src/engine/commands.js +++ b/src/engine/commands.js @@ -5,21 +5,42 @@ const defaultCommands = [ [["use", "interact"], UseCommand] ]; +const directionMap = [ + ["n", "north"], + ["ne", "northeast"], + ["e", "east"], + ["se", "southeast"], + ["s", "south"], + ["sw", "southwest"], + ["w", "west"], + ["nw", "northwest"], + ["u", "up"], + ["d", "down"] +]; + export default class Commands { constructor(context, commands) { this.context = context; this.commands = commands || new Map(); + this.enabled = true; this.addDefaultCommands(); } doCommand(str) { + if (!this.enabled) { + this.context.print(`You can't seem to do anything at the moment.`); + return; + } const room = this.context.getRoom(this.context.player.currentRoom); const split = str.split(" "); if (this.commands.get(split[0])) { this.commands.get(split[0])(split, this.context); } - if (room.getExit(split[0])) { - this.context.move(room.getExit(split[0])); + + const direction = this.matchDirection(split[0]); + + if (room.getExit(direction)) { + this.context.move(room.getExit(direction)); } } @@ -40,4 +61,10 @@ export default class Commands { addDefaultCommands() { this.addCommands(defaultCommands); } + + matchDirection(str) { + for (let dir of directionMap) { + if (dir[0] == str) return dir[1]; + } + } } \ No newline at end of file diff --git a/src/engine/commands/look.js b/src/engine/commands/look.js index 9c0a0bc..70e4519 100644 --- a/src/engine/commands/look.js +++ b/src/engine/commands/look.js @@ -1,5 +1,5 @@ export default function LookCommand(args, context) { - if (args.length == 0) { + if (args.length == 1) { context.examineRoom(); } else { const room = context.getRoom(context.player.currentRoom); diff --git a/src/engine/index.js b/src/engine/index.js index b8d28f5..97cc660 100644 --- a/src/engine/index.js +++ b/src/engine/index.js @@ -89,4 +89,8 @@ export default class Game { this.visitedRooms.set(roomID, true); } } + + enableCommandInput(value) { + this.commandHandler.enabled = value; + } } \ No newline at end of file diff --git a/src/game/rooms/start.js b/src/game/rooms/start.js index 1e5b17e..6682aef 100644 --- a/src/game/rooms/start.js +++ b/src/game/rooms/start.js @@ -8,11 +8,13 @@ export default new RoomBuilder() .withExit("north", "tunnel_1") .withEnterCallback(async function(context) { const { output, wait } = context; + context.enableCommandInput(false); output.say("You slowly wake up"); await wait(5000); output.say("It's strange. You never used to be able to be conscious about the fact that you were waking up."); await wait(5000); output.say("Yet here we are."); + context.enableCommandInput(true); }) .withItem("stone") .create(); \ No newline at end of file