assassin-bug/src/engine/commands.js

126 lines
3.5 KiB
JavaScript
Raw Normal View History

2021-11-04 19:58:37 +00:00
import LookCommand from "./commands/look";
import UseCommand from "./commands/use";
2021-11-04 21:47:09 +00:00
import TakeCommand from "./commands/take";
import DropCommand from "./commands/drop";
2021-11-04 21:51:50 +00:00
import EchoCommand from "./commands/echo";
2021-11-05 19:02:20 +00:00
import SaveCommand from "./commands/save";
import LoadCommand from "./commands/load";
import VolumeCommand from "./commands/volume";
2021-11-05 22:22:28 +00:00
import InventoryCommand from "./commands/inventory";
2021-11-04 21:47:09 +00:00
2021-11-04 19:58:37 +00:00
const defaultCommands = [
2025-07-18 18:41:49 +00:00
LookCommand,
UseCommand,
TakeCommand,
DropCommand,
EchoCommand,
SaveCommand,
LoadCommand,
VolumeCommand,
InventoryCommand
2021-11-04 19:58:37 +00:00
];
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;
2025-07-18 14:33:56 +00:00
this.mobileCommands = null;
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(" ");
2025-07-18 18:41:49 +00:00
const command = this.commands.get(split[0]);
if (command) {
if (command.execute) {
// New Command object
command.execute(split, this.context);
} else {
// Old function-based command
command(split, this.context);
}
2025-07-18 14:33:56 +00:00
return;
2021-11-04 19:58:37 +00:00
}
2021-11-04 21:19:50 +00:00
2025-07-18 14:33:56 +00:00
// Try to match direction (handles both short and full forms)
2021-11-04 21:19:50 +00:00
const direction = this.matchDirection(split[0]);
2025-07-18 14:33:56 +00:00
const actualDirection = direction || split[0]; // Use the input if no mapping found
2021-11-04 21:19:50 +00:00
2025-07-18 14:33:56 +00:00
if (room.getExit(actualDirection)) {
this.context.move(room.getExit(actualDirection));
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);
}
}
2025-07-18 18:41:49 +00:00
addCommandObject(commandObj) {
// Add the main command name
this.commands.set(commandObj.name, commandObj);
// Add all aliases
commandObj.aliases.forEach(alias => {
this.commands.set(alias, commandObj);
});
}
2021-11-04 19:58:37 +00:00
addCommands(commands) {
commands.forEach((command) => {
2025-07-18 18:41:49 +00:00
if (command.execute && command.getAllNames) {
// New Command object
this.addCommandObject(command);
} else if (Array.isArray(command)) {
// Old array format
this.addCommand(command[0], command[1]);
} else {
// Old single command
this.addCommand(command.name, command);
}
2021-11-04 19:58:37 +00:00
});
}
addDefaultCommands() {
this.addCommands(defaultCommands);
}
2021-11-04 21:19:50 +00:00
2025-07-18 14:33:56 +00:00
setMobileCommands(mobileCommands) {
this.mobileCommands = mobileCommands;
}
refreshMobileCommands() {
if (this.mobileCommands) {
this.mobileCommands.refreshCommands();
}
}
2021-11-04 21:19:50 +00:00
matchDirection(str) {
for (let dir of directionMap) {
2025-07-18 14:33:56 +00:00
if (dir[0] == str) return dir[1]; // short form -> long form
if (dir[1] == str) return dir[1]; // long form -> long form
2021-11-04 21:19:50 +00:00
}
2025-07-18 14:33:56 +00:00
return null;
2021-11-04 21:19:50 +00:00
}
2021-11-04 19:58:37 +00:00
}