From f9a7db6f957a5ddcc0718836c75a925eb7f1da1f Mon Sep 17 00:00:00 2001 From: cha0s Date: Sun, 21 Apr 2019 21:54:41 -0500 Subject: [PATCH] feat: animation jitter --- packages/timing/traits/animated.trait.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/timing/traits/animated.trait.js b/packages/timing/traits/animated.trait.js index 95bc9a9..e9d2bbd 100644 --- a/packages/timing/traits/animated.trait.js +++ b/packages/timing/traits/animated.trait.js @@ -61,6 +61,13 @@ class AnimatedBase extends Trait { this.entity.container.removeChild(animationView); } + jitterFor(key) { + if (!this._animations[key] || !this._animations[key].jitter) { + return 0; + } + return this._animations[key].jitter; + } + loadAnimations() { if (this.animationsPromise) { return; @@ -224,11 +231,14 @@ class AnimatedBase extends Trait { return; } // Only tick current animation. - const animation = this.animations[this.entity.currentAnimation]; + const currentAnimation = this.entity.currentAnimation; + const animation = this.animations[currentAnimation]; if (!animation) { return; } - animation.tick(elapsed); + const jitter = Math.random() * this.jitterFor(currentAnimation); + const halfJitter = jitter / 2; + animation.tick(elapsed + (jitter - halfJitter)); } }