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

#include "eJaunty.h"
#include "../WorldManager.h"
#include "../WorldState.h"

eBoss1::eBoss1()
{
	real = true;
	toDelete = false;
	stat = false;
	type = 2;
   genre = 0;
   explosionH = 0;
	explosionAlpha = 0; 
}
   	
eBoss1::~eBoss1()
{
   
}

void eBoss1::Load()
{
   //On charge les images
	ulSetTransparentColor(RGB15(31, 0, 31));
	eye.assign(worldMan->resMan->LoadImage("efs:/Boss1_Eye.png", UL_IN_VRAM, UL_PF_PAL4));
	body.assign(worldMan->resMan->LoadImage("efs:/Boss1_Body.png", UL_IN_VRAM, UL_PF_PAL4));
	miss.assign(worldMan->resMan->LoadImage("efs:/Boss1_Ball.png", UL_IN_VRAM, UL_PF_PAL2));
	laserElem.assign(worldMan->resMan->LoadImage("efs:/FX_Laser_Element.png", UL_IN_VRAM, UL_PF_PAL2));	
	laserExt.assign(worldMan->resMan->LoadImage("efs:/FX_Laser_Ext.png", UL_IN_VRAM, UL_PF_PAL2));
	explosion.assign(worldMan->resMan->LoadImage("efs:/FX_Explosion_1.png", UL_IN_VRAM, UL_PF_PAL2));
	sprFx2.assign(worldMan->resMan->LoadImage("efs:/FX_Impact_2.png", UL_IN_VRAM, UL_PF_PAL2));	
	ulDisableTransparentColor();
	
	isBossActivated = true; //stub
	sequence = 0;
	
	
	life = 200;
	hit = false;
	timer = 0;
	hitTime = 0;
	canFight = true;
	isDashing = false;
	for(int k = 0; k < 12; k++)
	{
		balls[k].free = true;  
		balls[k].rect.x1 = 2; 
		balls[k].rect.y1 = 2; 
		balls[k].rect.x2 = 6; 
		balls[k].rect.y2 = 6; 
	}
	   
	//Parametrage des animations
	eye.setFrameWH(32, 32);
	eye.addAnim(0, 0, 3, 7);     //Anim : Standing
	eye.addAnim(1, 0, 0, 7);     //Anim : Hit
	laserExt.setFrameWH(16, 16);
	laserExt.addAnim(0,0,1,5);
	rect.x1 = 0;
	rect.y1 = 0;
	rect.x2 = 32;
	rect.y2 = 32;
	laserHitbox.x1 = 0;
	laserHitbox.y1 = 8;
	laserHitbox.x2 = 0;
	laserHitbox.y2 = 8;
	laserW = 0;
	orix = x;
	sprFx2.scale(32,32);
	sprFx2.setAlpha(true, 20);
}
   
void eBoss1::Draw(int layer)
{
   //On affiche le boss
   if(actif && layer == 1)
   {
      //On affiche le laser
      int lxscr = (x+2-laserW) - worldMan->curLevel->map->scrollX;
		int lyscr = (y+8) - worldMan->curLevel->map->scrollY;
		
		if(laserAlpha > 0)
		{
			laserElem.setAlpha(true, laserAlpha);
	      laserElem.setXY(lxscr, lyscr);
	      laserElem.scale(laserW, 16);
	      laserElem.draw();
	      laserExt.setAlpha(true, laserAlpha);
	      laserExt.setXY(lxscr, lyscr);
	      laserExt.play();
	      laserExt.draw();
		}   
		//Sprite
		if(hitTime != 0)
			eye.animLoop(1);
		else
			eye.animLoop(0);
		eye.setXY(x_screen, y_screen);
		eye.play();
		eye.draw();
		body.setXY(x_screen+32, y_screen-32);
		body.draw();
		
		if(hit)
      {
	      sprFx2.setXY(x_screen-8, y_screen);
	      sprFx2.draw();
		}
		if(hitTime == 10)
			hit = false;
			
		//On affiche les boulettes
		for(int k = 0; k < 12; k++)
		{
		   if(balls[k].free == false && life > 0)
		   {
			   int bxscr = balls[k].x - FIX(worldMan->curLevel->map->scrollX);
			   int byscr = balls[k].y - FIX(worldMan->curLevel->map->scrollY);
				miss.setXY(UNFIX(bxscr), UNFIX(byscr));
				//ulDebug("bx:%d\nby:%d\n", UNFIX(bxscr), UNFIX(byscr));
				miss.draw();
			}			
		}
		
		//Explosion
		if(life <= 0)
		{						 
			explosionH += 1;
			explosionAlpha += 64;
			explosion.scale(explosionH, explosionH);
			
			if(explosionH == 1)
			{
				AS_SoundDefaultPlay(sndDie, 155520, soundVolume, 64, 0, 1);  
			}   
			//On change l'état moteur quand l'explosion est terminée
			if(explosionH == 256)
			{
			 	//On change l'état moteur
			 	WorldState::Instance()->FadeWhite();
			}
			if(explosionH >= 256+30)
			{
				//On passe a l'état de fin de niveau
				worldMan->isBossKilled = true;
			}   			
			if(explosionAlpha >= FIX(31))
				explosionAlpha = FIX(31);   
			//On affiche l'explosion
			explosion.setXY(x_screen-explosionH/2, y_screen-explosionH/2);
			explosion.setAlpha(true, UNFIX(explosionAlpha));
			explosion.draw();
		}   
	}
}
   
void eBoss1::Update()
{
   //Calcul de la position écran de l'entité
   x_screen = x - worldMan->curLevel->map->scrollX;
	y_screen = y - worldMan->curLevel->map->scrollY;
	
	//On vérifie si l'informateur est à l'écran
   if(x_screen < (-32) || x_screen > (256+32) || y_screen < (-32) || y_screen > (192+32))
   	actif = false;
   else
   	actif = true;
   
   if(actif && isBossActivated)
   {
      worldMan->isFightingBoss = true;

	   //Sequences d'attaque
	   timer++;
	   isDashing = false;
		if(life > 100)
		{
			//Idle
			if(timer < 180)
				sequence = 0;
			//Balls
			if(timer > 180 && timer < 480)
				sequence = 1;
			//Idle
			if(timer > 480 && timer < 660)
				sequence = 0;
			//Laser
			if(timer > 660 && timer < 960)
				sequence = 2;
			//Maj du timer
			if(timer > 960)
				timer = 0;
		}
		if(life <= 100)
		{
			//Idle
			if(timer < 180)
				sequence = 0;
			//Balls
			if(timer > 180 && timer < 480)
				sequence = 1;
			//Idle
			if(timer > 480 && timer < 660)
				sequence = 0;
			//Laser
			if(timer > 660 && timer < 960)
				sequence = 2;
			//Dash
			if(timer > 960 && timer < 1020)
				sequence = 3; 
			//Maj du timer
			if(timer > 1020)
				timer = 0;
		}
		//Programmation des séquences d'attaque
		if(sequence == 0)
		{
			//Idle
			canFight == true; //Le boss peut perdre de la vie   
			laserW = laserAlpha = 0;
		}
		if(sequence == 1)
		{
		 	//Balls
			//On tire 3 balles dans 3 directions (h, diago, semi-diago)
			int timeBalls = timer-180;
			//ulDebug("tb:%d\n", timeBalls);
			if(timeBalls == 1)
			{
			   //Boulette 1
			   balls[0].x = FIX(x+54);
			   balls[0].y = FIX(y);
			   balls[0].vx = -256;
			   balls[0].vy = 0;
			   balls[0].free = false;
			   AS_SoundDefaultPlay(sndFire, 43776, soundVolume, 64, 0, 1);
			}
			if(timeBalls == 30)
			{
			   //Boulette 2
			   balls[1].x = FIX(x+54);
			   balls[1].y = FIX(y);
			   balls[1].vx = -256;
			   balls[1].vy = -128;
			   balls[1].free = false;
			   AS_SoundDefaultPlay(sndFire, 43776, soundVolume, 64, 0, 1);
			}
			if(timeBalls == 60)
			{
			   //Boulette 3
			   balls[2].x = FIX(x+54);
			   balls[2].y = FIX(y);
			   balls[2].vx = -256;
			   balls[2].vy = -256;
			   balls[2].free = false;
			   AS_SoundDefaultPlay(sndFire, 43776, soundVolume, 64, 0, 1);
			}
			if(timeBalls == 90)
			{
			   //Boulette 4
			   balls[3].x = FIX(x+54);
			   balls[3].y = FIX(y);
			   balls[3].vx = -256;
			   balls[3].vy = 0;
			   balls[3].free = false;
			   AS_SoundDefaultPlay(sndFire, 43776, soundVolume, 64, 0, 1);
			}
			if(timeBalls == 120)
			{
			   //Boulette 5
			   balls[4].x = FIX(x+54);
			   balls[4].y = FIX(y);
			   balls[4].vx = -256;
			   balls[4].vy = +64;
			   balls[4].free = false;
			   AS_SoundDefaultPlay(sndFire, 43776, soundVolume, 64, 0, 1);
			}
			if(timeBalls == 150)
			{
			   //Boulette 6
			   balls[5].x = FIX(x+54);
			   balls[5].y = FIX(y);
			   balls[5].vx = -256;
			   balls[5].vy = 32;
			   balls[5].free = false;
			   AS_SoundDefaultPlay(sndFire, 43776, soundVolume, 64, 0, 1);
			}
			if(timeBalls == 180)
			{
			   //Boulette 7
			   balls[6].x = FIX(x+54);
			   balls[6].y = FIX(y);
			   balls[6].vx = -256;
			   balls[6].vy = -32;
			   balls[6].free = false;
			   AS_SoundDefaultPlay(sndFire, 43776, soundVolume, 64, 0, 1);
			}
			if(timeBalls == 210)
			{
			   //Boulette 8
			   balls[7].x = FIX(x+54);
			   balls[7].y = FIX(y);
			   balls[7].vx = -512;
			   balls[7].vy = 0;
			   balls[7].free = false;
			   AS_SoundDefaultPlay(sndFire, 43776, soundVolume, 64, 0, 1);
			}
			if(timeBalls == 240)
			{
			   //Boulette 9
			   balls[8].x = FIX(x+54);
			   balls[8].y = FIX(y);
			   balls[8].vx = -512;
			   balls[8].vy = 32;
			   balls[8].free = false;
			   AS_SoundDefaultPlay(sndFire, 43776, soundVolume, 64, 0, 1);
			}
			if(timeBalls == 270)
			{
			   //Boulette 10
			   balls[9].x = FIX(x+54);
			   balls[9].y = FIX(y);
			   balls[9].vx = -512;
			   balls[9].vy = 8;
			   balls[9].free = false;
			   AS_SoundDefaultPlay(sndFire, 43776, soundVolume, 64, 0, 1);
			}
			if(timeBalls == 300)
			{
			   //Boulette 11
			   balls[10].x = FIX(x+54);
			   balls[10].y = FIX(y);
			   balls[10].vx = -512;
			   balls[10].vy = 0;
			   balls[10].free = false;
			   AS_SoundDefaultPlay(sndFire, 43776, soundVolume, 64, 0, 1);
			}
			if(timeBalls == 300)
			{
			   //Boulette 12
			   balls[11].x = FIX(x+54);
			   balls[11].y = FIX(y);
			   balls[11].vx = -512;
			   balls[11].vy = -64;
			   balls[11].free = false;
			   AS_SoundDefaultPlay(sndFire, 43776, soundVolume, 64, 0, 1);
			}		 
		}
		if(sequence == 3)
		{
		   //Dash
		   int timeDash = timer-960;
		   if(timeDash < 30)
		   {
		   	x -= 2;
		   	isDashing = true;
		 	}  	
		   if(timeDash >= 30)
		   {
		   	x += 2;
		 	}  	
		   if(x > orix)
		   	x = orix;
		   
		}
		if(sequence == 2)
		{
		   //Laser
		   laserW += 1;
		   laserHitbox.x1 = 0;
		   laserHitbox.x2 = laserW;
		   int timeLaser = timer-660;
		   if(timeLaser == 1)
		   {
		   	laserAlpha = 31; 
		   }
			if(timeLaser > 200)
		   {
		   	laserAlpha--;
		   	laserHitbox.x1 = 0;
		   	laserHitbox.x2 = 0;
		   }   
		   
		}
		
		//On update les boulettes
		for(int k = 0; k < 12; k++)
		{
			if(balls[k].free == false && life > 0)
			{
			   balls[k].x += balls[k].vx;
			   balls[k].y += balls[k].vy;
			   //Vérif des limites
			   if(balls[k].x < x-FIX(256))
			   	balls[k].free = true;
				if(balls[k].y < y-FIX(192))
			   	balls[k].free = true;
			}      
		}	   
	}			
}

void eBoss1::CheckInteractions()
{
	//Attaques provenant du héros
	//ulDebug("life:%d\n", life);
	if(life > 0)
	{
	   hitTime--;
	   if(hitTime < 0)
	   	hitTime = 0;
		//On vérifie les collisions avec l'oeil
		if(worldMan->hero->isGroundBattle)
	   {
	   	if(worldMan->hero->sensH)
			{
				//Collision combo coup droit 
				if(hitTime == 0 && RectCollision(x, y, &rect, UNFIX(worldMan->hero->x), UNFIX(worldMan->hero->y), &worldMan->hero->rectBattleRight) )
				{
				   life -= 5;
				   hit = true;
					hitTime = 15; 
				}   
			}
			else
			{
			   //Collision combo coup gauche
				if(hitTime == 0 && RectCollision(x, y, &rect, UNFIX(worldMan->hero->x), UNFIX(worldMan->hero->y), &worldMan->hero->rectBattleLeft) )
				{
				   life -= 5;
				   hit = true;
				   hitTime = 15; 
				}
			}						 
	   }
	   
	   //On vérifie les collisions entre les boulettes et le hero
	   for(int k = 0; k < 12; k++)
	   {
	   	if(balls[k].free == false)
	   	{
	   	   if(RectCollision(UNFIX(balls[k].x), UNFIX(balls[k].y), &balls[k].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;
						//On fait perdre de la vie aussi
						worldMan->hero->life -= 10; 
					}
	         }						  
	   	}   
	   }
		
		//Collisions entre le dash et le héros
		if(isDashing)
		{
		   if(RectCollision(x, 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;
					//On fait perdre de la vie aussi
					worldMan->hero->life -= 20; 
				}
	      }
		}
		
		//Collisions entre le laser et le héros
		if(laserAlpha == 31)
		{
			if(RectCollision(x-laserW, y+8, &laserHitbox, UNFIX(worldMan->hero->x), UNFIX(worldMan->hero->y), &worldMan->hero->rect) )
	   	{
		   	if(worldMan->hero->dontHitTime == -1)
				{
					worldMan->hero->dontHitTime = 30;  
					worldMan->hero->dontMoveTime = 3;
					//On fait perdre de la vie aussi
					worldMan->hero->life -= 5; 
				}
	      }  
		}		 
		  
	}   
}   

void eBoss1::Destroy()
{
   
}
