gamehud = {
	center = {x=320,y=50},
	textures = {
		zcircle = texture.load("hud/zcircle.tga",false),
		point = texture.load("hud/point.tga",false),
		hudcircle = texture.load("hud/hudcircle.tga",false),
		present = texture.load("hud/present.tga",false),
	},
	scalex = 0.8,scaley = 0.8,
	points = {},
	l2ds = {},
	isvisible = true
}

function gamehud.visible (b)
	gamehud.isvisible = b
	for i,v in pairs(gamehud.l2ds) do
		v:rfNodraw(not b)
	end
	for i,v in pairs(gamehud.points) do
		v.l2d:rfNodraw(not b)
	end
end

function gamehud.addtext (x,y,str)
	local self = {}
	self.l2d = l2dtext.new("text",str or "",xmasfont)
	self.l2d:parent(l2dlist.getroot())
	self.l2d:pos(x,y,-1)
	self.l2d:spacing(12)
	
	table.insert(gamehud.l2ds,self.l2d)

	return self
end

function gamehud.addimage (img, x,y,w,h)
	local self = {l2d = l2dimage.new(img,texture.load(img,false))}
	self.l2d:parent(l2dlist.getroot())
	self.l2d:pos(x,y,0)
	self.l2d:scale(w,h,1)
	table.insert(gamehud.l2ds,self.l2d)
	return self
end

function gamehud.addpoint (x,y)
	local self = {}
	
	self.l2d = l2dimage.new("point",gamehud.textures.point)
	self.l2d:parent(l2dlist.getroot())
	self.x,self.y = x,y
	self.w = 4
	self.h = 4
	self.l2d:scale(self.w,self.h,1)
	self.l2d:rfBlend(true)
	self.l2d:rsBlendmode(blendmode.decal())
--	self.l2d:rsBlendinvert(true)
	self.l2d:color(1,0,0,1)
	self.l2d:sortid(2)
	
	table.insert(gamehud.points,self)
	
	function self.deactivate (self)
		self.l2d:color(0.3,0.3,1,0.4)
		self.l2d:sortid(1)
	end
	
	function self.delete (self)
		self.l2d:delete()
		for i,v in ipairs(gamehud.points) do
			if (v==self) then
				table.remove(gamehud.points,i)
				break
			end
		end
	end
	
	return self
end

function gamehud.updatescore (self)
	self.scoretext = l2dtext.new("","")		
	self.scoretext:parent(l2dlist.getroot())
	self.scoretext:pos(10,440,0)
	self.scoretext:scale(0.8,1,1)
	self.scoretext:spacing(8)
	
	self.scoretext:text(string.format("score: %i, %i hits, %i misses, %i rows, %i maxrows",
		self.player.score, self.player.hits, self.player.misses, self.player.rowhits,
		self.player.maxrowhits))
end

do
	local smallpos = 
		{{0,0},{-10,8},
		 {16,5},{6,13},
		 {0,-14},{-10,-6},
		 {16,-9},{6,-1}}
	local right,bottom = 630,470

	function gamehud.hit (game)
		local ps,ps2 = 60,30
		local scale = 1
		gamehud.boxstack = gamehud.boxstack or {}
		local n = game.player.rowhits
		local mod = table.getn(smallpos)+1
		local mn = math.mod(n,mod)
		local h = math.floor((n-1)/mod)*ps*0.5
		if (mn==0) then
			for i=1,mod-2 do table.remove(gamehud.boxstack):delete() end
			local img = gamehud.boxstack[table.getn(gamehud.boxstack)]
			img:scale(ps*scale,ps*scale,1)
			img:pos(right-ps,bottom-ps-h,0)
		else
			local img = l2dimage.new("present",gamehud.textures.present)
			local x,y = smallpos[mn][1],smallpos[mn][2]
--			_print(x,y)
			img:parent(l2dlist.getroot())
			img:pos(right-ps*0.78+x,bottom-h+y-ps2*1.5,0)
			img:rfBlend(true)
			img:rsBlendmode(blendmode.decal())
			img:scale(ps2*scale,ps2*scale,1)
			table.insert(gamehud.boxstack,img)
		end
	end
end

function gamehud.miss (game)
	if (not gamehud.boxstack) then return end
	for i,v in ipairs(gamehud.boxstack) do
		v:delete()
	end
	gamehud.boxstack = {}
end

function gamehud.clear ()
	if (not gamehud.boxstack) then return end
	for i,v in ipairs(gamehud.boxstack) do
		v:delete()
	end
	gamehud.boxstack = {}
end

function gamehud.update(x,y,rot)
	local maxd = 48
	for a,b in ipairs(gamehud.points) do 
		local scale = 0.1
		local px,py = (b.x-x)*scale,(-b.y+y)*scale
		local c,s = math.cos(rot),math.sin(rot)
		px,py = c*px+s*py,c*py-s*px
		
		local dist = (px*px+py*py)^0.5
		local s = 6

		if (dist>maxd) then
			px = maxd*px/dist
			py = maxd*py/dist
			s = 3
		end
		b.w,b.h = s*gamehud.scalex,s*gamehud.scaley
		px,py = px*gamehud.scalex,py*gamehud.scaley
		--px,py = -px,py
		
		b.l2d:scale(b.w,b.h,1)
		b.l2d:pos(	-b.w*0.5+gamehud.center.x+px,
					-b.h*0.5+gamehud.center.y+py,0);
	end
end

function gamehud.init () 
--[[gamehud.l2ds.zcircle = l2dimage.new("zcircle",gamehud.textures.zcircle)
	gamehud.l2ds.zcircle:scale(128,128,1)
	gamehud.l2ds.zcircle:pos(gamehud.center.x-64,gamehud.center.y-64,-10)
	gamehud.l2ds.zcircle:rfAlphatest(true)
	gamehud.l2ds.zcircle:rfBlend(true)
	gamehud.l2ds.zcircle:rsBlendmode(blendmode.add())
	gamehud.l2ds.zcircle:rsAlphacompare(comparemode.greater())
	gamehud.l2ds.zcircle:rsAlphavalue(0.5)
	gamehud.l2ds.zcircle:sortid(-1)]]
	
	gamehud.l2ds.circle = l2dimage.new("hudcircle",gamehud.textures.hudcircle)
	gamehud.l2ds.circle:parent(l2dlist.getroot())
	local sx,sy = gamehud.scalex,gamehud.scaley
	gamehud.l2ds.circle:scale(128*sx,128*sy,1)
	gamehud.l2ds.circle:pos(gamehud.center.x-64*sx,gamehud.center.y-64*sy,1)
	gamehud.l2ds.circle:rfBlend(true)
	gamehud.l2ds.circle:rfNodepthmask(true)
	gamehud.l2ds.circle:rfNodepthtest(true)
	gamehud.l2ds.circle:rsBlendmode(blendmode.modulate())
	gamehud.l2ds.circle:sortid(0)


	gamehud.timerpos = {320,445}
	gamehud.timershad = gamehud.addtext(300,450,"")
	gamehud.timershad.l2d:color(0,0,0,0.7)
	gamehud.timer = gamehud.addtext(300,450,"")
	gamehud.timer.l2d:color(1,0,0,1)

	gamehud.timerimg = gamehud.addimage("hud/time.tga",320-16,450-32,32,32)
	gamehud.timerimg.l2d:rfBlend(true)
	gamehud.timerarr = gamehud.addimage("hud/timearr.tga",320-16,450-32,32,32)
	gamehud.timerarr.l2d:rfBlend(true)
	gamehud.timerarr.l2d:rfNodraw(true)
	gamehud.timerarr.l2d:rotcenter(16,16,0)
	gamehud.timerarr2 = gamehud.addimage("hud/timearr.tga",320-16,450-32,32,32)
	gamehud.timerarr2.l2d:rfBlend(true)
	gamehud.timerarr2.l2d:rotcenter(16,16,0)
	
	gamehud.scoreshad = gamehud.addtext(40,454+2,"SCORE")
	gamehud.score = gamehud.addtext(40,454,"SCORE")
	gamehud.scoreshad.l2d:color(0,0,0,0.7)
	gamehud.score.l2d:color(1,0,0,1)
	
end

function gamehud.setscorefn (fn)
	gamehud.scorefn = fn
end

function gamehud.setcurposfn (fn)
	gamehud.currentposfn = fn
end

function gamehud.settimer (fn)
	gamehud.timerfn = fn
end

do
	function thinkhud () 
		if (gamehud.currentposfn) then 
			local x,y,rot = gamehud.currentposfn()
			gamehud.update(x,y,rot)
		end
		if (gamehud.timerfn) then
			local t,pt,mtime = gamehud.timerfn()
			local s = t/1000
			local m = math.floor(s/60)
			local ms = math.mod(s,1)*100
			local str = string.format("%0.2i:%0.2i.%0.2i",m,math.mod(s,60),ms)
			local len = string.len(str)
			gamehud.timer.l2d:text(str)
			gamehud.timer.l2d:pos(gamehud.timerpos[1]-6*len,gamehud.timerpos[2]+8,0)
			gamehud.timershad.l2d:text(str)
			gamehud.timershad.l2d:pos(gamehud.timerpos[1]-6*len,gamehud.timerpos[2]+8+2,0)
			gamehud.timerimg.l2d:pos(gamehud.timerpos[1]-16,gamehud.timerpos[2]-30,0)
			gamehud.timerarr.l2d:pos(gamehud.timerpos[1]-16,gamehud.timerpos[2]-30,0)
			gamehud.timerarr.l2d:rotdeg(0,0,t*0.1)
			gamehud.timerarr.l2d:rfNodraw(true)
			gamehud.timerarr2.l2d:pos(gamehud.timerpos[1]-16,gamehud.timerpos[2]-30,0)
			gamehud.timerarr2.l2d:rotdeg(0,0,pt/mtime*360)
		end
		if (gamehud.scorefn) then
			local str = string.format("Score: %i",gamehud.scorefn())
			gamehud.score.l2d:text(str)
			gamehud.scoreshad.l2d:text(str)
		end
	end
	
	Timer.set("thinkhud",thinkhud)
end

gamehud.init()

Keyboard.setKeyBinding (string.byte('5'),function () gamehud.visible(not gamehud.isvisible) end)
gamehud.visible(false)