feat: animation type and framerate packet
This commit is contained in:
parent
18ff2a7bb4
commit
1ff08e8fda
|
@ -31,6 +31,8 @@ export default {
|
|||
types: [
|
||||
'@avocado/math',
|
||||
'@avocado/entity',
|
||||
'@avocado/timing',
|
||||
// ...
|
||||
'@avocado/behavior',
|
||||
],
|
||||
'types.decorate': [
|
||||
|
|
13
packages/timing/src/base-animation.js
Normal file
13
packages/timing/src/base-animation.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
import {JsonResource} from '@avocado/resource';
|
||||
|
||||
export default class BaseAnimation extends JsonResource {
|
||||
|
||||
static children() {
|
||||
return {
|
||||
frameRate: {
|
||||
type: 'number',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
}
|
|
@ -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$/),
|
||||
|
|
|
@ -5,6 +5,7 @@ export default () => class TraitUpdateAnimatedPacket extends Packet {
|
|||
static get data() {
|
||||
return {
|
||||
currentAnimation: 'string',
|
||||
frameRate: 'float32',
|
||||
isAnimating: 'bool',
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
]];
|
||||
|
|
8
packages/timing/src/types.js
Normal file
8
packages/timing/src/types.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
import BaseAnimation from './base-animation';
|
||||
|
||||
export default () => ({
|
||||
animation: {
|
||||
children: BaseAnimation.children,
|
||||
infer: (v) => v?.constructor?.children === BaseAnimation.children,
|
||||
},
|
||||
});
|
Loading…
Reference in New Issue
Block a user