Add command enable/disable

This commit is contained in:
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]
];
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 {
constructor(context, commands) {
this.context = context;
this.commands = commands || new Map();
this.enabled = true;
this.addDefaultCommands();
}
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 split = str.split(" ");
if (this.commands.get(split[0])) {
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() {
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) {
if (args.length == 0) {
if (args.length == 1) {
context.examineRoom();
} else {
const room = context.getRoom(context.player.currentRoom);

View File

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