2019-03-18 22:20:03 -05:00
|
|
|
import {compose} from '@avocado/core';
|
|
|
|
import {Image, Sprite} from '@avocado/graphics';
|
|
|
|
import {Vector} from '@avocado/math';
|
|
|
|
|
|
|
|
import {simpleState, Trait} from '../trait';
|
|
|
|
|
|
|
|
const decorate = compose(
|
|
|
|
simpleState('currentFrame', {
|
|
|
|
track: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
class AnimatedBase extends Trait {
|
|
|
|
|
|
|
|
static defaultParams() {
|
|
|
|
return {
|
|
|
|
frameCount: 0,
|
|
|
|
frameRate: 0,
|
|
|
|
frameSize: [0, 0],
|
|
|
|
image: '',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static defaultState() {
|
|
|
|
return {
|
|
|
|
currentFrame: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-19 11:25:42 -05:00
|
|
|
initialize() {
|
2019-03-18 22:20:03 -05:00
|
|
|
this.sprite = undefined;
|
2019-03-19 10:02:18 -05:00
|
|
|
this.frameCount = this.params.get('frameCount');
|
|
|
|
this.frameRate = this.params.get('frameRate');
|
|
|
|
this.frameSize = this.params.get('frameSize');
|
|
|
|
this.image = this.params.get('image');
|
|
|
|
this.frameCaret = this.frameRate;
|
2019-03-19 10:40:59 -05:00
|
|
|
this.loadSpriteImageIfPossible();
|
2019-03-18 22:20:03 -05:00
|
|
|
}
|
|
|
|
|
2019-03-19 10:40:59 -05:00
|
|
|
loadSpriteImageIfPossible() {
|
2019-03-19 11:25:42 -05:00
|
|
|
if (!this.entity.container) {
|
2019-03-19 10:40:59 -05:00
|
|
|
return;
|
|
|
|
}
|
2019-03-19 10:46:20 -05:00
|
|
|
if (!this.image) {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-19 10:40:59 -05:00
|
|
|
Image.load(this.image).then((image) => {
|
|
|
|
this.sprite = new Sprite(image);
|
|
|
|
this.entity.container.addChild(this.sprite);
|
|
|
|
this.updateFrameRect();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-19 10:22:35 -05:00
|
|
|
updateFrameRect() {
|
2019-03-19 11:29:07 -05:00
|
|
|
if (!this.sprite) {
|
|
|
|
return;
|
2019-03-19 10:22:35 -05:00
|
|
|
}
|
2019-03-19 11:29:07 -05:00
|
|
|
const direction = ('direction' in this.entity) ? this.entity.direction : 0;
|
|
|
|
this.sprite.sourceRectangle = [
|
|
|
|
this.entity.currentFrame * this.frameSize[0],
|
|
|
|
direction * this.frameSize[1],
|
|
|
|
this.frameSize[0],
|
|
|
|
this.frameSize[1],
|
|
|
|
];
|
2019-03-19 10:22:35 -05:00
|
|
|
}
|
|
|
|
|
2019-03-18 22:20:03 -05:00
|
|
|
listeners() {
|
|
|
|
return {
|
|
|
|
currentFrameChanged: () => {
|
2019-03-19 10:22:35 -05:00
|
|
|
this.updateFrameRect();
|
2019-03-18 22:20:03 -05:00
|
|
|
},
|
|
|
|
directionChanged: () => {
|
2019-03-19 10:22:35 -05:00
|
|
|
this.updateFrameRect();
|
2019-03-18 22:20:03 -05:00
|
|
|
},
|
|
|
|
tick: (elapsed) => {
|
|
|
|
this.frameCaret -= elapsed;
|
|
|
|
if (this.frameCaret > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const newFrame = (this.entity.currentFrame + 1) % this.frameCount;
|
|
|
|
this.entity.currentFrame = newFrame;
|
|
|
|
if (this.frameCaret < 0) {
|
|
|
|
this.frameCaret = this.frameRate - this.frameCaret;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
traitAdded: (trait) => {
|
|
|
|
if ('graphical' !== trait.constructor.type()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-19 10:40:59 -05:00
|
|
|
this.loadSpriteImageIfPossible();
|
2019-03-18 22:20:03 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Animated extends decorate(AnimatedBase) {}
|