/**
    Displays an animated Object
    @original author <a href="mailto:matthewcasperson@gmail.com">Matthew Casperson</a>
*/
function AnimatedObject()
{
	gConsole.Log(6, "[ANIM] Init");
    /**
        Defines the current frame that is to be rendered
        @type Number
     */
    this.currentFrame = 0;
    /**
        Defines the frames per second of the animation
        @type Number
     */
    this.timeBetweenFrames = 0;
    /**
        The number of individual frames held in the image
        @type Number
     */
    /**
        Time until the next frame
        @type number
     */
    this.timeSinceLastFrame = 0;
    /**
        The width of each individual frame
        @type Number
     */
    this.frameWidth = 0;

    /**
        Initialises this object
        @param image The image to be displayed
        @param frameCount The number of animation frames in the image
		@param invSpeed Inverse of the speed of the animation
    */
    this.Setup = function(/**Image*/ image, /**Number*/ frameCount, /**Number*/ invSpeed)
    {
		gConsole.AssertMsg(frameCount > 0, "framecount can not be <= 0");
		gConsole.AssertMsg(invSpeed > 0, "invSpeed can not be <= 0");
		
		this.image = image;
        this.currentFrame = 0;
        this.frameCount = frameCount;
        this.timeBetweenFrames = invSpeed;
        this.timeSinceLastFrame = this.timeBetweenFrames;
        this.frameWidth = this.image.width / this.frameCount;
		
		gConsole.Log(4, "[ANIM] Setup '" + image.name + "' frames " + frameCount + " (" + this.frameWidth + ", " + this.image.height + ") at speed 1/" + invSpeed);
    }

    /**
        Draws this element to the back buffer
		@param x Position to draw the animation
		@param y Position to draw the animation
    */
    this.Draw = function(/**Number*/ x, /**Number*/ y)
    {
        var sourceX = this.frameWidth * this.currentFrame;
        gDraw.context.drawImage(this.image, sourceX, 0, this.frameWidth, this.image.height, x - gDraw.canvas.offsetLeft, y - gDraw.canvas.offsetTop, this.frameWidth, this.image.height);

        this.timeSinceLastFrame -= 1;
        if (this.timeSinceLastFrame <= 0)
        {
           this.timeSinceLastFrame = this.timeBetweenFrames;
           this.currentFrame++;
           this.currentFrame %= this.frameCount;
        }
    }
}
