63 lines
No EOL
1.8 KiB
C++
63 lines
No EOL
1.8 KiB
C++
#include <memory>
|
|
|
|
#include <SFML/Graphics.hpp>
|
|
#include <SFML/Graphics/CircleShape.hpp>
|
|
|
|
#include "Bubble.h"
|
|
|
|
Bubble::Bubble(
|
|
const unsigned int INITIAL_X,
|
|
const unsigned int INITIAL_Y,
|
|
const double INITIAL_X_DIR,
|
|
const double INITIAL_Y_DIR,
|
|
const float RADIUS,
|
|
const sf::Color COLOR,
|
|
std::shared_ptr<sf::Music> BOING_SOUND
|
|
): _pBoingSound(std::move(BOING_SOUND)) {
|
|
_circleShape = sf::CircleShape(RADIUS);
|
|
_circleShape.setPosition(INITIAL_X, INITIAL_Y);
|
|
_circleShape.setFillColor(COLOR);
|
|
_xDir = INITIAL_X_DIR;
|
|
_yDir = INITIAL_Y_DIR;
|
|
}
|
|
|
|
void Bubble::draw(sf::RenderWindow& window) const {
|
|
window.draw(_circleShape);
|
|
}
|
|
|
|
void Bubble::updatePosition(const unsigned int WINDOW_WIDTH, const unsigned int WINDOW_HEIGHT) {
|
|
sf::Vector2f currentPosition = _circleShape.getPosition();
|
|
|
|
// While inverting sign of the direction is simpler, that method gets bubbles stuck on the wall
|
|
// Giving explicit sign forces the ball in the correct direction instead.
|
|
|
|
if (
|
|
(currentPosition.x < 0 && _xDir < 0)
|
|
|| (
|
|
(currentPosition.x + _circleShape.getRadius() * 2) > WINDOW_WIDTH
|
|
&& _xDir > 0
|
|
)
|
|
) {
|
|
// If over left wall & going left, invert
|
|
// OR if over right wall & going right, invert
|
|
_xDir = -_xDir;
|
|
_pBoingSound->play();
|
|
}
|
|
|
|
if (
|
|
(currentPosition.y < 0 && _yDir < 0)
|
|
|| (
|
|
(currentPosition.y + _circleShape.getRadius() * 2) > WINDOW_HEIGHT
|
|
&& _yDir > 0
|
|
)
|
|
) {
|
|
// If over top wall & going up, invert
|
|
// OR if over bottom wall & going down, invert
|
|
_yDir = -_yDir;
|
|
_pBoingSound->play();
|
|
}
|
|
|
|
currentPosition.x += _xDir;
|
|
currentPosition.y += _yDir;
|
|
_circleShape.setPosition(currentPosition);
|
|
} |