UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

173 lines (150 loc) 3.48 kB
import { combine_hash } from "../../../core/collection/array/combine_hash.js"; import List from '../../../core/collection/list/List.js'; import { computeHashFloat } from "../../../core/primitives/numbers/computeHashFloat.js"; import { AnimationClip } from "./AnimationClip.js"; import { AnimationFlags } from "./AnimationFlags.js"; /** * @class */ export class Animation { /** * * @param options * @property {List.<AnimationClip>} clips * @constructor */ constructor(options) { /** * * @type {List<AnimationClip>} */ this.clips = new List(); this.debtTime = 0; if (options !== undefined) { this.fromJSON(options); } /** * @protected * @type {Future|null} */ this.mixer = null; /** * * @type {number} */ this.flags = AnimationFlags.MeshSizeCulling | AnimationFlags.Playing; } /** * * @return {boolean} */ get isPlaying() { return this.getFlag(AnimationFlags.Playing); } /** * * @param {boolean} v */ set isPlaying(v) { this.writeFlag(AnimationFlags.Playing, v); } /** * * @param {number|AnimationFlags} flag * @returns {void} */ setFlag(flag) { this.flags |= flag; } /** * * @param {number|AnimationFlags} flag * @returns {void} */ clearFlag(flag) { this.flags &= ~flag; } /** * * @param {number|AnimationFlags} flag * @param {boolean} value */ writeFlag(flag, value) { if (value) { this.setFlag(flag); } else { this.clearFlag(flag); } } /** * * @param {number|AnimationFlags} flag * @returns {boolean} */ getFlag(flag) { return (this.flags & flag) === flag; } /** * * @param {Animation} other * @returns {boolean} */ equals(other) { if (other === undefined || other === null) { return false; } if (other.isAnimation !== true) { return false; } return this.clips.equals(other.clips) && this.debtTime === other.debtTime; } /** * * @returns {number} */ hash() { return combine_hash( this.clips.hash(), computeHashFloat(this.debtTime) ); } /** * * @param json * @returns {Animation} */ static fromJSON(json) { const a = new Animation(); a.fromJSON(json); return a; } fromJSON(json) { if (json.clips instanceof Array) { this.clips.fromJSON(json.clips, AnimationClip); } else { this.clips.reset(); } if (typeof json.debtTime === "number") { this.debtTime = json.debtTime; } else { this.debtTime = 0; } } toJSON() { return { clips: this.clips.toJSON(), debtTime: this.debtTime }; } } /** * @readonly * @type {boolean} */ Animation.prototype.isAnimation = true; /** * @readonly * @type {string} */ Animation.typeName = "Animation";