@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
84 lines (69 loc) • 2.2 kB
JavaScript
export class AnimationRule {
constructor(startEvent, animationName, speed, transitionTime, loop = false) {
this.startEvent = startEvent;
this.stopEvent = null;
this.animation = animationName;
this.speed = speed;
this.transition = transitionTime;
this.weight = 1;
this.loop = loop;
}
toJSON() {
return {
startEvent: this.startEvent,
stopEvent: this.stopEvent,
animation: this.animation,
speed: this.speed,
transition: this.transition,
loop: this.loop,
weight: this.weight
};
}
fromJSON(json) {
this.startEvent = json.startEvent;
this.stopEvent = json.stopEvent;
this.animation = json.animation;
if (typeof json.speed === "number") {
this.speed = json.speed;
} else {
this.speed = 1;
}
this.transition = json.transition;
if (typeof json.loop === "boolean") {
this.loop = json.loop;
} else {
this.loop = false;
}
if (typeof json.weight === "number") {
this.weight = json.weight;
} else {
this.weight = 1;
}
}
/**
*
* @param {BinaryBuffer} buffer
*/
toBinaryBuffer(buffer) {
buffer.writeUTF8String(this.animation);
buffer.writeUTF8String(this.startEvent);
buffer.writeUTF8String(this.stopEvent);
buffer.writeUint8(this.loop ? 1 : 0);
buffer.writeFloat64(this.weight);
buffer.writeFloat64(this.transition);
buffer.writeFloat64(this.speed);
}
/**
*
* @param {BinaryBuffer} buffer
*/
fromBinaryBuffer(buffer) {
this.animation = buffer.readUTF8String();
this.startEvent = buffer.readUTF8String();
this.stopEvent = buffer.readUTF8String();
this.loop = buffer.readUint8() !== 0;
this.weight = buffer.readFloat64();
this.transition = buffer.readFloat64();
this.speed = buffer.readFloat64();
}
}