Skip to content

Commit d58ef44

Browse files
committed
Added multiple lives
1 parent dd19793 commit d58ef44

File tree

8 files changed

+536
-61
lines changed

8 files changed

+536
-61
lines changed

OpenRocket/OpenRocket/Constants.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ const float Constants::bulletSpeed = 0.1;
2323
const double Constants::bulletSpawnCooldown = 0.1;
2424
const float Constants::bulletRadius = 2.5f;
2525
const int Constants::nrOfBulletsPerFrame = 8;
26+
const int Constants::maxLives = 3;
27+
const double Constants::timeToEscapeAsteroid = 1;

OpenRocket/OpenRocket/Constants.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ class Constants
2727
static const double bulletSpawnCooldown;
2828
static const float bulletRadius;
2929
static const int nrOfBulletsPerFrame;
30+
static const int maxLives;
31+
static const double timeToEscapeAsteroid;
3032
};

OpenRocket/OpenRocket/Game.cpp

Lines changed: 71 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,6 @@
99
#include <GL/freeglut.h>
1010
#include <GLFW/glfw3.h>
1111

12-
13-
void displayMatrix(glm::mat4 matrix) {
14-
for (int ii = 0; ii < 4; ii++)
15-
{
16-
for (int jj = 0; jj < 4; jj++)
17-
cout << matrix[ii][jj] << " ";
18-
cout << endl;
19-
};
20-
cout << "\n";
21-
22-
};
23-
2412
void Game::move(void) {
2513
for (auto& asteroid : asteroids) {
2614
float translatedDistance = asteroid->getTranslatedDistance();
@@ -71,29 +59,8 @@ Game::Game(int initial_pos_x, int initial_pos_y) :
7159
maxX = width / 2;
7260
maxY = height / 2;
7361

74-
//// Triunghiul de sus
75-
//775.f, 160.f, 0.f, 1.f,
76-
//825.f, 160.f, 0.f, 1.f,
77-
//800.f, 185.f, 0.f, 1.f,
78-
79-
//// Triunghiul de jos
80-
//775.f, 100.f, 0.f, 1.f,
81-
//825.f, 100.f, 0.f, 1.f,
82-
//800.f, 165.f, 0.f, 1.f,
83-
84-
//// Deptunghiul
85-
//790.f, 100.f, 0.f, 1.f, // stanga jos
86-
//810.f, 100.f, 0.f, 1.f, // dr jos
87-
//810.f, 175.f, 0.f, 1.f,
88-
//790.f, 175.f, 0.f, 1.f,
89-
90-
//// Triunghiul din varf
91-
//790.f, 175.f, 0.f, 1.f,
92-
//810.f, 175.f, 0.f, 1.f,
93-
//800.f, 210.f, 0.f, 1.f,
94-
95-
9662
InitializeGlew();
63+
CreateHeartBuffers();
9764
CreateBackgroundBuffers();
9865
CreateRocketBuffers();
9966
CreateAsteroidBuffers();
@@ -195,11 +162,15 @@ void Game::FireAnimation() {
195162

196163
void Game::RenderFunction(void) {
197164
Rocket* rocket = Rocket::getInstance();
165+
166+
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
167+
glClear(GL_COLOR_BUFFER_BIT);
168+
198169
if (rocket->getIsDead()) {
170+
// TODO: render a png with the text GAME OVER
171+
//glutSwapBuffers();
199172
return;
200173
}
201-
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
202-
glClear(GL_COLOR_BUFFER_BIT);
203174

204175
if (rotationAngle > 360.f) {
205176
rotationAngle = 0.0f;
@@ -271,26 +242,25 @@ void Game::RenderFunction(void) {
271242
glDrawArrays(GL_POLYGON, 0, Constants::nrOfVerticesPerCircle);
272243
}
273244

274-
/*glBindVertexArray(squareVao);
275-
matrix = scaleMatrix * translateMatrix;
276-
myMatrixLocation = glGetUniformLocation(ProgramId, "myMatrix");
277-
glUniformMatrix4fv(myMatrixLocation, 1, GL_FALSE, &matrix[0][0]);
278-
279-
glDrawArrays(GL_POLYGON, 0, 4);*/
280-
281245
rocket->RocketAsteroidsCollision(asteroids);
246+
247+
glm::mat4 heartMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(4.7f, 4.7f, 1.0f));
248+
heartMatrix = backgroundMatrix * heartMatrix;
249+
250+
glm::mat4 heartTranslateMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(10.0f, 0.0f, 0.0f));
251+
for (int i = 0; i < rocket->getRemainingLives(); i++) {
252+
if (i != 0) {
253+
heartMatrix = heartMatrix * heartTranslateMatrix;
254+
}
255+
glUniformMatrix4fv(myMatrixLocation, 1, GL_FALSE, &heartMatrix[0][0]);
256+
glBindVertexArray(heartVao);
257+
glDrawArrays(GL_POLYGON, 0, 14);
258+
}
282259

283260
glutPostRedisplay();
284261
glFlush();
285262
}
286263

287-
bool colliding(Rocket rocket, Asteroid asteroid)
288-
{
289-
return false;
290-
}
291-
292-
293-
294264
void Game::CreateBackgroundBuffers() {
295265

296266
const int nrOfVertices = 1000 * 5;
@@ -464,24 +434,20 @@ void Game::CreateAsteroidBuffers() {
464434
GLfloat Colors[1000];
465435
for (int k = 0; k < Constants::nrOfVerticesPerCircle; k++) {
466436
float theta = Constants::TWO_PI * k / Constants::nrOfVerticesPerCircle;
467-
cout << "angle " << float(Constants::TWO_PI * float(k)) / float(Constants::nrOfVerticesPerCircle) << "\n";
468437
float x = cos(theta);
469438
float y = sin(theta);
470439
// varfurile corespunzatoare cercului
471440
Vertices[4 * k] = x;
472441
Vertices[4 * k + 1] = y;
473442
Vertices[4 * k + 2] = 0.0f;
474443
Vertices[4 * k + 3] = 1.0f;
475-
cout << 4*k << " " << Vertices[4 * k] << " " << Vertices[4 * k + 1] << " " << Vertices[4 * k + 2] << " " << Vertices[4 * k + 3] << "\n";
476444

477445
Colors[4 * k] = 1.0f;
478446
Colors[4 * k + 1] = 0.0f;
479447
Colors[4 * k + 2] = 0.0f;
480448
Colors[4 * k + 3] = 1.0f;
481449
}
482-
for (int i = 0; i < Constants::nrOfVerticesPerCircle * 4 - 4; i+=4) {
483-
//cout << Vertices[i] << " " << Vertices[i + 1] << " " << Vertices[i + 2] << " " << Vertices[i + 3] << "\n";
484-
}
450+
485451
int verticesCount = sizeof(Vertices) / sizeof(GLfloat);
486452

487453
glGenBuffers(1, &asteroidVbo);
@@ -614,3 +580,53 @@ void Game::UpdateBullets() {
614580

615581
bullets.erase(end, bullets.end());
616582
}
583+
584+
void Game::CreateHeartBuffers() {
585+
586+
GLfloat Vertices[] = {
587+
7.0f, 3.7f, 0.0f, 1.0f,
588+
589+
8.0f, 4.5f, 0.0f, 1.0f,
590+
9.6f, 6.0f, 0.0f, 1.0f,
591+
10.4f, 7.5f, 0.0f, 1.0f,
592+
10.0f, 9.0f, 0.0f, 1.0f,
593+
8.7f, 9.8f, 0.0f, 1.0f,
594+
7.8f, 9.7f, 0.0f, 1.0f,
595+
596+
7.0f, 8.9f, 0.0f, 1.0f,
597+
598+
6.2f, 9.7f, 0.0f, 1.0f,
599+
5.3f, 9.8f, 0.0f, 1.0f,
600+
4.0f, 9.0f, 0.0f, 1.0f,
601+
3.6f, 7.5f, 0.0f, 1.0f,
602+
4.4f, 6.0f, 0.0f, 1.0f,
603+
6.0f, 4.5f, 0.0f, 1.0f,
604+
};
605+
606+
const int verticesCount = sizeof(Vertices) / sizeof(GLfloat);
607+
608+
GLfloat Colors[verticesCount];
609+
610+
for (int i = 0; i < verticesCount; i += 4) {
611+
Colors[i] = 1.0f;
612+
Colors[i + 1] = 0.0f;
613+
Colors[i + 2] = 0.0f;
614+
Colors[i + 3] = 1.0f;
615+
}
616+
617+
glGenBuffers(1, &heartVbo);
618+
glBindBuffer(GL_ARRAY_BUFFER, heartVbo);
619+
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
620+
621+
glGenVertexArrays(1, &heartVao);
622+
glBindVertexArray(heartVao);
623+
624+
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
625+
glEnableVertexAttribArray(0);
626+
627+
glGenBuffers(1, &heartColorBufferId);
628+
glBindBuffer(GL_ARRAY_BUFFER, heartColorBufferId);
629+
glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW);
630+
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 0);
631+
glEnableVertexAttribArray(1);
632+
}

OpenRocket/OpenRocket/Game.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ class Game {
6262
GLuint squareVbo;
6363
GLuint squareColorBufferId;
6464

65+
GLuint heartVao;
66+
GLuint heartVbo;
67+
GLuint heartColorBufferId;
68+
6569
int width;
6670
int height;
6771

@@ -108,6 +112,7 @@ class Game {
108112
void CreateRocketBuffers();
109113
void CreateAsteroidBuffers();
110114
void CreateBulletBuffers();
115+
void CreateHeartBuffers();
111116

112117
void moveRocket(int key, int x, int y);
113118

OpenRocket/OpenRocket/Rocket.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
#include <GL/glew.h>
66
#include <GL/freeglut.h>
7+
#include <GLFW/glfw3.h>
78

89
Rocket* Rocket::instance = nullptr;
910

10-
Rocket::Rocket()
11+
Rocket::Rocket() :
12+
remainingLives(Constants::maxLives)
1113
{
1214
frontTriangle.top = { 800.f, 210.f, 0.f, 1.f };
1315
frontTriangle.left = { 790.f, 175.f, 0.0f, 1.f };
@@ -222,8 +224,17 @@ void Rocket::RocketAsteroidsCollision(vector<Asteroid*> asteroids)
222224
if (condition1 || condition2 || condition3 || condition4 || condition5 || condition6 ||
223225
condition7 || condition8 || condition9 || condition10 || condition11 || condition12 )
224226
{
225-
cout << "coliziunee\n";
226-
isDead = true;
227+
double now = glfwGetTime();
228+
229+
if (now - lastCollisionTime > Constants::timeToEscapeAsteroid) {
230+
lastCollisionTime = glfwGetTime();
231+
remainingLives--;
232+
cout << "Lives left: " << remainingLives << "\n";
233+
}
234+
235+
if (remainingLives == 0) {
236+
isDead = true;
237+
}
227238
}
228239

229240
}

OpenRocket/OpenRocket/Rocket.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ class Rocket {
4848

4949
float moveAmount = 20;
5050

51+
int remainingLives;
5152
bool isDead = false;
53+
double lastCollisionTime;
5254

5355
public:
5456
Triangle frontTriangle;
@@ -78,6 +80,10 @@ class Rocket {
7880
return bulletStartY;
7981
}
8082

83+
int getRemainingLives() {
84+
return remainingLives;
85+
}
86+
8187
void setBulletStartX(float x) {
8288
bulletStartX = x;
8389
}

0 commit comments

Comments
 (0)