-- animation system for getting sprites based on timings... woo ....ing hoo!


AnimationModel = class(function(o, sprites, ...)

	-- setup the defaults
	local arg = {...}
	arg[1] = arg[1] or {}
	if arg[1].loop == nil then o.loop = true else o.loop = arg[1].loop end
	o.rate = arg[1].rate or 120 -- the rate in ms, this is 8 frames per second
  	o.endCallback = arg[1].endCallback or function() end
	o.sprites = sprites
	o.startTime = nil
end)
function AnimationModel:startAnimation()
	-- get the start time off of the bludG... this will change the timings of the rest of the sprites
	self.startTime = bludG.last_t
end
function AnimationModel:getSprite()
	local arrayLength = #self.sprites
	local currentTime = bludG.last_t
	if not self.startTime then self:startAnimation() end
	
	local currentFrame = math.floor(((currentTime - self.startTime)*1000) / self.rate)+1
	local returnframe;
	currentFrame = currentFrame
	returnframe = self.sprites[currentFrame]

	-- if we are not looping, then check to see if we are past the total time
	if not self.loop and currentFrame > #self.sprites then
		-- since we are still calling this, lets call the endcallback
		self.endCallback()
		-- self.startTime = nil
		-- if we are past the total time, then just return the last frame
		returnframe = self.sprites[#self.sprites]
	end
	if self.loop and currentFrame > #self.sprites then
		self:startAnimation()
		return self:getSprite()
	end
	return returnframe
end