2021-11-04 19:58:37 +00:00
|
|
|
import LookCommand from "./commands/look";
|
|
|
|
import UseCommand from "./commands/use";
|
|
|
|
const defaultCommands = [
|
|
|
|
[["look", "l"], LookCommand],
|
|
|
|
[["use", "interact"], UseCommand]
|
|
|
|
];
|
|
|
|
|
2021-11-04 21:19:50 +00:00
|
|
|
const directionMap = [
|
|
|
|
["n", "north"],
|
|
|
|
["ne", "northeast"],
|
|
|
|
["e", "east"],
|
|
|
|
["se", "southeast"],
|
|
|
|
["s", "south"],
|
|
|
|
["sw", "southwest"],
|
|
|
|
["w", "west"],
|
|
|
|
["nw", "northwest"],
|
|
|
|
["u", "up"],
|
|
|
|
["d", "down"]
|
|
|
|
];
|
|
|
|
|
2021-11-04 19:58:37 +00:00
|
|
|
export default class Commands {
|
|
|
|
constructor(context, commands) {
|
|
|
|
this.context = context;
|
|
|
|
this.commands = commands || new Map();
|
2021-11-04 21:19:50 +00:00
|
|
|
this.enabled = true;
|
2021-11-04 19:58:37 +00:00
|
|
|
this.addDefaultCommands();
|
|
|
|
}
|
|
|
|
|
|
|
|
doCommand(str) {
|
2021-11-04 21:19:50 +00:00
|
|
|
if (!this.enabled) {
|
|
|
|
this.context.print(`You can't seem to do anything at the moment.`);
|
|
|
|
return;
|
|
|
|
}
|
2021-11-04 19:58:37 +00:00
|
|
|
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);
|
|
|
|
}
|
2021-11-04 21:19:50 +00:00
|
|
|
|
|
|
|
const direction = this.matchDirection(split[0]);
|
|
|
|
|
|
|
|
if (room.getExit(direction)) {
|
|
|
|
this.context.move(room.getExit(direction));
|
2021-11-04 19:58:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addCommand(name, func) {
|
|
|
|
if (Array.isArray(name)) {
|
|
|
|
name.forEach((command) => this.commands.set(command, func));
|
|
|
|
} else {
|
|
|
|
this.commands.set(name, func);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addCommands(commands) {
|
|
|
|
commands.forEach((command) => {
|
|
|
|
this.addCommand(command[0], command[1]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addDefaultCommands() {
|
|
|
|
this.addCommands(defaultCommands);
|
|
|
|
}
|
2021-11-04 21:19:50 +00:00
|
|
|
|
|
|
|
matchDirection(str) {
|
|
|
|
for (let dir of directionMap) {
|
|
|
|
if (dir[0] == str) return dir[1];
|
|
|
|
}
|
|
|
|
}
|
2021-11-04 19:58:37 +00:00
|
|
|
}
|