feat: perishable
This commit is contained in:
parent
2326185fb7
commit
c9ecfbadae
32
packages/entity/traits/perishable.trait.js
Normal file
32
packages/entity/traits/perishable.trait.js
Normal file
|
@ -0,0 +1,32 @@
|
|||
import {compose} from '@avocado/core';
|
||||
|
||||
import {StateProperty, Trait} from '../trait';
|
||||
|
||||
const decorate = compose(
|
||||
);
|
||||
|
||||
export class Perishable extends decorate(Trait) {
|
||||
|
||||
static defaultParams() {
|
||||
return {
|
||||
ttl: 300,
|
||||
};
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user