--DA v0.1

if not level then
level=1
end

if not manacollected then
manacollected = 0
end

bg = image.load("img/bg.jpg")
white = color.new(255,255,255) 
red = color.new(255,0,0)
blue = color.new(0,0,255)

dofile("map"..level..".txt")
dofile("collisionmap"..level..".txt")
dofile("functions.lua")
dofile("player.txt")
dofile("enemy.txt")
dofile("diamonds.txt")


screenhalfwidth  = (480  / 2)
screenhalfheight = (272 / 2) 

player.HalfWidth  = (player.width  / 2)
player.HalfHeight = (player.height / 2)
player.RelativeX = screenhalfwidth  - player.HalfWidth
player.RelativeY = screenhalfheight - player.HalfHeight 

gamestate = "play"
current = 1

OffsetX = 0 
OffsetY = 0

--Function to detect collision and make player move
function tilecollision(dirX, dirY, object)
   objectx = object.x + (object.speedX * dirX)
   objecty = object.y + (object.speedY * dirY) 

   if (dirX == 1) then
   getobjectCorners(objectx, object.y, object)
      if (collisionmap[down][right] == 0 and collisionmap[up][right]  == 0) then
      fall(player)
      object.x = object.x + (object.speedX * dirX)
      else
      object.x = (((right - 1)*tilesizeX) - object.width)
	  stand="true"
   end
   end
   
   if (dirX == -1) then
   getobjectCorners(objectx, object.y, object)
      if (collisionmap[down][left] == 0 and collisionmap[up][left] == 0) then
      fall(player)
      object.x = object.x + (object.speedX * dirX)
      else
       object.x = (left*tilesizeX)
	   stand="true"
    end
   end
   
   if (dirY == 1) then 
      if (collisionmap[down][left] == 0 and collisionmap[down][right] == 0) then
      else
      object.jumping = "false"
      object.speedY = 0
      object.y = ((down-1) * tilesizeY) - object.height
      end
   end
   
   if (dirY == -1) then 
   getobjectCorners(object.x, objecty, player)
      if(collisionmap[up][left] == 0 and collisionmap[up][right] == 0) then
      else
      object.jumping="false"
      object.speedY =0
      object.y = (up * tilesizeY)
      end
   end

   end
   
--Enemy ai function
function enemytilecollision(dirX, dirY,object)

   objectx = object.x + (object.speedX * dirX)
   objecty = object.y + (object.speedY * dirY)
   
   if (dirX == 1) then
   getobjectCorners(objectx, object.y+1, object)
      if (collisionmap[down][right] == 1 and collisionmap[up][right] == 0) then
      object.x = object.x + (object.speedX * dirX)
	  object.direction="right"
	  animate(enemywalkright,20, object)
      else
      object.xmove=-1
      object.x = (((right - 1)*tilesizeX) - object.width)
   end
   end
   
   if (dirX == -1) then
   getobjectCorners(objectx, object.y+1, object)
      if (collisionmap[down][left] == 1 and collisionmap[up][left] == 0) then
      object.x = object.x + (object.speedX * dirX)
	  object.direction="left"
	  animate(enemywalkleft,20, object)
      else
	  object.xmove=1
      object.x = (left*tilesizeX)
	--[[  Enemyjump = math.random(5)
      if Enemyjump == 1 and object.jumping=="false" then
	  object.jumping = "true"
	  object.speedY=12
      end]]
	  end
   end
   
   if (dirY == -1) then 
   getobjectCorners(object.x, objecty, object)
      if (collisionmap[up][left] == 1 and collisionmap[up][right] == 1) then
      object.jumping="false"
      object.speedY =0
      object.y = (up * tilesizeY)
     end
   end
   
   if (dirY == 1) then 
    if (collisionmap[down][left] == 1 and collisionmap[down][right] == 1) then
    object.jumping = "false"
    object.speedY = 0
    object.y = ((down-1) * tilesizeY) - object.height
     end
   end
   --End of function
end  
  
   
--Jump function--
function jump(object, grav)
grav = grav or 0.8

   if controls.press("cross") and object.jumping=="false" then
   object.speedY=15
   object.jumping = "true"
   end

   if object.jumping=="true" then
   stand="false"
    if object.direction=="left" then
    object.img = object.lj
	elseif object.direction=="right" then
    object.img = object.rj
	end
   tilecollision(0,-1,object)
   object.speedY = object.speedY - grav
   object.y = object.y - object.speedY
   tilecollision(0,1,object)
   end
   
end

--Jump function--
function enemyjump(object)
grav = grav or 0.8

   if object.jumping=="true" then
   enemytilecollision(0, -1, object)
   object.speedY = object.speedY - grav
   object.y = object.y - object.speedY
   enemytilecollision(0, 1, object)

 end
end

--Function to make player fall--
function fall(object)
if object.alive=="true" then
getobjectCorners(object.x , object.y + 1 , object)
if object.jumping=="false" then
 if (collisionmap[down][left] == 0 and collisionmap[down][right] == 0) then
  if object == player then
object.speedY = -1
else
object.speedY = -4
end
object.jumping = "true"
 end
end
end
end
	
function movePlayer()-- use to move the player

--Walk left--
if controls.left() and player.x>0 then
player.speedX=2
stand = "false"
player.direction="left"
animate(playerwalkleft,10,player)
if controls.r() then
player.speedX=4
animate(playerrunleft,5,player)
end
tilecollision(-1,0,player)
end

--Walk right--
if controls.right() then
player.speedX=2
stand = "false"
player.direction="right"
animate(playerwalkright,10,player)
if controls.r() then
player.speedX=4
animate(playerrunright,5,player)
end
tilecollision(1,0,player)
end

--Sprite down--
if controls.down() then
stand = "false"
if player.direction=="left" then
player.img = player.ld
elseif player.direction=="right" then
player.img = player.Rd
end
end

end


--Function used to make the player stand when not in motion--
function imgstand()
if stand=="true" and player.direction=="left" then
player.img = playerwalkleft[1]
elseif stand== "true" and player.direction=="right" then
player.img = playerwalkright[1]
end
end

function death()
if player.y>((#world-1.5)*tilesizeY) or player.health==0 then
gamestate = "finished"
screen.print(190,145, "You have died",0.8,red,0*0)
screen.print(190,155, "Do you want to continue?",0.8,red,0*0)
menu()
end
end


function pause()
if controls.press("start") and gamestate=="play" then
gamestate="pause"
elseif controls.press("start") and gamestate=="pause" then
gamestate="play"
end

if gamestate=="pause" then
screen.print(240,136, "Paused",0.8,red,0*0)
screen.print(190,145, "Press start to resume",0.8,red,0*0)
end
end

generateenemies()
--Enemy checks
function enemychecks()

 for a = 1, #Enemy - 1 do
	for b = a+1, #Enemy do
		--Check for collision between enemies
		if boxCollision(Enemy[a], Enemy[b]) and Enemy[a].alive=="true" and Enemy[b].alive=="true" then
		  Enemy[a].xmove=Enemy[a].xmove*-1
		  Enemy[b].xmove=Enemy[b].xmove*-1
	      if Enemy[a].play=="true" or Enemy[b].play=="true" then	
          Enemy[a].xmove=0
		  Enemy[b].xmove=0
          end		  
  end
 end
end

for i = 1 , #Enemy  do 
 if Enemy[i].alive=="true" then

   showobject(Enemy[i])
   
    if Enemy[i].y>((Mapheight-2.5)*tilesizeY) or Enemy[i].health==0 then
   Enemy[i].alive="false"
   end
  
   if Enemy[i].show=="true" then
   enemytilecollision(Enemy[i].xmove, 0, Enemy[i])
 
   if Enemy[i].play=="true" then
    if Enemy[i].direction=="left" then
    animate(enemydieleft,4, Enemy[i])
    elseif Enemy[i].direction=="right" then
    animate(enemydieright,4, Enemy[i])
    end
    if Enemy[i].timer>=16 then
    Enemy[i].alive="false"
    Enemy[i].play="false"
    end
   end  
 
  
  if boxCollision(player, Enemy[i]) and Enemy[i].play=="false" then
   if player.health > 0 then
   player.health = player.health - 0.5 
   end
   
   if player.jumping=="true" and player.speedY<0 then
    Enemy[i].xmove=0
    Enemy[i].play="true"
	player.health = player.health + 0.5 
   end
   

  player.x = oldx
  player.y = oldy
  Enemy[i].x=oldEnemy[i].x
  Enemy[i].y=oldEnemy[i].y
  player.speedY=10
  stand = "true"
  end

  end
 
  enemyjump(Enemy[i])
  fall(Enemy[i])
  
  image.blit(Enemy[i].x + OffsetX, Enemy[i].y + OffsetY, Enemy[i].img)
  
end


end 


end

--Blit Image--
function blitimg()

--Player
image.blit(player.RelativeX, player.RelativeY, player.img)

--Player Health bar
draw.gradrect(115, 4, 100/2, 10, black, black, black, black)
draw.fillrect(115, 4, player.health/2, 10, red)
draw.rect(115,4,100/2, 10, black)
screen.print(128, 4, player.health,0.6, white, 0*0) 
screen.print(5, 5, "Player health:", white) 

--Mana bar
draw.gradrect(115, 20, 100/2, 10, black, black, black, black)
if manacollected>0 then
draw.fillrect(115, 20, manacollected/2, 10, blue)
end
draw.rect(115, 20, 100/2, 10, black)

screen.print(128, 19, manacollected, 0.6, white, 0*0) 
screen.print(5, 20, "Mana:", white) 
 

 --End of function
end


--Variables that need to be updated regularly--	
function updatevariables()
 oldx = player.x
 oldy = player.y
 player.height=player.img:height()
 player.width=player.img:width()-5
 for a=1, #Enemy do 
 oldEnemy[a]  =  {} 
 oldEnemy[a].x = Enemy[a].x
 oldEnemy[a].y = Enemy[a].y
 end
end 

--Function to check for endpoint--	
function finished(object)
if player.x>2272 and player.y==352 and player.x<2300 then
gamestate = "Restart"
screen.print(180,130, "Level Ended",red)
screen.print(190,145, "Restart?",red)
menu()
end
end	

--function to collect diamonds
function cdiamonds()
for a = 1 , #diamonds  do 
showobject(diamonds[a])
 if diamonds[a].display=="true" then
 
  if diamonds[a].show == "true" then
   if diamonds[a].play=="false" then
   animate(diamondimage,5, diamonds[a])
   end
  image.blit(diamonds[a].x + OffsetX, diamonds[a].y + OffsetY, diamonds[a].img)
  end 
  
  if boxCollision(player, diamonds[a]) then
  diamonds[a].play="true"
    end
  end
 if diamonds[a].play=="true" then 
 animate(disdiamondimage,5,diamonds[a])
  if diamonds[a].timer>=20 then
    diamonds[a].display = "false"
	diamonds[a].play="false"
	if manacollected<101 then
    manacollected=manacollected+1
    end
   end 
  end
 
 end
 
end

while true do
 os.cpu (333)
 controls.read()
 screen:clear()
 
 death()

 if gamestate~= "finished" then
 image.blit(0, 0, bg)
 updatevariables()
 updateMap()
 drawMap()
 finished(player)

 if gamestate=="play" then
  movePlayer()
  jump(player) 
  cdiamonds()
  enemychecks()
 end 


 fall(player)
 imgstand() 
 blitimg()
 pause()
 end
 
 collectgarbage("collect")
 
 screen.flip() 
 screen.waitvblankstart()
 end
