assassin-bug/src/engine/commands.js

43 lines
1.1 KiB
JavaScript
Raw Normal View History

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]
];
export default class Commands {
constructor(context, commands) {
this.context = context;
this.commands = commands || new Map();
this.addDefaultCommands();
}
doCommand(str) {
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]));
}
}
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);
}
}