Add input history

master
Kevin Weispfennig 2022-10-26 16:45:32 +02:00
parent 8d74237a6d
commit 9a6ce1f832
4 changed files with 65 additions and 42 deletions

View File

@ -20,11 +20,15 @@ export default class ItemBuilder {
return this; return this;
} }
withType(type) {
this.item.type = type;
}
withState(key, value) { withState(key, value) {
this.item.setState(key, value); this.item.setState(key, value);
return this; return this;
} }
isUsable(value) { isUsable(value) {
this.item.usable = value; this.item.usable = value;
return this; return this;

View File

@ -1,19 +1,19 @@
export default function DropCommand(args, context) { export default function DropCommand(args, context) {
const room = context.getRoom(context.player.currentRoom); const room = context.getRoom(context.player.currentRoom);
const items = context.player.getInventory(); const items = context.player.getInventory();
let item = null; let item = null;
for (let i of items) { for (let i of items) {
if (i.name.includes(args[1])) { if (i.name.includes(args[1])) {
item = i; item = i;
break; break;
} }
} }
if (!item) { if (!item) {
context.print(`You're not carrying a ${args[1]}`); context.print(`You're not carrying a ${args[1]}`);
} else { } else {
context.player.removeItem(item.id); context.player.removeItem(item.id);
room.addItem(item.id); room.addItem(item.id);
context.print(`You set ${item.name} down on the floor.`); context.print(`You set ${item.name} down on the floor.`);
item.onDrop(); item.onDrop();
} }
} }

View File

@ -1,24 +1,42 @@
export default class Input { export default class Input {
constructor(commandHandler, outputHandler) { constructor(commandHandler, outputHandler) {
this.handler = commandHandler; this.handler = commandHandler;
this.output = outputHandler; this.output = outputHandler;
this.echoInput = true; this.echoInput = true;
this.inputField = document.getElementById("input-area"); this.inputField = document.getElementById("input-area");
this.init(); this.history = [];
} this.historyCursor = 0;
this.init();
setEcho(value) { }
this.echoInput = value;
} setEcho(value) {
this.echoInput = value;
init() { }
this.inputField.addEventListener("keydown", (e) => {
if (e.which == 13) { init() {
const val = this.inputField.value; this.inputField.addEventListener("keydown", (e) => {
this.inputField.value = ""; if (e.key === "ArrowUp") {
if (this.echoInput) this.output.say(`> ${val}`); if (this.historyCursor > 0) {
this.handler.doCommand(val); 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;
}
} }

View File

@ -5,6 +5,7 @@ export default class Item {
this.id = "item"; this.id = "item";
this.name = "An item"; this.name = "An item";
this.description = "You see nothing special about this item"; this.description = "You see nothing special about this item";
this.type = "item";
this.state = new State(); this.state = new State();
this.usable = true; this.usable = true;
this.takeable = true; this.takeable = true;