Player={}
function Player.new(fichier,l2,h2,x2,y2)
     local pic=Image.load(fichier)
     local player={x=x2 or 0,
	               y=y2 or 10,
				   l=l2,
	               h=h2,
				   anim1=Animation.new(fichier,4,0,perso:height()/2,perso:width(),perso:height()/4,2),
				   anim2=Animation.new(fichier,4,0,perso:height()/4,perso:width(),perso:height()/4,2),
				   dy=0,
				   dy=0,
				   dt=0,
				   dtempo=0,
				   odtempo=0,
				   lx=1,
				   ly=0,
				   ll=perso:width()/4,
				   hh=perso:height()/4,
				   oldzone="0",
				   pts=0,
				   dir=1,
				   stopg=false}
				   
	 function player:clear()
	     if player.dt==0 then
		     	 player.lx=player.dx
				 player.ly=player.dy
				 
		     	 player.dx=0
				 player.dy=0
				 player.dt=0
		 end
     end	 
				  
     function player:stopGravity()
	     if player.stopg==true then
		     if GameCursor.mana>0 then
	             GameCursor.mana=GameCursor.mana-1
			 else
			     player.stopg=false
			 end
		 end
	 end


				  
	 function player:move(x,y,t,te) --Deplace le perso
	     player.dx=x
		 player.dy=y
		 if x~=0 then
		     player.dir=player.dx
		 end
		 player.dt=t or 1
		 player.dtempo=te or 1
		 player.odtempo=te or 1
	 end
	 
	 function player:play() --Calcule ses deplacement selon le tempo et le mouvement demandé
		if GameMap:getKill(player.x,player.y) or GameMap:getKill(player.x,player.y+1) then  -- si sur une pique
		     player:kill()
		end
		 
		 if player.dt~=0 then
		     if GameMap:getB(player.x+player.dx,player.y+player.dy)==false and GameMap:getB(player.x+player.dx,player.y+1+player.dy)==false  then
			     player.dtempo=player.dtempo-1
				 if (player.dtempo==0) then
				     GameMap:set(player.x,player.y,player.oldzone) -- remise de l'etat de la zone
					 
					 --Changement de coordonnée
					 player.x=player.x+player.dx
				     player.y=player.y+player.dy
					 
					 player.oldzone=GameMap:get(player.x,player.y) --Sauveagrde de l'etat de la zone
					 GameMap:set(player.x,player.y,"b") -- mise en zone bloquante
				     player.dt=player.dt-1
					 player.dtempo=player.odtempo
				 end
				 return true
			 else
			     return false
			 end
			 
		 else
		     return false
		 end
	 end
	 
	 function player:blit()
	     local x=GameMap.xr+(player.x*20+(20-player.ll))+1
		 local y=GameMap.yr+((player.y+1)*20+(20-player.hh))+3
	     if player.dir>=1 and player.dx~=0 then
	         player.anim1:play(screen,x,y)
		 elseif player.dir<=-1 and player.dx~=0  then
		     player.anim2:play(screen,x,y)
		 else
		     if player.dir>=1 then
		         screen:blit(x,y,player.anim1.pic,player.ll,player.hh*2,player.ll,player.hh)
			 else
			     screen:blit(x,y,player.anim1.pic,player.ll,player.hh,player.ll,player.hh)
			 end
		 end
		 
	     --screen:fillRect(player.x*20,player.y*20,player.l*20,player.h*20)
	 end
	 
	 function player:controls(pad)
	     if pad:right() then
		     player:move(1,0)
			 
		 elseif pad:left() then
		     player:move(-1,0)
			 
		 elseif pad:up() and GameMap:getB(player.x,player.y+2)==true then --Peut sauter si au contact du sol
		     if GameMap:getB(player.x-1,player.y)==true and GameMap:getB(player.x+1,player.y)==false then
			     player:move(1,-1)
			 elseif GameMap:getB(player.x+1,player.y)==true and GameMap:getB(player.x-1,player.y)==false then
			     player:move(-1,-1)
			 else
		         if player.dir>=1 then
		             player:move(1,-1)
			     elseif player.dir<=-1 then
			         player:move(-1,-1)
			     else
			        player:move(0,-2)
			     end
			 end
			 
			 
		 elseif pad:down() then
		     player:move(0,1)
			 
		 elseif pad:square() then
			 player.stopg=true
			 
		 elseif pad:triangle() then
			 player.stopg=false
			 
		 end
	 end
	 
	 function player:reality()
	 	 if PlayerIA.get(player.x,player.y) then --contact avec enemis
		     player:kill()
		 end
		 
		 if player:XY(GlobalAnim.endx,GlobalAnim.endy) then --victoire
		     player:win()
		 end
	     if player.dy==0 then
		     if GameMap:getB(player.x,player.y+2)==false then
			     player:move(0,1)
			 end
		 end
	 end
	 
	 function player:kill()
	     --a mettre game over
		 GameCursor.continue=false
	 end
	 
	 
	 function player:win()
	     --a mettre winner
		 GameCursor.continue=false
	 end
	 
	 function player:XY(x,y)
	     if (player.x==x and player.y==y)  then
		     return true
	     end
		 if (player.x==x and player.y==y+1) then
		     return true
		 end
		 return false
	 end
	 
	 function player:killXY(x,y)
	     if (player.x==x and player.y==y)  then
		     player:kill()
	     end
		 if (player.x==x and player.y==y+1) then
		     player:kill()
		 end
	 end
	 
	 function player:playIa()
	     player:clear()
	     player:reality()
	     if player:play()==false then 
		     player:move(math.random(-1,1),0,math.random(0,4),6)
		 end
	     player:blit()
	 end
	 
     return player
end

Monster={}
function Monster.new(fichier,l2,h2,x2,y2)
     local pic=Image.load(fichier)
     local monster={x=x2 or 0,
	               y=y2 or 10,
				   l=l2,
	               h=h2,
				   anim1=Animation.new(fichier,2,0,0,30,15,2),
				   anim2=Animation.new(fichier,2,0,15,30,15,2),
				   dy=0,
				   dy=0,
				   dt=0,
				   dtempo=0,
				   odtempo=0,
				   lx=1,
				   ly=0,
				   oldzone="0",
				   pts=0,
				   dir=1,
				   stopg=false,
				   etat=true}
				   
	 function monster:clear()
	     if monster.dt==0 then
		     	 monster.lx=monster.dx
				 monster.ly=monster.dy
				 
		     	 monster.dx=0
				 monster.dy=0
				 monster.dt=0
		 end
     end	 
				  
     function monster:stopGravity()
	     if monster.stopg==true then
		     if GameCursor.mana>0 then
	             GameCursor.mana=GameCursor.mana-1
			 else
			     monster.stopg=false
			 end
		 end
	 end


				  
	 function monster:move(x,y,t,te) --Deplace le perso
	     monster.dx=x
		 monster.dy=y
		 if x~=0 then
		     monster.dir=monster.dx
		 end
		 monster.dt=t or 1
		 monster.dtempo=te or 1
		 monster.odtempo=te or 1
	 end
	 
	 function monster:play() --Calcule ses deplacement selon le tempo et le mouvement demandé
	     if monster.dt~=0 then
		     if GameMap:getB(monster.x+monster.dx,monster.y+monster.dy)==false and GameMap:getB(monster.x+monster.dx,monster.y+1+monster.dy)==false then
			     monster.dtempo=monster.dtempo-1
				 if (monster.dtempo==0) then
				     GameMap:set(monster.x,monster.y,monster.oldzone) -- remise de l'etat de la zone
					 
					 --Changement de coordonnée
					 monster.x=monster.x+monster.dx
				     monster.y=monster.y+monster.dy
					 
					 monster.oldzone=GameMap:get(monster.x,monster.y) --Sauveagrde de l'etat de la zone
					 GameMap:set(monster.x,monster.y,"b") -- mise en zone bloquante
				     monster.dt=monster.dt-1
					 monster.dtempo=monster.odtempo
				 end
				 return true
			 else
			     return false
			 end
			 
		 else
		     return false
		 end
	 end
	 
	 function monster:blit()
	     if monster.dir>=1 and monster.dx~=0 then
	         monster.anim1:play(screen,GameMap.xr+monster.x*20,GameMap.yr+monster.y*20+25)
		 elseif monster.dir<=-1 and monster.dx~=0  then
		     monster.anim2:play(screen,GameMap.xr+monster.x*20,GameMap.yr+monster.y*20+25)
		 else
		     if monster.dir>=1 then
		         screen:blit(GameMap.xr+monster.x*20,GameMap.yr+monster.y*20+25,monster.anim1.pic,15,0,15,15)
			 else
			     screen:blit(GameMap.xr+monster.x*20,GameMap.yr+monster.y*20+25,monster.anim1.pic,15,15,15,15)
			 end
		 end
		 
	     --screen:fillRect(monster.x*20,monster.y*20,monster.l*20,monster.h*20)
	 end
	 
	 
	 function monster:reality()
	     if monster.dy==0 then
		     if GameMap:getB(monster.x,monster.y+2)==false then
			     monster:move(0,1)
			 end
		 end
	 end
	 
	 function monster:kill()
	     monster.etat=false
	 end
	 
	 function monster:playIa()
	     if monster.etat then
		     monster:clear()
	         monster:reality()
	         if monster:play()==false then 
		         monster:move(math.random(-1,1),0,math.random(0,4),6)
		     end
	         monster:blit()
		 end
	 end
	 
     return monster
end
