Take, drop and command echo
parent
abb3c475db
commit
37759d7b9f
|
@ -1,8 +1,13 @@
|
|||
import LookCommand from "./commands/look";
|
||||
import UseCommand from "./commands/use";
|
||||
import TakeCommand from "./commands/take";
|
||||
import DropCommand from "./commands/drop";
|
||||
|
||||
const defaultCommands = [
|
||||
[["look", "l"], LookCommand],
|
||||
[["use", "interact"], UseCommand]
|
||||
[["use", "interact"], UseCommand],
|
||||
[["take", "get"], TakeCommand],
|
||||
[["drop", "put"], DropCommand]
|
||||
];
|
||||
|
||||
const directionMap = [
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
export default function DropCommand(args, context) {
|
||||
const room = context.getRoom(context.player.currentRoom);
|
||||
const items = context.player.getInventory();
|
||||
let item = null;
|
||||
for (let i of items) {
|
||||
if (i.name.includes(args[1])) {
|
||||
item = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!item) {
|
||||
context.print(`You're not carrying a ${args[1]}`);
|
||||
} else {
|
||||
context.player.removeItem(item.id);
|
||||
room.addItem(item.id);
|
||||
context.print(`You set ${item.name} down on the floor.`);
|
||||
item.onDrop();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
export default function TakeCommand(args, context) {
|
||||
const room = context.getRoom(context.player.currentRoom);
|
||||
const items = room.getItems();
|
||||
let item = null;
|
||||
for (let i of items) {
|
||||
if (i.name.includes(args[1])) {
|
||||
item = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!item) {
|
||||
context.print(`You can't find any ${args[1]}`);
|
||||
} else {
|
||||
if (!item.takeable) {
|
||||
context.print(`You can't take ${item.name}`);
|
||||
} else {
|
||||
room.removeItem(item.id);
|
||||
context.player.addItem(item.id);
|
||||
context.print(`You take ${item.name}`);
|
||||
item.onTake();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -13,7 +13,7 @@ export default class Game {
|
|||
this.items = [];
|
||||
this.output = new Output();
|
||||
this.commandHandler = new Commands(this);
|
||||
this.input = new Input(this.commandHandler);
|
||||
this.input = new Input(this.commandHandler, this.output);
|
||||
this.visitedRooms = new Map();
|
||||
}
|
||||
|
||||
|
@ -34,6 +34,7 @@ export default class Game {
|
|||
this.state = data.state;
|
||||
this.commandHandler.addCommands(data.commands);
|
||||
this.player = new Player();
|
||||
this.player.context = this;
|
||||
this.move(this.player.currentRoom);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
export default class Input {
|
||||
constructor(commandHandler) {
|
||||
constructor(commandHandler, outputHandler) {
|
||||
this.handler = commandHandler;
|
||||
this.output = outputHandler;
|
||||
this.inputField = document.getElementById("input-area");
|
||||
this.init();
|
||||
}
|
||||
|
@ -10,6 +11,7 @@ export default class Input {
|
|||
if (e.which == 13) {
|
||||
const val = this.inputField.value;
|
||||
this.inputField.value = "";
|
||||
this.output.say(`> ${val}`);
|
||||
this.handler.doCommand(val);
|
||||
}
|
||||
})
|
||||
|
|
|
@ -7,6 +7,7 @@ export default class Item {
|
|||
this.takeable = true;
|
||||
this.useCallback = null;
|
||||
this.takeCallback = null;
|
||||
this.dropCallback = null;
|
||||
this.context = null;
|
||||
}
|
||||
|
||||
|
@ -25,4 +26,8 @@ export default class Item {
|
|||
addTakeCallback(callback) {
|
||||
this.takeCallback = callback.bind(this);
|
||||
}
|
||||
|
||||
addDropCallback(callback) {
|
||||
this.dropCallback = callback.bind(this);
|
||||
}
|
||||
}
|
|
@ -2,5 +2,18 @@ export default class Player {
|
|||
constructor() {
|
||||
this.inventory = [];
|
||||
this.currentRoom = "start";
|
||||
this.context = null;
|
||||
}
|
||||
|
||||
addItem(id) {
|
||||
this.inventory.push(id);
|
||||
}
|
||||
|
||||
removeItem(id) {
|
||||
this.inventory = this.inventory.filter((item) => item != id);
|
||||
}
|
||||
|
||||
getInventory() {
|
||||
return this.inventory.map((item) => this.context.getItem(item));
|
||||
}
|
||||
}
|
|
@ -49,6 +49,10 @@ export default class Room {
|
|||
this.objects.push(item);
|
||||
}
|
||||
|
||||
removeItem(id) {
|
||||
this.objects = this.objects.filter((item) => item != id);
|
||||
}
|
||||
|
||||
addEnterCallback(callback) {
|
||||
this.enterCallback = callback.bind(this);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue