From 9e95b4c20271a77db42613d9806d013f0173adb4 Mon Sep 17 00:00:00 2001 From: Talon Date: Fri, 5 Nov 2021 23:22:28 +0100 Subject: [PATCH] Add loading functionality --- src/engine/commands.js | 4 +++- src/engine/commands/inventory.js | 15 ++++++++++++ src/engine/commands/look.js | 11 ++++++++- src/engine/index.js | 10 +++++--- src/game/index.html | 16 +++++++++++-- src/game/index.js | 39 +++++++++++++++++++++++++------- 6 files changed, 80 insertions(+), 15 deletions(-) create mode 100644 src/engine/commands/inventory.js 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 @@

Assassin bug

-
- + + + +
+

Welcome

+ +
\ No newline at end of file diff --git a/src/game/index.js b/src/game/index.js index 6a3dd9c..976972f 100644 --- a/src/game/index.js +++ b/src/game/index.js @@ -3,12 +3,35 @@ import Rooms from './rooms'; import Items from './items'; import MeowCommand from './commands/meow'; -const game = new Game(); +if (localStorage.getItem("save")) { + document.getElementById("save-game-found").hidden = false; + document.getElementById("before-play").hidden = true; + document.getElementById("load-save-game").addEventListener("click", () => { + document.getElementById("save-game-found").hidden = true; + document.getElementById("play-area").hidden = false; + startGame(false); + }) + document.getElementById("start-new-game").addEventListener("click", () => { + document.getElementById("save-game-found").hidden = true; + document.getElementById("play-area").hidden = false; + startGame(true); + }) +} -game.init({ - rooms: Rooms, - commands: [ - [["meow", "mew"], MeowCommand] - ], - items: Items -}); \ No newline at end of file +document.getElementById("begin").addEventListener("click", () => { + document.getElementById("before-play").hidden = true; + document.getElementById("play-area").hidden = false; + startGame(true); +}) + +function startGame(newGame) { + const game = new Game(newGame); + + game.init({ + rooms: Rooms, + commands: [ + [["meow", "mew"], MeowCommand] + ], + items: Items + }); +}