--[[                                 
********************************************************
PsPmad LF2 v1.60
********************************************************
]]--


green = Color.new(0, 255, 0)
blue = Color.new(0, 0, 255)
black = Color.new(0, 0, 0)
yellow = Color.new(255, 255, 0)
red = Color.new(255, 0, 0)
green = Color.new(0, 255, 0)
white = Color.new(255, 255, 255)
screen:clear()
screen:print(190,120, "Loading.......",red)
screen.flip()

function variabili()
  standD=0
  standS=0

  pugnoD=0
  PD=false

  pugnoS=0
  PS=false

  salto=false


  camminaD=0
  camminaS=0
  D=1
  S=0
  x_ken=5
  y_ken=150
  x_sfondo=0
  colpitoD=false
  colpito_count=0
  energia=6

  nemico_cammina=0
  x_enemy=500
  nemico_colpito=false
  scroll=0
end


img_sfondo_1 = Image.load("data/files/lvl/sf1.png")
img_sfondo_2a = Image.load("data/files/lvl/sf2.png")
img_sfondo_2b = Image.load("data/files/lvl/sf2.png")
img_sfondo_2c = Image.load("data/files/lvl/sf2.png")
img_sfondo_3a = Image.load("data/files/lvl/sf3.png")
img_sfondo_3b = Image.load("data/files/lvl/sf3.png")
img_sfondo_3c = Image.load("data/files/lvl/sf3.png")
img_game_over = Image.load("data/files/gameover.png")




barrak = {     barra6= Image.load("data/files/Health/barra6.png"),  
               barra5= Image.load("data/files/Health/barra5.png"),  
               barra4= Image.load("data/files/Health/barra4.png"), 
               barra3= Image.load("data/files/Health/barra3.png"), 
               barra2= Image.load("data/files/Health/barra2.png"), 
               barra1= Image.load("data/files/Health/barra1.png"), 
               barra0= Image.load("data/files/Health/barra0.png"), 
          }


ken = {     s1= Image.load("data/files/charL/s1.png"),
            s2= Image.load("data/files/charL/s2.png"),
            s3= Image.load("data/files/charL/s3.png"),
            c1= Image.load("data/files/charL/c1.png"),
            c2= Image.load("data/files/charL/c2.png"),
            c3= Image.load("data/files/charL/c3.png"),
            c4= Image.load("data/files/charL/c4.png"),
            c5= Image.load("data/files/charL/c5.png"),
            c6= Image.load("data/files/charL/c6.png"),
            c7= Image.load("data/files/charL/c7.png"),
            c8= Image.load("data/files/charL/c8.png"),
            s1= Image.load("data/files/charL/s1.png"),
            s2= Image.load("data/files/charL/s2.png"),
            s3= Image.load("data/files/charL/s3.png"),
            pgD1= Image.load("data/files/charL/pgD1.png"),
            pgD2= Image.load("data/files/charL/pgD2.png"),
            pgD3= Image.load("data/files/charL/pgD3.png"),
            pgD4= Image.load("data/files/charL/pgD4.png"),
            saltoD= Image.load("data/files/charL/saltoD.png"),
            colpitoD= Image.load("data/files/charL/colpitoD.png"),
            calcioD= Image.load("data/files/charL/calcioD.png"),



            sc1= Image.load("data/files/charR/sc1.png"),
            sc2= Image.load("data/files/charR/sc2.png"),
            sc3= Image.load("data/files/charR/sc3.png"),
            cs1= Image.load("data/files/charR/cs1.png"),
            cs2= Image.load("data/files/charR/cs2.png"),
            cs3= Image.load("data/files/charR/cs3.png"),
            cs4= Image.load("data/files/charR/cs4.png"),
            cs5= Image.load("data/files/charR/cs5.png"),
            cs6= Image.load("data/files/charR/cs6.png"),
            cs7= Image.load("data/files/charR/cs7.png"),
            cs8= Image.load("data/files/charR/cs8.png"),
            pgS1= Image.load("data/files/charR/pgS1.png"),
            pgS2= Image.load("data/files/charR/pgS2.png"),
            pgS3= Image.load("data/files/charR/pgS3.png"),
            pgS4= Image.load("data/files/charR/pgS4.png"),
            saltoS= Image.load("data/files/charR/saltoS.png"),
            calcioS= Image.load("data/files/charR/calcioS.png"),
      }

nemico1 = {     e1= Image.load("data/files/enm1/e1.png"),
                e2= Image.load("data/files/enm1/e2.png"),
                e3= Image.load("data/files/enm1/e3.png"),
                ea1= Image.load("data/files/enm1/ea1.png"),
                ea2= Image.load("data/files/enm1/ea2.png"),
               
                ed1= Image.load("data/files/enm1/ed1.png"),
                ed2= Image.load("data/files/enm1/ed2.png"),
                ed3= Image.load("data/files/enm1/ed3.png"),
                ed4= Image.load("data/files/enm1/ed4.png"),
          }
livelli=0


function disegna_ken_standD()
     if standD<=10 then
     screen:blit(x_ken,y_ken,ken.s1)
     end
     if standD>10 and standD <20 then
     screen:blit(x_ken,y_ken-1,ken.s2)
     end
     if standD>=20 and standD <30 then
     screen:blit(x_ken,y_ken-2,ken.s3)
     end
     if standD>=30 then
     screen:blit(x_ken,y_ken,ken.s1)
     standD=0
     end 
     standD=standD+1
end
--
function disegna_ken_standS()
     if standS<=10 then
     screen:blit(x_ken,y_ken,ken.sc1)
     end
     if standS>10 and standS <20 then
     screen:blit(x_ken,y_ken-1,ken.sc2)
     end
     if standS>=20 and standS <30 then
     screen:blit(x_ken,y_ken-2,ken.sc3)
     end
     if standS>=30 then
     screen:blit(x_ken,y_ken,ken.sc1)
     standS=0
     end 
     standS=standS+1
end
--
function disegna_ken_pugnoD()
     if pugnoD<=5 then
     screen:blit(x_ken,y_ken,ken.pgD1)
     end
     if pugnoD>5 and pugnoD <10 then
     screen:blit(x_ken,y_ken-1,ken.pgD2)
     end
     if pugnoD>=10 and pugnoD <15 then
     screen:blit(x_ken,y_ken-2,ken.pgD3)
     end
     if pugnoD>=15 and pugnoD <20 then
     screen:blit(x_ken,y_ken-2,ken.pgD4)
     end
     if pugnoD>=19 then
     PD=false
     end 
     pugnoD=pugnoD+1
end
--
function disegna_ken_pugnoS()
     if pugnoS<=5 then
     screen:blit(x_ken,y_ken,ken.pgS1)
     end
     if pugnoS>5 and pugnoS <10 then
     screen:blit(x_ken+1,y_ken-1,ken.pgS2)
     end
     if pugnoS>=10 and pugnoS <15 then
     screen:blit(x_ken-24,y_ken-2,ken.pgS3)
     end
     if pugnoS>=15 and pugnoS <20 then
     screen:blit(x_ken-18,y_ken-2,ken.pgS4)
     end
     if pugnoS>=19 then
     PS=false
     end 
     pugnoS=pugnoS+2
end
--
function disegna_ken_camminaD()
     if camminaD<=10 then
     screen:blit(x_ken,y_ken-3,ken.c1)
     end
     if camminaD>10 and camminaD <20 then
     screen:blit(x_ken,y_ken-3,ken.c2)
     end
     if camminaD>=20 and camminaD <30 then
     screen:blit(x_ken,y_ken-3,ken.c3)
     end
      if camminaD>=30 and camminaD <40 then
     screen:blit(x_ken,y_ken-3,ken.c4)
     end
      if camminaD>=40 and camminaD <50 then
     screen:blit(x_ken,y_ken-3,ken.c5)
     end
      if camminaD>=50 and camminaD <60 then
     screen:blit(x_ken,y_ken-3,ken.c6)
     end
      if camminaD>=60 and camminaD <70 then
     screen:blit(x_ken,y_ken-3,ken.c7)
     end
      if camminaD>=70 and camminaD <80 then
     screen:blit(x_ken,y_ken-3,ken.c8)
     end

     if camminaD>=80 then
     screen:blit(x_ken,y_ken,ken.c1)
     camminaD=0
     end 
     camminaD=camminaD+2
end
--
function disegna_ken_camminaS()
     if camminaS<=10 then
     screen:blit(x_ken,y_ken-3,ken.cs1)
     end
     if camminaS>10 and camminaS <20 then
     screen:blit(x_ken,y_ken-3,ken.cs2)
     end
     if camminaS>=20 and camminaS <30 then
     screen:blit(x_ken,y_ken-3,ken.cs3)
     end
      if camminaS>=30 and camminaS <40 then
     screen:blit(x_ken,y_ken-3,ken.cs4)
     end
      if camminaS>=40 and camminaS <50 then
     screen:blit(x_ken,y_ken-3,ken.cs5)
     end
      if camminaS>=50 and camminaS <60 then
     screen:blit(x_ken,y_ken-3,ken.cs6)
     end
      if camminaS>=60 and camminaS <70 then
     screen:blit(x_ken,y_ken-3,ken.cs7)
     end
      if camminaS>=70 and camminaS <80 then
     screen:blit(x_ken,y_ken-3,ken.cs8)
     end

     if camminaS>=80 then
     screen:blit(x_ken,y_ken,ken.cs1)
     camminaS=0
     end 
     camminaS=camminaS+2
end

function enemy()

    if x_enemy>=x_ken+90 and nemico_colpito==false then
    disegna_nemicoS()
    x_enemy=x_enemy-1
    end

    if x_enemy<x_ken+90 and nemico_colpito==false then
    disegna_nemicoS_attacco()
    end

    if nemico_colpito==true then
    disegna_nemico_M_S()
    end



end


function disegna_nemicoS()
nemico_cammina=nemico_cammina+1

   if nemico_cammina<=10 then
   screen:blit(x_enemy,145,nemico1.e1)
   end
   if nemico_cammina>10 and nemico_cammina<20 then
   screen:blit(x_enemy,145,nemico1.e2)
   end
   if nemico_cammina>=20 and nemico_cammina<30 then
   screen:blit(x_enemy,145,nemico1.e3)
   end
   if nemico_cammina>=28 then
   nemico_cammina=0
   end

end
--
function disegna_nemicoS_attacco()
nemico_cammina=nemico_cammina+1

   if nemico_cammina<=60 then
   screen:blit(x_enemy,145,nemico1.ea1)
   end
   if nemico_cammina>60 and nemico_cammina<70 then
   screen:blit(x_enemy-20,145,nemico1.ea2)

   --ken colpito--
                if x_enemy>=x_ken and x_enemy<=x_ken+70 then
                colpitoD=true
                end
   end
   if nemico_cammina>=69 then
   nemico_cammina=0
   end

end
--
function disegna_nemico_M_S()
nemico_cammina=nemico_cammina+2

   if nemico_cammina<=10 then
   screen:blit(x_enemy,145,nemico1.ed1)
   end
   if nemico_cammina>10 and nemico_cammina<20 then
   screen:blit(x_enemy,145,nemico1.ed2)
   end
   if nemico_cammina>=20 and nemico_cammina<40 then
   screen:blit(x_enemy,145,nemico1.ed3)
   end
    if nemico_cammina>=40 and nemico_cammina<50 then
   screen:blit(x_enemy,145,nemico1.ed4)
   x_enemy=x_enemy+10
   end
   if nemico_cammina>=49 then
   nemico_cammina=0
   x_enemy=500
   nemico_colpito=false
   end
end



function controlli()
      fermo=1
	pad = Controls.read()
	       if pad:right()and PD==false and PS==false and salto==false  and colpitoD==false then
		 disegna_ken_camminaD()
             x_ken=x_ken+1.5
             fermo=0
             D=1
             S=0 
	       end

             if pad:left() and PD==false and PS==false and salto==false and colpitoD==false then
		 disegna_ken_camminaS()
             x_ken=x_ken-1.5
             fermo=0
             D=0
             S=1
	       end

             

             if pad:cross()and PD==false and PS==false and salto==false  and colpitoD==false then 
             fermo=0
                if D>=1 then
                pugnoD=0
                PD=true
                end
                
                if S>=1 then
                pugnoS=0
                PS=true
                end

                if x_enemy<=x_ken+70 then
                nemico_cammina=0
                nemico_colpito=true
                end
	       end

             if fermo>=1 and D>=1 and PD==false and PS==false and salto==false and colpitoD==false then
            disegna_ken_standD()
             end

             if fermo>=1 and S>=1 and PD==false and PS==false and salto==false and colpitoD==false then
            disegna_ken_standS()
             end
             
             if PD==true and salto==false and colpitoD==false then
             disegna_ken_pugnoD()
             end
             
             if PS==true and salto==false and colpitoD==false then
             disegna_ken_pugnoS()
             end
             
       
             if pad:square() and salto==false and colpitoD==false then
             salto=true
             i=0
             end
          
             if pad:right() and salto==true and colpitoD==false then
             x_ken=x_ken+1
             end

             if pad:left() and salto==true and colpitoD==false then
             x_ken=x_ken-1
             end

             if pad:select() then
                if scroll<=0 then
                scroll=1
                elseif scroll>=1 then
                scroll=0
                end
             end

      
             if colpitoD==true then
                   colpito_count=colpito_count+1
                   if colpito_count<=10 then
                   screen:blit(x_ken-10,y_ken-3,ken.colpitoD)
                   end
                
                   if colpito_count>10 then
                   energia=energia-1
                   colpitoD=false
                   colpito_count=0
                   end
             end
 

             if salto==true then
                i=i+1
                if i<=30  then
                y_ken=y_ken-4
                end

                if i>30 and i<=60 then
                y_ken=y_ken+4
                end

                if i>60 then
                salto=false
                i=0
                end
                
                if D==1 then
                     if pad:cross()then
                     screen:blit(x_ken,y_ken,ken.calcioD)
                        if x_enemy<=x_ken+90 then
                        nemico_cammina=0
                        nemico_colpito=true
                        end
                     else
                     screen:blit(x_ken,y_ken,ken.saltoD)
                     end     
                end
                if S==1 then
                     if pad:cross()then
                     screen:blit(x_ken,y_ken,ken.calcioS)
                        if x_enemy<=x_ken-90 then
                        nemico_cammina=0
                        nemico_colpito=true
                        end
                     else
                     screen:blit(x_ken,y_ken,ken.saltoS)
                     end     
                end

             end


end

-- Menu
function intromenu()

local opcao    = 0
local teclaAux = 0
local pressOx  = 150
local pressOy  = 200
local pressOdirecao = Direita
   
   while (Sair == false) and (Jogar == false) do

	screen:blit(0,0,img_intro.IMGmenu)

	tecla = Controls.read()

   	if tecla ~= teclaAux then

         if tecla:up() and (opcao == 1) then

	   	opcao = 0

         end

   	   if tecla:down() and (opcao == 0) then

	      opcao = 1

   	   end

   	   if tecla:circle() and (opcao == 0) then

	   	Jogar = true

   	   end

         if tecla:start() then

	   	Sair = true

   	   end   

      end

      teclaAux = tecla

   	if pressOdirecao == Direita then

	   pressOx  = pressOx + 3
   	else
	   pressOx  = pressOx - 3

   	end

   	if pressOx == 210 then

	   pressOdirecao = Esquerda

   	elseif pressOx == 150 then

	   pressOdirecao = Direita

   	end

   	-- Desenhar "pressO"
   	screen:blit(pressOx,200,img_intro.IMGpressO)

   	-- Desenhar Ponteiro
   	if opcao == 0 then

	   screen:blit(150,100,img_intro.IMGponteiro)
   	else
	   screen:blit(150,140,img_intro.IMGponteiro)

   	end

	screen.waitVblankStart(2)
   	screen.flip()
	
   end


   --Music.stop()
   livelli=1
   variabili()
end

function livello_uno()
   screen:clear()
   while livelli==1  do
   screen:blit(0,0,img_sfondo_1)
   screen:blit(x_sfondo,230,img_sfondo_3a)
   screen:blit(x_sfondo+498,230,img_sfondo_3b)
   screen:blit(x_sfondo+498+498,230,img_sfondo_3c)
   screen:blit(x_sfondo,120,img_sfondo_2a)
   screen:blit(x_sfondo+498,120,img_sfondo_2b)
   screen:blit(x_sfondo+498+498,120,img_sfondo_2c)


      if energia==6 then
     screen:blit(1,2,barrak.barra6)
     end
      if energia==5 then
     screen:blit(1,2,barrak.barra5)
     end 
      if energia==4 then
     screen:blit(1,2,barrak.barra4)
     end 
      if energia==3 then
     screen:blit(1,2,barrak.barra3)
     end 
      if energia==2 then
     screen:blit(1,2,barrak.barra2)
     end 
      if energia==1 then
     screen:blit(1,2,barrak.barra1)
     end 
      if energia==0 then
     screen:blit(1,2,barrak.barra0)
      livelli=-1
     end 

   controlli()
   enemy()

   if x_ken>=250 then
     if x_sfondo>-1000 then
     x_sfondo=x_sfondo-1
     x_ken=249
     x_enemy=x_enemy-0.5
     end
   end

   if x_ken>=480 then
   livelli=0
    variabili()
   end

   screen.waitVblankStart(scroll)
   screen.flip()
   screen:clear()
   end
end


function game_over()
conta=100
 while livelli==-1  do
 screen:clear()
 screen:blit(0,0,img_game_over)
 screen.flip()
 screen.waitVblankStart()
  if conta<=0 then
    livelli=0
    variabili()
  end
 conta=conta-1
 end

end


while true do

  intromenu()
  livello_uno()
  game_over()
dofile("./data/mainmenue.lua")
end



