--Simple Generic box collision function
function boxCollision(obj1, obj2)
   if (obj1.x + obj1.width >obj2.x)  and (obj1.x < obj2.x + obj2.width) and (obj1.y + obj1.height > obj2.y) and (obj1.y < obj2.y + obj2.height) then
      return true
   else
      return false
   end
end

--Draws the map
function drawMap()

local Mapwidth  = #world[1]
local Mapheight = #world
local screenwidth = 480
local screenheight = 272

   for Y = 1, Mapheight do
   
      if Y - 1 > (math.floor((OffsetY * -1) / tilesizeY) - 1) and Y - 1 < math.floor(((OffsetY * -1 ) + screenheight) / tilesizeY) + 1 then
   
         for X = 1, Mapwidth do
            
            if X - 1 > math.floor((OffsetX * -1) / tilesizeX) - 1 and X - 1 < math.floor(((OffsetX * -1 ) + screenwidth) / tilesizeX) + 1 then
               
              if world[Y][X] > 0 then


		
                  image.blit( ((X - 1) * tilesizeX) + OffsetX, ((Y - 1) * tilesizeY) + OffsetY, tile[world[Y][X]])            
               end 
			  
            end
         end
      end
   end
   stand="true"
end

--Update the map, focus map on the player--
function updateMap()
local Mapwidth  = #world[1]
local Mapheight = #world
local screenwidth = 480
local screenheight = 272

   if (player.x + player.HalfWidth - screenhalfwidth) < 0 and OffsetX==0 then
   
      player.RelativeX = player.x
      
   elseif (player.x + screenhalfwidth + player.HalfWidth) > (Mapwidth * tilesizeX) then

      player.RelativeX = screenwidth - ((Mapwidth * tilesizeX) - player.x)
      
   else
      player.RelativeX = screenhalfwidth - player.HalfWidth
      OffsetX = (player.x - screenhalfwidth + player.HalfWidth ) * -1
      
   end
   
   if (player.y + player.HalfHeight - screenhalfheight) < 0 and OffsetY==0 then

      player.RelativeY = player.y  
      
    elseif (player.y + screenhalfheight + player.HalfHeight) > (Mapheight * tilesizeY) then
   
     player.RelativeY = screenheight - ((Mapheight * tilesizeY) - player.y )
      
   else
      player.RelativeY = 120
      OffsetY = (player.y - screenhalfheight + player.HalfHeight) * -1
	  
      
   end
   
   if player.y<player.RelativeY then
   OffsetY=0
   end
   
end


--Animation function--
function animate(imgtable,interval, object)
	 local numFrames = table.getn(imgtable) 
	 if object.timer >= numFrames*interval then object.timer = 0 end 
	 object.timer = object.timer + 1
	 for i = 1, numFrames do 
		 if object.timer > interval*(i-1) and object.timer <= interval*i then 
			 object.img = imgtable[i] 
			 break
		 end
	 end
	 return object.img
end

--Function to get corners
function getobjectCorners(cornerX, cornerY, object)
    if cornerY>0 then
	down = math.floor(((cornerY + object.height - 1) / tilesizeY)+1)
	up = math.floor((cornerY / tilesizeY)+1)
	else
	down=1
	up=1
	end
	left = math.floor((cornerX/ tilesizeX)+1)
	right = math.floor((((cornerX + object.width-1) / tilesizeY)+1))
	
end

--Function to determine when to show object
function showobject(object)
 if ((player.x-player.RelativeX-player.width-object.x)>0) or (object.x-player.x)>(480-object.width) then
  object.show = "false"
  else
  object.show = "true"
  end
end

--Yes/No menu
function menu()
if current == 1 then
screen.print(190,165, "Yes",0.6,white,0*0)
screen.print(190,175, "No",0.6,red,0*0)
end

if current == 2 then
screen.print(190,165, "Yes",0.6,red,0*0)
screen.print(190,175, "No",0.6,white,0*0)
end

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

if controls.press("down") then
current = current + 1
end
  
if controls.press("cross") then
if current == 1 then
dofile("menu.lua")
elseif controls.press("cross") then
os.quit()
end   
end

if current == 3 then
current = 1
end
if current == 0 then
current = 2
end

end