Add game ticks
parent
b257784553
commit
90d7fe9a81
File diff suppressed because one or more lines are too long
|
@ -40,6 +40,11 @@ export default class ItemBuilder {
|
|||
return this;
|
||||
}
|
||||
|
||||
withTickCallback(callback) {
|
||||
this.item.addTickCallback(callback);
|
||||
return this;
|
||||
}
|
||||
|
||||
create() {
|
||||
return this.item;
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import Output from './output';
|
|||
import Input from './input';
|
||||
import Commands from './commands';
|
||||
|
||||
|
||||
export default class Game {
|
||||
constructor() {
|
||||
this.player = new Player();
|
||||
|
@ -15,6 +16,7 @@ export default class Game {
|
|||
this.commandHandler = new Commands(this);
|
||||
this.input = new Input(this.commandHandler, this.output);
|
||||
this.visitedRooms = new Map();
|
||||
this.interval = null;
|
||||
}
|
||||
|
||||
print(string) {
|
||||
|
@ -36,6 +38,21 @@ export default class Game {
|
|||
this.player = new Player();
|
||||
this.player.context = this;
|
||||
this.move(this.player.currentRoom);
|
||||
this.start();
|
||||
}
|
||||
|
||||
advanceTick() {
|
||||
this.items.forEach((item) => item.onTick());
|
||||
this.rooms.forEach((room) => room.onTick());
|
||||
}
|
||||
|
||||
start() {
|
||||
this.interval = setInterval(() => this.advanceTick(), 1000);
|
||||
}
|
||||
|
||||
stop() {
|
||||
clearInterval(this.interval);
|
||||
this.interval = null;
|
||||
}
|
||||
|
||||
examineRoom() {
|
||||
|
|
|
@ -8,6 +8,7 @@ export default class Item {
|
|||
this.useCallback = null;
|
||||
this.takeCallback = null;
|
||||
this.dropCallback = null;
|
||||
this.tickCallback = null;
|
||||
this.context = null;
|
||||
}
|
||||
|
||||
|
@ -19,6 +20,10 @@ export default class Item {
|
|||
if (this.takeCallback) return this.takeCallback();
|
||||
}
|
||||
|
||||
async onTick() {
|
||||
if (this.tickCallback) return this.tickCallback(this.context);
|
||||
}
|
||||
|
||||
addUseCallback(callback) {
|
||||
this.useCallback = callback.bind(this);
|
||||
}
|
||||
|
@ -30,4 +35,8 @@ export default class Item {
|
|||
addDropCallback(callback) {
|
||||
this.dropCallback = callback.bind(this);
|
||||
}
|
||||
|
||||
addTickCallback(callback) {
|
||||
this.tickCallback = callback.bind(this);
|
||||
}
|
||||
}
|
|
@ -76,4 +76,8 @@ export default class Room {
|
|||
getItems() {
|
||||
return this.objects.map((item) => this.context.getItem(item));
|
||||
}
|
||||
|
||||
async onTick() {
|
||||
if (this.tickCallback) return this.tickCallback(this.context);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue