--Intro
bg = image.load("Menu/DA.png")
start = image.load("Menu/pressstart.png")
dofile("intromap.txt")

diamondimage = {image.load("Menu/diamond1.png"),
image.load("Menu/diamond2.png"),
image.load("Menu/diamond3.png"),
image.load("Menu/diamond4.png")
}

diamonds={}
for i = 1 , 2  do
diamonds[i]  =  { }  
diamonds[i].img = diamondimage[1]
diamonds[i].width = diamonds[i].img:width()
diamonds[i].height = diamonds[i].img:height()
diamonds[i].timer = 0
end

player={}
player.x = 148
player.y = 183
player.velocity = 1 
player.timer=0
player.show = "true"
player.jumpspeed=-10

gravity=0.5

spyke={}
spyke.x = 58
spyke.y = 183
spyke.timer=0
spyke.show = "true"
spyke.velocity = 0

venra={}
venra.x = 30
venra.y = 27
venra.timer=0
venra.show = "true"
venra.velocity = 10
venra.jumpspeed=-13

player.jump = image.load("Menu/RunR/jump.png")

playerwalkright = {image.load("Menu/RunR/stand.png"),
image.load("Menu/RunR/walk1.png"),
image.load("Menu/RunR/walk2.png")}

playerrunright = {
image.load("Menu/RunR/Rstand.png"),
image.load("Menu/RunR/run1.png"),
image.load("Menu/RunR/run2.png")}

playerdashright={
image.load("Menu/RunR/rr.png"),
image.load("Menu/RunR/rr2.png"),
image.load("Menu/RunR/rr3.png") }

spykewalkright = {image.load("Menu/spykeR/spyker1.png"),
image.load("Menu/spykeR/spyker2.png")}

Venrafwd = {
image.load("Menu/Venra/venrafwd1.png"),
image.load("Menu/Venra/venrafwd2.png"),
image.load("Menu/Venra/venrafwd3.png")
}

Venrafly = {
image.load("Menu/Venra/venrafly1.png"),
image.load("Menu/Venra/venrafly2.png"),
image.load("Menu/Venra/venrafly3.png")
}

player.img = playerwalkright[1]
spyke.img = spykewalkright[1]
venra.img = Venrafwd[1]

starttilex = 1
starttiley = 1

Xmax= 145
t=0

--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 drawmaporig()
Mapwidth=#world[1]
Mapheight=#world
numberoftilesx=16
numberoftilesy=10

    if starttilex > (Mapwidth - numberoftilesx) then
		c = Mapwidth
	elseif starttilex <= (Mapwidth - numberoftilesx) then
		c = (starttilex + numberoftilesx)
	end
	if starttiley > (Mapheight - numberoftilesy) then
		d = Mapheight
	elseif starttiley <= (Mapheight - numberoftilesy) then
		d = (starttiley + numberoftilesy)
	end
	
	for Y = math.floor(starttiley), math.ceil(d) do
     for X = math.floor(starttilex), math.ceil(c) do
	  if world[Y][X] > 0 then
			image.blit((X-starttilex) * 30,(Y-starttiley) * 27, tile[world[Y][X]])
	   end
	 end
	end
 end

while true do
 controls.read()
 screen:clear()  

if venra.y>-20  then
animate(Venrafwd, 10, venra)
venra.jumpspeed=venra.jumpspeed+gravity
venra.y=venra.y-venra.jumpspeed
venra.x=venra.x+venra.velocity
else
venra.show="false"
end
  
  
if player.show=="true" then
  t=t+1
  if t<61 then
 animate(playerwalkright, 10, player)
 elseif t>60 and t<121 then
 animate(playerrunright, 5, player)
 elseif t>120 then
 animate(playerdashright, 5, player)
 end
 
 if player.x>250 then
player.jumpspeed=player.jumpspeed+gravity
player.y=player.y+player.jumpspeed
player.velocity=player.velocity+7
player.img=player.jump
end
   
 animate(spykewalkright, 10, spyke)
 player.x=player.x+player.velocity
 spyke.x=spyke.x+spyke.velocity
   
 end
 
 if starttilex<Xmax and t>15 then 
 starttilex=starttilex+0.2
 player.velocity=-3
 spyke.velocity=-2
 if player.x<30 then 
 player.x=30
 player.velocity=0 
 end
end
if starttilex>10 then
player.velocity=player.velocity+5
spyke.show="false"
end

if player.y>183 then
player.show="false"
end

if starttilex>130 then
venra.x=4410
venra.y=54
venra.show="true"
animate(Venrafly, 10, venra)
end

 drawmaporig()

 image.blit(86, 48, bg)
 
 if player.show=="true" then
 image.blit(player.x, player.y, player.img)
 end
 
 if spyke.show=="true" then
 image.blit(spyke.x, spyke.y, spyke.img)
 end
 
 if venra.show=="true" then
 image.blit(venra.x-(starttilex*30), venra.y, venra.img)
 end
 
 animate(diamondimage, 10, diamonds[1])
 animate(diamondimage, 10, diamonds[2])
 
 image.blit(140, 160, diamonds[1].img)
 
 image.blit(280, 160, diamonds[2].img)
 
 image.blit(180, 160, start)
 
 if controls.press("start") then
 dofile("menu.lua")
 end

 screen.waitvblankstart()
 screen.flip() 
end