avocado-old/packages/entity/traits/perishable.trait.js
2020-06-19 22:01:45 -05:00

42 lines
654 B
JavaScript

import {compose} from '@avocado/core';
import {StateProperty, Trait} from '../trait';
const decorate = compose(
);
export default class Perishable extends decorate(Trait) {
static defaultParams() {
return {
ttl: 300,
};
}
static describeParams() {
return {
ttl: {
type: 'number',
label: 'Time-to-live in seconds',
},
};
}
static type() {
return 'perishable';
}
constructor(entity, params, state) {
super(entity, params, state);
this.ttl = this.params.ttl;
}
tick(elapsed) {
this.ttl -= elapsed;
if (this.ttl <= 0) {
this.entity.destroy();
}
}
}