diff --git a/src/engine/builders/item.js b/src/engine/builders/item.js index c997668..0589afc 100644 --- a/src/engine/builders/item.js +++ b/src/engine/builders/item.js @@ -20,11 +20,15 @@ export default class ItemBuilder { return this; } + withType(type) { + this.item.type = type; + } + withState(key, value) { this.item.setState(key, value); return this; } - + isUsable(value) { this.item.usable = value; return this; diff --git a/src/engine/commands/drop.js b/src/engine/commands/drop.js index dbd9604..9465bc9 100644 --- a/src/engine/commands/drop.js +++ b/src/engine/commands/drop.js @@ -1,19 +1,19 @@ -export default function DropCommand(args, context) { - const room = context.getRoom(context.player.currentRoom); - const items = context.player.getInventory(); - let item = null; - for (let i of items) { - if (i.name.includes(args[1])) { - item = i; - break; - } - } - if (!item) { - context.print(`You're not carrying a ${args[1]}`); - } else { - context.player.removeItem(item.id); - room.addItem(item.id); - context.print(`You set ${item.name} down on the floor.`); - item.onDrop(); - } +export default function DropCommand(args, context) { + const room = context.getRoom(context.player.currentRoom); + const items = context.player.getInventory(); + let item = null; + for (let i of items) { + if (i.name.includes(args[1])) { + item = i; + break; + } + } + if (!item) { + context.print(`You're not carrying a ${args[1]}`); + } else { + context.player.removeItem(item.id); + room.addItem(item.id); + context.print(`You set ${item.name} down on the floor.`); + item.onDrop(); + } } \ No newline at end of file diff --git a/src/engine/input.js b/src/engine/input.js index 4574bc1..867d1f4 100644 --- a/src/engine/input.js +++ b/src/engine/input.js @@ -1,24 +1,42 @@ -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 = ""; - if (this.echoInput) this.output.say(`> ${val}`); - this.handler.doCommand(val); - } - }) - } +export default class Input { + constructor(commandHandler, outputHandler) { + this.handler = commandHandler; + this.output = outputHandler; + this.echoInput = true; + this.inputField = document.getElementById("input-area"); + this.history = []; + this.historyCursor = 0; + this.init(); + } + + setEcho(value) { + this.echoInput = value; + } + + init() { + this.inputField.addEventListener("keydown", (e) => { + if (e.key === "ArrowUp") { + if (this.historyCursor > 0) { + this.historyCursor--; + this.inputField.value = this.history[this.historyCursor]; + } + } else if (e.key === "ArrowDown") { + if (this.historyCursor < this.history.length) { + this.historyCursor++; + this.inputField.value = this.history[this.historyCursor] ?? ""; + } + } else if (e.key === "Enter") { + this.addToInputHistory(this.inputField.value); + const val = this.inputField.value; + this.inputField.value = ""; + if (this.echoInput) this.output.say(`> ${val}`); + this.handler.doCommand(val); + } + }) + } + + addToInputHistory(str) { + this.history.push(str); + this.historyCursor = this.history.length; + } } \ No newline at end of file diff --git a/src/engine/item.js b/src/engine/item.js index 46f4393..a67301a 100644 --- a/src/engine/item.js +++ b/src/engine/item.js @@ -5,6 +5,7 @@ export default class Item { this.id = "item"; this.name = "An item"; this.description = "You see nothing special about this item"; + this.type = "item"; this.state = new State(); this.usable = true; this.takeable = true;