Add loading functionality

This commit is contained in:
2021-11-05 23:22:28 +01:00
parent 6d0548455e
commit 9e95b4c202
6 changed files with 80 additions and 15 deletions

View File

@@ -6,6 +6,7 @@ import EchoCommand from "./commands/echo";
import SaveCommand from "./commands/save";
import LoadCommand from "./commands/load";
import VolumeCommand from "./commands/volume";
import InventoryCommand from "./commands/inventory";
const defaultCommands = [
[["look", "l"], LookCommand],
@@ -15,7 +16,8 @@ const defaultCommands = [
["echo", EchoCommand],
["save", SaveCommand],
["load", LoadCommand],
["volume", VolumeCommand]
["volume", VolumeCommand],
[["i", "inv", "inventory"], InventoryCommand]
];
const directionMap = [

View File

@@ -0,0 +1,15 @@
export default function InventoryCommand(args, context) {
const items = context.player.getInventory();
if (items.length < 1) return context.print(`You're not carrying anything.`);
let itemDescription = `You are carrying `;
items.forEach((item, index) => {
if (index < items.length - 2) {
itemDescription += `${item.name}, `;
} else if (index < items.length - 1) {
itemDescription += `${item.name} and `;
} else {
itemDescription += item.name
}
});
context.print(itemDescription + ".");
}

View File

@@ -11,10 +11,19 @@ export default function LookCommand(args, context) {
break;
}
}
if (!item) {
const items = context.player.getInventory();
for (let i of items) {
if (i.name.includes(args[1])) {
item = i;
break;
}
}
}
if (!item) {
context.output.say(`I could not find a ${args[1]}.`);
} else {
context.output.say(item.name);
context.output.say(`You look at ${item.name}.`);
context.output.say(item.description);
}
}

View File

@@ -7,7 +7,8 @@ import Commands from './commands';
import Serialization from './serialization';
export default class Game {
constructor() {
constructor(newGame = true) {
this.newGame = newGame;
this.player = new Player();
this.state = State;
this.rooms = [];
@@ -37,9 +38,12 @@ export default class Game {
this.commandHandler.addCommands(data.commands);
this.player = new Player();
this.player.context = this;
this.move(this.player.currentRoom);
if (this.newGame) {
this.move(this.player.currentRoom);
} else {
this.Serialization.load();
}
this.start();
this.Serialization.save();
}
advanceTick() {