-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.cpp
More file actions
executable file
·94 lines (79 loc) · 2.74 KB
/
table.cpp
File metadata and controls
executable file
·94 lines (79 loc) · 2.74 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
#include "table.h"
void Table::render(QPainter &painter) {
// our table colour
painter.setBrush(m_brush);
// draw table
painter.drawRect(
getX(),
getY(),
getWidth(),
getHeight()
);
}
void Table::resolveCollision(Ball *ball, Game* game) {
QVector2D bPos = ball->getPosition();
// resulting multiplicity of direction. If a component is set to -1, it
// will flip the velocity's corresponding component
QVector2D vChange(1,1);
// ball is beyond left side of table's bounds
if (bPos.x() - ball->getRadius() <= getX()) {
// flip velocity if wrong dir
if (ball->getVelocity().x() <= 0) vChange.setX(-1);
// ball is beyond right side of table's bounds
} else if (bPos.x() + ball->getRadius() >= getX() + getWidth()) {
// flip velocity if wrong dir
if (ball->getVelocity().x() >= 0) vChange.setX(-1);
}
// ball is above top of the table's bounds
if (bPos.y() - ball->getRadius() <= getY()) {
// flip iff we're travelling in the wrong dir
if (ball->getVelocity().y() <= 0) vChange.setY(-1);
// ball is beyond bottom of table's bounds
} else if (bPos.y() + ball->getRadius() >= getY() + getHeight()) {
// if we're moving down (we want to let the ball bounce up if its heading back)
if (ball->getVelocity().y() >= 0) vChange.setY(-1);
}
ball->multiplyVelocity(vChange);
}
void StageTwoTable::resolveCollision(Ball *ball, Game* game) {
Table::resolveCollision(ball, game);
//check if it goes into a pocket on the table
for (Pocket* pocket : *m_pockets) {
//if fell into table
if (fellInside(ball, pocket)) {
game->makeDead(ball);
}
}
}
bool StageTwoTable::fellInside(Ball* ball, Pocket* pocket) {
return pocket->getRadius() >= ball->getRadius()
&& ball->getPosition().distanceToPoint(pocket->getPosition())
<= pocket->getRadius();
}
void StageTwoTable::render(QPainter &painter) {
// this renders the table in the old fashioned style tick tick
Table::render(painter);
// this gives us our pockety pockets sitting on the table
for (Pocket* p : *m_pockets) p->render(painter);
}
StageTwoTable::~StageTwoTable() {
for (auto pocket : *m_pockets) delete pocket;
delete m_pockets;
}
Pocket::~Pocket() { delete m_ball; }
Pocket::Pocket(int radius, QVector2D position)
: m_ball(
new StageOneBall(QColor("black"),
QVector2D(position),
QVector2D(0.0, 0.0),
0,
radius)) {}
void Pocket::render(QPainter &painter) {
m_ball->render(painter);
}
double Pocket::getRadius() const {
return m_ball->getRadius();
}
QVector2D Pocket::getPosition() const {
return m_ball->getPosition();
}