diff --git a/src/engine/commands.js b/src/engine/commands.js index e8212c4..25d5ce4 100644 --- a/src/engine/commands.js +++ b/src/engine/commands.js @@ -2,12 +2,14 @@ import LookCommand from "./commands/look"; import UseCommand from "./commands/use"; import TakeCommand from "./commands/take"; import DropCommand from "./commands/drop"; +import EchoCommand from "./commands/echo"; const defaultCommands = [ [["look", "l"], LookCommand], [["use", "interact"], UseCommand], [["take", "get"], TakeCommand], - [["drop", "put"], DropCommand] + [["drop", "put"], DropCommand], + ["echo", EchoCommand] ]; const directionMap = [ diff --git a/src/engine/commands/echo.js b/src/engine/commands/echo.js new file mode 100644 index 0000000..bd8f6d5 --- /dev/null +++ b/src/engine/commands/echo.js @@ -0,0 +1,8 @@ +export default function EchoCommand(args, context) { + if (args[1] != "on" && args[1] != "off") { + context.print(`Usage: echo `); + } else { + context.setInputEcho(args[1] == "on" ? true : false); + context.print(`Command echo is now ${args[1]}`); + } +} \ No newline at end of file diff --git a/src/engine/index.js b/src/engine/index.js index 2733cfa..8ee4af8 100644 --- a/src/engine/index.js +++ b/src/engine/index.js @@ -94,4 +94,8 @@ export default class Game { enableCommandInput(value) { this.commandHandler.enabled = value; } + + setInputEcho(value) { + this.input.setEcho(value); + } } \ No newline at end of file diff --git a/src/engine/input.js b/src/engine/input.js index 0596e2e..4574bc1 100644 --- a/src/engine/input.js +++ b/src/engine/input.js @@ -2,16 +2,21 @@ export default class Input { constructor(commandHandler, outputHandler) { this.handler = commandHandler; this.output = outputHandler; + this.echoInput = true; this.inputField = document.getElementById("input-area"); this.init(); } + setEcho(value) { + this.echoInput = value; + } + init() { this.inputField.addEventListener("keydown", (e) => { if (e.which == 13) { const val = this.inputField.value; this.inputField.value = ""; - this.output.say(`> ${val}`); + if (this.echoInput) this.output.say(`> ${val}`); this.handler.doCommand(val); } })