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;
}
withType(type) {
this.item.type = type;
}
withState(key, value) {
this.item.setState(key, value);
return this;
}
isUsable(value) {
this.item.usable = value;
return this;

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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;