diff --git a/packages/entity/traits/perishable.trait.js b/packages/entity/traits/perishable.trait.js new file mode 100644 index 0000000..99d1df0 --- /dev/null +++ b/packages/entity/traits/perishable.trait.js @@ -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(); + } + } + +}