silphius/app/ecs-components/ticking.js

40 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-06-26 21:08:09 -05:00
import Component from '@/ecs/component.js';
2024-06-26 21:08:09 -05:00
export default class Ticking extends Component {
instanceFromSchema() {
const Instance = super.instanceFromSchema();
2024-06-26 21:08:09 -05:00
Instance.prototype.$$finished = [];
Instance.prototype.$$tickingPromises = [];
Instance.prototype.addTickingPromise = function(tickingPromise) {
this.$$tickingPromises.push(tickingPromise);
tickingPromise.then(() => {
this.$$finished.push(tickingPromise);
});
}
Instance.prototype.tick = function(elapsed) {
for (const tickingPromise of this.$$finished) {
this.$$tickingPromises.splice(
this.$$tickingPromises.indexOf(tickingPromise),
1,
);
}
this.$$finished = [];
for (const tickingPromise of this.$$tickingPromises) {
tickingPromise.tick(elapsed);
}
2024-06-26 21:08:09 -05:00
for (const tickingPromise of this.$$finished) {
this.$$tickingPromises.splice(
this.$$tickingPromises.indexOf(tickingPromise),
1,
);
}
2024-06-26 21:08:09 -05:00
this.$$finished = [];
}
2024-06-26 21:08:09 -05:00
return Instance;
}
static properties = {
isTicking: {defaultValue: 1, type: 'uint8'},
};
}