C++ Project 2016-2017: Arkanoid
ball_sfml.cpp
Go to the documentation of this file.
1 
3 #include "ball_sfml.h"
4 
5 #include <SFML/Graphics.hpp>
6 #include <iostream>
7 
8 using namespace std;
9 
10 namespace arkanoidSFML {
11 
12  BallSFML::BallSFML(double x, double y, sf::RenderWindow &window, double speed, const string &textureFile) :
13  screenOrigin(x, y), windowSFML(window), transformation(Transformation::getInstance()), Ball(0, 0, speed) {
14 
15  // Set texture
16  if(!texture.loadFromFile(textureFile)) {
17  throw runtime_error("Couldn't load ball texture image: " + textureFile);
18  }
19  texture.setSmooth(true);
20  sprite.setTexture(texture);
21 
22  // Set position
23  sprite.setPosition(x, y);
24  setPosition(std::move(transformation->convertVector(sprite.getPosition())));
25 
26  // Set size (width and height) of PlayerSFML
27  sf::FloatRect rect = sprite.getLocalBounds();
28  setSize(make_pair(transformation->convertX(rect.width), transformation->convertY(rect.height)));
29  }
30 
32 
34  if(invisDuration > 0) {
35  invisDuration--;
36  }
37 
38  if(!notMoving) {
39  // Move the ball
40  sprite.move(velocity.x, velocity.y);
41  setPosition(std::move(transformation->convertVector(sprite.getPosition())));
42  } else {
43  if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) {
44  notMoving = false;
45  }
46  }
47  }
48 
49  void BallSFML::draw() const {
50  if(invisDuration == 0) {
51  windowSFML.draw(sprite);
52  }
53  }
54 
55  void BallSFML::reset() {
56  sprite.setPosition(screenOrigin.x, screenOrigin.y);
57  Ball::reset();
58  }
59 }
void setSize(pair< double, double > newSize)
Definition: entity.cpp:30
Singleton: This class provides a method to convert screen pixels to their corresponding coordinates i...
All the game gui elements of the Arkanoid game.
Definition: ball_sfml.cpp:10
arkanoid::Vector2D convertVector(const sf::Vector2f &vector)
double y
The &#39;y&#39; component of the Vector2D.
Definition: vector2D.h:18
double x
The &#39;x&#39; component of the Vector2D.
Definition: vector2D.h:17
void setPosition(double x, double y)
Definition: entity.cpp:21