Esempio n. 1
0
void Player::Move(sf::Time &deltaTime, Tilemap &t)
{
    //moving right and slowing down
    if(m_isMovingRight == true)
        m_movement.x += m_movSpeedConst * deltaTime.asMilliseconds();
    else if(m_isMovingRight == false && m_movement.x > 0)
    {
        m_movement.x -= m_movSpeedConst * 1.5 * deltaTime.asMilliseconds();
        if(m_movement.x < 0)
            m_movement.x = 0;
    }

    //moving left and slowing down
    if(m_isMovingLeft == true)
        m_movement.x -= m_movSpeedConst * deltaTime.asMilliseconds();
    else if(m_isMovingLeft == false && m_movement.x < 0)
    {
        m_movement.x += m_movSpeedConst* 1.5 * deltaTime.asMilliseconds();
        if(m_movement.x > 0)
            m_movement.x = 0;
    }

    //not over max speed
    if(m_movement.x >= m_movSpeedMax)
        m_movement.x = m_movSpeedMax;
    else if(m_movement.x <= -m_movSpeedMax)
        m_movement.x = -m_movSpeedMax;

    //don't move left right if colliding with wall
    if(int(m_bottom) % 64 == 0 && (t.getLevelMap((m_bottom + 2)/64, m_left/64) == 192 || t.getLevelMap((m_bottom + 2)/64, m_left/64) == 191 || t.getLevelMap((m_bottom + 2)/64, m_right/64) == 191 || t.getLevelMap((m_bottom + 2)/64, m_right/64) == 190 || t.getLevelMap((m_bottom + 2)/64, m_left/64) == 192 || t.getLevelMap((m_bottom + 2)/64, m_left/64) == 592 || t.getLevelMap((m_bottom + 2)/64, m_left/64) == 591 || t.getLevelMap((m_bottom + 2)/64, m_right/64) == 591 || t.getLevelMap((m_bottom + 2)/64, m_right/64) == 590))
    {}
    else if((t.getLevelMap(m_bottom/64, m_left/64) == 192 || t.getLevelMap(m_bottom/64, m_left/64) == 292 || t.getLevelMap(m_bottom/64, m_left/64) == 392 || t.getLevelMap(m_bottom/64, m_left/64) == 490 || t.getLevelMap(m_bottom/64, m_left/64) == 592 || t.getLevelMap(m_bottom/64, m_left/64) == 191 || t.getLevelMap(m_bottom/64, m_left/64) == 291 || t.getLevelMap(m_bottom/64, m_left/64) == 391 || t.getLevelMap(m_bottom/64, m_left/64) == 491 || t.getLevelMap(m_bottom/64, m_left/64) == 591) && m_movement.x < 0)
        m_movement.x = 0;
    else if((t.getLevelMap(m_bottom/64, m_right/64) == 190 || t.getLevelMap(m_bottom/64, m_right/64) == 290 || t.getLevelMap(m_bottom/64, m_right/64) == 390 || t.getLevelMap(m_bottom/64, m_right/64) == 492 || t.getLevelMap(m_bottom/64, m_right/64) == 590 || t.getLevelMap(m_bottom/64, m_right/64) == 191 || t.getLevelMap(m_bottom/64, m_right/64) == 291 || t.getLevelMap(m_bottom/64, m_right/64) == 391 || t.getLevelMap(m_bottom/64, m_right/64) == 491 || t.getLevelMap(m_bottom/64, m_right/64) == 591) && m_movement.x > 0)
        m_movement.x = 0;
    else if((t.getLevelMap((m_top + 2)/64, m_left/64) == 192 || t.getLevelMap((m_top + 2)/64, m_left/64) == 292 || t.getLevelMap((m_top + 2)/64, m_left/64) == 392 || t.getLevelMap((m_top + 2)/64, m_left/64) == 490 || t.getLevelMap((m_top + 2)/64, m_left/64) == 592 || t.getLevelMap((m_top + 2)/64, m_left/64) == 191 || t.getLevelMap((m_top + 2)/64, m_left/64) == 291 || t.getLevelMap((m_top + 2)/64, m_left/64) == 391 || t.getLevelMap((m_top + 2)/64, m_left/64) == 491 || t.getLevelMap((m_top + 2)/64, m_left/64) == 591) && m_movement.x < 0)
        m_movement.x = 0;
    else if((t.getLevelMap((m_top + 2)/64, m_right/64) == 190 || t.getLevelMap((m_top + 2)/64, m_right/64) == 290 || t.getLevelMap((m_top + 2)/64, m_right/64) == 390 || t.getLevelMap((m_top + 2)/64, m_right/64) == 492 || t.getLevelMap((m_top + 2)/64, m_right/64) == 590 || t.getLevelMap((m_top + 2)/64, m_right/64) == 191 || t.getLevelMap((m_top + 2)/64, m_right/64) == 291 || t.getLevelMap((m_top + 2)/64, m_right/64) == 391 || t.getLevelMap((m_top + 2)/64, m_right/64) == 491 || t.getLevelMap((m_top + 2)/64, m_right/64) == 591) && m_movement.x > 0)
        m_movement.x = 0;

    //don't move if colliding with a colliding crate!
    for(int i = 0; i < t.getCrateSize(); i++)
    {
        if(t.getCrateOnLeft(i) == true || t.getCrateOnRight(i) == true)
        {
            if(m_right < t.getCrateLeft(i) || m_left > t.getCrateRight(i) || m_top > t.getCrateBottom(i) || m_bottom < t.getCrateTop(i))
            {}
            else if(m_right >= t.getCrateLeft(i) && (m_right + m_left)/2 <= t.getCrateLeft(i) && m_movement.x > 0 &&(m_bottom - 2) >= t.getCrateTop(i))
                m_movement.x = 0;
            else if(m_left <= t.getCrateRight(i) && (m_right + m_left)/2 >= t.getCrateLeft(i) && m_movement.x < 0 && (m_bottom - 2) >= t.getCrateTop(i))
                m_movement.x = 0;
        }
    }

    //jumping
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Space) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        m_jumped = true;

    //if hit top of block, fall
    if((t.getLevelMap(m_top/64, (m_left + 2)/64) == 190 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 190 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 191 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 191 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 192 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 192 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 290 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 290 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 291 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 291 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 292 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 292 ||
            t.getLevelMap(m_top/64, (m_left + 2)/64) == 390 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 390 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 391 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 391 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 392 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 392 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 490 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 490 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 491 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 491 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 492 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 492 ||
            t.getLevelMap(m_top/64, (m_left + 2)/64) == 590 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 590 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 591 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 591 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 592 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 592) && m_movement.y < 0)
        m_movement.y = 0;

    //setting jump stuff
    if(m_jumped == true && m_onGround == true)
    {
        m_movement.y = -m_gravity;
        m_jumped = false;
    }
    else
    {
        m_jumped = false;

        if(!m_onGround)
            m_movement.y += m_gravity * 0.003 * deltaTime.asMilliseconds();
        else
            m_movement.y = 0;

        if(m_movement.y > 0.5)
            m_movement.y = 0.5;
    }

    //move if on a platform
    for(int i = 0; i < t.getPlatformSize(); i++)
    {
        //not touching
        if(m_right < t.getPlatformLeft(i) || m_left > t.getPlatformRight(i) || m_top > t.getPlatformBottom(i) || m_bottom < t.getPlatformTop(i))
        {}
        else if(m_bottom - 2 > t.getPlatformTop(i))
        {}
        else if(m_left > t.getPlatformLeft(i) && m_right < t.getPlatformRight(i))
            m_movement.x += t.getPlatformMovement(i).x;
        else if( m_left < t.getPlatformLeft(i) && (m_left + m_right)/2 > t.getPlatformLeft(i) && int(m_movement.x) == 0)
            m_movement.x += t.getPlatformMovement(i).x;
        else if( m_right > t.getPlatformRight(i) && (m_left + m_right)/2 < t.getPlatformRight(i) && int(m_movement.x) == 0)
            m_movement.x += t.getPlatformMovement(i).x;
    }
    //move if on a crate (movement will be non-zero if crate is on a platform so...)
    /*for(int i = 0; i < t.getCrateSize(); i++)
    {
    	if(m_right < t.getCrateLeft(i) || m_left > t.getCrateRight(i) || m_top > t.getCrateBottom(i) || m_bottom < t.getCrateTop(i))
    		{}
    	else if(int(m_bottom) <= t.getCrateTop(i))
    		m_movement.x += t.getCrateMovement(i).x;
    }*/

    //the actual move part
    m_rect.move(m_movement.x, m_movement.y);
    m_sprite.move(m_movement.x, m_movement.y);

    //change the movement after actually moving so it doesn't mess up earlier processes
    for(int i = 0; i < t.getPlatformSize(); i++)
    {
        //not touching
        if(m_right < t.getPlatformLeft(i) || m_left > t.getPlatformRight(i) || m_top > t.getPlatformBottom(i) || m_bottom < t.getPlatformTop(i))
        {}
        else if(m_bottom - 2 > t.getPlatformTop(i))
        {}
        else if(m_left > t.getPlatformLeft(i) && m_right < t.getPlatformRight(i))
            m_movement.x -= t.getPlatformMovement(i).x;
        else if( m_left < t.getPlatformLeft(i) && (m_left + m_right)/2 > t.getPlatformLeft(i) && int(m_movement.x) == 0)
            m_movement.x -= t.getPlatformMovement(i).x;
        else if( m_right > t.getPlatformRight(i) && (m_left + m_right)/2 < t.getPlatformRight(i) && int(m_movement.x) == 0)
            m_movement.x -= t.getPlatformMovement(i).x;
    }
    //change the movement after actually moving the crate so it doesn't mess up earlier processes
    /*for(int i = 0; i < t.getCrateSize(); i++)
    {
    	if(m_right < t.getCrateLeft(i) || m_left > t.getCrateRight(i) || m_top > t.getCrateBottom(i) || m_bottom < t.getCrateTop(i))
    		{}
    	else if(int(m_bottom) <= t.getCrateTop(i))
    		m_movement.x -= t.getCrateMovement(i).x;
    }*/

    /*if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) //FOR GOD MODE!!
    	m_movement.y = -0.25;
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
    	m_movement.y = 0.25;
    else
    	m_movement.y = 0;
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
    	m_movement.x = -0.25;
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
    	m_movement.x = 0.25; */
}
Esempio n. 2
0
void Player::Update(Tilemap &t, sf::RenderWindow &window, BackgroundProcesses &b)
{
    m_top = m_rect.getPosition().y;
    m_bottom = m_rect.getPosition().y + m_rect.getSize().y;
    m_left = m_rect.getPosition().x;
    m_right = m_rect.getPosition().x + m_rect.getSize().x;

    //collision with portal
    if(t.getLevelMap(m_bottom/64, m_left/64) == 999 || t.getLevelMap(m_bottom/64, m_right/64) == 999 || t.getLevelMap(m_top/64, m_left/64) == 999 || t.getLevelMap(m_top/64, m_right/64) == 999)
    {
        setPosition(sf::Vector2f(0, 0));
        t.setMap(window);
    }

    //respawning when falling through the bottom of the map!!
    if(m_bottom >= 30*64)
    {
        setPosition(sf::Vector2f(0, 0));
        t.setMap(t.getLevel(), window);
    }

    //downward collision on tilemap
    if(m_bottom / 64 < 0 || m_right < 0)
        m_onGround = false;
    else if(int(m_bottom) % 64 != 0)
        m_onGround = false;
    else if( (t.getLevelMap(m_bottom/64, m_left/64) >= 100 && t.getLevelMap(m_bottom/64, m_left/64) < 200) || (t.getLevelMap(m_bottom/64, m_left/64) >= 500 && t.getLevelMap(m_bottom/64, m_left/64) < 600) || (t.getLevelMap(m_bottom/64, m_right/64) >= 100 && t.getLevelMap(m_bottom/64, m_right/64) < 200) || (t.getLevelMap(m_bottom/64, m_right/64) >= 500 && t.getLevelMap(m_bottom/64, m_right/64) < 600) )
        m_onGround = true;
    else
        m_onGround = false;

    //downward collision on platform
    for(int i = 0; i < t.getPlatformSize(); i++)
    {
        if(m_right < t.getPlatformLeft(i) || m_left > t.getPlatformRight(i) || m_top > t.getPlatformBottom(i) || m_bottom < t.getPlatformTop(i))
        {}
        else if(int(m_bottom) <= t.getPlatformTop(i))
            m_onGround = true;
    }

    int whichCrate = -1;
    for(int i = 0; i < t.getCrateSize(); i++)
    {
        //downward collision on crate
        if(m_right < t.getCrateLeft(i) || m_left > t.getCrateRight(i) || m_top > t.getCrateBottom(i) || m_bottom < t.getCrateTop(i))
        {}
        else if(m_bottom >= t.getCrateTop(i) && m_bottom - 2 <= t.getCrateTop(i) && m_right - 2 > t.getCrateLeft(i) && m_left + 2 < t.getCrateRight(i))
        {
            m_onGround = true;
            m_onCrate = true;
            whichCrate = i;
            break;
        }
        else
        {
            m_onCrate = false;
        }
    }

    for(int i = 0; i < t.getCrateSize(); i++)
    {
        //downward collision on crate
        /*if(m_right < t.getCrateLeft(i) || m_left > t.getCrateRight(i) || m_top > t.getCrateBottom(i) || m_bottom < t.getCrateTop(i))
        {}
        else if(m_bottom >= t.getCrateTop(i) && m_bottom - 2 <= t.getCrateTop(i) && m_right - 2 > t.getCrateLeft(i) && m_left + 2 < t.getCrateRight(i))
        {
        	m_onGround = true;
        	m_onCrate = true;
        }
        else
        {
        	m_onCrate = false;
        } */

        if(i != whichCrate)
        {
            //not colliding with crate
            if(m_right < t.getCrateLeft(i) || m_left > t.getCrateRight(i) || m_top > t.getCrateBottom(i) || m_bottom < t.getCrateTop(i))
            {}

            //on top of crate
            else if(m_top <= t.getCrateBottom(i) && m_top >= (t.getCrateBottom(i) - 2))
            {
                t.setCratePos(i, sf::Vector2f(t.getCrateLeft(i), m_top - (t.getCrateBottom(i) - t.getCrateTop(i))));
            }

            //pushing crate right
            else if(m_right >= t.getCrateLeft(i) && m_left < t.getCrateLeft(i))
            {
                if(m_movement.x > 0)
                    t.setCratePos(i, sf::Vector2f(m_right, t.getCrateTop(i)));
            }

            //pushing crate left
            else if(m_left <= t.getCrateRight(i) && m_left > t.getCrateLeft(i))
            {
                if(m_movement.x < 0)
                    t.setCratePos(i, sf::Vector2f(m_left - (t.getCrateRight(i) - t.getCrateLeft(i)), t.getCrateTop(i)));
            }
        }
    }
}