avocado-old/packages/entity/traits/animated.js

98 lines
2.2 KiB
JavaScript
Raw Normal View History

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;
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;
this.loadSpriteImageIfPossible();
2019-03-18 22:20:03 -05:00
}
loadSpriteImageIfPossible() {
2019-03-19 11:25:42 -05:00
if (!this.entity.container) {
return;
}
2019-03-19 10:46:20 -05:00
if (!this.image) {
return;
}
Image.load(this.image).then((image) => {
this.sprite = new Sprite(image);
this.entity.container.addChild(this.sprite);
this.updateFrameRect();
});
}
updateFrameRect() {
2019-03-19 11:29:07 -05:00
if (!this.sprite) {
return;
}
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-18 22:20:03 -05:00
listeners() {
return {
currentFrameChanged: () => {
this.updateFrameRect();
2019-03-18 22:20:03 -05:00
},
directionChanged: () => {
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;
}
this.loadSpriteImageIfPossible();
2019-03-18 22:20:03 -05:00
},
};
}
}
export class Animated extends decorate(AnimatedBase) {}