Skip to content

Commit

Permalink
Replace task system with EventBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
pomle committed Mar 24, 2020
1 parent 45064ca commit 3d9ad5b
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions public/js/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,26 @@ export const Sides = {
};

export class Trait {
static EVENT_TASK = Symbol('task');

constructor(name) {
this.NAME = name;
this.tasks = [];
this.listeners = [];
}

listen(name, callback) {
this.listeners.push({name, callback});
listen(name, callback, count = Infinity) {
this.listeners.push({name, callback, count});
}

finalize(entity) {
for (const listener of this.listeners) {
this.listeners = this.listeners.filter(listener => {
entity.events.process(listener.name, listener.callback);
}

this.tasks.forEach(task => task());
this.tasks.length = 0;
return --listener.count;
});
}

queue(task) {
this.tasks.push(task);
this.listen(Trait.EVENT_TASK, task, 1);
}

collides(us, them) {
Expand Down Expand Up @@ -85,6 +84,8 @@ export default class Entity {
}

finalize() {
this.events.emit(Trait.EVENT_TASK);

this.traits.forEach(trait => {
trait.finalize(this);
});
Expand Down

0 comments on commit 3d9ad5b

Please sign in to comment.