Add command enable/disable

master
Talon 2021-11-04 22:19:50 +01:00
parent a745ff299e
commit a968f89bb3
4 changed files with 36 additions and 3 deletions

View File

@ -5,21 +5,42 @@ const defaultCommands = [
[["use", "interact"], UseCommand] [["use", "interact"], UseCommand]
]; ];
const directionMap = [
["n", "north"],
["ne", "northeast"],
["e", "east"],
["se", "southeast"],
["s", "south"],
["sw", "southwest"],
["w", "west"],
["nw", "northwest"],
["u", "up"],
["d", "down"]
];
export default class Commands { export default class Commands {
constructor(context, commands) { constructor(context, commands) {
this.context = context; this.context = context;
this.commands = commands || new Map(); this.commands = commands || new Map();
this.enabled = true;
this.addDefaultCommands(); this.addDefaultCommands();
} }
doCommand(str) { doCommand(str) {
if (!this.enabled) {
this.context.print(`You can't seem to do anything at the moment.`);
return;
}
const room = this.context.getRoom(this.context.player.currentRoom); const room = this.context.getRoom(this.context.player.currentRoom);
const split = str.split(" "); const split = str.split(" ");
if (this.commands.get(split[0])) { if (this.commands.get(split[0])) {
this.commands.get(split[0])(split, this.context); this.commands.get(split[0])(split, this.context);
} }
if (room.getExit(split[0])) {
this.context.move(room.getExit(split[0])); const direction = this.matchDirection(split[0]);
if (room.getExit(direction)) {
this.context.move(room.getExit(direction));
} }
} }
@ -40,4 +61,10 @@ export default class Commands {
addDefaultCommands() { addDefaultCommands() {
this.addCommands(defaultCommands); this.addCommands(defaultCommands);
} }
matchDirection(str) {
for (let dir of directionMap) {
if (dir[0] == str) return dir[1];
}
}
} }

View File

@ -1,5 +1,5 @@
export default function LookCommand(args, context) { export default function LookCommand(args, context) {
if (args.length == 0) { if (args.length == 1) {
context.examineRoom(); context.examineRoom();
} else { } else {
const room = context.getRoom(context.player.currentRoom); const room = context.getRoom(context.player.currentRoom);

View File

@ -89,4 +89,8 @@ export default class Game {
this.visitedRooms.set(roomID, true); this.visitedRooms.set(roomID, true);
} }
} }
enableCommandInput(value) {
this.commandHandler.enabled = value;
}
} }

View File

@ -8,11 +8,13 @@ export default new RoomBuilder()
.withExit("north", "tunnel_1") .withExit("north", "tunnel_1")
.withEnterCallback(async function(context) { .withEnterCallback(async function(context) {
const { output, wait } = context; const { output, wait } = context;
context.enableCommandInput(false);
output.say("You slowly wake up"); output.say("You slowly wake up");
await wait(5000); await wait(5000);
output.say("It's strange. You never used to be able to be conscious about the fact that you were waking up."); output.say("It's strange. You never used to be able to be conscious about the fact that you were waking up.");
await wait(5000); await wait(5000);
output.say("Yet here we are."); output.say("Yet here we are.");
context.enableCommandInput(true);
}) })
.withItem("stone") .withItem("stone")
.create(); .create();