Skip to content

Commit 1295295

Browse files
authored
Merge pull request #36 from DianaVasiliu/diana
Added multiple lives
2 parents a37821a + 765db31 commit 1295295

File tree

8 files changed

+540
-29
lines changed

8 files changed

+540
-29
lines changed

OpenRocket/OpenRocket/Constants.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ const float Constants::bulletSpeed = 0.1;
3232
const double Constants::bulletSpawnCooldown = 0.1;
3333
const float Constants::bulletRadius = 2.5f;
3434
const int Constants::nrOfBulletsPerFrame = 8;
35+
const int Constants::maxLives = 3;
36+
const double Constants::timeToEscapeAsteroid = 1;

OpenRocket/OpenRocket/Constants.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ class Constants
3030
static const double bulletSpawnCooldown;
3131
static const float bulletRadius;
3232
static const int nrOfBulletsPerFrame;
33+
static const int maxLives;
34+
static const double timeToEscapeAsteroid;
3335
};

OpenRocket/OpenRocket/Game.cpp

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Game::Game(int initial_pos_x, int initial_pos_y) :
6464
maxY = height / 2;
6565

6666
InitializeGlew();
67+
CreateHeartBuffers();
6768
CreateBackgroundBuffers();
6869
CreateRocketBuffers();
6970
CreateAsteroidBuffers();
@@ -165,11 +166,15 @@ void Game::FireAnimation() {
165166

166167
void Game::RenderFunction(void) {
167168
Rocket* rocket = Rocket::getInstance();
169+
170+
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
171+
glClear(GL_COLOR_BUFFER_BIT);
172+
168173
if (rocket->getIsDead()) {
174+
// TODO: render a png with the text GAME OVER
175+
//glutSwapBuffers();
169176
return;
170177
}
171-
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
172-
glClear(GL_COLOR_BUFFER_BIT);
173178

174179
if (rotationAngle > 360.f) {
175180
rotationAngle = 0.0f;
@@ -243,16 +248,26 @@ void Game::RenderFunction(void) {
243248
glBindVertexArray(bulletVao);
244249
glDrawArrays(GL_POLYGON, 0, Constants::nrOfVerticesPerCircle);
245250
}
251+
246252
rocket->RocketAsteroidsCollision(asteroids);
253+
254+
glm::mat4 heartMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(4.7f, 4.7f, 1.0f));
255+
heartMatrix = backgroundMatrix * heartMatrix;
256+
257+
glm::mat4 heartTranslateMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(10.0f, 0.0f, 0.0f));
258+
for (int i = 0; i < rocket->getRemainingLives(); i++) {
259+
if (i != 0) {
260+
heartMatrix = heartMatrix * heartTranslateMatrix;
261+
}
262+
glUniformMatrix4fv(myMatrixLocation, 1, GL_FALSE, &heartMatrix[0][0]);
263+
glBindVertexArray(heartVao);
264+
glDrawArrays(GL_POLYGON, 0, 14);
265+
}
247266

248267
glutPostRedisplay();
249268
glFlush();
250269
}
251270

252-
bool colliding(Rocket rocket, Asteroid asteroid)
253-
{
254-
return false;
255-
}
256271
void Game::CreateBackgroundBuffers() {
257272

258273
const int nrOfVertices = 1000 * 5;
@@ -391,21 +406,18 @@ void Game::CreateAsteroidBuffers() {
391406
GLfloat Vertices[1000];
392407
for (int k = 0; k < Constants::nrOfVerticesPerCircle; k++) {
393408
float theta = Constants::TWO_PI * k / Constants::nrOfVerticesPerCircle;
394-
//cout << "angle " << float(Constants::TWO_PI * float(k)) / float(Constants::nrOfVerticesPerCircle) << "\n";
395409
float x = cos(theta);
396410
float y = sin(theta);
397411
// varfurile corespunzatoare cercului
398412
Vertices[9 * k] = x;
399413
Vertices[9 * k + 1] = y;
400414
Vertices[9 * k + 2] = 0.0f;
401415
Vertices[9 * k + 3] = 1.0f;
402-
//cout << 4*k << " " << Vertices[4 * k] << " " << Vertices[4 * k + 1] << " " << Vertices[4 * k + 2] << " " << Vertices[4 * k + 3] << "\n";
403416

404417
Vertices[9 * k + 4] = 1.0f;
405418
Vertices[9 * k + 5] = 0.0f;
406419
Vertices[9 * k + 6] = 0.0f;
407-
408-
//cout << (float)k / 18 << " ";
420+
409421
Vertices[9 * k + 7] = (sin(theta) + 1) / 2;
410422
Vertices[9 * k + 8] = (cos(theta) + 1)/ 2;
411423
}
@@ -435,6 +447,7 @@ void Game::CreateAsteroidBuffers() {
435447
glEnableVertexAttribArray(2);
436448
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 9 * sizeof(GLfloat), (GLvoid*)(7 * sizeof(GLfloat)));
437449
}
450+
438451
void Game::UpdateAsteroids() {
439452
auto end = std::remove_if(asteroids.begin(),
440453
asteroids.end(),
@@ -481,6 +494,7 @@ void Game::GenerateAsteroids(int nrOfAsteroids) {
481494
this->asteroids.push_back(Game::GenerateSingleAsteroid());
482495
}
483496
}
497+
484498
void Game::LoadTexture(GLuint &texture, const char* imageName)
485499
{
486500
glGenTextures(1, &texture);
@@ -499,6 +513,7 @@ void Game::LoadTexture(GLuint &texture, const char* imageName)
499513
SOIL_free_image_data(image);
500514
glBindTexture(GL_TEXTURE_2D, 0);
501515
}
516+
502517
void Game::loadTextures() {
503518
for (auto& imageName : Constants::textureImages) {
504519
GLuint texture;
@@ -572,3 +587,53 @@ void Game::UpdateBullets() {
572587

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

OpenRocket/OpenRocket/Game.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ class Game {
6565
GLuint squareVbo;
6666
GLuint squareColorBufferId;
6767

68+
GLuint heartVao;
69+
GLuint heartVbo;
70+
GLuint heartColorBufferId;
71+
6872
int width;
6973
int height;
7074
int codCol;
@@ -113,6 +117,7 @@ class Game {
113117
void CreateRocketBuffers();
114118
void CreateAsteroidBuffers();
115119
void CreateBulletBuffers();
120+
void CreateHeartBuffers();
116121

117122
void moveRocket(int key, int x, int y);
118123

OpenRocket/OpenRocket/Rocket.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,12 @@
44
#include "Rocket.h"
55
#include <GL/glew.h>
66
#include <GL/freeglut.h>
7+
#include <GLFW/glfw3.h>
78

89
Rocket* Rocket::instance = nullptr;
910

10-
void displayMatrix(glm::mat4 matrix)
11-
{
12-
//cout << "intra aici";
13-
for (int ii = 0; ii < 4; ii++)
14-
{
15-
for (int jj = 0; jj < 4; jj++)
16-
cout << matrix[ii][jj] << " ";
17-
cout << endl;
18-
};
19-
cout << "\n";
20-
21-
};
22-
23-
Rocket::Rocket()
11+
Rocket::Rocket() :
12+
remainingLives(Constants::maxLives)
2413
{
2514
frontTriangle.top = { 800.f, 210.f, 0.f, 1.f };
2615
frontTriangle.left = { 790.f, 175.f, 0.0f, 1.f };
@@ -235,8 +224,17 @@ void Rocket::RocketAsteroidsCollision(vector<Asteroid*> asteroids)
235224
if (condition1 || condition2 || condition3 || condition4 || condition5 || condition6 ||
236225
condition7 || condition8 || condition9 || condition10 || condition11 || condition12 )
237226
{
238-
cout << "coliziunee\n";
239-
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+
}
240238
}
241239

242240
}

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)