C++ Project 2016-2017: Arkanoid
player_sfml.cpp
Go to the documentation of this file.
1 
3 #include "player_sfml.h"
4 
5 #include <SFML/Graphics.hpp>
6 #include <iostream>
7 
8 using namespace std;
9 
10 namespace arkanoidSFML {
11 
12  PlayerSFML::PlayerSFML(double x, double y, sf::RenderWindow &window, double speed, const string &textureFile) :
13  screenOrigin(x, y), windowSFML(window), transformation(Transformation::getInstance()), Player(0, 0, speed) {
14 
15  // Set texture
16  if(!texture.loadFromFile(textureFile)) {
17  throw runtime_error("Couldn't load player 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 
35  // Check if any special effects ran out
36  if(speedUpDuration > 0) {
38  if(speedUpDuration == 0) {
39  speed = originalSpeed;
40  }
41  }
42 
43  if(!notMoving) {
44 
45  // Note: prevents user from pressing both left and right arrow key
46  if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left) && sprite.getPosition().x < windowSFML.getSize().x - 33 - sprite.getLocalBounds().width) {
47  velocity.x = speed;
48  } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right) && sprite.getPosition().x > 33) {
49  velocity.x = -speed;
50  } else {
51  // Player is not moving
52  velocity.x = 0;
53  }
54 
55  sprite.move(velocity.x, velocity.y);
56  setPosition(std::move(transformation->convertVector(sprite.getPosition())));
57 
58  } else {
59  if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) {
60  notMoving = false;
61  }
62  }
63  }
64 
65  void PlayerSFML::draw() const {
66  windowSFML.draw(sprite);
67  }
68 
70  sprite.setPosition(screenOrigin.x, screenOrigin.y);
71  Player::reset();
72  }
73 }
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...
int speedUpDuration
In frames.
Definition: player.h:25
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