refactor: check keys before access

This commit is contained in:
cha0s 2019-05-02 20:21:02 -05:00
parent dd5a0d7d79
commit a00287c021

View File

@ -64,7 +64,7 @@ class AnimatedBase extends Trait {
}
jitterFor(key) {
if (!this._animations[key] || !this._animations[key].jitter) {
if (!(key in this._animations) || !this._animations[key].jitter) {
return 0;
}
return this._animations[key].jitter;
@ -127,7 +127,7 @@ class AnimatedBase extends Trait {
}
offsetFor(key) {
if (!this._animations[key] || !this._animations[key].offset) {
if (!(key in this._animations) || !this._animations[key].offset) {
return [0, 0];
}
return this._animations[key].offset;
@ -148,10 +148,10 @@ class AnimatedBase extends Trait {
if (!this.animationViews) {
return;
}
const animationView = this.animationViews[key];
if (!animationView) {
if (!(key in this.animationViews)) {
return;
}
const animationView = this.animationViews[key];
this.entity.container.addChild(animationView);
}
@ -160,13 +160,13 @@ class AnimatedBase extends Trait {
visibleAabbs: () => {
const key = this._currentAnimation;
if (this._cachedAabbs[key]) {
if (key in this._cachedAabbs) {
return this._cachedAabbs[key];
}
const animation = this.animations[key];
if (!animation) {
if (!(key in this.animations)) {
return [0, 0, 0, 0];
}
const animation = this.animations[key];
const viewPosition = this.offsetFor(key);
const size = animation.frameSize;
const rectangle = Rectangle.compose(viewPosition, size);
@ -186,8 +186,8 @@ class AnimatedBase extends Trait {
currentAnimationChanged: (oldKey, currentAnimation) => {
this._currentAnimation = currentAnimation;
// Reset old animation.
const oldAnimation = this.animations[oldKey];
if (oldAnimation) {
if (oldKey in this.animations) {
const oldAnimation = this.animations[oldKey];
oldAnimation.reset();
}
// Bounding box update.
@ -245,10 +245,10 @@ class AnimatedBase extends Trait {
}
// Only tick current animation.
const currentAnimation = this._currentAnimation;
const animation = this.animations[currentAnimation];
if (!animation) {
if (!(currentAnimation in this.animations)) {
return;
}
const animation = this.animations[currentAnimation];
const jitterAmount = this.jitterFor(currentAnimation);
if (jitterAmount > 0) {
const jitter = Math.random() * jitterAmount;