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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
/*********************************/
/* THRESHOLD ( // */
/* gameplay ( )/ */
/* by salade )(/ */
/* ________________ ( /) */
/* ()__)____________))))) :^} */
/*********************************/
#include "gameplay.hpp"
#include <raylib.h>
#include <fstream>
#include <iostream>
#include <string>
#include "raymath.h"
#include "weapon.hpp"
#include "window.hpp"
#include "wp_assaultrifle.hpp"
#include "wp_enemyslingshot.hpp"
#include "wp_shotty.hpp"
Game::Game(std::string const& path) : current(path) {
std::ifstream ifs(path);
std::string tok;
auto radius = 0;
auto ehp = 0;
std::cout << "Init: reading map file [" << path << "]" << std::endl;
while (ifs >> tok) {
if (tok == "ENEMIES") {
ifs >> tok;
std::cout << "will spawn " << tok << " enemies";
nEnemies = std::atoi(tok.c_str());
ifs >> tok;
radius = std::atoi(tok.c_str());
}
if (tok == "NEXT") {
ifs >> tok;
next = tok;
std::cout << "next level is " << next;
}
if (tok == "BOSS") {
ifs >> tok;
ehp = (tok == "0") ? 1 : 0;
if (ehp == 0) {
ifs >> tok;
ehp = std::atoi(tok.c_str());
}
}
if (tok == "WAVES") {
ifs >> tok;
nWaves = std::atoi(tok.c_str());
ifs >> tok;
nPerWave = std::atoi(tok.c_str());
}
if (tok == "BACKGROUND") {
ifs >> tok;
background = tok;
}
}
ifs.close();
enemies = new std::vector<Entity>;
InitAudioDevice();
AWeapon* shotty = new wp_shotty(SHOTTY_BANG, SHOTTY_RELOAD);
AWeapon* ar = new wp_assaultrifle(AR_BANG, SHOTTY_RELOAD);
AWeapon* sling = new wp_enemysling(SHOTTY_BANG, SHOTTY_RELOAD);
for (auto i = 0; i < nPerWave; i++) {
if (ehp == 1) {
Entity en(ehp);
en.radius = radius;
en.idleTex = LoadTexture(SBIRE_TEX_IDLE);
en.hurtTex = LoadTexture(SBIRE_TEX_HURT);
en.currentWeapon = nullptr;
enemies->push_back(en);
} else {
Entity en(ehp);
en.radius = radius;
en.wp[0] = sling;
en.currentWeapon = en.wp[0];
en.idleTex = LoadTexture(BOSS_TEX_IDLE);
en.hurtTex = LoadTexture(BOSS_TEX_HURT);
enemies->push_back(en); // legacy code. TODO: remove AKchually no
}
}
player = new Entity;
player->posX = 0;
player->posY = SCREENHEIGHT / 2.0f;
player->direction.x = 100;
player->direction.y = 0;
player->radius = 10;
player->victims = 0;
player->fury = 0;
crosshair = LoadTexture(CROSSHAIR_TEX);
player->wp[0] = shotty;
player->wp[1] = ar;
player->currentWeapon = player->wp[0];
player->idleTex = LoadTexture(MUCHACHO_TEX);
}
Game::~Game() {
delete enemies;
delete player;
}
void Game::start() {
std::cout << "----- Gameplay: Start -----" << std::endl;
std::cout << "Gameplay: " << nEnemies << "enemies need to be spawned"
<< std::endl;
frameWidth = player->idleTex.width;
frameHeight = player->idleTex.height;
sourceRec = {0.0f, 0.0f, (float)frameWidth, (float)frameHeight};
origin = {(float)frameWidth, (float)frameHeight};
}
// draw bad boys and player
void Game::draw() {
auto left = std::to_string(enemies->size());
for (auto& en : *enemies) {
if (en.hp == 0)
DrawTextureEx(en.hurtTex,
(Vector2){en.posX - en.radius, en.posY - en.radius},
1.0f, 0.6f, WHITE);
else {
DrawTextureEx(en.idleTex,
(Vector2){en.posX - en.radius, en.posY - en.radius},
1.0f, 0.6f, WHITE);
}
}
// Destination rectangle
Rectangle destRec = {player->posX, player->posY, frameWidth * 1.8f,
frameHeight * 1.8f};
// Origin of the texture (rotation/scale point), it's relative to
// destination rectangle size
DrawTexturePro(player->idleTex, sourceRec, destRec, origin,
Vector2Angle((Vector2){0.0f, 0.0f}, player->direction),
WHITE);
DrawText("Enemies left : ", 10, 10, 20, GREEN);
DrawText(left.c_str(), 150, 10, 20, RED);
if (player->fury >= 5) {
DrawText("[E] FURY", SCREENWIDTH - 300, 10, 50, RED);
}
for (auto i = 0; i < player->currentWeapon->barrel;
i++) { // draw weapon ammo
DrawRectangle(40 + (i * 20), SCREENHEIGHT - 60, 10, 30, RED);
}
if (enemies->at(0).hp >= 2) { // draw hp in boss stages
for (auto i = 0; i < enemies->size(); i++) {
if (enemies->at(i).hp >= 2) {
for (auto j = 0; j < enemies->at(i).hp; j++) {
DrawRectangle(400 + (j * 40), 80 + (i * 40), 40, 30,
COOLPURPLE);
}
}
}
}
}
// progress the game & check for player death
// NEW: go towards player NEXT: spawn at different furyTimes
int Game::tick() {
auto target = GetMousePosition();
DrawTexture(crosshair, target.x, target.y, WHITE);
auto v2 = (Vector2){target.x - player->posX, target.y - player->posY};
DrawText(std::to_string(v2.x).c_str(), 1400, 10, 20, RED);
DrawText(std::to_string(v2.y).c_str(), 1400, 30, 20, RED);
player->direction = v2;
if (player->victims == nPerWave && nWaves > 1) {
nWaves--;
for (int i = 0; i < nPerWave; i++) {
Entity en(1);
en.radius = 20;
en.idleTex = LoadTexture(SBIRE_TEX_IDLE);
en.hurtTex = LoadTexture(SBIRE_TEX_HURT);
en.currentWeapon = nullptr;
enemies->push_back(en);
}
}
for (auto en = enemies->begin(); en != enemies->end(); en++) {
if (en->hp > 0) {
if (en->posX >= SCREENWIDTH || en->posX <= 0) {
en->direction.x = -en->direction.x;
}
if (en->posY >= SCREENHEIGHT || en->posY <= 0) {
en->direction.y = -en->direction.y;
}
if (en->posX >= player->posX) {
en->posX -= 2.1f;
en->direction.x -= 0.1f;
}
if (en->posY >= player->posY) {
en->posY -= 2.1f;
en->direction.y -= 0.1f;
}
if (en->posX <= player->posX) {
en->posX += 2.1f;
en->direction.x += 0.1f;
}
if (en->posY <= player->posY) {
en->posY += 2.1f;
en->direction.y += 0.1f;
}
if ((GetRandomValue(0, 100) == 50) &&
(en->currentWeapon != nullptr)) {
std::cout << "spawn enemy" << std::endl;
en->currentWeapon->bang(enemies, &(*en));
nEnemies++;
return (0);
}
} else {
if (en->posX >= SCREENWIDTH || en->posX <= 0 ||
en->posY >= SCREENHEIGHT) {
enemies->erase(en);
return (0);
}
}
en->posX += en->direction.x; // zoning better
en->posY += en->direction.y;
if (en->hp != 0 && // check for player death (one shot one kill)
CheckCollisionCircles((Vector2){player->posX, player->posY}, 10,
(Vector2){en->posX, en->posY}, 40)) {
return (1);
}
}
return (0);
}
int Game::getKeys() {
auto oldX = 0, oldY = 0,
speed = 7; // get position before processing keys to check
// for player movement in threshold mode
oldX = player->posX;
oldY = player->posY;
if (IsKeyDown(KEY_W)) {
player->posX += 0;
if ((player->posY + speed) < 0)
player->posY = 0;
else
player->posY += -speed;
}
if (IsKeyDown(KEY_S)) {
player->posX += 0;
if ((player->posY + speed) > SCREENHEIGHT)
player->posY = SCREENHEIGHT;
else
player->posY += speed;
}
if (IsKeyDown(KEY_A)) {
if ((player->posX - speed) < 0) // avoid leaving the map
player->posX = 0;
else
player->posX += -speed;
player->posY += 0;
}
if (IsKeyDown(KEY_D)) {
if ((player->posX + speed) > SCREENWIDTH)
player->posX = SCREENWIDTH;
else
player->posX += speed;
player->posY += 0;
}
if (player->fury >= 5 && IsKeyDown(KEY_E)) {
player->furyTime = GetTime();
player->threshold = true;
player->fury = 0;
}
if (IsKeyDown(KEY_ONE)) {
player->currentWeapon = player->wp[0];
}
if (IsKeyDown(KEY_TWO)) {
player->currentWeapon = player->wp[1];
}
if (IsKeyPressed(KEY_SPACE) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
if (shoot()) {
return (0);
}
if (player->victims == nEnemies) {
return (2);
}
}
if (player->threshold) {
if (GetTime() >= (player->furyTime + 5)) {
player->fury = 0;
player->threshold = false;
}
if (oldX != player->posX || oldY != player->posY) {
if (this->tick()) {
return (1);
}
}
} else {
if (this->tick()) {
return (1);
}
}
if (player->currentWeapon->empty) {
if (GetTime() >= (player->reloadTime + 2)) {
player->currentWeapon->refill();
player->currentWeapon->empty = false;
}
}
return (0);
}
int Game::shoot() const {
if (player->currentWeapon->empty == true) {
return (0);
}
player->currentWeapon->bang(enemies, player);
if (player->currentWeapon->empty == true) {
player->reloadTime = GetTime();
}
return (0);
}
std::string const& Game::getNext() const {
return next;
}
std::string const& Game::getCurrent() const {
return current;
}
std::string const& Game::getBackground() const {
return background;
}
int const& Game::getKills() const {
return player->victims;
}
|