-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
executable file
·197 lines (176 loc) · 5.47 KB
/
game.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "game.h"
#include "core/sprite.h"
#include "player.h"
#include "ufo.h"
#include <iostream>
#include <random>
Game::Game(int w, int h, char *title, int fps)
{
InitWindow(w, h, title);
// Otherwise escape will exit the window.
SetExitKey(0);
SetTargetFPS(fps);
clearColor = RAYWHITE;
player = new Player();
// Initalize.
score = 0;
health = 10;
}
void Game::createUFO()
{
// Create a bullet.
UFO *ufo = new UFO(nullptr);
Vector2 p;
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> posx(0, GetScreenWidth());
p.x = posx(rng);
p.y = -20;
ufo->setPosition(p);
ufos.push_back(ufo);
}
void Game::renderUFOs(bool paused)
{
for (int i = 0; i < ufos.size(); i++)
{
if (ufos[i]->getPosition().y > GetScreenHeight())
{
// ufos[i]->update(paused);
ufos.erase(ufos.begin() + i);
// std::cout<<"Current ufos: "<<ufos.size()<<std::endl;
}
else
{
ufos[i]->update(paused);
}
}
}
void Game::checkUFOBulletCollision(std::vector<Bullet *> bullets)
{
bool col = false;
for (int i = 0; i < ufos.size(); i++)
{
for (int j = 0; j < bullets.size(); j++)
{
Vector2 ufoCenter;
Vector3 ccUFO = ufos[i]->getCollisionCircle();
ufoCenter.x = ccUFO.x;
ufoCenter.y = ccUFO.y;
float ufoRadius = ccUFO.z;
Vector2 bulletCenter;
Vector3 ccBullet = bullets[j]->getCollisionCircle();
bulletCenter.x = ccBullet.x;
bulletCenter.y = ccBullet.y;
float bulletRadius = ccBullet.z;
col = CheckCollisionCircles(ufoCenter, ufoRadius, bulletCenter, bulletRadius);
// DrawCircle(ufoCenter.x, ufoCenter.y, ufoRadius, BLUE);
if (col)
{
ufos.erase(ufos.begin() + i);
score += 10;
player->deleteBullet(j);
// No need to check the rest of the bullets.
continue;
}
}
}
}
void Game::checkPlayerUFOCollision()
{
for (int i = 0; i < ufos.size(); i++)
{
Vector2 ufoCenter;
Vector3 ccUFO = ufos[i]->getCollisionCircle();
ufoCenter.x = ccUFO.x;
ufoCenter.y = ccUFO.y;
float ufoRadius = ccUFO.z;
Vector2 playerCenter;
Vector3 playerCC = player->getCollisionCircle();
playerCenter.x = playerCC.x;
playerCenter.y = playerCC.y;
float playerRadius = playerCC.z;
bool col = CheckCollisionCircles(ufoCenter, ufoRadius, playerCenter, playerRadius);
if (col)
{
health -= 1;
std::cout << "=====================Player hit==========================";
ufos.erase(ufos.begin() + i);
}
}
}
void Game::restartGame()
{
health = 10;
score = 0;
delete player;
player = new Player();
ufos.clear();
}
void Game::run()
{
bool paused = false;
int frame = 0;
while (!WindowShouldClose())
{
BeginDrawing();
if (health <= 0)
{
Vector2 dimentions = MeasureTextEx(GetFontDefault(), "Game Over", 50, 1);
DrawText("Game Over", 512 - dimentions.x / 2, 512 - dimentions.y / 2, 50, RED);
Vector2 dimentions2 = MeasureTextEx(GetFontDefault(), "Press 'Q' to exit, Press 'R' to restart the game.", 20, 1);
DrawText("Press 'Q' to exit, Press 'R' to restart the game.", 512 - dimentions2.x / 2, 512 + dimentions2.y + dimentions.y / 2, 20, GRAY);
if (IsKeyDown(KEY_Q))
exit(0);
if (IsKeyDown(KEY_R))
restartGame();
EndDrawing();
continue;
}
if (IsKeyPressed(KEY_P) || IsKeyPressed(KEY_ESCAPE))
{
paused = !paused;
}
if (IsKeyDown(KEY_R) && paused)
{
restartGame();
paused = false;
}
if (IsKeyDown(KEY_Q) && paused)
exit(0);
if (frame % 10 == 0 && !paused)
{
createUFO();
}
frame++;
ClearBackground(clearColor);
player->update(paused);
if (paused)
{
Vector2 dimentions = MeasureTextEx(GetFontDefault(), "PAUSED", 50, 1);
DrawText("PAUSED", 512 - dimentions.x / 2, 512 - dimentions.y / 2, 50, GRAY);
Vector2 dimentions2 = MeasureTextEx(GetFontDefault(), "Press 'Q' to exit, Press 'R' to restart the game.", 20, 1);
DrawText("Press 'Q' to exit, Press 'R' to restart the game.", 512 - dimentions2.x / 2, 512 + dimentions2.y + dimentions.y / 2, 20, GRAY);
Vector2 dimentions3 = MeasureTextEx(GetFontDefault(), "Press 'P' to unpause.", 20, 1);
DrawText("Press 'P' to unpause.", 512 - dimentions3.x / 2, 512 + dimentions2.y + dimentions.y + dimentions2.y / 2, 20, GRAY);
}
player->getPosition();
renderUFOs(paused);
checkUFOBulletCollision(player->getBullets());
checkPlayerUFOCollision();
Color healthColor = GREEN;
if (health < 8 && health > 5)
{
healthColor = ORANGE;
}
else if (health <= 5)
{
healthColor = RED;
}
DrawText(TextFormat("Score: %02i", score), 50, 60, 20, GRAY);
DrawText(TextFormat("Health: %02i", health), 50, 90, 20, healthColor);
EndDrawing();
}
}
Game::~Game()
{
}