dofile ("lua/libFenetre.lua")

RPGdatabase ()

choix = 1
choixSkills = -3
ch = 1
chSk = 1
chmax = 4
atak = 0
def = 0
skillsok = false

function newBattle (monst)
  screen.blit (SCREEN_UP, 0, 64, heros.a.img)
  if monst==ghost then
    ms = monsters.a
    
  else 
    ms = nil
  end
end

function battleback ()

end

function timeStart ()
   tmr = Timer.new ()
   tmr:start ()
   time = tmr:time ()
   secondes = 0
end

function timeStop ()
   tmr:reset ()
end

function heroBattle ()
  if atak==1 then
     cri = math.random (1, 3)
     if cri==1 or cri==2 then
       hpdim = (heros.a.att+heros.a.skills.atk.att)-monsters.a.def
       mpdim = 1
     elseif cri==3 then
       hpdim = (heros.a.att+heros.a.skills.atk.att)-monsters.a.def
       mpdim = 1
       screen.print (SCREEN_UP, 50, 50, "Coup critique !")
     end
     monsters.a.hp = monsters.a.hp-hpdim
     heros.a.mp = heros.a.mp-mpdim
     atak = 0
  elseif atak==2 then
     cri = math.random (1, 5)
     if cri==1 or cri==2 or cri==3 or cri==4 then
      screen.print(SCREEN_UP,50, 0, "Joueur 1 attaque")
       hpdim = (heros.a.att+heros.a.skills.skill1.att)-monsters.a.def
       mpdim = 25
     elseif cri==5 then
      screen.print(SCREEN_UP,50, 0, "Joueur 1 attaque")
       hpdim = (heros.a.att+heros.a.skills.skill1.att)-monsters.a.def
       mpdim = 25
       screen.print (SCREEN_UP, 50, 50, "Coup critique !")
     end
     monsters.a.hp = monsters.a.hp-hpdim
     heros.a.mp = heros.a.mp-mpdim
     atak = 0
  end
end

function monsterBattle ()
  cri = math.random (1, 5)
  if cri==1 or cri==2 or cri==3 or cri==4 then
      screen.print(SCREEN_UP,50, 0, "Joueur 2 attaque")
    hpdim = (monsters.a.att+monsters.a.skills.atk.att)-heros.a.def
    mpdim = 25
  elseif cri==5 then
      screen.print(SCREEN_UP,50, 0, "Joueur 2 attaque")
    hpdim = ((monsters.a.att+monsters.a.skills.atk.att)-heros.a.def)*2
    mpdim = 25
    screen.print (SCREEN_UP, 50, 50, "Coup critique !")
  end
  heros.a.hp = heros.a.hp-hpdim
  monsters.a.mp = monsters.a.mp-mpdim
end

function twoPlayersBattle ()
   if atak~=0 and def==0 then
      -- Anim.play
      timeStart ()
      repeat until tmr:time() >= 1000
      heroBattle ()
      timeStop ()
      -- Anim.play
      timeStart ()

      repeat until tmr:time() >= 1000
      timeStop ()
      tmr:stop ()
      monsterBattle ()
	  
if heros.a.hp < 1 then
dofile("lua/lose.lua")
end
	
   elseif atak==0 and def~=0 then
      heros.a.def = heros.a.def*2
      timeStart ()
      repeat until tmr:time() >= 1000
      timeStop ()
      tmr:stop ()
      monsterBattle ()
      heros.a.def = heros.a.def/2

   end
end

function newStatsWindow ()


end

function skillsWindowVisible ()
  skwvis = true
end

function skillsWindowHide ()
  skwvis = false
end

function newSkillsWindow ()

  s1 = ""..heros.a.skills.skill1.name..""
  s2 = ""..heros.a.skills.skill2.name..""
  s3 = ""..heros.a.skills.skill3.name..""
  s4 = ""..heros.a.skills.skill4.name..""
  newBWindow  (65, 70, s1, s2, s3, s4)
  skillsok = true
  if skwvis==true then
    initSWindow2 (SCREEN_DOWN, 100, 92)
  else 
    
  end
  if Keys.newPress.A and choixSkills<=4 and skillsok==true and heros.a.mp > 24 then  
    choixSkills = choixSkills+4
  end 

	
  if Keys.newPress.B and choixSkills<=4 then  
    atak = 0
    skillsok = false
    choix = choix-4
    ch = 1
  end 
 
  if Keys.released.A and chSk~=0 then
    newStatsWindow ()
    skillsWindowVisible ()
  elseif choixSkills==5 then -- Skill1
    chSk = 0
    atak = 2
    twoPlayersBattle ()
    atak = 0
    choixSkills = choixSkills-4
    chSk = 1
  elseif choixSkills==6 then -- Skill2
    atak = 0
    choixSkills = choixSkills-4
    chSk = 1
  end 
end

function initCmds ()
    if Keys.newPress.A and choix<=4 then 
      choix = choix+4
    end 
    
    if Keys.released.A and ch~=0 then
      newStatsWindow ()
      initBWindow (SCREEN_DOWN, 0, 132)
    elseif choix==5 then -- Attaque
      ch = 0
      atak = 1
      twoPlayersBattle ()
      atak = 0
      choix = choix-4
      ch = 1
    elseif choix==6 then
      ch = 0
      atak = 0
      newSkillsWindow ()
    elseif choix==7 then
      ch = 0
      choix = choix-4
      ch = 1
    elseif choix==8 then
      ch = 0
      atak = 0
      def = 1
      twoPlayersBattle ()
      atak = 0
      def = 0
      choix = choix-4
      ch = 1
    end 
end

function initBattle ()

  battleback ()
  newBattle (ghost)
  s1 = "Attaquer"
  s2 = "Compétence"
  s3 = "Objets"
  s4 = "Se défendre"
  newBWindow (90, 60, s1, s2, s3, s4)
  initCmds ()
end

function clearBattle ()
  Image.destroy (cur)
  cur = nil
  Image.destroy (monsters.a.img)
  monsters.a.img = nil
  Image.destroy (heros.a.img)
  heros.a.img = nil
  heros = nil
  monsters = nil
  choix = nil
  ch = nil
  choixSkills = nil
  chSk = nil
  chmax = nil
  xtx = nil
  herosHp = nil
  monstersHp = nil
  hpdim = nil
  mpdim = nil
  ms = nil
  tmr = nil
  time = nil
  second = nil
  play = nil
  s1 = nil
  s2 = nil
  s3 = nil
  s4 = nil
  atak = nil
  def = nil
end
