silphius/app/ecs/components/ttl.js
2024-08-01 21:10:16 -05:00

29 lines
634 B
JavaScript

import Component from '@/ecs/component.js';
export default class Ttl extends Component {
instanceFromSchema() {
const {ecs} = this;
return class TtlInstance extends super.instanceFromSchema() {
$$elapsed = 0;
destroy() {
this.$$elapsed = 0;
}
get elapsed() {
return this.$$elapsed;
}
set elapsed(elapsed) {
this.$$elapsed = elapsed;
}
tick(elapsed) {
this.$$elapsed += elapsed;
if (this.$$elapsed >= this.ttl) {
ecs.destroy(this.entity);
}
}
}
}
static properties = {
ttl: {type: 'float32'},
};
}