-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubble.cpp
130 lines (107 loc) · 2.5 KB
/
Bubble.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "main.h"
using namespace std;
Bubble::Bubble()
{
trapping = false;
alive = true;
}
void Bubble::setMaterial(Material m)
{
mtl = m;
}
void Bubble::setSize(float s) {
size = s;
}
void Bubble::setDirection(DIRECT k) {
direction = k;
}
void Bubble::initialize()
{
Material mtl;
mtl.setEmission(0.0f, 0.0f, 0.0f, 1.0f);
mtl.setAmbient(0.0f, 0.1f, 0.06f, 1.0f);
mtl.setDiffuse(0.0f, 0.5098f, 0.5098f, 1.0f);
mtl.setSpecular(0.502f, 0.502f, 0.502f, 1.0f);
mtl.setShininess(32.0f);
setMaterial(mtl);
glMaterialfv(GL_FRONT, GL_EMISSION, mtl.getEmission());
glMaterialfv(GL_FRONT, GL_AMBIENT, mtl.getAmbient());
glMaterialfv(GL_FRONT, GL_DIFFUSE, mtl.getDiffuse());
glMaterialfv(GL_FRONT, GL_SPECULAR, mtl.getSpecular());
glMaterialfv(GL_FRONT, GL_SHININESS, mtl.getShininess());
setSize(1.0);
}
void Bubble::grow()
{
setSize(min(size + 0.1f, 1.0f));
}
bool Bubble::checkVerticalBoundary()
{
return (getPositionX() - getRadius() < -0.95f || getPositionX() + getRadius() > 0.95f);
}
bool Bubble::checkUpperBoundary()
{
return (getPositionY() > 1.0f);
}
bool Bubble::isGrown()
{
return size == 1.0f;
}
bool Bubble::isTrapping()
{
return trapping;
}
bool Bubble::isAlive()
{
return alive;
}
void Bubble::kill()
{
alive = false;
}
bool Bubble::mapCollision(vector<vector<float>> borderHard)
{
float bb_x = getPositionX(); float bb_y = getPositionY();
for (const auto& border : borderHard) {
float xLeft = border[0]; float yTop = border[1];
float xRight = xLeft + border[2]; float yBottom = yTop - border[3];
if (getHitbox().collisionDetection(Hitbox(border))) {
size = 1.0f;
switch (direction) {
case (D_LEFT):
if (bb_y - max_radius < yBottom && yBottom < bb_y + max_radius) setPositionY(yBottom - max_radius);
else setPositionX(xRight + max_radius);
return true;
case (D_RIGHT):
if (bb_y - max_radius < yBottom && yBottom < bb_y + max_radius) setPositionY(yBottom - max_radius);
else setPositionX(xLeft - max_radius);
return true;
case (D_UP):
setPositionY(yBottom - max_radius);
return true;
}
}
}
return false;
}
float Bubble::getRadius()
{
return size * max_radius;
}
Hitbox Bubble::getHitbox()
{
float r = getRadius();
return Hitbox(getPositionX() - r, getPositionX() + r, getPositionY() - r, getPositionY() + r);
}
clock_t Bubble::getCreatedTime()
{
return createdTime;
}
void Bubble::draw()
{
glPushMatrix();
glTranslatef(getPositionX(), getPositionY(), 0.0f);
glScalef(size, size, size);
glutSolidSphere(max_radius, 20, 20);
glPopMatrix();
}