-- ANIMATION LIBRARY 4 FIXED --
-- GRIMFATE126 --
-- 5/12/07 --


-- FUNCTION METATABLE (NO USES AS OF NOW) --

ANIM = {} 

----------------------------------------------------------|
----------------------------------------------------------|
-- SIMPLE FUNCTIONS -----------------------|
----------------------------------------------------------|
----------------------------------------------------------|


-- LOAD ANIMATION --

function ANIM.new(header, extension, frames, where)

where = where or "./"

frames = frames
header = header
extension = extension

local load

local c = setmetatable(
	{
		interval = 0,
		currentFrame = 1,
		maxFrame = frames,
		looped = 0, -- Looping or not --
		loops = 1, -- How many loops have occured --
		frame = {},
		timer = Timer.new(),
	},
    	{
     		__index = ANIM
    	}
)

for load = 1, frames do
	c.frame[load] = Image.load(where..header..load.."."..extension) 
end

return c

end



-- BLIT ANIMATION --

function ANIM:blit(x, y, delay, numLoops)

numLoops = numLoops or 0

if numLoops == 0 then
	self.looped = 0
	self.loops = 0
else
	self.looped = 1
	if self.loops == 0 then -- Just in case the animation reverts back to looped --
		self.loops = 1
	end
end

self.interval = delay
self.timer:start()

	if self.loops <= numLoops then

		if self.timer:time() >= self.interval then
			self.currentFrame = self.currentFrame + 1
			self.timer:reset()
		end	

		if self.currentFrame > self.maxFrame then
			if self.looped == 1 then
				self.loops = self.loops + 1
			end
			self.currentFrame = 1
		end 

		Image.blit(x, y, self.frame[self.currentFrame])

	end
end


-- PAUSE ANIMATION --

function ANIM:pause()
	self.timer:stop()
end

-- RESUME ANIMATION --

function ANIM:resume()
	self.timer:start()
end

-- RESET ANIMATION --

function ANIM:reset(num)
	num = num or 1
	self.currentFrame = num
end

-- FREE ANIMATION FROM RAM --

function ANIM:free()
	local f
	
	for f = 1, self.maxFrame do
		self.frame[f] = nil
	end
	
	f = nil
	
	collectgarbage()
end

----------------------------------------------------------|
----------------------------------------------------------|
-- ADVANCED FUNCTIONS -----------------|
----------------------------------------------------------|
----------------------------------------------------------|

-- RESTORE LOOP NUMBER --

function ANIM:resetLoop()
	self.loops = 1
end

-- RETURN THE CURRENT TIMER TIME --

function ANIM:getTime()
	return self.timer:time()
end

-- RETURN THE CURRENT FRAME --

function ANIM:getFrame()
	return self.currentFrame
end

-- RETURN THE CURRENT NUMBER OF LOOPS MADE --

function ANIM:getLoops()
	return self.loops
end



----------------------------------------------------------|
----------------------------------------------------------|
-- SPRITESHEET FUNCTIONS -----------------|
----------------------------------------------------------|
----------------------------------------------------------|


SS = {} 


-- LOAD SPRITESHEET --

function SS.new(header, extension, nWidth, nHeight, where)

where = where or "./"

header = header
extension = extension
nRows = nRows
nColumns = nColumns
nWidth = nWidth
nHeight = nHeight


local c = setmetatable(
	{
		interval = 0,
		width = nWidth,
		height = nHeight,
		ppR = 0,	-- Pics Per Row --	
		ppC = 0,	-- Pics Per Column --
		
		cRow = 0,
		cColumn = 0,

		looped = 0, -- Looping or not --
		loops = 1, -- How many loops have occured --
		pic = nil,
		timer = Timer.new(),
	},
    	{
     		__index = SS
    	}
)

-- Load Image --

c.pic = Image.load(where..header.."."..extension) 

-- Set Sizes --

c.ppR = (c.pic:width() / c.width) - 1
c.ppC = (c.pic:height() / c.height) - 1

return c

end


-- BLIT SS --

function SS:blit(x, y, delay, numLoops)

numLoops = numLoops or 0

if numLoops == 0 then
	self.looped = 0
	self.loops = 0
else
	self.looped = 1
	if self.loops == 0 then -- Just in case the animation reverts back to looped --
		self.loops = 1
	end
end

self.interval = delay
self.timer:start()

	if self.loops <= numLoops then

		-- Loop --
	
		if self.timer:time() >= self.interval then
			self.cColumn = self.cColumn + 1
			self.timer:reset()
		end

		-- Limits --

		if self.cColumn > self.ppR then
			self.cRow = self.cRow + 1
			self.cColumn = 0
		end

		if self.cRow > self.ppC then
			if self.looped == 1 then
				self.loops = self.loops + 1
			end
			self.cRow = 0
		end
	

		Image.blit(x, y, self.pic, self.cColumn * self.width, self.cRow * self.height, self.width, self.height) 
	
	end
end

----------------------------------------------------------|
----------------------------------------------------------|
-- ADVANCED FUNCTIONS -----------------|
----------------------------------------------------------|
----------------------------------------------------------|

-- PAUSE ANIMATION --

function SS:pause()
	self.timer:stop()
end

-- RESUME ANIMATION --

function SS:resume()
	self.timer:start()
end

-- RESET ANIMATION --

function SS:reset()
	self.cRow = 0
	self.cColumn = 0
end

-- FREE ANIMATION FROM RAM --

function SS:free()
	self.pic = nil
	collectgarbage()
end


-- RESTORE LOOP NUMBER --

function SS:resetLoop()
	self.loops = 1
end

-- RETURN THE CURRENT TIMER TIME --

function SS:getTime()
	return self.timer:time()
end

-- RETURN THE CURRENT Row --

function SS:getRow()
	return self.cRow
end

-- RETURN THE CURRENT Column --

function SS:getColumn()
	return self.cColumn
end

-- RETURN THE CURRENT NUMBER OF LOOPS MADE --

function SS:getLoops()
	return self.loops
end

