var Class = require('../../utils/Class'); var Animation = new Class({ initialize: function Animation(parent){ this.parent = parent; this.animationManager = parent.scene.sys.anims; this.animationManager.once('remove', this.remove, this); this.isPlaying = false ; this.currentAnim = null ; this.currentFrame = null ; this._timeScale = 1; this.frameRate = 0; this.duration = 0; this.msPerFrame = 0; this.skipMissedFrames = true ; this._delay = 0; this._repeat = 0; this._repeatDelay = 0; this._yoyo = false ; this.forward = true ; this.accumulator = 0; this.nextTick = 0; this.repeatCounter = 0; this.pendingRepeat = false ; this._paused = false ; this._wasPlaying = false ; this._callbackArgs = [parent, null ] ; this._updateParams = [] ; } , delay: function (value){ if (value === undefined) { return this._delay; } else { this._delay = value; return this; } } , delayedPlay: function (delay, key, startFrame){ this.play(key, true , startFrame); this.nextTick += (delay * 1000); return this; } , getCurrentKey: function (){ if (this.currentAnim) { return this.currentAnim.key; } } , load: function (key, startFrame){ if (startFrame === undefined) { startFrame = 0; } if (this.isPlaying) { this.stop(); } _AN_Call_load('load', this.animationManager, this, key, startFrame); return this; } , pause: function (atFrame){ if (!this._paused) { this._paused = true ; this._wasPlaying = this.isPlaying; this.isPlaying = false ; } if (atFrame !== undefined) { this.updateFrame(atFrame); } return this; } , paused: function (value){ if (value !== undefined) { if (value) { return this.pause(); } else { return this.resume(); } } else { return this._paused; } } , play: function (key, ignoreIfPlaying, startFrame){ if (ignoreIfPlaying === undefined) { ignoreIfPlaying = false ; } if (startFrame === undefined) { startFrame = 0; } if (ignoreIfPlaying && this.isPlaying && this.currentAnim.key === key) { return this; } _AN_Call_load('load', this, key, startFrame); var anim = this.currentAnim; var gameObject = this.parent; this.repeatCounter = (this._repeat === -1)? Number.MAX_VALUE: this._repeat; anim.getFirstTick(this); this.forward = true ; this.isPlaying = true ; this.pendingRepeat = false ; if (anim.showOnStart) { gameObject.visible = true ; } if (anim.onStart) { anim.onStart.apply(anim.callbackScope, this._callbackArgs.concat(anim.onStartParams)); } gameObject.setSizeToFrame(); gameObject.updateDisplayOrigin(); return this; } , progress: function (value){ if (value === undefined) { var p = this.currentFrame.progress; if (!this.forward) { p = 1 - p; } return p; } else { return this; } } , remove: function (event){ if (event === undefined) { event = this.currentAnim; } if (this.isPlaying && event.key === this.currentAnim.key) { this.stop(); var sprite = this.parent; var frame = this.currentAnim.frames[0]; this.currentFrame = frame; sprite.texture = frame.frame.texture; sprite.frame = frame.frame; } } , repeat: function (value){ if (value === undefined) { return this._repeat; } else { this._repeat = value; this.repeatCounter = 0; return this; } } , repeatDelay: function (value){ if (value === undefined) { return this._repeatDelay; } else { this._repeatDelay = value; return this; } } , restart: function (includeDelay){ if (includeDelay === undefined) { includeDelay = false ; } this.currentAnim.getFirstTick(this, includeDelay); this.forward = true ; this.isPlaying = true ; this.pendingRepeat = false ; this._paused = false ; this.updateFrame(this.currentAnim.frames[0]); return this; } , resume: function (fromFrame){ if (this._paused) { this._paused = false ; this.isPlaying = this._wasPlaying; } if (fromFrame !== undefined) { this.updateFrame(fromFrame); } return this; } , stop: function (dispatchCallbacks){ if (dispatchCallbacks === undefined) { dispatchCallbacks = false ; } this.isPlaying = false ; var anim = this.currentAnim; if (dispatchCallbacks && anim.onComplete) { anim.onComplete.apply(anim.callbackScope, this._callbackArgs.concat(anim.onCompleteParams)); } return this; } , timeScale: function (value){ if (value === undefined) { return this._timeScale; } else { this._timeScale = value; return this; } } , totalFrames: function (){ return (_AN_Read_length('length', this.currentAnim.frames)); } , totalProgres: function (){ } , update: function (timestamp, delta){ if (!this.isPlaying || this.currentAnim.paused) { return ; } this.accumulator += delta * this._timeScale; if (this.accumulator >= this.nextTick) { this.currentAnim.setFrame(this); } } , updateFrame: function (animationFrame){ var sprite = this.parent; this.currentFrame = animationFrame; sprite.texture = animationFrame.frame.texture; sprite.frame = animationFrame.frame; if (this.isPlaying) { if (animationFrame.setAlpha) { sprite.alpha = animationFrame.alpha; } var anim = this.currentAnim; if (anim.onUpdate) { anim.onUpdate.apply(anim.callbackScope, this._updateParams); } if (animationFrame.onUpdate) { animationFrame.onUpdate(sprite, animationFrame); } } } , yoyo: function (value){ if (value === undefined) { return this._yoyo; } else { this._yoyo = value; return this; } } , destroy: function (){ } } ); module.exports = Animation;