bool Enemy::Update()
{
	bool ret = true;
	
	if (type == explosionE)
	{
		current_anim = &up;
		if (current_anim->Finished())
			ret = false;

		return ret;
	}

	last_position = position;

	current_anim = &right;

	switch (direction_enemy)
	{
	case upD:
		position.y -= VELOCITY_ENEMY;
		current_anim = &up;
		break;
	case downD:
		position.y += VELOCITY_ENEMY;
		current_anim = &down;
		break;
	case rightD:
		position.x += VELOCITY_ENEMY;
		current_anim = &right;
		break;
	case leftD:
		position.x -= VELOCITY_ENEMY;
		current_anim = &left;
		break;
	}


	if (collider != NULL)
	{
		SDL_Rect r = current_anim->PeekCurrentFrame();
		collider->rect = { position.x, position.y+(r.h-TILE), TILE, TILE };
	}

	//Limits-----------------------------------------------------------------
	if (collider->rect.x < 24 || collider->rect.x + 16 > 232 || collider->rect.y < 40 || collider->rect.y > 216 - 16)
	{
		position = last_position;
		GetNewDirection();
	}

	return ret;
}
void MLBSTrackingFilter< NumImageFeatures >::ThreadedGenerateData(const InputImageRegionType &regionForThread, ThreadIdType threadId)
{
    m_Mutex.Lock();
    m_Threads++;
    m_Mutex.Unlock();
    typedef ImageRegionConstIterator< ItkUcharImgType >     MaskIteratorType;
    MaskIteratorType    sit(m_SeedImage, regionForThread );
    MaskIteratorType    mit(m_MaskImage, regionForThread );

    sit.GoToBegin();
    mit.GoToBegin();
    itk::Point<double> worldPos;
    while( !sit.IsAtEnd() )
    {
        if (sit.Value()==0 || mit.Value()==0)
        {
            ++sit;
            ++mit;
            continue;
        }

        for (int s=0; s<m_SeedsPerVoxel; s++)
        {
            FiberType fib;
            double tractLength = 0;
            typename FeatureImageType::IndexType index = sit.GetIndex();
            itk::ContinuousIndex<double, 3> start;
            unsigned int counter = 0;

            if (m_SeedsPerVoxel>1)
            {
                start[0] = index[0]+GetRandDouble(-0.5, 0.5);
                start[1] = index[1]+GetRandDouble(-0.5, 0.5);
                start[2] = index[2]+GetRandDouble(-0.5, 0.5);
            }
            else
            {
                start[0] = index[0];
                start[1] = index[1];
                start[2] = index[2];
            }

            // get staring position
            m_SeedImage->TransformContinuousIndexToPhysicalPoint( start, worldPos );

            // get starting direction
            int candidates = 0;
            double prob = 0;
            vnl_vector_fixed<double,3> dirOld; dirOld.fill(0.0);
            vnl_vector_fixed<double,3> dir = Classify(worldPos, candidates, dirOld, 0, prob);
            if (dir.magnitude()<0.0001)
                continue;

            // forward tracking
            tractLength = FollowStreamline(threadId, worldPos, dir, &fib, 0, false);
            fib.push_front(worldPos);

            if (m_RemoveWmEndFibers)
            {
                itk::Point<double> check = fib.back();
                dirOld.fill(0.0);
                vnl_vector_fixed<double,3> check2 = GetNewDirection(check, dirOld);
                if (check2.magnitude()>0.001)
                {
                    MITK_INFO << "Detected WM ending. Discarding fiber.";
                    continue;
                }
            }

            // backward tracking
            tractLength = FollowStreamline(threadId, worldPos, -dir, &fib, tractLength, true);
            counter = fib.size();

            if (m_RemoveWmEndFibers)
            {
                itk::Point<double> check = fib.front();
                dirOld.fill(0.0);
                vnl_vector_fixed<double,3> check2 = GetNewDirection(check, dirOld);
                if (check2.magnitude()>0.001)
                {
                    MITK_INFO << "Detected WM ending. Discarding fiber.";
                    continue;
                }
            }

            if (tractLength<m_MinTractLength || counter<2)
                continue;

            m_Mutex.Lock();
            m_Tractogram.push_back(fib);
            m_Mutex.Unlock();

            if (m_AbortTracking)
                break;
        }
        if (m_AbortTracking)
            break;
        ++sit;
        ++mit;
    }
    m_Threads--;
    std::cout << "Thread " << threadId << " finished tracking" << std::endl;
}
double MLBSTrackingFilter< NumImageFeatures >::FollowStreamline(ThreadIdType threadId, itk::Point<double, 3> pos, vnl_vector_fixed<double,3> dir, FiberType* fib, double tractLength, bool front)
{
    vnl_vector_fixed<double,3> dirOld = dir;
    dirOld = dir;

    for (int step=0; step< m_MaxLength/2; step++)
    {
        while (m_PauseTracking){}
        if (m_DemoMode)
        {
            m_Mutex.Lock();
            m_BuildFibersReady++;
            m_Tractogram.push_back(*fib);
            BuildFibers(true);
            m_Stop = true;
            m_Mutex.Unlock();
            while (m_Stop){}
        }

        // get new position
        CalculateNewPosition(pos, dir);

        // is new position inside of image and mask
        if (!IsValidPosition(pos) || m_AbortTracking)   // if not end streamline
        {
            return tractLength;
        }
        else    // if yes, add new point to streamline
        {
            tractLength +=  m_StepSize;
            if (front)
                fib->push_front(pos);
            else
                fib->push_back(pos);

            if (m_AposterioriCurvCheck)
            {
                int curv = CheckCurvature(fib, front);  // TODO: Move into classification ???
                if (curv>0)
                {
                    tractLength -= m_StepSize*curv;
                    while (curv>0)
                    {
                        if (front)
                            fib->pop_front();
                        else
                            fib->pop_back();
                        curv--;
                    }
                    return tractLength;
                }
            }

            if (tractLength>m_MaxTractLength)
                return tractLength;
        }

        dir = GetNewDirection(pos, dirOld);

        if (dir.magnitude()<0.0001)
            return tractLength;
    }
    return tractLength;
}