/* * Mostra na tela os indicadores básicos do jogo: * vidas restantes, HP e pontuação. */ void Jogo::exibeHud() { float x = 0.10 * janela.getSize().x; float y = 0.15 * janela.getSize().y; /* Desenha vidas extras da nave */ static sf::CircleShape losango(20.0, 4); for (int i = 0; i < nave->getVidas(); i++) { double deltaX = 2.5 * losango.getRadius(); losango.setPosition(x + (i * deltaX), y); janela.draw(losango); } /* Desenha caixa da lifebar */ y += 3.0 * losango.getRadius(); static sf::RectangleShape caixa({200.0, 10.0}); caixa.setPosition(x, y); caixa.setFillColor(sf::Color::Blue); janela.draw(caixa); /* Desenha lifebar com parcial do HP */ float k = (float) nave->getHP() / nave->getHPMax(); sf::RectangleShape lifebar({k * caixa.getSize().x, caixa.getSize().y}); lifebar.setPosition(x, y); lifebar.setFillColor(k > 0.25 ? sf::Color::Green : sf::Color::Red); janela.draw(lifebar); /* Imprime pontuação (e, se for o caso, mensagem de pausa) */ y += 2.5 * lifebar.getSize().y; sf::String str = "Score: " + std::to_string(nave->getScore()); if (pausado) str += " (pausa)"; sf::Text texto(str, fonte, 20); texto.setPosition(x, y); janela.draw(texto); }
/* Name: checkCircleCollision Desc: given to circles, it will check to see if they are colliding. Args: p_circle1, the first circle p_circle2, the second circle to check against. Rtrn: bool, if the two are colliding */ bool Game::checkCircleCollision(const sf::CircleShape &p_circle1, const sf::CircleShape &p_circle2) { float xDistance = p_circle1.getPosition().x - p_circle2.getPosition().x; float yDistance = p_circle1.getPosition().y - p_circle2.getPosition().y; return sqrt((xDistance * xDistance) + (yDistance * yDistance)) < p_circle1.getRadius() + p_circle2.getRadius(); }
bool Knight::collisionSP(sf::CircleShape & player) { sf::FloatRect circle1Box = swordCircles[0].getGlobalBounds(); sf::FloatRect circle2Box = swordCircles[1].getGlobalBounds(); sf::FloatRect circle3Box = swordCircles[2].getGlobalBounds(); //if not swinging, we don't want collision to register so return false //if sword already intersected, return false to prevent multiple intersections on one swing (light, quick check) if (!isSwinging || swordIntersected) { return false; } //full collision check between sword circles and circle of player bool ret = false; sf::Vector2f playerCenter = sf::Vector2f(player.getGlobalBounds().left + player.getGlobalBounds().width / 2.0f, player.getGlobalBounds().top + player.getGlobalBounds().height / 2.0f); // circle 1 if (circle1Box.intersects(player.getGlobalBounds())) { sf::Vector2f circle1Center = sf::Vector2f(circle1Box.left + circle1Box.width / 2.0f, circle1Box.top + circle1Box.height / 2.0f); float dx = (playerCenter.x - circle1Center.x) * (playerCenter.x - circle1Center.x); float dy = (playerCenter.y - circle1Center.y) * (playerCenter.y - circle1Center.y); ret = dx + dy < std::pow(player.getRadius() + swordCircles[0].getRadius(), 2); if (ret) { swordIntersected = true; return true; } } // circle 2 if (circle2Box.intersects(player.getGlobalBounds())) { sf::Vector2f circle2Center = sf::Vector2f(circle2Box.left + circle2Box.width / 2.0f, circle2Box.top + circle2Box.height / 2.0f); float dx = (playerCenter.x - circle2Center.x) * (playerCenter.x - circle2Center.x); float dy = (playerCenter.y - circle2Center.y) * (playerCenter.y - circle2Center.y); ret = dx + dy < std::pow(player.getRadius() + swordCircles[1].getRadius(), 2); if (ret) { swordIntersected = true; return true; } } // circle 3 if (circle3Box.intersects(player.getGlobalBounds())) { sf::Vector2f circle3Center = sf::Vector2f(circle3Box.left + circle3Box.width / 2.0f, circle3Box.top + circle3Box.height / 2.0f); float dx = (playerCenter.x - circle3Center.x) * (playerCenter.x - circle3Center.x); float dy = (playerCenter.y - circle3Center.y) * (playerCenter.y - circle3Center.y); ret = dx + dy < std::pow(player.getRadius() + swordCircles[2].getRadius(), 2); if (ret) { swordIntersected = true; return true; } } return false; }
bool Engine::collisionCircle(sf::FloatRect box1, sf::CircleShape circle) { int d2 = (box1.left-circle.getPosition().x)*(box1.left-circle.getPosition().x) + (box1.top-circle.getPosition().y)*(box1.top-circle.getPosition().y); if (d2>pow(circle.getRadius(),2)) return false; else return true; }
sf::Vector2f World::validMovement(const sf::Vector2f& translation, const sf::CircleShape& obstacle) const { auto beginPlayerPos{center(player)}; auto obstaclePos{center(obstacle)}; auto endPlayerPos{center(player) + translation}; auto r = player.getRadius(); auto R = obstacle.getRadius(); auto rR = r + R; auto dist_s = dist_sqr(endPlayerPos, obstaclePos); auto scaling_factor = 1.f; if (dist_s < rR * rR) { scaling_factor = compute_scaling_factor( translation, obstaclePos - beginPlayerPos, r + R); } return translation * scaling_factor; }
float radius() const noexcept { return shape.getRadius(); }
float bottom () { return y() + shape.getRadius(); }
float top() { return y() - shape.getRadius(); }
float left() { return x() - shape.getRadius(); }
float bottom() const noexcept { return y() + shape.getRadius(); }
float top() const noexcept { return y() - shape.getRadius(); }
float right() const noexcept { return x() + shape.getRadius(); }
float left() const noexcept { return x() - shape.getRadius(); }
sf::Vector2f collide(sf::CircleShape ball, sf::RectangleShape block) { sf::Vector2f ball_center = ball.getPosition() + sf::Vector2f(ball.getRadius(), ball.getRadius()); sf::Vector2f block_center = block.getPosition() + sf::Vector2f(block.getSize().x/2, block.getSize().y/2); sf::Vector2f diff = ball_center - block_center; // Not intersecting if(abs(diff.x) > block.getSize().x/2 + ball.getRadius() || abs(diff.y) > block.getSize().y/2 + ball.getRadius()) { return sf::Vector2f(0,0); } std::cout << diff.x << ", " << diff.y << std::endl; // hit edge of block (top/bottom) if(abs(diff.x) <= PADDLE_WIDTH/2) { if(diff.y <= 0) { return sf::Vector2f(0,-1); } else if(diff.y > 0) { return sf::Vector2f(0,1); } } // hit edge of block (left/right) else if(abs(diff.y) <= PADDLE_HEIGHT/2) { if(diff.x <= 0) { return sf::Vector2f(-1,0); } else if(diff.x > 0) { return sf::Vector2f(1,0); } } float distance = getDistance(ball_center, block.getPosition()); if(distance <= ball.getRadius()) { return (ball_center - block.getPosition()) / distance; } sf::Vector2f block_corner = block.getPosition() + sf::Vector2f(block.getSize().x,0); distance = getDistance(ball_center, block_corner); if(distance <= ball.getRadius()) { return (ball_center - block_corner) / distance; } block_corner = block.getPosition() + sf::Vector2f(0, block.getSize().y); distance = getDistance(ball_center, block_corner); if(distance <= ball.getRadius()) { return (ball_center - block_corner) / distance; } block_corner = block.getPosition() + block.getSize(); distance = getDistance(ball_center, block_corner); if(distance <= ball.getRadius()) { return (ball_center - block_corner) / distance; } // Not intersecting, even in the corners return sf::Vector2f(0,0); }
// Circle Shape void Unit::setRadius(float radius) { _circle.setRadius(radius); _circle.setOrigin(_circle.getRadius(), _circle.getRadius()); }
// How big am I? inline float get_size() const { return circ.getRadius(); }