Map = {}
--[[
      Element :
	  t : terre
	  v : herbe
	  e: eau
	  m : magie
]]--

Element={n=4}
Element[1]={n="t",tile=Tileset.new("data/image/terre.png"),b=true,cout=5,name="Terre",dep=0,rm=0,g=true,kill=false}
Element[2]={n="v",tile=Tileset.new("data/image/herbe.png"),b=false,cout=2,name="Verdure",dep=0,rm=0,g=true,kill=false}
Element[3]={n="e",tile=Tileset.new("data/image/pique.png"),b=false,cout=100,name="Pique",dep=0,rm=0,g=false,kill=true}
Element[4]={n="m",tile=Tileset.new("data/image/magie.png"),b=true,cout=40,name="Terre magique",dep=3,rm=2,g=false,kill=false}

function Map.new(fichier,l2,h2)
     local map={scrol=0,
	      texture={},
		  l=l2,
		  h=h2,
		  lr=EarthEngine.l,
		  hr=EarthEngine.h,
		  xr=0,
		  yr=-320}
		  
		  
	 --Lecture de la map
	 dofile(fichier..".map")
	 map.texture=map_tab
     map.l=string.len(map.texture[0])
	 map.h=table.getn(map.texture)+1
	 
	 function map:clear()
	     --screen:clear()
	 end
	 
	 function map:getKill(x,y)
	         if map:get(x,y)~= "0" and map:get(x,y)~= "b" then
			     for i=1,Element.n do
			         if map:get(x,y)==Element[i].n then
				         return Element[i].kill
				     end
			     end
			 else
			     return false
			 end
	 end
	 
	 function map:getRm(x,y)
         if x<map.l and x>=0 and y>=0 and y<map.h then
	         if map:get(x,y)~= "0" and map:get(x,y)~= "b" then
			     for i=1,Element.n do
			         if map:get(x,y)==Element[i].n then
				         return Element[i].rm
				     end
			     end
			 elseif map:get(x,y)== "b" then
			     return 0
			 else
			     return 1
			 end
		 else
		     return 1
		 end
	 end
	 
	 function map:get(x,y)
	     if x<map.l and x>=0 and y>=0 and y<map.h then
	         return string.sub(map.texture[y],x+1,x+1)
		 else
		     return "0"
		 end
	 end
	 
	 function map:getB(x,y)
	     if x<map.l and x>=0 and y>=0 and y<map.h then
	         if map:get(x,y)~= "0" and map:get(x,y)~= "b" then
			     for i=1,Element.n do
			         if map:get(x,y)==Element[i].n then
				         return Element[i].b
				     end
			     end
			 elseif map:get(x,y)== "b" then
			     return true
			 else
			     return false
			 end
		 else
		     return true
		 end
	 end
	 
     function map:set(x,y,e) 
	     if e~="0" then
         for i=1,Element.n do
		     if e==Element[i].n then
			     if GamePlayer.stopg==false then
			         if Element[i].dep==0 and map:getB(x,y+1)==true then
	                     map.texture[y] = string.sub(map.texture[y],0,x)..e.. string.sub(map.texture[y],x+2) 
                     elseif Element[i].dep==1 and map:getB(x,y+1)==true then
                     	 if map:get(x-1,y)==Element[i].n or map:getB(x-1,y)==true then 
						     map.texture[y] = string.sub(map.texture[y],0,x)..e.. string.sub(map.texture[y],x+2) 
						 end
                     elseif Element[i].dep==3 then
						     map.texture[y] = string.sub(map.texture[y],0,x)..e.. string.sub(map.texture[y],x+2) 
					 end
				 else 
				     map.texture[y] = string.sub(map.texture[y],0,x)..e.. string.sub(map.texture[y],x+2) 
				 end
				 
             end
		 end
		 else
		      map.texture[y] = string.sub(map.texture[y],0,x)..e.. string.sub(map.texture[y],x+2) 
		 end
	 end
	 
	 function map:position()
	     if (map.xr+GamePlayer.x*20)>(map.lr/2) and map.l*20>map.lr and GamePlayer.x<((map.l-(map.lr/40))+1) then --si plus de la moitié du terrain et que le terrain plus grand que l'affichage
             if GamePlayer.dx~=nil and  GamePlayer.dx>0 then --dep vers droite
			     map.xr=map.xr-20
			 end
		 end
	      if (map.xr+GamePlayer.x*20)<(map.lr/2) and map.l*20>map.lr and GamePlayer.x>(map.lr/40) then 
		     if GamePlayer.dx~=nil and GamePlayer.dx<0 then --dep vers gauche
			     map.xr=map.xr+20
			 end
		 end
	      if (map.yr+GamePlayer.y*20)<(map.hr/2) and map.h*20>map.hr and GamePlayer.y>(map.hr/40) then 
		     if GamePlayer.dy~=nil and GamePlayer.dy<0 then --dep vers gauche
			     map.yr=map.yr+20
			 end
		 end
	      if (map.yr+GamePlayer.y*20)<(map.hr/2) and map.h*20>map.hr and GamePlayer.y<(map.h-(map.hr/60)) then 
		     if GamePlayer.dy~=nil and GamePlayer.dy>0 then --dep vers gauche
			     map.yr=map.yr-20
			 end
		 end
	 end
	 
	 function map:refresh()
	     map:clear()
		 for x=0,map.l do
		     for y=0,map.h do

			     --Coordonnée d'affichage
			     xp=map.xr+(x*20)
				 yp=map.yr+(y*20)
				 if xp<0 or xp>(map.lr-20) or yp<0 or yp>(map.hr)then
				     
                 else
				 
			     for i=1,Element.n do
				     if map:get(x,y)==Element[i].n then
					     
					 	 if map:getB(x,y+1)==false and Element[i].g and GamePlayer.stopg==false then
				                      -- Effet de la gravité sur tous les element
				             map:set(x,y+1,map:get(x,y))
					         map:set(x,y,"0")
				         end
						 
						 --Effet special sur l'eau qui coule :
					 	 if map:getB(x-1,y+1)==false and Element[i].dep==1  then
				             map:set(x-1,y+1,map:get(x,y))
					         map:set(x,y,"0")
						 elseif  map:getB(x+1,y+1)==false and Element[i].dep==1 then
				             map:set(x+1,y+1,map:get(x,y))
					         map:set(x,y,"0")
				         end
						 
					 
					     if (map:get(x-1,y)==Element[i].n) and (map:get(x+1,y)==Element[i].n) and (map:get(x,y-1)~=Element[i].n) then
						     Element[i].tile:blit(screen,xp,yp,2)
						 elseif (map:get(x-1,y)~=Element[i].n) and (map:get(x,y-1)~=Element[i].n) then
						     Element[i].tile:blit(screen,xp,yp,1)
						 elseif (map:get(x+1,y)~=Element[i].n) and (map:get(x,y-1)~=Element[i].n) then
						     Element[i].tile:blit(screen,xp,yp,3)
						 elseif (map:get(x-1,y)==Element[i].n) and (map:get(x+1,y)==Element[i].n) and (map:get(x,y-1)==Element[i].n)  and (map:get(x,y+1)==Element[i].n)  then
						     Element[i].tile:blit(screen,xp,yp,5)
						 elseif(map:get(x+1,y)~=Element[i].n) and (map:get(x,y-1)==Element[i].n) and (map:get(x,y+1)==Element[i].n) then
						     Element[i].tile:blit(screen,xp,yp,6)
						 elseif (map:get(x-1,y)~=Element[i].n) and (map:get(x,y+1)~=Element[i].n) and (map:get(x+1,y)==Element[i].n) then
						     Element[i].tile:blit(screen,xp,yp,7)
						 elseif (map:get(x+1,y)==Element[i].n) and (map:get(x-1,y)==Element[i].n)  then
						     Element[i].tile:blit(screen,xp,yp,8)
						 elseif (map:get(x+1,y)~=Element[i].n) and (map:get(x-1,y)==Element[i].n) and (map:get(x,y+1)~=Element[i].n) then
						     Element[i].tile:blit(screen,xp,yp,9)
						 else
						     Element[i].tile:blit(screen,xp,yp,4)
						 end
						 --screen:print(xp,yp,Element[i].n)
					 end
				 end
				 end
			 end
		 end
		 
	 end
	 return map
end
