Take, drop and command echo

master
Talon 2021-11-04 22:47:09 +01:00
parent abb3c475db
commit 37759d7b9f
8 changed files with 75 additions and 3 deletions

View File

@ -1,8 +1,13 @@
import LookCommand from "./commands/look"; import LookCommand from "./commands/look";
import UseCommand from "./commands/use"; import UseCommand from "./commands/use";
import TakeCommand from "./commands/take";
import DropCommand from "./commands/drop";
const defaultCommands = [ const defaultCommands = [
[["look", "l"], LookCommand], [["look", "l"], LookCommand],
[["use", "interact"], UseCommand] [["use", "interact"], UseCommand],
[["take", "get"], TakeCommand],
[["drop", "put"], DropCommand]
]; ];
const directionMap = [ const directionMap = [

View File

@ -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();
}
}

View File

@ -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();
}
}
}

View File

@ -13,7 +13,7 @@ export default class Game {
this.items = []; this.items = [];
this.output = new Output(); this.output = new Output();
this.commandHandler = new Commands(this); this.commandHandler = new Commands(this);
this.input = new Input(this.commandHandler); this.input = new Input(this.commandHandler, this.output);
this.visitedRooms = new Map(); this.visitedRooms = new Map();
} }
@ -34,6 +34,7 @@ export default class Game {
this.state = data.state; this.state = data.state;
this.commandHandler.addCommands(data.commands); this.commandHandler.addCommands(data.commands);
this.player = new Player(); this.player = new Player();
this.player.context = this;
this.move(this.player.currentRoom); this.move(this.player.currentRoom);
} }

View File

@ -1,6 +1,7 @@
export default class Input { export default class Input {
constructor(commandHandler) { constructor(commandHandler, outputHandler) {
this.handler = commandHandler; this.handler = commandHandler;
this.output = outputHandler;
this.inputField = document.getElementById("input-area"); this.inputField = document.getElementById("input-area");
this.init(); this.init();
} }
@ -10,6 +11,7 @@ export default class Input {
if (e.which == 13) { if (e.which == 13) {
const val = this.inputField.value; const val = this.inputField.value;
this.inputField.value = ""; this.inputField.value = "";
this.output.say(`> ${val}`);
this.handler.doCommand(val); this.handler.doCommand(val);
} }
}) })

View File

@ -7,6 +7,7 @@ export default class Item {
this.takeable = true; this.takeable = true;
this.useCallback = null; this.useCallback = null;
this.takeCallback = null; this.takeCallback = null;
this.dropCallback = null;
this.context = null; this.context = null;
} }
@ -25,4 +26,8 @@ export default class Item {
addTakeCallback(callback) { addTakeCallback(callback) {
this.takeCallback = callback.bind(this); this.takeCallback = callback.bind(this);
} }
addDropCallback(callback) {
this.dropCallback = callback.bind(this);
}
} }

View File

@ -2,5 +2,18 @@ export default class Player {
constructor() { constructor() {
this.inventory = []; this.inventory = [];
this.currentRoom = "start"; 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));
} }
} }

View File

@ -49,6 +49,10 @@ export default class Room {
this.objects.push(item); this.objects.push(item);
} }
removeItem(id) {
this.objects = this.objects.filter((item) => item != id);
}
addEnterCallback(callback) { addEnterCallback(callback) {
this.enterCallback = callback.bind(this); this.enterCallback = callback.bind(this);
} }