diff --git a/src/engine/commands.js b/src/engine/commands.js index c5b86e3..ef85bc5 100644 --- a/src/engine/commands.js +++ b/src/engine/commands.js @@ -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 = [ diff --git a/src/engine/commands/inventory.js b/src/engine/commands/inventory.js new file mode 100644 index 0000000..89f2356 --- /dev/null +++ b/src/engine/commands/inventory.js @@ -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 + "."); +} \ No newline at end of file diff --git a/src/engine/commands/look.js b/src/engine/commands/look.js index 8c86620..74768a2 100644 --- a/src/engine/commands/look.js +++ b/src/engine/commands/look.js @@ -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); } } diff --git a/src/engine/index.js b/src/engine/index.js index c37e73d..d8fad20 100644 --- a/src/engine/index.js +++ b/src/engine/index.js @@ -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() { diff --git a/src/game/index.html b/src/game/index.html index 714d9e3..cee7ad7 100644 --- a/src/game/index.html +++ b/src/game/index.html @@ -4,7 +4,19 @@