-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRectangle.cpp
More file actions
94 lines (79 loc) · 1.58 KB
/
Rectangle.cpp
File metadata and controls
94 lines (79 loc) · 1.58 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "Rectangle.h"
Rectangle::Rectangle(sf::Vector2f position, sf::Vector2f size, sf::Color color, int value) : PhysicalObject(position)
{
this->size = size;
this->color = color;
this->value = value;
isAlive = true;
rectangle.setSize(size);
rectangle.setPosition(position);
rectangle.setFillColor(color);
}
Rectangle::~Rectangle()
{
}
void Rectangle::Draw(sf::RenderWindow & Window)
{
if (isAlive)
Window.draw(rectangle);
}
bool Rectangle::checkCollisoin(Ball *ball)
{
if (isAlive)
{
sf::Vector2f pos = ball->getPosition();
sf::Vector2f speed = ball->getSpeed();
int rad = ball->getRad();
sf::IntRect rect(position.x, position.y, size.x, size.y);
if (rect.contains(pos.x + rad, pos.y) || rect.contains(pos.x - rad, pos.y))
{
if ((pos.x + rad) < (position.x + (size.x / 2)))
{
pos.x = position.x - rad;
ball->setPosition(pos);
}
else
{
pos.x = position.x + size.x + rad;
ball->setPosition(pos);
}
speed.x *= -1;
ball->setSpeed(speed);
return true;
}
if (rect.contains(pos.x, pos.y + rad) || rect.contains(pos.x, pos.y - rad))
{
if ((pos.y + rad) < (position.y + (size.y/2)))
{
pos.y = position.y - rad;
ball->setPosition(pos);
}
else
{
pos.y = position.y + size.y + rad;
ball->setPosition(pos);
}
speed.y *= -1;
ball->setSpeed(speed);
return true;
}
}
return false;
}
// Useless method
void Rectangle::collisionAction()
{
isAlive = false;
}
sf::Vector2f Rectangle::getPos()
{
return position;
}
void Rectangle::addValue()
{
value++;
}
int Rectangle::getValue()
{
return value;
}