assassin-bug/src/engine/commands.js

101 lines
2.8 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 = [
[["look", "l"], LookCommand],
2021-11-04 21:47:09 +00:00
[["use", "interact"], UseCommand],
[["take", "get"], TakeCommand],
2021-11-04 21:51:50 +00:00
[["drop", "put"], DropCommand],
2021-11-05 19:02:20 +00:00
["echo", EchoCommand],
["save", SaveCommand],
["load", LoadCommand],
2021-11-05 22:22:28 +00:00
["volume", VolumeCommand],
[["i", "inv", "inventory"], 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(" ");
if (this.commands.get(split[0])) {
this.commands.get(split[0])(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);
}
}
addCommands(commands) {
commands.forEach((command) => {
this.addCommand(command[0], command[1]);
});
}
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
}