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

264 lines
6.6 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} from '../animation';
import {AnimationView} from '../animation-view';
2019-03-18 22:20:03 -05:00
const decorate = compose(
2019-03-23 23:24:18 -05:00
StateProperty('currentAnimation', {
track: true,
}),
2019-04-21 03:43:00 -05:00
StateProperty('isAnimating', {
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-04-21 03:43:00 -05:00
isAnimating: true,
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;
2019-05-01 21:35:38 -05:00
this._cachedAabbs = {};
2019-05-01 20:03:36 -05:00
this._currentAnimation = this.entity.currentAnimation;
2019-03-20 23:23:34 -05:00
}
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);
}
2019-04-21 21:54:41 -05:00
jitterFor(key) {
2019-05-02 20:21:02 -05:00
if (!(key in this._animations) || !this._animations[key].jitter) {
2019-04-21 21:54:41 -05:00
return 0;
}
return this._animations[key].jitter;
}
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-16 13:30:15 -05:00
this.entity.updateVisibleBoundingBox();
});
}
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.offsetFor(key);
// Ensure animation is made visible upfront.
2019-05-01 20:03:36 -05:00
const isCurrentAnimation = key === this._currentAnimation;
if (isCurrentAnimation) {
2019-03-19 20:58:19 -05:00
this.showAnimation(key);
}
});
2019-04-19 16:49:41 -05:00
this.setSpriteScale();
});
}
offsetFor(key) {
2019-05-02 20:21:02 -05:00
if (!(key in this._animations) || !this._animations[key].offset) {
return [0, 0];
}
return this._animations[key].offset;
}
2019-04-19 16:49:41 -05:00
setSpriteScale() {
if (!this.animationViews) {
return;
}
2019-05-01 21:35:38 -05:00
const rawScale = this._visibleScale;
2019-04-19 16:49:41 -05:00
for (const key in this.animationViews) {
const animationView = this.animationViews[key];
animationView.scale = rawScale;
}
}
2019-03-19 20:58:19 -05:00
showAnimation(key) {
if (!this.animationViews) {
return;
}
2019-05-02 20:21:02 -05:00
if (!(key in this.animationViews)) {
2019-03-19 20:58:19 -05:00
return;
}
2019-05-02 20:21:02 -05:00
const animationView = this.animationViews[key];
2019-03-19 20:58:19 -05:00
this.entity.container.addChild(animationView);
}
hooks() {
return {
visibleAabbs: () => {
2019-05-01 20:03:36 -05:00
const key = this._currentAnimation;
2019-05-02 20:21:02 -05:00
if (key in this._cachedAabbs) {
2019-05-01 21:35:38 -05:00
return this._cachedAabbs[key];
}
2019-05-02 20:21:02 -05:00
if (!(key in this.animations)) {
return [0, 0, 0, 0];
}
2019-05-02 20:21:02 -05:00
const animation = this.animations[key];
const viewPosition = this.offsetFor(key);
2019-04-19 16:49:41 -05:00
const size = animation.frameSize;
2019-05-01 20:16:38 -05:00
const rectangle = Rectangle.compose(viewPosition, size);
2019-04-19 16:49:41 -05:00
const expanded = Rectangle.expand(
rectangle,
2019-05-01 21:35:38 -05:00
Vector.sub(Vector.mul(size, this._visibleScale), size),
2019-04-19 16:49:41 -05:00
);
2019-05-01 21:35:38 -05:00
return this._cachedAabbs[key] = expanded;
},
}
}
2019-03-18 22:20:03 -05:00
listeners() {
return {
2019-04-19 15:51:57 -05:00
2019-05-01 20:03:36 -05:00
currentAnimationChanged: (oldKey, currentAnimation) => {
this._currentAnimation = currentAnimation;
// Reset old animation.
2019-05-02 20:21:02 -05:00
if (oldKey in this.animations) {
const oldAnimation = this.animations[oldKey];
oldAnimation.reset();
}
// Bounding box update.
2019-04-16 13:30:15 -05:00
this.entity.updateVisibleBoundingBox();
// Only client/graphics.
if (!this.animationViews) {
return;
}
// Swap the animation.
2019-03-19 20:58:19 -05:00
this.hideAnimation(oldKey);
2019-05-01 20:03:36 -05:00
this.showAnimation(this._currentAnimation);
},
2019-04-19 15:51:57 -05:00
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
},
2019-04-19 15:51:57 -05:00
dying: () => {
2019-04-21 03:43:00 -05:00
this.isAnimating = false;
2019-04-19 15:51:57 -05:00
},
2019-04-19 17:09:48 -05:00
visibleScaleChanged: () => {
2019-05-01 21:35:38 -05:00
this._cachedAabbs = {};
2019-04-19 16:49:41 -05:00
this.setSpriteScale();
},
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-05-01 21:35:38 -05:00
if (this.entity.is('visible')) {
this._visibleScale = this.entity.rawVisibleScale;
}
2019-03-18 22:20:03 -05:00
},
2019-04-19 16:49:41 -05:00
2019-05-01 21:35:38 -05:00
visibleScaleChanged: () => {
this._visibleScale = this.entity.rawVisibleScale;
}
2019-03-18 22:20:03 -05:00
};
}
tick(elapsed) {
2019-04-21 03:43:00 -05:00
if (!this.isAnimating) {
2019-04-19 15:51:57 -05:00
return;
}
// Only tick current animation.
2019-05-01 20:03:36 -05:00
const currentAnimation = this._currentAnimation;
2019-05-02 20:21:02 -05:00
if (!(currentAnimation in this.animations)) {
return;
}
2019-05-02 20:21:02 -05:00
const animation = this.animations[currentAnimation];
2019-05-01 20:03:36 -05:00
const jitterAmount = this.jitterFor(currentAnimation);
if (jitterAmount > 0) {
const jitter = Math.random() * jitterAmount;
const halfJitter = jitter / 2;
elapsed += jitter - halfJitter;
}
animation.tick(elapsed);
}
2019-03-18 22:20:03 -05:00
}
export class Animated extends decorate(AnimatedBase) {}