avocado-old/packages/timing/traits/animated.trait.js

209 lines
5.3 KiB
JavaScript
Raw Normal View History

2019-03-18 22:20:03 -05:00
import {compose} from '@avocado/core';
2019-04-14 20:21:52 -05:00
import {StateProperty, Trait} from '@avocado/entity';
2019-03-19 20:58:19 -05:00
import {Rectangle, Vector} from '@avocado/math';
import {Animation, AnimationView} from '@avocado/timing';
2019-03-18 22:20:03 -05:00
const decorate = compose(
2019-03-23 23:24:18 -05:00
StateProperty('currentAnimation', {
track: true,
}),
2019-03-18 22:20:03 -05:00
);
class AnimatedBase extends Trait {
static defaultParams() {
return {
animations: {},
2019-03-18 22:20:03 -05:00
};
}
static defaultState() {
return {
currentAnimation: 'idle',
2019-03-18 22:20:03 -05:00
};
}
2019-03-20 23:23:34 -05:00
initialize() {
this._animations = this.params.get('animations').toJS();
2019-03-20 23:23:34 -05:00
this.animations = {};
this.animationViews = undefined;
this.animationsPromise = undefined;
}
destroy() {
if (this.animationViews) {
for (const key in this.animationViews) {
this.hideAnimation(key);
const animationView = this.animationViews[key];
animationView.destroy();
}
}
2019-03-21 00:09:17 -05:00
for (const key in this.animations) {
const animation = this.animations[key];
animation.destroy();
}
2019-03-20 23:23:34 -05:00
}
2019-03-19 20:58:19 -05:00
hideAnimation(key) {
if (!this.animationViews) {
return;
}
const animationView = this.animationViews[key];
if (!animationView) {
return;
}
this.entity.container.removeChild(animationView);
}
loadAnimations() {
if (this.animationsPromise) {
return;
}
const animationPromises = [];
// Load all animations.
for (const key in this._animations) {
const {uri} = this._animations[key];
const promise = Animation.load(uri).then((animation) => {
// Zip with key to make populating animations and views trivial.
return {animation, key};
});
animationPromises.push(promise);
2019-03-19 10:46:20 -05:00
}
this.animationsPromise = Promise.all(animationPromises);
// Store keyed animations.
this.animations = {};
this.animationsPromise.then((animations) => {
animations.forEach(({animation, key}) => {
this.animations[key] = animation;
// Set direction upfront.
animation.direction = this.entity.direction;
});
// Bounding box update.
2019-04-14 18:42:13 -05:00
this.entity.emit('visibleBoundingBoxesUpdated');
});
}
loadAnimationImagesIfPossible() {
if (!this.entity.container) {
2019-03-19 11:29:07 -05:00
return;
}
if (!this.animationsPromise) {
return;
}
if (this.animationViews) {
return;
}
// Store keyed animation views.
this.animationViews = {};
this.animationsPromise.then((animations) => {
animations.forEach(({animation, key}) => {
this.animationViews[key] = new AnimationView(animation);
2019-03-20 21:58:14 -05:00
// Calculate any offset.
const animationView = this.animationViews[key];
animationView.position = this.viewPositionFor(key);
// Ensure animation is made visible upfront.
const isCurrentAnimation = key === this.entity.currentAnimation;
if (isCurrentAnimation) {
2019-03-19 20:58:19 -05:00
this.showAnimation(key);
}
});
});
}
offsetFor(key) {
if (!this._animations[key] || !this._animations[key].offset) {
return [0, 0];
}
return this._animations[key].offset;
}
2019-03-19 20:58:19 -05:00
showAnimation(key) {
if (!this.animationViews) {
return;
}
const animationView = this.animationViews[key];
if (!animationView) {
return;
}
this.entity.container.addChild(animationView);
}
viewPositionFor(key) {
const animation = this.animations[key];
if (!animation) {
return [0, 0];
}
const size = animation.frameSize;
const halfway = Vector.scale(size, -0.5);
const offset = this.offsetFor(key);
return Vector.add(halfway, offset);
}
hooks() {
return {
2019-04-14 18:42:13 -05:00
visibleBoundingBoxes: () => {
const key = this.entity.currentAnimation;
const animation = this.animations[key];
if (!animation) {
return [0, 0, 0, 0];
}
const viewPosition = this.viewPositionFor(key);
2019-04-11 12:20:24 -05:00
const position = Vector.add(this.entity.position, viewPosition);
return Rectangle.compose(position, animation.frameSize);
},
}
}
2019-03-18 22:20:03 -05:00
listeners() {
return {
currentAnimationChanged: (oldKey) => {
// Reset old animation.
const oldAnimation = this.animations[oldKey];
if (oldAnimation) {
oldAnimation.reset();
}
// Bounding box update.
2019-04-14 18:42:13 -05:00
this.entity.emit('visibleBoundingBoxesUpdated');
// Only client/graphics.
if (!this.animationViews) {
return;
}
// Swap the animation.
2019-03-19 20:58:19 -05:00
this.hideAnimation(oldKey);
this.showAnimation(this.entity.currentAnimation);
},
2019-03-18 22:20:03 -05:00
directionChanged: () => {
// All animations track direction.
for (const key in this.animations) {
const animation = this.animations[key];
animation.direction = this.entity.direction;
}
2019-03-18 22:20:03 -05:00
},
traitAdded: (type) => {
if (-1 === [
'animated',
2019-04-14 18:42:13 -05:00
'visible',
].indexOf(type)) {
2019-03-18 22:20:03 -05:00
return;
}
this.loadAnimations();
this.loadAnimationImagesIfPossible();
2019-03-18 22:20:03 -05:00
},
};
}
tick(elapsed) {
// Only tick current animation.
const animation = this.animations[this.entity.currentAnimation];
if (!animation) {
return;
}
animation.tick(elapsed);
}
2019-03-18 22:20:03 -05:00
}
export class Animated extends decorate(AnimatedBase) {}