Add loading functionality
parent
6d0548455e
commit
9e95b4c202
|
@ -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 = [
|
||||||
|
|
|
@ -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 + ".");
|
||||||
|
}
|
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
if (this.newGame) {
|
||||||
this.move(this.player.currentRoom);
|
this.move(this.player.currentRoom);
|
||||||
|
} else {
|
||||||
|
this.Serialization.load();
|
||||||
|
}
|
||||||
this.start();
|
this.start();
|
||||||
this.Serialization.save();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
advanceTick() {
|
advanceTick() {
|
||||||
|
|
|
@ -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>
|
|
@ -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", () => {
|
||||||
|
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,
|
rooms: Rooms,
|
||||||
commands: [
|
commands: [
|
||||||
[["meow", "mew"], MeowCommand]
|
[["meow", "mew"], MeowCommand]
|
||||||
],
|
],
|
||||||
items: Items
|
items: Items
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue