File: animation\System.js
/**
System responsible for updating animations. Requires Animation and Sprite components to be present on an entity.
@class AnimationSystem
@constructor
@param entitySystemManager {Manager} The entity system manager whose entities this system will be working on.
*/
function AnimationSystem(entitySystemManager){
var animationEntities = entitySystemManager.createAspect([Animation, Sprite]);
/**
Updates all animations.
@method update
@param deltaTime {Number}
*/
this.update = function(deltaTime){
animationEntities.iterate(function(entity){
var animationComponent = entity.get(Animation),
spriteComponent = entity.get(Sprite);
if(animationComponent._currentAnimation){
var animation = animationComponent._currentAnimation.getAsset(),
animationEnded = false;
animationComponent._currentFrameTime += deltaTime;
while(animationComponent._currentFrameTime >= animation.frames[animationComponent._currentFrame].frameDuration * animationComponent._rate){
animationComponent._currentFrameTime -= animation.frames[animationComponent._currentFrame].frameDuration * animationComponent._rate;
animationComponent._currentFrame++;
if(animationComponent._currentFrame === animation.frames.length){
if(animation.loop){
animationComponent._currentFrame = 0;
}else{
animationComponent._currentFrame--;
animationEnded = true;
break;
}
}
}
spriteComponent.setSpriteSheetHandle(animation.spriteSheetHandle);
spriteComponent.setSpriteIndex(animation.frames[animationComponent._currentFrame].spriteIndex);
if(animationEnded){
if(animationComponent._finishedCallback){
animationComponent._finishedCallback(animationComponent._callbackData);
}
animationComponent._currentAnimation = null;
}
}
});
}
/**
@method destroy
*/
this.destroy = function(){
animationEntities.destroy();
}
}