Add loading functionality

master
Talon 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 SaveCommand from "./commands/save";
import LoadCommand from "./commands/load"; import LoadCommand from "./commands/load";
import VolumeCommand from "./commands/volume"; import VolumeCommand from "./commands/volume";
import InventoryCommand from "./commands/inventory";
const defaultCommands = [ const defaultCommands = [
[["look", "l"], LookCommand], [["look", "l"], LookCommand],
@ -15,7 +16,8 @@ const defaultCommands = [
["echo", EchoCommand], ["echo", EchoCommand],
["save", SaveCommand], ["save", SaveCommand],
["load", LoadCommand], ["load", LoadCommand],
["volume", VolumeCommand] ["volume", VolumeCommand],
[["i", "inv", "inventory"], InventoryCommand]
]; ];
const directionMap = [ 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; break;
} }
} }
if (!item) {
const items = context.player.getInventory();
for (let i of items) {
if (i.name.includes(args[1])) {
item = i;
break;
}
}
}
if (!item) { if (!item) {
context.output.say(`I could not find a ${args[1]}.`); context.output.say(`I could not find a ${args[1]}.`);
} else { } else {
context.output.say(item.name); context.output.say(`You look at ${item.name}.`);
context.output.say(item.description); context.output.say(item.description);
} }
} }

View File

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

View File

@ -4,7 +4,19 @@
</head> </head>
<body> <body>
<h1>Assassin bug</h1> <h1>Assassin bug</h1>
<div aria-live="polite" id="output-area"></div> <div id="play-area" hidden=true>
<input type="text" id="input-area" placeholder="Type command" /> <div aria-live="polite" id="output-area"></div>
<input type="text" id="input-area" placeholder="Type command" />
</div>
<div id="save-game-found" hidden=true>
<h1>Found a save game</h1>
<button id="load-save-game">Load</button>
<button id="start-new-game">New</button>
</div>
<div id="before-play">
<h1>Welcome</h1>
<button id="begin">Begin the adventure</button>
</div>
</body> </body>
</html> </html>

View File

@ -3,12 +3,35 @@ import Rooms from './rooms';
import Items from './items'; import Items from './items';
import MeowCommand from './commands/meow'; 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({ document.getElementById("begin").addEventListener("click", () => {
rooms: Rooms, document.getElementById("before-play").hidden = true;
commands: [ document.getElementById("play-area").hidden = false;
[["meow", "mew"], MeowCommand] startGame(true);
], })
items: Items
}); function startGame(newGame) {
const game = new Game(newGame);
game.init({
rooms: Rooms,
commands: [
[["meow", "mew"], MeowCommand]
],
items: Items
});
}