void CDeathPirate::Update(float fElapsedTime)
{
	CIdleEnemy::Update(fElapsedTime);

	m_fTimeWaited += fElapsedTime;

	if(GetGround())
	{
		SetBaseVelY(0);
	}
	else
	{
		SetBaseVelY(GetBaseVelY() + 50*fElapsedTime);

		if(GetBaseVelY() > 75)
			SetBaseVelY(75);

		SetPosY(GetPosY() + GetBaseVelY() *fElapsedTime);
	}

	switch(ReturnAIState())
	{
	case Idle:
		GetAnimations()->SetCurrentAnimation(0);
		break;
	case iActive:
		if(m_fTimeWaited >= 15.0f && !GetCulling())
		{
			m_fTimeWaited = 0.0f;

			if(Spawn_01 == NULL)
			{
				CGame::GetInstance()->GetMessageSystemPointer()->SendMsg(new CCreateEnemyMessage(6, (int)GetPosX()-100, (int)GetPosY()-150, NULL));
			}
			else if(Spawn_02 == NULL)
			{
				CGame::GetInstance()->GetMessageSystemPointer()->SendMsg(new CCreateEnemyMessage(6, (int)GetPosX()+GetWidth()+150, (int)GetPosY()-150, NULL));
			}
		}

		GetAnimations()->SetCurrentAnimation(1);
		if(!GetAnimations()->SameFrame() && GetAnimations()->GetTrigger() != 0)
		{
			CGame::GetInstance()->GetMessageSystemPointer()->SendMsg(new CCreateBulletMessage(this));
		}
		break;
	case iDead:
		ReleaseSpawner();
		CGame::GetInstance()->GetMessageSystemPointer()->SendMsg(new CDestroyEnemyMessage((CBaseEnemy*)this));
		CSinglePlayerState::GetInstance()->SetWin(true);
		break;
	};
}
CDeathPirate::CDeathPirate(int nImageID, float PosX, float PosY,int Width, int Height, int nState,
				int nMaxHP, int nCurrentHP, int nSightRange, int nAttackRange, int nGlobalType, float fRateOfFire, 
				float fSpeed) : CIdleEnemy(nState, nGlobalType, nImageID, nMaxHP, nCurrentHP, nSightRange, 
				nAttackRange, fRateOfFire, fSpeed, PosX, PosY, Width, Height)
{
	SetAnimations(CMapLoad::GetInstance()->CreateAnimation(Boss_Pirate));
	GetAnimations()->SetCurrentAnimation(0);
	Spawn_01 = NULL;
	Spawn_02 = NULL;
	m_fTimeWaited = 5;
}
void CFighter::Update(float fElapsedTime)
{
	// Update common properties across all enemies
	CEnemy::Update(fElapsedTime);

	// if this enemy is cc'd don't 
	if (m_pEState->GetTimer() == true)
	{
		m_pEState->CEntityState::Update(fElapsedTime);
		return;
	}

	if (GetAnimInfo()->GetAnimationName() == GetAnimations("Idle") && m_bAttacking == true)
		m_bSecondAttackQueued = m_bAttacking = false;


	if (m_bAttacking == true)
		return;

	m_fUpdateOldPos += fElapsedTime;

	if(GetTarget() != nullptr)
	{
		float tar_pos_x = GetTarget()->GetPosX() - 32;
		float tar_pos_y = GetTarget()->GetPosY();

		if(tar_pos_x > GetPosX())
		{
			//set Grunt's animation's facing to the right
			SetFlipped( true );
		}
		else
		{
			SetFlipped( false );
		}


		if(m_fMoveAway <= 0)
		{

			//Simple Pathing twards the player
			if(tar_pos_y != GetPosY())//Above the Player
			{
				float min_Distance = (float)(GetTarget()->GetWidth()/2 + GetWidth()/2);
				if(GetPosX() + min_Distance > tar_pos_x && GetPosX() - min_Distance < tar_pos_x)
				{
					if( tar_pos_x < GetPosX())
						SetVelX(speed * fElapsedTime);
					else
						SetVelX(-speed * fElapsedTime);
				}
				else
				{
					if( tar_pos_y < GetPosY())
						SetVelY(-speed * fElapsedTime);
					else
						SetVelY(speed * fElapsedTime);
					if( tar_pos_x < GetPosX())
						SetVelX(-speed * fElapsedTime);
					else
						SetVelX(speed * fElapsedTime);
				}
				int threshold = 5;
				if(tar_pos_x - GetPosX() < threshold && tar_pos_x - GetPosX() > -1 * threshold)
					SetVelX(0);
				if(tar_pos_y - GetPosY() < threshold && tar_pos_y - GetPosY() > -1 * threshold)
					SetVelY(0);
			}
			else
			{
				SetVelY(0);
				if( tar_pos_x < GetPosX())
					SetVelX(-speed * fElapsedTime);
				else
					SetVelX(speed * fElapsedTime);
			}

		}
		else
		{
			m_fMoveAway -= fElapsedTime;
			if(m_bEvadeUp)
				SetVelY(-speed * fElapsedTime);
			else
				SetVelY(speed * fElapsedTime);
		}

		//Check Colider
		if(TileManager::GetInstance()->CheckBlockedTiles(GetPosX(), GetPosY(), GetVelX(), 0))
			SetPosX(GetPosX() + GetVelX());
		else if(TileManager::GetInstance()->CheckBlockedTiles(GetPosX(), GetPosY(), -GetVelX(), 0))
			SetPosX(GetPosX() - GetVelX());
		else
		{
			if(TileManager::GetInstance()->CheckBlockedTiles(GetPosX(), GetPosY(), 0, GetVelY()))
				SetPosY(GetPosY() + GetVelY());
			else if(TileManager::GetInstance()->CheckBlockedTiles(GetPosX(), GetPosY(), 0, -GetVelY()))
				SetPosY(GetPosY() - GetVelY());
			else
			{
				SetPosX(m_fStartPosX);
				SetPosY(m_fStartPosY);
			}
		}
		if(TileManager::GetInstance()->CheckBlockedTiles(GetPosX(), GetPosY(), 0, GetVelY()))
			SetPosY(GetPosY() + GetVelY());
		else if(TileManager::GetInstance()->CheckBlockedTiles(GetPosX(), GetPosY(), 0, -GetVelY()))
			SetPosY(GetPosY() - GetVelY());
		if(GetPosX() < 0)
			SetPosX(0);

		if(cooldown <= 0)
		{
			if((GetPosX() - tar_pos_x) < 120 && (GetPosX() - tar_pos_x) > -64 )
			{
				if(GetPosY() - tar_pos_y < 16 && GetPosY() - tar_pos_y > -16)
				{
					GetAnimInfo()->SetAnimationName(m_vAttacks[rand() % 4]);
					m_bAttacking = true;
					cooldown = 3.0f;
				}
			}
		}
		else
			cooldown -= fElapsedTime;

		//Update/use move away
		if(m_fUpdateOldPos >= 0.5f)
		{
			if(m_fPosXOld + 5 > GetPosX() && m_fPosXOld - 5 < GetPosX())
			{
				if(m_fPosYOld + 5 > GetPosY() && m_fPosYOld - 5 < GetPosY())
				{
					//if(!m_bRushing)
					{
						m_bEvadeUp = !m_bEvadeUp;
						if(m_fMoveAway <= 0)
							m_fMoveAway = 3.0f;
					}
				}
			}
			m_fPosXOld = GetPosX();
			m_fPosYOld = GetPosY();
			m_fUpdateOldPos = 0.0f;
		}
	}
}