/*****************************/
/* Inside The Machine - MKS  */
/*****************************/

#include "eFish.h"
#include "../WorldManager.h"

eFish::eFish()
{
	real = true;
	stat = false;
	toDelete = false;
	sensH = true;
	//distx = FIX(distx);
	distanceX = 0;
	
	//Parametrage des hitboxes
	rect.x1 = 4;
	rect.y1 = 4;
	rect.x2 = 28;
	rect.y2 = 12;
}
   	
eFish::~eFish()
{
}

void eFish::Load()
{
   //Chargement du spritesheet
   ulSetTransparentColor(RGB15(31, 0, 31));
	spr.assign(worldMan->resMan->LoadImage("efs:/Ennemy_Fish.png", UL_IN_VRAM, UL_PF_PAL4));
	ulDisableTransparentColor();
		
	//Paramètrage des animations
	spr.setFrameWH(32, 16);
	spr.addAnim(0, 0, 2, 7);     //Anim : Idle Right
	spr.addAnim(1, 0, 2, 7);     //Anim : Idle Left
}
   
void eFish::Draw(int layer)
{
   //On le perso
   if(actif && layer == 0)
   {
      //On affiche le sprite
   	spr.setXY(x_screen, y_screen);
   	spr.play();
   	spr.draw();
	}	
}
   
void eFish::Update()
{
   //Calcul de la position écran de l'entité
   x_screen = UNFIX(x - FIX(worldMan->curLevel->map->scrollX));
	y_screen = UNFIX(y - FIX(worldMan->curLevel->map->scrollY));
	
	//On vérifie si chuck est actif
   if(x_screen < -32 || x_screen > (256+32) || y_screen < -16 || y_screen > (192))
   	actif = false;
   else
   	actif = true;
   
   if(actif)
   {
      //On fait faire une ronde au personnage
	   if(sensH)
	   {
			x += vitesse;
			spr.animLoop(0);			
	   }
		else
		{
			x -= vitesse;
			spr.animLoop(1);			
		}
		
	   //On calcule la distance parcourue en X au sol
   	if(oldx < x)
   		distanceX += x-oldx;
   	if(oldx > x)
   		distanceX -= oldx-x;
		
      //On change le sens de déplacement
      if(distanceX > distx)
      	sensH = false;
      if(distanceX < 0)
      	sensH = true;
	}
	
}

   
void eFish::CheckInteractions()
{
	//Collisions avec le personnage (on enlève de la vie)  if(worldMan->hero->dontMoveTime == -1)		  
	if(RectCollision(UNFIX(x), UNFIX(y), &rect, UNFIX(worldMan->hero->x), UNFIX(worldMan->hero->y), &worldMan->hero->rect) )
	{
	   if(worldMan->hero->dontHitTime == -1)
	   {
			worldMan->hero->dontHitTime = 30;
			worldMan->hero->dontMoveTime = 3;
			worldMan->hero->life -= 5;
		}			 
	}   
}   

void eFish::Destroy()
{

}
