diff --git a/src/engine/commands.js b/src/engine/commands.js index 8a26e51..e8212c4 100644 --- a/src/engine/commands.js +++ b/src/engine/commands.js @@ -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 = [ diff --git a/src/engine/commands/drop.js b/src/engine/commands/drop.js new file mode 100644 index 0000000..dbd9604 --- /dev/null +++ b/src/engine/commands/drop.js @@ -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(); + } +} \ No newline at end of file diff --git a/src/engine/commands/take.js b/src/engine/commands/take.js index e69de29..e0c0082 100644 --- a/src/engine/commands/take.js +++ b/src/engine/commands/take.js @@ -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(); + } + } +} \ No newline at end of file diff --git a/src/engine/index.js b/src/engine/index.js index 97cc660..2733cfa 100644 --- a/src/engine/index.js +++ b/src/engine/index.js @@ -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); } diff --git a/src/engine/input.js b/src/engine/input.js index b17f7b0..0596e2e 100644 --- a/src/engine/input.js +++ b/src/engine/input.js @@ -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); } }) diff --git a/src/engine/item.js b/src/engine/item.js index e0471a1..a768669 100644 --- a/src/engine/item.js +++ b/src/engine/item.js @@ -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); + } } \ No newline at end of file diff --git a/src/engine/player.js b/src/engine/player.js index c4d3485..b500abc 100644 --- a/src/engine/player.js +++ b/src/engine/player.js @@ -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)); } } \ No newline at end of file diff --git a/src/engine/room.js b/src/engine/room.js index a2f8cb6..53851b7 100644 --- a/src/engine/room.js +++ b/src/engine/room.js @@ -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); }