--[[
8Out

Just incase:
A game Heavily inspired by Dactyl.
All graphics are new, No original sounds were used 
and code is all written by me.
]]--

dofile("./data.lua")

--[[
Load Images
]]--
images = {}
images.bomb = Image.load("./DATA/IMG/dactylBomb.png")
images.bomb_exp = Image.load("./DATA/IMG/dactylBombExplode.png")
images.back1 = Image.load("./DATA/IMG/Main.png")
images.back2 = Image.load("./DATA/IMG/PLAY.png")
images.select = Image.load("./DATA/IMG/select.png")
images.match = Image.load("./DATA/IMG/MATCH.PNG")
images.exp = Image.load("./DATA/IMG/explosion.PNG")
images.splash1 = Image.load("./DATA/IMG/logo1.png")
images.splash2 = Image.load("./DATA/IMG/logo2.png")


--Font
foo = Font.load("./foo.ttf")
foo:setPixelSizes(0,22)

--Mode
mode = 1

last_lit = 1
--1 = menu, 2 = play, 3 = highscores

--This options are for menu
menu_select = 1
max_menu_select = 3

splash_fade = 0
fade_timer = 0

function splash1()
	screen:clear(Color.new(0,0,0,splash_fade))
	screen:blit(0,0,images.splash1)
	fade_timer = fade_timer + 1
	if fade_timer>100 then
		splash_fade = splash_fade + 2
	end
	
	if fade_timer>100+255/2 then
		fade_timer = 0
		mode = 2
	end
end

function splash2()
	screen:clear(Color.new(0,0,0,splash_fade))
	screen:blit(0,0,images.splash2)
	fade_timer = fade_timer + 1
	if fade_timer>100 then
		splash_fade = splash_fade + 2
	end
	
	if fade_timer>100+255/2 then
		fade_timer = 0
		mode = 3
	end
end
	
	
function menu()
	screen:clear(Color.new(0,0,0))
	
	screen:blit(0,0,images.back1)
	
	if pad:down() and pad:down() ~= oldpad:down() then
		menu_select = menu_select + 1
	end
	
	if pad:up() and pad:up() ~= oldpad:up() then
		menu_select = menu_select - 1
	end
	
	if menu_select==1 then
		screen:blit(0,130,images.select)
	end
	if menu_select==2 then
		screen:blit(0,180,images.select)
	end
	if menu_select==3 then
		screen:blit(430,15,images.match)
	end
	
	if menu_select>3 then
		menu_select = 1 
	end
	if menu_select < 1 then
		menu_select = 3
	end
	
	if pad:cross() and pad:cross()~= oldpad:cross() then
		if menu_select == 1 then
			mode = 4
		end
		if menu_select == 2 then
			mode = 5 
		end
		if menu_select == 3 then
			os.exit()
		end
	end
	
end

function credits()
	screen:clear()
	screen:fontPrint(foo,0,20,"Splash screen 1 By me71",Color.new(0,255,0))
	screen:fontPrint(foo,0,50,"Splash screen 2 By MHX AIR", Color.new(0,255,0))
	screen:fontPrint(foo,0,80,"Graphics and Coding By 10$man", Color.new(0,255,0))
	screen:fontPrint(foo,0,110,"Sound effects from: No Sounds O_O", Color.new(0,255,0))
	screen:fontPrint(foo,0,140,"Game by Team Hit n' Run",Color.new(0,0,255))
	screen:fontPrint(foo,0,180,"Please support by Joining: bb.waratte.us",Color.new(255,0,0))
	screen:fontPrint(foo,0,240,"Press O to return to previous menu",Color.new(0,255,0))
	if pad:circle() and pad:circle()~=oldpad:circle() then
		mode = 3
	end
end

--Play mode variables
bomb = {}
for a = 1, 8 do
	bomb[a] = 1
end


--2=lit
bomb_timer = 0
bomb_speed = 20
bomb_speedup=1
in_game = 1
reset = 0
score = 0


function play_mode()
	screen:clear()
	screen:blit(0,0,images.back2)
	screen:fontPrint(foo,5,70,"Score: "..score.."   Highscore"..high_score,Color.new(0,0,0))
	if reset == 0 then
		if score > high_score then
			high_score = score
			sc = io.open("DATA.LUA","w")
			sc:write("high_score = ".. high_score)
			sc:close()
		end
		score = 0
		bomb_timer=0
		for a = 1, 8 do
			bomb[a] = 1
		end
		dofile("./DATA.LUA")
		reset = 1
	end
	
	if reset == 1 then
		if in_game == 1 then

			bomb_timer = bomb_timer + 1
			if bomb_timer > bomb_speed then
				bomb_timer = 1
				bomb_speedup = bomb_speedup+1
				bomb_speed = bomb_speed - bomb_speedup
				
				if bomb_speed < 10 then
					bomb_speed = 10
				end
				if bomb[last_lit] == 2 and bomb_speed < 20 then
					in_game = 0
				end
				last_lit = math.random(1,8)
				bomb[last_lit] = 2
			end
			
			if pad:left() and pad:left()~= oldpad:left() then
				if bomb[1] == 2 then
					bomb[1] = 1
					score = score + 1
				elseif bomb[1] == 1 then
					in_game = 0
					reset = 0
				end
			end
			if pad:up() and pad:up()~= oldpad:up() then
				if bomb[2] == 2 then
					bomb[2] = 1
					score = score + 1
				elseif bomb[2] == 1 then
					in_game = 0
					reset = 0
				end
			end
			if pad:right() and pad:right()~= oldpad:right() then
				if bomb[3] == 2 then
					bomb[3] = 1
					score = score + 1
				elseif bomb[3] == 1 then
					in_game = 0
					reset = 0
				end
			end
			if pad:down() and pad:down()~= oldpad:down() then
				if bomb[4] == 2 then
					bomb[4] = 1
					score = score + 1
				elseif bomb[4] == 1 then
					in_game = 0
					reset = 0
				end
			end
			if pad:square() and pad:square()~= oldpad:square() then
				if bomb[5] == 2 then
					bomb[5] = 1
					score = score + 1
				elseif bomb[5] == 1 then
					in_game = 0
					reset = 0
				end
			end
			if pad:triangle() and pad:triangle()~= oldpad:triangle() then
				if bomb[6] == 2 then
					bomb[6] = 1
					score = score + 1
				elseif bomb[6] == 1 then
					in_game = 0
					reset = 0
				end
			end
			if pad:circle() and pad:circle()~= oldpad:circle() then
				if bomb[7] == 2 then
					bomb[7] = 1
					score = score + 1
				elseif bomb[7] == 1 then
					in_game = 0
					reset = 0
				end
			end
			if pad:cross() and pad:cross()~= oldpad:cross() then
				if bomb[8] == 2 then
					bomb[8] = 1
					score = score + 1
				elseif bomb[8] == 1 then
					in_game = 0
					reset = 0
				end
			end
		end
	end

	if bomb[1] == 1 then
		screen:blit(32,130,images.bomb)
	else
		screen:blit(32,130,images.bomb_exp)
	end
	if bomb[2] == 1 then
		screen:blit(96,130-64,images.bomb)
	else
		screen:blit(96,130-64,images.bomb_exp)
	end
	if bomb[3] == 1 then
		screen:blit(64+96,130,images.bomb)
	else
		screen:blit(64+96,130,images.bomb_exp)
	end
	if bomb[4] == 1 then
		screen:blit(96,130+64,images.bomb)
	else
		screen:blit(96,130+64,images.bomb_exp)
	end
	
	if bomb[5] == 1 then
		screen:blit(480-128-96,130,images.bomb)
	else
		screen:blit(480-128-96,130,images.bomb_exp)
	end
	if bomb[6] == 1 then
		screen:blit(480-96-64,130-64,images.bomb)
	else
		screen:blit(480-96-64,130-64,images.bomb_exp)
	end
	if bomb[7] == 1 then
		screen:blit(480-96,130,images.bomb)
	else
		screen:blit(480-96,130,images.bomb_exp)
	end
	if bomb[8] == 1 then
		screen:blit(480-96-64,130+64,images.bomb)
	else
		screen:blit(480-96-64,130+64,images.bomb_exp)
	end
	if in_game == 0 then
		screen:blit(0,0,images.exp)
		screen:fontPrint(foo,0,32,"Press R to Reset, Press Circle to exit",Color.new(0,255,0))
		if pad:r() then
			reset = 0
			in_game = 1
		end
		if pad:circle() then
			mode = 3
		end
	end
end
oldpad=Controls.read()
while true do
pad = Controls.read()
if mode==1 then
	splash1()
end
if mode==2 then
	splash2()
end
if mode==3 then
	menu()
end
if mode==4 then
	play_mode()
end
if mode==5 then
	credits()
end
screen.flip()
screen.waitVblankStart()
oldpad=pad
end