-
Notifications
You must be signed in to change notification settings - Fork 0
/
paddle.cpp
72 lines (55 loc) · 2.33 KB
/
paddle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "paddle.h"
Paddle::Paddle(float start_x, float start_y, float width, float height,
float speed) :
MovableObject(sf::Vector2f(start_x, start_y), new sf::RectangleShape(),
speed, sf::Vector2f(0.f, 0.f), sf::Color::Magenta)
{
setSize(sf::Vector2f(width, height));
dynamic_cast<sf::RectangleShape*>(shape)->setOrigin(width / 2.f, height / 2.f);
}
Paddle::~Paddle() {
if(shape != nullptr)
delete dynamic_cast<sf::RectangleShape*>(shape);
shape = nullptr;
}
float Paddle::getWidth() const {
return dynamic_cast<sf::RectangleShape*>(shape)->getSize().x;
}
float Paddle::getHeight() const {
return dynamic_cast<sf::RectangleShape*>(shape)->getSize().y;
}
void Paddle::setSize(const sf::Vector2f &size) {
dynamic_cast<sf::RectangleShape*>(shape)->setSize(size);
}
float Paddle::getRight() const {
return getPosition().x + getWidth() / 2.f;
}
float Paddle::getLeft() const {
return getPosition().x - getWidth() / 2.f;
}
float Paddle::getTop() const {
return getPosition().y - getHeight() / 2.f;
}
float Paddle::getBottom() const {
return getPosition().y + getHeight() / 2.f;
}
void Paddle::moveLeft() {
setVelocityX(-move_rate);
}
void Paddle::moveRight() {
setVelocityX(move_rate);
}
void Paddle::stayAtPlace() {
setVelocityX(0.f);
}
void Paddle::handleBallPosChange(Ball &ball) {
if(!isIntersects(ball)) return;
if(ball.getPosition().x < getPosition().x )
ball.setVelocityX(-ball.getMoveRate());
else
ball.setVelocityX(ball.getMoveRate());
ball.setVelocityY(-ball.getMoveRate());
}
void Paddle::draw(sf::RenderTarget& target, sf::RenderStates states) const {
target.draw(*dynamic_cast<sf::RectangleShape*>(shape), states);
}