-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
265 lines (219 loc) · 9.75 KB
/
main.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
Space Warrior is a 2D game written by Abhilekh Gautam. It uses C++
and olc pixel game engine for drawing and olc sound wave engine for music stuff
For more about olc Pixel Game engine visit: https://github.com/OneLoneCoder/olcPixelGameEngine
For more about olc sound wave engine visit: https://github.com/OneLoneCoder/olcSoundWaveEngine
*/
#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"
#define OLC_SOUNDWAVE
#include "olcSoundWaveEngine.h"
/*
I Really hate this approach of coding, everything in a single file will implement soon it in
entity component model....
Did this because I wanted to get things done quickly.
Collision Detection can be greatly improved. I simply used crude collision detection
technique that worked pretty well..
Yuck.. this approach
*/
struct Bullet{
float x;
float y;
bool dead;
};
struct Enemy{
float x;
float y;
bool alive;
};
class Game : public olc::PixelGameEngine {
public:
Game() {
sAppName = "Space Warrior";
}
// set enemy to their default position..
void produceEnemy() {
for (int i = 0; i < 70; ++i) {
if (i < 18)
vEnemy.emplace_back(Enemy{float(ScreenWidth()) / 2 + (float) i * 10 - 100, 40.0f, true});
else if (i < 36)
vEnemy.emplace_back(Enemy{float(ScreenWidth()) / 2 + 10.0f * (float) i - 280, 55.0f, true});
else if (i < 54)
vEnemy.emplace_back(Enemy{float(ScreenWidth()) / 2 + 10.0f * (float) i - 460, 75.0f, true});
else
vEnemy.emplace_back(Enemy{float(ScreenWidth()) / 2 + 10.0f * (float) i - 640, 95.0f, true});
}
}
void reset(){
vEnemy.clear(); // clear all enemy
vBullet.clear(); // clear all bullet stuff
produceEnemy();
life_count = 3;
score = 0;
fPlayerPositionX = 185.0f;
fPlayerPositionY = 250.0f;
}
bool OnUserCreate() override {
bgmEngine.InitialiseAudio();
// background music
bgm = std::make_unique<olc::sound::Wave>("../music/bgm.wav");
gunShot = std::make_unique<olc::sound::Wave>("../music/gunshot.wav");
blast = std::make_unique<olc::sound::Wave>("../music/enemyDeath.wav");
produceEnemy();
// sprites...
sprEnemy = std::make_unique<olc::Sprite>("../sprites/enemy9.png");
sprPlayer = std::make_unique<olc::Sprite>("../sprites/player.png");
sprExplosion = std::make_unique<olc::Sprite>("../sprites/blast.png");
sprLife = std::make_unique<olc::Sprite>("../sprites/life-full.png");
// play music
bgmEngine.PlayWaveform(bgm.get(), true);
return true;
}
bool OnUserUpdate(float fElapsedTime) override {
// initial user prompt, wait for enter key to be pressed...
if (!GetKey(olc::Key::ENTER).bPressed && count == 0) {
DrawString(ScreenWidth() / 2 - 100, ScreenHeight() / 2 - 40, "Space Warrior", olc::WHITE, 3);
DrawString(ScreenWidth() / 2 - 100, ScreenHeight() / 2 + 50, "By: Abhilekh Gautam", olc::WHITE, 2);
DrawString(ScreenWidth() / 2 - 100, ScreenHeight() / 2 + 80, "Press Enter to Continue", olc::WHITE, 1);
} else if (life_count > 0 && life_count <= 3 && score < 350) {
count = 1;
Clear(olc::BLACK);
// draw player
DrawSprite(olc::vi2d((int) fPlayerPositionX, (int) fPlayerPositionY), sprPlayer.get());
// wait for certain keys to be pressed
if (GetKey(olc::Key::LEFT).bHeld) {
if (fPlayerPositionX > 0.0f)
fPlayerPositionX = fPlayerPositionX - fPlayerVel * fElapsedTime;
}
if (GetKey(olc::Key::RIGHT).bHeld) {
if (fPlayerPositionX + (float)sprPlayer->width < (float) ScreenWidth() - 10)
fPlayerPositionX = fPlayerPositionX + fPlayerVel * fElapsedTime;
}
if (GetKey(olc::Key::UP).bHeld) {
fPlayerPositionY = fPlayerPositionY - fPlayerVel * fElapsedTime;
}
if (GetKey(olc::Key::DOWN).bHeld) {
if (fPlayerPositionY + (float)sprPlayer->height < (float) ScreenHeight())
fPlayerPositionY = fPlayerPositionY + fPlayerVel * fElapsedTime;
}
// shoot
if (GetKey(olc::Key::SPACE).bPressed) {
bgmEngine.PlayWaveform(gunShot.get(), false);
float ftempX = fPlayerPositionX;
float ftempY = fPlayerPositionY;
vBullet.emplace_back(Bullet{ftempX + float(sprPlayer->width) / 2, ftempY, false});
}
// detect collision for player's bullet against enemy.
for (auto &elm: vBullet) {
for (auto &enemy: vEnemy) {
if (!elm.dead && enemy.alive && elm.y > enemy.y && elm.x + 1 >= enemy.x &&
elm.x - 1 <= enemy.x + float(sprEnemy->width) &&
elm.y - 1 <= enemy.y + float(sprEnemy->height)) {
// kill both bullet and enemy.
DrawSprite(olc::vi2d(int(enemy.x), int(enemy.y)), sprExplosion.get());
bgmEngine.PlayWaveform(blast.get(), false);
elm.dead = true;
enemy.alive = false;
score = score + 5;
}
}
// only take care of bullets which are visible on the screen
if (elm.y > -1 && !elm.dead) {
FillCircle(int(elm.x), int(elm.y), 1, olc::RED);
elm.y = elm.y - fBulletVel * fElapsedTime;
}
}
// move random enemy towards the player only if the enemy is alive
if (vEnemy[index].alive) {
// distance calculation between enemy and player..
float tempX = (vEnemy[index].x + float(sprEnemy->height) + float(sprEnemy->width) / 2 -
(fPlayerPositionX + float(sprPlayer->width) / 2
));
float tempY = (vEnemy[index].y + float(sprEnemy->height) - fPlayerPositionY);
// simple pythagoras theorem
float tempHypo = powf(tempX, 2) + powf(tempY, 2);
float Hypo = sqrtf(tempHypo);
// angle stuffs
float sinTheta = (tempY / Hypo);
float cosTheta = (tempX / Hypo);
// simply moves enemy towards player..
vEnemy[index].x = vEnemy[index].x - fEnemyVel * cosTheta * fElapsedTime;
vEnemy[index].y = vEnemy[index].y - fEnemyVel * sinTheta * fElapsedTime;
// enemy vs player collision detection
// increase score
// reduce life
if (vEnemy[index].x + float(sprEnemy->width) >= fPlayerPositionX &&
vEnemy[index].x <= fPlayerPositionX + float(sprPlayer->width) &&
vEnemy[index].y + float(sprEnemy->height) >= fPlayerPositionY) {
bgmEngine.PlayWaveform(blast.get(), false);
vEnemy[index].alive = false;
DrawSprite(olc::vi2d(int(vEnemy[index].x), int(vEnemy[index].y)), sprExplosion.get());
score = score + 5;
life_count = life_count - 1;
}
} else {
// sorry rand() is too short and attractive.. ;)
index = rand() % 70;
}
// Draw enemy here..
for (auto elm: vEnemy) {
if (elm.alive)
DrawSprite(olc::vi2d(int(elm.x), int(elm.y)), sprEnemy.get());
}
// Score board ..
DrawString(0, 5, "SCORE:" + std::to_string(score));
// load life sprite..
for (int i = 0; i < life_count; ++i) {
DrawSprite(olc::vi2d(270 + sprLife->width + 25 * i, 5), sprLife.get());
}
} else if (life_count <= 0 && score < 350) { // player lost
Clear(olc::BLACK);
DrawString(ScreenWidth() / 2 - 100, ScreenHeight() / 2 - 30, "You Lose!!!", olc::WHITE, 3);
DrawString(ScreenWidth() / 2 - 100, ScreenHeight() / 2 + 60, "Press Enter to Restart", olc::WHITE, 1);
//reset everything
if (GetKey(olc::Key::ENTER).bPressed) {
reset();
}
} else { // player won
Clear(olc::BLACK);
DrawString(ScreenWidth() / 2 - 100, ScreenHeight() / 2 - 30, "You Won!!!", olc::WHITE, 3);
DrawString(ScreenWidth() / 2 - 100, ScreenHeight() / 2 + 60, "Press Enter to Restart", olc::WHITE, 1);
// reset everything
if (GetKey(olc::Key::ENTER).bPressed) {
reset();
}
}
return true;
}
private:
// players starting position
float fPlayerPositionX = 185.0f;
float fPlayerPositionY = 250.0f;
// game universe constants
float fPlayerVel = 90.0f;
float fBulletVel = 180.0f;
float fEnemyVel = 180.0f;
std::vector<Bullet> vBullet;
std::vector<Enemy> vEnemy;
int count = 0;
int life_count = 3;
int score = 0;
// choose random enemy first (for movement)
int index = rand() % 70;
// sprite stuffs...
std::unique_ptr<olc::Sprite> sprEnemy;
std::unique_ptr<olc::Sprite> sprPlayer;
std::unique_ptr<olc::Sprite> sprExplosion;
std::unique_ptr<olc::Sprite> sprLife;
// music stuffs..
olc::sound::WaveEngine bgmEngine;
std::unique_ptr<olc::sound::Wave> bgm;
std::unique_ptr<olc::sound::Wave> gunShot;
std::unique_ptr<olc::sound::Wave> blast;
};
int main(){
Game game;
if (game.Construct(450, 340, 4, 4))
game.Start();
return 0; // happy compiler
}