void EnemySniper::Tick(double deltaTime) {

    if (m_State != STATE_DIE) {

        m_PointingDir = m_PlayerPtr->GetPos() - m_Pos;


        //DOUBLE2 screenLeftTop=  (*m_MatViewPtr).Inverse().TransformPoint( DOUBLE2() );
        DOUBLE2 screenRightDown = (*m_MatViewPtr).Inverse().TransformPoint(DOUBLE2(GAME_ENGINE->GetWidth(), GAME_ENGINE->GetHeight()));

        if (m_Pos.x > screenRightDown.x - 16) return;

        double newMax = 2 * M_PI / 12; // Kartelig laten bewegen
        double tussenWaarde = -m_PointingDir.AngleWith(DOUBLE2(1, 0));
        tussenWaarde /= newMax;
        double naKomma = tussenWaarde - (int)tussenWaarde;
        tussenWaarde = (int)tussenWaarde;
        if (naKomma > 0.5) ++tussenWaarde;
        m_DirRadians = tussenWaarde *newMax;

        m_Pos = m_Pos;

        ++m_ShootCounter;
        if (m_ShootCounter > 20) {
            m_ShootCounter = 0;
            if (m_State == STATE_NORMAL) {
                ++m_bulletsToShoot; // Switch systeem
                if (m_bulletsToShoot >= 3) m_State = STATE_SHOOTING;

            }
            else if (m_State == STATE_SHOOTING) {
                --m_bulletsToShoot; // Switch systeem
                if (m_bulletsToShoot <= 0) m_State = STATE_NORMAL;

                DOUBLE2 bulletDir(cos(m_DirRadians), sin(m_DirRadians));
                ObjectBase *bulletPtr = new Bullet(m_Pos + bulletDir * 15, bulletDir * 100, TYPE_ENEMY_BULLET, BULLET_NORMAL);
                m_ObjectListPtr->Add(bulletPtr);
            }
        }

    }
    else { //STATE_DIE

        m_Pos += DOUBLE2(100 * -Sign(m_PointingDir.x), -110)*deltaTime;

        m_TimeToDie -= deltaTime;
        if (m_TimeToDie <= 0) {
            m_ObjectListPtr->Delete(this);
        }

    }
}
void PlayerInputComponent::Update(float dt)
{
	//Check game pad direction
	if (M5Input::GamePadIsConnected())
	{
		M5Vec2 dir;
		M5Input::GetLeftThumb(dir);


		if (dir.LengthSquared() > 0)
		{
			m_pObj->rotation = std::atan2f(dir.y, dir.x);
			M5Vec2::Scale(dir, dir, m_forwardSpeed * dt);
			m_pObj->vel += dir;
			M5Vec2::Scale(m_pObj->vel, m_pObj->vel, m_speedDamp);
		}
	}


	//first check for rotation
	if (M5Input::IsPressed(M5_A)) 
	{
		m_pObj->rotationVel += m_rotationSpeed * dt;
		m_pObj->rotationVel *= m_rotationalDamp;
	}
	else if (M5Input::IsPressed(M5_D))
	{
		m_pObj->rotationVel -= m_rotationSpeed * dt; 
		m_pObj->rotationVel *= m_rotationalDamp;
	}
	else
		m_pObj->rotationVel = 0;

	//Then check for forward movement
	if (M5Input::IsPressed(M5_W))
	{
		//Get vector from rotation
		M5Vec2 dir(std::cos(m_pObj->rotation), std::sin(m_pObj->rotation));
		M5Vec2::Scale(dir, dir, m_forwardSpeed * dt);
		m_pObj->vel += dir;
		M5Vec2::Scale(m_pObj->vel, m_pObj->vel, m_speedDamp);
	}

	//then check for bullets 
	if (M5Input::IsTriggered(M5_SPACE) || M5Input::IsTriggered(M5_GAMEPAD_A))
	{
	
		M5Object* bullet1 = M5ObjectManager::CreateObject(AT_Bullet);
		M5Object* bullet2 = M5ObjectManager::CreateObject(AT_Bullet);
		bullet2->rotation = bullet1->rotation = m_pObj->rotation;

		M5Vec2 bulletDir(std::cos(bullet1->rotation), std::sin(bullet1->rotation));
		M5Vec2 perp(bulletDir.y, -bulletDir.x);
		bullet1->pos = m_pObj->pos + perp * .5f * m_pObj->scale.y;
		bullet2->pos = m_pObj->pos - perp * .5f * m_pObj->scale.y;

		M5Vec2::Scale(bulletDir, bulletDir, m_bulletSpeed * dt);

		bullet1->vel = m_pObj->vel + bulletDir;
		bullet2->vel = m_pObj->vel + bulletDir;

	}
}