Пример #1
0
void Character::update(sf::Time frametime, Input input) {

    if(!canMove)
        return;
    
    int x0 = position.x / Block::WIDTH;
    int y0 = position.y / Block::HEIGHT;
    float seconds = frametime.AsSeconds();

    //Carve

    if (input.LeftCarve && !lastInput.LeftCarve) {
        Block* b = world->getBlock(x0 - 1, y0 + 1);
        Block* c = world->getBlock(x0 - 1, y0);
        if (b && b->getType() == Block::WALL && !c->isSolid() && !c->isLadder() && !c->isRope())
            b->setActive(false);
    }

    if (input.RightCarve && !lastInput.RightCarve) {
        Block* b = world->getBlock(x0 + 1, y0 + 1);
        Block* c = world->getBlock(x0 + 1, y0);
        if (b && b->getType() == Block::WALL && !c->isSolid() && !c->isLadder() && !c->isRope())
            b->setActive(false);
    }
        lastInput = input;
    sf::Vector2f direction = sf::Vector2f(0, 0);
    Block* rope = world->getCollidingRope(getBbox());
    Block* ladder = world->getCollidingLadder(getBbox());

    bool isCentring = false;
    if (ladder) {
        
        isClimbing = true;

        if (input.Up)
            direction -= sf::Vector2f(0, speed.y);
        else if (input.Down)
            direction += sf::Vector2f(0, speed.y);

        if (input.Up || input.Down) {
            if (fabs(getCenter().x - ladder->getCenter().x) < speed.y * seconds)
                alignToGridX();
            else if (getCenter().x < ladder->getCenter().x)
                direction.x += speed.x;
            else if (getCenter().x > ladder->getCenter().x)
                direction.x -= speed.x;

            isCentring = true;
        }
        
    } else {
        isClimbing = false;
    }

    //Align to rope
    if (rope && (input.Left || input.Right || isFalling) && !(input.Down)) {
        isHanging = true;

        int deltaY = rope->getPosition().y - getPosition().y;
        if (abs(deltaY) < 4)
            setPosition(sf::Vector2f(position.x, rope->getPosition().y));
    }

    if (!rope || input.Down || rope->getPosition().y != getPosition().y)
        isHanging = false;

    if (isClimbing && isHanging)
        isClimbing = false;

    if (!isHanging && !isClimbing)
        isFalling = true;
    else
        isFalling = false;

    if (isFalling)
        direction += sf::Vector2f(0, speed.y);

    if (direction.y != 0) {
        setPosition(sf::Vector2f(position.x, position.y + direction.y * seconds));

        Entity *b = world->getCollidingSolid(getBbox());
        while (b != NULL) {
            if (direction.y < 0)
                setPosition(sf::Vector2f(position.x, b->getBbox().Top + b->getBbox().Height));
            else {
                setPosition(sf::Vector2f(position.x, b->getBbox().Top - getBbox().Height));
                isFalling = false;
            }
            b = world->getCollidingSolid(getBbox());
        }

        //Walk on ladder
        b = world->getCollidingLadder(getBbox());
        if (b && b->getPosition().y > getBbox().Top && isFalling && !input.Down) {
            setPosition(sf::Vector2f(position.x, b->getBbox().Top - getBbox().Height));
            isFalling = false;
        }
    }

    //Left Right - Up Down (ladder)
    if (!isFalling && !isCentring) {
        if (input.Left)
            direction -= sf::Vector2f(speed.x, 0);

        if (input.Right)
            direction += sf::Vector2f(speed.x, 0);
    }

    //Update X pos, and solve collisions
    if (direction.x != 0) {
        setPosition(sf::Vector2f(position.x + direction.x * seconds, position.y));


        Entity * b = world->getCollidingEnnemy(getBbox());
        if (b != NULL && b != this && b != world->getPlayer() && this != world->getPlayer()) {
            if (b->getPosition().x < getPosition().x)
                setPosition(getPosition() + sf::Vector2f(speed.x / 2 * seconds, 0));
            else
                setPosition(getPosition() - sf::Vector2f(speed.x / 2 * seconds, 0));
        }

        b = world->getCollidingSolid(getBbox());
        if (b != NULL) {
            if (direction.x < 0)
                setPosition(sf::Vector2f(b->getBbox().Left + b->getBbox().Width, position.y));
            else
                setPosition(sf::Vector2f(b->getBbox().Left - getBbox().Width, position.y));
        }


    }

    if (direction.x == 0)
        alignToGridX();

    if (direction.y == 0)
        alignToGridY();
}
Пример #2
0
QSizeF FreePathway::layout()
{
    foreach (DSBReaction *reac, m_reactions)
    {
        reac->layout();
    }

    nodemap nodeMap;
    ogdf::Graph *G = getOGDFGraph(nodeMap);
    jog(10.0, nodeMap);
    ogdf::GraphAttributes GA(*G);
    extractPosAndSize(nodeMap, GA);
    ogdf::StressMajorization strmaj;
    strmaj.call(GA);
    injectPositions(nodeMap, GA);
    QRectF box = getBbox(nodeMap);
    m_size = box.size();
    return m_size;
}

QRectF FreePathway::getBbox(nodemap &nodeMap)
{
    QList<DSBNode*> nodes = nodeMap.keys();
    assert(nodes.size() > 0);
    DSBNode *f = nodes.first();
    QRectF box = f->getShape()->boundingRect();
    for (int i = 1; i < nodes.size(); i++)
    {
        box = box.united(nodes.at(i)->getShape()->boundingRect());
    }
    return box;
Пример #3
0
void Character::alignToGridX() {
    setPosition(sf::Vector2f((int) (getCenter().x / Block::WIDTH) * Block::WIDTH + (Block::WIDTH - getBbox().Width) / 2, getPosition().y));
}
Пример #4
0
void Player::update(sf::Time frametime, Input input) {
  //First update velocity x
  
  _acc += frametime;
  while(_acc >= sf::milliseconds(GameConstant::SIMULATION_TIME_PER_UPDATE)) {
    _acc -= sf::milliseconds(GameConstant::SIMULATION_TIME_PER_UPDATE);
    float seconds = GameConstant::SIMULATION_TIME_PER_UPDATE / 1000.0;
    //Update velocity X
    if (input.Left) 
      _velocity.x = std::max(_velocity.x - 2 * _acceleration.x * seconds, -_max_walk_speed);
    else if(input.Right) {
      _velocity.x =  std::min(_velocity.x + 2* _acceleration.x * seconds, _max_walk_speed);
    }
  
    //Update velocity Y
    if(_noclip && input.Up)
      _velocity.y = std::max(_velocity.y - 2 * _acceleration.y * seconds, -_max_fall_speed);
    else if(_noclip && input.Down) 
      _velocity.y = std::min(_velocity.y + 2 * _acceleration.y * seconds, _max_fall_speed);
    else if(input.Up && !_is_flying) 
      _velocity.y -= _jump_force;
  
    //Now we can update position

    //First apply some friction
    if(_velocity.x > 0) 
      _velocity.x = std::max(0.F, _velocity.x - (_acceleration.x) * seconds);
    else if(_velocity.x < 0)
      _velocity.x = std::min(0.F, _velocity.x + (_acceleration.x) * seconds);
  
    if(_noclip && _velocity.y > 0) 
      _velocity.y = std::max(0.F, _velocity.y - (_acceleration.y) * seconds);
    else if(_noclip && _velocity.y < 0)
      _velocity.y = std::min(0.F, _velocity.y + (_acceleration.y) * seconds);
    else
      _velocity.y = std::min(_velocity.y + (_acceleration.y) * seconds, _max_fall_speed);
    
    //Update position and check for collision
    if(_velocity.x != 0){
      _position.x += _velocity.x * seconds;
    
      Cube *c = _world->getCollidingCube(getBbox());
      if( c != NULL){
	if(_velocity.x < 0)
	  _position.x = c->getBbox().left + c->getBbox().width;
	else
	  _position.x = c->getBbox().left - getBbox().width;
	_velocity.x = 0;
      }
    }
  
    if(_velocity.y != 0){
      _position.y += _velocity.y * seconds;
      _is_flying = true;
      Cube *c = _world->getCollidingCube(getBbox());
      if( c != NULL){
	if(_velocity.y < 0)
	  _position.y = c->getBbox().top + c->getBbox().height;
	else{
	  _position.y = c->getBbox().top - getBbox().height;
	  _is_flying = false;
	}
	_velocity.y = 0;
      }
    }
  }

}