From c9ecfbadae78ab4e082e85030980c281d6486649 Mon Sep 17 00:00:00 2001 From: cha0s Date: Wed, 6 Nov 2019 15:59:52 -0600 Subject: [PATCH] feat: perishable --- packages/entity/traits/perishable.trait.js | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 packages/entity/traits/perishable.trait.js 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(); + } + } + +}