dofile("select.ini")

dofile("animlib.lua")

math.randomseed(os.time())

verde = color.new(0,255,0)

blanco = color.new(255,255,255)

rojo = color.new(255,0,0)
bala = image.load("imagenes/bala.png")

bala2 = image.load("imagenes/bala2.png")

misil = image.load("imagenes/misil.png")

misil2 = image.load("imagenes/misil.png")

laser = sound.load("sounds/laser.mp3")
menu1 = image.load("pause/1.png")
menu2 = image.load("pause/2.png")
menu3 = image.load("pause/3.png")
menu4 = image.load("pause/4.png")

win = sound.load("sounds/win.mp3")

pom = sound.load("sounds/pom.wav")

nave = {x = 230,y = 240,w=47,h=46}

fuente = font.load("fuente.pgf")

vida = 100

puntos = 0 

explosion = {}

explosion[1]= image.load("explotion/1.png")

explosion[2]= image.load("explotion/2.png")

explosion[3]= image.load("explotion/3.png")

explosion[4]= image.load("explotion/4.png")

back={}

back[1]=image.load("background/1.png")

back[2]=image.load("background/2.png")

back[3]=image.load("background/3.png")

back[4]=image.load("background/4.png")

back[5]=image.load("background/5.png")

back[6]=image.load("background/6.png")

function nautilus()
	
ini.write("scores.ini","puntos",puntos)

end


if selector == 1 then

nave.img = image.load("spacecraft/nave1.png")

end

if selector == 2 then

nave.img = image.load("spacecraft/nave2.png")

end

if selector == 3 then

nave.img = image.load("spacecraft/nave3.png")

end

if selector == 4 then

nave.img = image.load("spacecraft/nave4.png")

end

if selector == 5 then

nave.img = image.load("spacecraft/nave5.png")

end

------Cargando libreria animaciones----
------AnimLib - Chimecho---------------


dofile("weaponlib.lua")


disparo =  weapon.create(bala2,7,100)


function moverPersonaje()

controls.read()

if math.abs(controls.analogy())<30 and math.abs(controls.analogx())<30 then

	if controls.left() then

	   nave.x = nave.x - 5

	end

	if controls.right() then

	   nave.x = nave.x + 5

	end

	if controls.up() then

	   nave.y = nave.y - 5

	end

	if controls.down() then
	
   nave.y = nave.y + 5

	end

else
if math.abs(controls.analogy())>30 then
 nave.y = nave.y + controls.analogy()/25
 end

if math.abs(controls.analogx())>30 then 
nave.x = nave.x + controls.analogx()/25 
end

end


if nave.x + nave.w >= 480 then
	nave.x = 1

elseif nave.x <= 0 then
	nave.x = 480-nave.w

end

if nave.y + nave.h >= 272 then

	nave.y = 272 - nave.h

elseif nave.y <= 0 then

	nave.y = 0

end


end


meteo = {}

meteoimg = image.load("imagenes/meteo.png")

meteo_t={x=10,y=-30,w=45,h=40}

meteo = {x=nil,y=nil,w=meteoimg:width(),h=meteoimg:height(),tipo="cuadro"}


function createmeteo()

meteo.y = -400-math.random(50,100)

meteo.x = math.random(0,448)

end

explosion_nave=false


-----------------------------------
-------Animacione-----------------
-----------------------------------

ground=anim.create(back,2000)

ground:start()

nave_animacion = anim.create(nave.img,47,46,200)

nave_animacion:start()

explosion_animacion = anim.create(explosion,200)

explosion_animacion:start()

weapon.action(disparo,"start")

createmeteo()

meteoscaidos=1


while true do

controls.read()

anim.blit(ground,0,0)

screen.print(fuente,1,10,"Life "..vida,1,blanco,rojo)

screen.print(fuente,320,10,"Score: "..puntos,1,blanco,rojo)

draw.pbar(2,25,100,7,color.new(0,240,255),color.new(0,0,255),vida,100)

if weapon.blit(disparo,true,meteo.x,meteo.y,meteo.w,meteo.h) then

	createmeteo()

	sound.stop(laser)

sound.play(pom)

puntos = puntos + 5

end
if controls.start() then

for i = 0,255,2 do
	image.blend(menu1,45,11,0+i)
	screen.flip()
end
menu = 1
while true do
controls.read()
menu1:blit(45,11)

if menu == 1 then
menu1:blit(45,11)
if controls.press("cross") then
break
end
end
if menu == 2 then
menu2:blit(45,11)
if controls.press("cross") then
dofile("selector.lua")
end
end
if menu == 3 then
menu3:blit(45,11)
if controls.press("cross") then
dofile("script.lua")
end
end
if menu == 4 then
menu4:blit(45,11)
if controls.press("cross") then
os.exit()
end
end

if controls.press("up") then
	menu = menu-1
end

if controls.press("down") then
	menu = menu+1
end

if menu < 1 then
	menu = 4
end

if menu > 4 then
	menu = 1
end

screen.flip()
end
end

meteo.y = meteo.y + 7


if puntos == 150 then

sound.play(win)

	os.message("Go to the next level")
	
dofile("niveles/nivel2.lua")

end

if vida == 0 then

	os.message("You loser")

	dofile("script.lua")

end


if controls.r() then

	weapon.shoot(disparo,nave.x+nave.w/2-disparo.w/2-1,nave.y,"up",laser)


end



weapon.blit(disparo)

moverPersonaje()

nave_animacion:blit(nave.x,nave.y)

meteoimg:blit(meteo.x,meteo.y)

if meteo.y >272 then
 createmeteo() 
end

if nave.x + nave.w >= meteo.x and

	nave.x <=	meteo.x+meteo.w and

	nave.y + nave.h >= meteo.y and
 
	nave.y <= meteo.y + meteo.h then

explosion_nave=true

sound.play(pom)
		
vida = vida - 1

end


if explosion_nave == true then

	explosion_animacion:blit(nave.x - 13,nave.y - 20)

	if explosion_animacion:currentframe() == 4 then

	explosion_nave = false

	end

end

screen.flip()

end