feat: animation type and framerate packet

This commit is contained in:
cha0s 2021-02-10 14:20:56 -06:00
parent 18ff2a7bb4
commit 1ff08e8fda
7 changed files with 66 additions and 23 deletions

View File

@ -31,6 +31,8 @@ export default {
types: [
'@avocado/math',
'@avocado/entity',
'@avocado/timing',
// ...
'@avocado/behavior',
],
'types.decorate': [

View File

@ -0,0 +1,13 @@
import {JsonResource} from '@avocado/resource';
export default class BaseAnimation extends JsonResource {
static children() {
return {
frameRate: {
type: 'number',
},
};
}
}

View File

@ -2,6 +2,7 @@ import {
decorateWithLatus,
gatherWithLatus,
} from '@latus/core';
import types from './types';
import typesDecorator from './types-decorator';
export {
@ -19,6 +20,7 @@ export {default as Transition, TransitionResult} from './transition';
export default {
hooks: {
'@avocado/behavior/types': types,
'@avocado/behavior/types.decorate': typesDecorator,
'@avocado/resource/resources': gatherWithLatus(
require.context('./resources', false, /\.js$/),

View File

@ -5,6 +5,7 @@ export default () => class TraitUpdateAnimatedPacket extends Packet {
static get data() {
return {
currentAnimation: 'string',
frameRate: 'float32',
isAnimating: 'bool',
};
}

View File

@ -1,8 +1,8 @@
import {Property} from '@avocado/core';
import {Rectangle, Vector} from '@avocado/math';
import {JsonResource} from '@avocado/resource';
import {compose, EventEmitter} from '@latus/core';
import BaseAnimation from '../base-animation';
import TimedIndex from '../timed-index';
const decorate = compose(
@ -20,7 +20,7 @@ const decorate = compose(
}),
);
export default () => class Animation extends decorate(JsonResource) {
export default () => class Animation extends decorate(BaseAnimation) {
constructor() {
super();

View File

@ -25,6 +25,8 @@ export default (latus) => class Animated extends decorate(Trait) {
#currentAnimation = '';
#forceUpdate = false;
constructor() {
super();
({
@ -35,10 +37,38 @@ export default (latus) => class Animated extends decorate(Trait) {
acceptPacket(packet) {
if ('TraitUpdateAnimated' === packet.constructor.type) {
this.entity.currentAnimation = packet.data.currentAnimation;
this.animation.frameRate = packet.data.frameRate;
this.entity.isAnimating = packet.data.isAnimating;
}
}
get animation() {
return this.#animations[this.#currentAnimation];
}
static children() {
return {
animation: {
type: 'animation',
},
isAnimating: {
type: 'bool',
label: 'Is animating',
},
currentAnimation: {
type: 'string',
label: 'Current animation',
options: (entity) => (
Object.keys(entity.trait('Animated').params.animations)
.map((key) => ({
label: key,
value: key,
}))
),
},
};
}
static defaultParams() {
return {
animations: {},
@ -58,26 +88,6 @@ export default (latus) => class Animated extends decorate(Trait) {
];
}
static children() {
return {
isAnimating: {
type: 'bool',
label: 'Is animating',
},
currentAnimation: {
type: 'string',
label: 'Current animation',
options: (entity) => (
Object.keys(entity.trait('Animated').params.animations)
.map((key) => ({
label: key,
value: key,
}))
),
},
};
}
destroy() {
if (this.#animationViews) {
const animationViews = Object.entries(this.#animationViews);
@ -175,6 +185,11 @@ export default (latus) => class Animated extends decorate(Trait) {
}
},
setAnimationRate: (rate) => {
this.#animations[this.#currentAnimation].frameRate = rate;
this.#forceUpdate = true;
},
startedDying: () => {
this.isAnimating = false;
},
@ -233,11 +248,13 @@ export default (latus) => class Animated extends decorate(Trait) {
packets() {
const {currentAnimation, isAnimating} = this.stateDifferences();
if (currentAnimation || isAnimating) {
if (this.#forceUpdate || currentAnimation || isAnimating) {
this.#forceUpdate = false;
return [[
'TraitUpdateAnimated',
{
currentAnimation: this.state.currentAnimation,
frameRate: this.animation.frameRate,
isAnimating: this.state.isAnimating,
},
]];

View File

@ -0,0 +1,8 @@
import BaseAnimation from './base-animation';
export default () => ({
animation: {
children: BaseAnimation.children,
infer: (v) => v?.constructor?.children === BaseAnimation.children,
},
});