-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.cpp
439 lines (341 loc) · 9.55 KB
/
backup.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
Dependencies & Libraries
*/
#include <SFML\Graphics.hpp>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <chrono>
#include <map>
#include <vector>
#include <thread>
using namespace std;
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono; // nanoseconds, system_clock, seconds
/*
Classes
*/
class asset {
public:
sf::Texture texture;
std::string identity;
};
class tile {
public:
std::string identity;
int location = NULL;
};
/*
Variables
*/
bool debugMode = true;
int charTile = 250;
bool characterDead = false;
bool invertCharacter = false;
bool shotRight = false;
bool shotLeft = false;
sf::Vector2f bulletPos = {0,0};
sf::Vector2f shotPos = { 0,0 };
std::vector<tile> tiles(1042);
std::vector<tile> groundTiles(1042);
float bulletPos;
int health = 5;
/*
Functions
*/
std::map<string, asset> loadAssets() {
std::map<string, asset> assets;
sf::Texture characterTexture;
characterTexture.loadFromFile("./dependencies/assets/character.png", sf::IntRect(96, 64, 32, 32));
characterTexture.setSmooth(true);
//
asset* charAsset = new asset;
charAsset->texture = characterTexture;
charAsset->identity = "character";
assets.insert(assets.begin(), pair<string, asset>("character", *charAsset)); // Insert a key-value pair of "character" to correspond with a pointer to charAsset
//
sf::Texture groundTexture1;
groundTexture1.loadFromFile("./dependencies/assets/Pixel Art Top Down - Basic/Texture/TX Tileset Grass.png", sf::IntRect(0, 0, 32, 32));
groundTexture1.setSmooth(true);
//
asset* ground1Asset = new asset;
ground1Asset->texture = groundTexture1;
ground1Asset->identity = "blank grass";
assets.insert(assets.begin(), pair<string, asset>("blank grass", *ground1Asset));
//
sf::Texture groundTexture2;
groundTexture2.loadFromFile("./dependencies/assets/Pixel Art Top Down - Basic/Texture/TX Tileset Grass.png", sf::IntRect(32, 64, 32, 32));
groundTexture2.setSmooth(true);
//
asset* ground2Asset = new asset;
ground2Asset->texture = groundTexture2;
ground2Asset->identity = "leaf grass";
assets.insert(assets.begin(), pair<string, asset>("leaf grass", *ground2Asset));
//
sf::Texture ainsleyTexture;
ainsleyTexture.loadFromFile("./dependencies/assets/ainsley.png", sf::IntRect(0, 0, 32, 32));
ainsleyTexture.setSmooth(true);
//
asset* ainsley = new asset;
ainsley->texture = ainsleyTexture;
ainsley->identity = "ainsley";
assets.insert(assets.begin(), pair<string, asset>("ainsley", *ainsley));
//
sf::Texture heartTexture;
heartTexture.loadFromFile("./dependencies/assets/heart.png", sf::IntRect(0, 0, 32, 32));
heartTexture.setSmooth(true);
//
asset* heart = new asset;
heart->texture = heartTexture;
heart->identity = "heart";
assets.insert(assets.begin(), pair<string, asset>("heart", *heart));
//
sf::Texture bulletTexture;
bulletTexture.loadFromFile("./dependencies/assets/bullets.png", sf::IntRect(0, 0, 8, 8));
bulletTexture.setSmooth(true);
//
asset* bullet = new asset;
bullet->texture = bulletTexture;
bullet->identity = "bullet";
assets.insert(assets.begin(), pair<string, asset>("bullet", *bullet));
return assets;
}
void generateGround() {
for (int i = 1; i != 1042; i++) {
int randomInt = rand() % 2 + 1;
if (randomInt == 1) {
// Blank Grass
tiles[i].identity = "blank grass";
tiles[i].location = i;
groundTiles[i].identity = "blank grass";
groundTiles[i].location = i;
}
else if (randomInt == 2) {
// Leaf Grass
tiles[i].identity = "leaf grass";
tiles[i].location = i;
groundTiles[i].identity = "leaf grass";
groundTiles[i].location = i;
}
}
}
void generateAinsley() {
for (int i = 0; i != 15; i++) {
int randomInt = rand() % 1041 + 1;
tiles[randomInt].identity = "ainsley";
}
}
void generateHeart() {
for (int i = 0; i != 5; i++) {
int randomInt = rand() % 1041 + 1;
tiles[randomInt].identity = "heart";
}
}
void updateTiles(sf::RenderWindow& window, std::map<string, asset> assets, std::vector<tile> tiles) {
// Sprite Creation, takes textures from the assets map and creates a sprite with it.
sf::Sprite character;
character.setTexture(assets.at("character").texture);
sf::Sprite ground1;
ground1.setTexture(assets.at("blank grass").texture);
sf::Sprite ground2;
ground2.setTexture(assets.at("leaf grass").texture);
sf::Sprite ainsley;
ainsley.setTexture(assets.at("ainsley").texture);
sf::Sprite heart;
heart.setTexture(assets.at("heart").texture);
sf::Sprite bullet;
bullet.setTexture(assets.at("bullet").texture);
int x = 0, y = 0;
for (int i = 1; i != 1042; i++) {
// Create a sprite for the current ground (For use with hearts and other transparent backgrounds)
sf::Sprite currGround;
currGround.setTexture(assets.at(groundTiles[i].identity).texture);
if (tiles[i].identity == "blank grass") {
ground1.setPosition(x, y);
window.draw(ground1);
}
else if (tiles[i].identity == "leaf grass") {
ground2.setPosition(x, y);
window.draw(ground2);
}
else if (tiles[i].identity == "character") {
if (characterDead == false) {
if (invertCharacter == true) {
character.setScale(-1, 1); // Flip (inverse char because they shot left)
character.setPosition(x + 32, y);
}
else {
character.setScale(1, 1);
character.setPosition(x, y);
}
window.draw(character);
}
else {
tiles[i].identity = groundTiles[i].identity;
}
}
else if (tiles[i].identity == "ainsley") {
ainsley.setPosition(x, y);
window.draw(ainsley);
}
else if (tiles[i].identity == "heart") {
currGround.setPosition(x, y);
window.draw(currGround);
heart.setPosition(x, y);
window.draw(heart);
}
if (shotLeft == true) {
shotLeft = false;
}
if (shotRight == true) {
shotRight = false;
}
x = x + 32;
if (i % 40 == 0) {
x = 0;
y = y + 32;
}
}
}
void shootLeft() {
int shootLocation = charTile - 2;
shotLeft = true;
if (tiles[shootLocation].identity == "ainsley") {
tiles[shootLocation].identity = groundTiles[shootLocation].identity;
}
else {
health = health - 1;
}
}
void shootRight() {
int shootLocation = charTile + 2;
shotRight = true;
if (tiles[shootLocation].identity == "ainsley") {
tiles[shootLocation].identity = groundTiles[shootLocation].identity;
}
else {
health = health - 1;
}
}
void updateHP(sf::RenderWindow& window, sf::Text HP, int health) {
HP.setString(to_string(health));
if (health < 1) {
tiles[charTile].identity = groundTiles[charTile].identity;
characterDead = true;
HP.setString("DEAD.");
}
window.draw(HP);
}
void updateBullet(sf::RenderWindow& window, sf::Vector2f bulletPos) {
}
/*
Main
*/
int main() {
// Rand seed
srand(time(NULL));
/*
SFML Setup
-- Make main window
-- call LoadAssets() function and assign the return map to "assets"
-- Generate the ground
-- Generate the Ainsley's
-- Generate the hearts
-- Create "HP" text to display heart count
*/
sf::RenderWindow window(sf::VideoMode(1280, 832), "Attack of the Ainsley's"); // Create main window
std::map<string, asset> assets = loadAssets();
// HP
sf::Font font;
font.loadFromFile("./dependencies/assets/arial.ttf");
sf::Text HP;
HP.setFont(font);
HP.setCharacterSize(32);
HP.setFillColor(sf::Color::Red);
// Generate the ground
generateGround();
generateAinsley();
generateHeart();
tiles[charTile].identity = "character";
sf::Clock Clock;
const float Speed = 50.f;
float Left = 0.f;
float Top = 0.f;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed)
window.close();
}
sf::Time ElapsedTime = Clock.getElapsedTime();
sf::Time res = Clock.restart();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) && characterDead == false)
{
int oldPos = charTile;
charTile = charTile - 40;
if (tiles[charTile].identity == "heart") {
health++;
}
tiles[oldPos].identity = groundTiles[oldPos].identity;
tiles[charTile].identity = "character";
cout << "moved" << endl;
sleep_for(milliseconds(100));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) && characterDead == false)
{
int oldPos = charTile;
charTile = charTile + 40;
if (tiles[charTile].identity == "heart") {
health++;
}
tiles[oldPos].identity = groundTiles[oldPos].identity;
tiles[charTile].identity = "character";
cout << "moved" << endl;
sleep_for(milliseconds(100));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) && characterDead == false)
{
int oldPos = charTile;
charTile = charTile - 1;
if (tiles[charTile].identity == "heart") {
health++;
}
tiles[oldPos].identity = groundTiles[oldPos].identity;
tiles[charTile].identity = "character";
cout << "moved" << endl;
sleep_for(milliseconds(100));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) && characterDead == false)
{
int oldPos = charTile;
charTile = charTile + 1;
if (tiles[charTile].identity == "heart") {
health++;
}
tiles[oldPos].identity = groundTiles[oldPos].identity;
tiles[charTile].identity = "character";
cout << "moved" << endl;
sleep_for(milliseconds(100));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q) && characterDead == false)
{
invertCharacter = true;
shootLeft = true;
sleep_for(milliseconds(100));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::E) && characterDead == false)
{
invertCharacter = false;
shootRight = true;
sleep_for(milliseconds(100));
}
window.clear(sf::Color::Black);
updateTiles(window, assets, tiles);
updateHP(window, HP, health);
updateBullet(window, bulletPos);
window.display();
}
return EXIT_SUCCESS;
}