Bind callbacks to object

master
Talon 2021-11-04 22:29:05 +01:00
parent a968f89bb3
commit abb3c475db
5 changed files with 19 additions and 11 deletions

File diff suppressed because one or more lines are too long

View File

@ -31,12 +31,12 @@ export default class ItemBuilder {
} }
withUseCallback(callback) { withUseCallback(callback) {
this.item.useCallback = callback; this.item.addUseCallback(callback);
return this; return this;
} }
withTakeCallback(callback) { withTakeCallback(callback) {
this.item.takeCallback = callback; this.item.addTakeCallback(callback);
return this; return this;
} }

View File

@ -17,4 +17,12 @@ export default class Item {
async onTake() { async onTake() {
if (this.takeCallback) return this.takeCallback(); if (this.takeCallback) return this.takeCallback();
} }
addUseCallback(callback) {
this.useCallback = callback.bind(this);
}
addTakeCallback(callback) {
this.takeCallback = callback.bind(this);
}
} }

View File

@ -50,23 +50,23 @@ export default class Room {
} }
addEnterCallback(callback) { addEnterCallback(callback) {
this.enterCallback = callback; this.enterCallback = callback.bind(this);
} }
addExitCallback(callback) { addExitCallback(callback) {
this.exitCallback = callback; this.exitCallback = callback.bind(this);
} }
addEnterLogic(func) { addEnterLogic(func) {
this.canEnterLogic = func; this.canEnterLogic = func.bind(this);
} }
addExitLogic(func) { addExitLogic(func) {
this.canExitLogic = func; this.canExitLogic = func.bind(this);
} }
addTickCallback(callback) { addTickCallback(callback) {
this.tickCallback = callback; this.tickCallback = callback.bind(this);
} }
getItems() { getItems() {

View File

@ -7,9 +7,9 @@ export default new ItemBuilder()
.isTakeable(true) .isTakeable(true)
.isUsable(true) .isUsable(true)
.withTakeCallback(async function(context) { .withTakeCallback(async function(context) {
context.output.say("It feels heavy in your hands."); context.print(`The ${this.id} feels heavy in your hands.`);
}) })
.withUseCallback(async function(context) { .withUseCallback(async function(context) {
context.output.say("You can't really figure out what to do with this yet."); context.print(`You can't really figure out what to do with ${this.name} yet`);
}) })
.create(); .create();