-
Notifications
You must be signed in to change notification settings - Fork 0
/
bonus.h
332 lines (311 loc) · 11.8 KB
/
bonus.h
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
#include <SFML/Graphics.hpp>
#include <iostream>
#include "SFML\Audio.hpp"
using namespace sf;
Music music;
int playerScore = 0;////
bool randomBonusFlag = true;////
float createObjectForMapTimer = 0;////
float kill_timer = 0;
float bonusX, bonusY;////
int bonus_last_J, bonus_last_I;
int get_Num(int i, int j, int max);
int RandomChoiser(int a1, int a2, int a3, int N);
Image imageCreator(String Name, const sf::Color c = sf::Color::Transparent);
class Pokeball {
public:
Texture texture;
Sprite sprite;
Pokeball(Image &image, float x, float y, int w, int h) {
texture.loadFromImage(image);
sprite.setTexture(texture);
sprite.setTextureRect(IntRect(0, 0, w, h));
sprite.setPosition(x, y);
}
};
class Simple_Bonus {
public:
float x, y;
int Score;
Text text;
float w, h;
Texture texture;
Sprite sprite;
String Name;
float C_frame = 0;
Simple_Bonus(Image &image, int X, int Y, int W, int H, int score, String n) {
w = W; h = H;
Name = n;
Score = score;
texture.loadFromImage(image);
sprite.setTexture(texture);
x = X; y = Y;
sprite.setPosition(x, y);
sprite.setTextureRect(IntRect(0, 0, w, h));
}
void Score_reset() { Score = 0; }
virtual ~Simple_Bonus() {}
virtual void update(float time) {}
FloatRect getRect() {
return FloatRect(x, y, w, h);
}
};
class Coin :public Simple_Bonus {
public:
Coin(Image &image, int X, int Y, float W, float H, int score, String n) :Simple_Bonus(image, X, Y, W, H, score, n) {
};
~Coin() {
playerScore += Score;
}
void update(float time) {
show_bonus_timer();
C_frame += 0.0042*time;
if (C_frame > 6) C_frame -= 6;
sprite.setTextureRect(IntRect(w * int(C_frame), 0, w, h));
};
virtual void show_bonus_timer() { }
};
class Ball :public Simple_Bonus {
public:
Ball(Image &image, int X, int Y, float W, float H, int score, String n) :Simple_Bonus(image, X, Y, W, H, score, n) {};
~Ball() {
playerScore += Score;
kill_timer = 5000;
music.play();
}
void update(float time) {
C_frame += 0.005*time;
if (C_frame > 8) C_frame -= 8;
sprite.setTextureRect(IntRect(0, int(C_frame)*h, w, h));
}
};
class GoldCoin :public Coin {
public:
Font font;
int colorloop = 0;
bool colorflag = true;
GoldCoin(Image &image, int X, int Y, float W, float H, int score, String n) :Coin(image, X, Y, W, H, score, n) {
font.loadFromFile("CyrilicOld.TTF");
text = Text("", font, 22);
text.setStyle(Text::Bold);
text.setFillColor(Color(34, 177, 76));
text.setPosition(32 + 4, 32 * 3 + 15);
text.setOutlineThickness(2);
text.setOutlineColor(Color::Black);
};
virtual ~GoldCoin() {
randomBonusFlag = true;
createObjectForMapTimer = 0;
}
void show_bonus_timer() {
std::ostringstream timerbonus;
timerbonus << 20 - int(createObjectForMapTimer) / 1000;
text.setString(timerbonus.str());
if (createObjectForMapTimer >= 11000) { text.setPosition(32 + 10, 32 * 3 + 14); }
if (createObjectForMapTimer >= 15000)
{
if (colorflag) {
text.setPosition(32 + 10, 32 * 3 + 13);
text.setFillColor(Color(255, 19, 0));
text.setCharacterSize(25);
text.setOutlineColor(Color::Yellow);
if (++colorloop == 40) { colorflag = !colorflag; colorloop = 0; };
}
else
{
text.setPosition(32 + 10, 32 * 3 + 14);
text.setFillColor(Color(255, 255, 255));
text.setCharacterSize(22);
if (++colorloop == 25) { colorflag = !colorflag; colorloop = 0; };
}
}
}
};
class Pokemon :public GoldCoin {
private:
Music poc_sound;
int ii, jj,l_i,l_j, dir, nextdir;
bool f=true;
public:
float speed, dx, dy, CurrentFrame = 0;
Pokemon(Image &image, float X, float Y, int W, int H, int score, String name) : GoldCoin(image, X, Y, W, H, score, name) {
speed = 0.09; dir = rand() % 4;
poc_sound.openFromFile("sounds/poc.wav");
poc_sound.play();
};
void update(float time)
{
show_bonus_timer();
if (CurrentFrame > 2) CurrentFrame -= 2;
CurrentFrame += 0.005*time;
ii = y / 32;
jj = x / 32;
nextdir = dir;
if ((char(RunMap[ii][jj]) != ' ') && f) {
switch (char(RunMap[ii][jj]))
{
case '0': { }
case '2': {
if ((dy > 0))dir = 1;
if ((dx > 0))dir = 3;
break; }
case '3': {
if ((dy < 0) && y <= ii * 32 + (32 - h) / 2) dir = 1;
if ((dx > 0)) dir = 2;
break; }
case '4': {
if ((dy < 0) && y <= ii * 32 + (32 - h) / 2) dir = 0;
if ((dx < 0) && x <= jj * 32 +(32-w)/2)dir = 2;
break; }
case '5': { /*std::cout << char(RunMap[ii][jj]);*/
if ((dy > 0)) dir = 0;
if ((dx < 0) && x <= jj * 32 +(32 - w) / 2)dir = 3;
break; }
case '6': { /*std::cout << char(RunMap[ii][jj]);*/
if ((dy > 0)) dir = RandomChoiser(1, 2, 0, 2);
if ((dy < 0) && y <= ii * 32 + (32 - h) / 2)dir = RandomChoiser(1, 3, 0, 2);
if ((dx > 0)) dir = RandomChoiser(2, 3, 0, 2);
break; }
case '7': { /*std::cout << char(RunMap[ii][jj]);*/
if ((dy > 0)) dir = RandomChoiser(0, 2, 0, 2);
if ((dy < 0) && y <= ii * 32 + (32 - h) / 2) dir = RandomChoiser(0, 3, 0, 2);
if ((dx < 0) && x <= jj * 32 + (32 - w) / 2) dir = RandomChoiser(2, 3, 0, 2);
break; }
case '8': { /*std::cout << char(RunMap[ii][jj]);*/
if ((dy > 0)) dir = RandomChoiser(0, 1, 0, 2);
if ((dx < 0) && x <= jj * 32 + (32 - w) / 2) dir = RandomChoiser(1, 3, 0, 2);
if ((dx > 0)) dir = RandomChoiser(0, 3, 0, 2);
break; }
case '9': { /*std::cout << char(RunMap[ii][jj]);*/
if ((dy < 0)&& y<=ii*32+(32-h)/2) dir = RandomChoiser(0, 1, 0, 2);
if ((dx < 0) && x <= jj * 32 + (32 - w) / 2) dir = RandomChoiser(1, 2, 0, 2);
if ((dx > 0)) dir = RandomChoiser(0, 2, 0, 2);
break; }
}
/*speed += 0.0003;*/
if (nextdir != dir) {
l_i = ii;
l_j = jj;
f = false;
}
}
if (((l_i != ii) || (l_j != jj))&&!f) f = true;
switch (dir)
{
case 0:sprite.setTextureRect(IntRect(w * int(CurrentFrame), 3 * h, w, h)); dx = speed; dy = 0; break;
case 1:sprite.setTextureRect(IntRect(w * int(CurrentFrame), h, w, h)); dx = -speed; dy = 0; break;
case 2:sprite.setTextureRect(IntRect(w * int(CurrentFrame), 0, w, h)); dx = 0; dy = speed; break;
case 3:sprite.setTextureRect(IntRect(w * int(CurrentFrame), 2 * h, w, h)); dx = 0; dy = -speed; break;
}
sprite.setPosition(x, y);
x += dx*time*0.95;
y += dy*time*0.95;
for (int i = y / 32; (i < (y + h) / 32); i++)
for (int j = x / 32; (j < (x + w) / 32); j++)
{
if (TileMap[i][j] == '0') {
if (dy > 0) { y = i * 32 - h; dir = RandomChoiser(0, 1, 0, 2); }//âíèç
if (dy < 0) { y = i * 32 + 32; dir = RandomChoiser(0, 1, 0, 2); }//ââåðõ
if (dx > 0) { x = j * 32 - w; dir = RandomChoiser(2, 3, 0, 2); }//âïðàâî
if (dx < 0) { x = j * 32 + 32; dir = RandomChoiser(2, 3, 0, 2); }//âëåâî
}
if ((x < 40) && (i = 9) && (dx < 0)) { j = 21; x = 675; };
if ((x > 675) && (i = 9) && (dx > 0)) { x = 40; };
}
}
};
Simple_Bonus *Bonuses_on_map[HEIGHT_MAP* WIDTH_MAP];
void Coins_create() {
music.openFromFile("sounds/coin.wav");
for (int i = 0; i < HEIGHT_MAP; i++)
for (int j = 0; j < WIDTH_MAP; j++) {
if (TileMap[i][j] == '1')
Bonuses_on_map[get_Num(i, j, WIDTH_MAP)] = new Coin(imageCreator("coin.png",Color(0,128,128)), j * 32 + 7, i * 32 + 7, 18, 18, 1, "coin");
else
Bonuses_on_map[get_Num(i, j, WIDTH_MAP)] = NULL;
if (((j==3)&&((i==18)||(i==2)))||((j== 19) && ((i == 18) || (i == 2))))Bonuses_on_map[get_Num(i, j, WIDTH_MAP)] = new Ball(imageCreator("ball.png", Color(0, 128, 128)), j * 32 + 7, i * 32 + 7, 22, 22, 1, "ball");
if ((((j == 8) || (j == 14)) && ((i >= 7) && (i <= 11))) || ( ((i == 7)||(i==11) )&& ((j >= 8) && (j <= 14)))) Bonuses_on_map[get_Num(i, j, WIDTH_MAP)] = new Coin(imageCreator("coin50.png"), j * 32 + 7, i * 32 + 7, 18, 18, 25, "goldcoin");
}
};
void randomBonusGenerate() {
srand(time(0));
int randomElementX = 2 + rand() % (WIDTH_MAP - 4);
int randomElementY = 1 + rand() % (HEIGHT_MAP - 2);
int itry = randomElementY = 1 + rand() % 10;
std::cout << "TRIED TO CREATE THE BONUS\n";
while ((TileMap[randomElementY][randomElementX] != ' ') && (itry)) {
randomElementX = 2 + rand() % (WIDTH_MAP - 4);
randomElementY = 1 + rand() % (HEIGHT_MAP - 2);
itry--;
}
if (TileMap[randomElementY][randomElementX] == ' ') {
randomBonusFlag = false;
switch (int choise = rand() % 9)
{
case 0: Bonuses_on_map[get_Num(randomElementY, randomElementX, WIDTH_MAP)] = new Pokemon(imageCreator("pocs/pikachu.png"), randomElementX * 32 + 5.5, randomElementY * 32 + 4, 21, 24, 100, "poc"); break;
case 1: Bonuses_on_map[get_Num(randomElementY, randomElementX, WIDTH_MAP)] = new Pokemon(imageCreator("pocs/poc1.png",Color(0,128,128)), randomElementX * 32 + 6.5, randomElementY * 32 + 6, 19, 20, 100, "poc"); break;
case 2: Bonuses_on_map[get_Num(randomElementY, randomElementX, WIDTH_MAP)] = new Pokemon(imageCreator("pocs/poc2.png", Color(0, 128, 128)), randomElementX * 32 + 5, randomElementY * 32 + 6, 22, 20, 100, "poc"); break;
case 3: Bonuses_on_map[get_Num(randomElementY, randomElementX, WIDTH_MAP)] = new Pokemon(imageCreator("pocs/poc3.png", Color(0, 128, 128)), randomElementX * 32 + 6.5, randomElementY * 32 + 6.5, 19, 19, 100, "poc"); break;
case 4: Bonuses_on_map[get_Num(randomElementY, randomElementX, WIDTH_MAP)] = new Pokemon(imageCreator("pocs/poc4.png", Color(0, 128, 128)), randomElementX * 32 + 4, randomElementY * 32 + 3.5, 24, 25, 100, "poc"); break;
case 5: Bonuses_on_map[get_Num(randomElementY, randomElementX, WIDTH_MAP)] = new Pokemon(imageCreator("pocs/poc5.png", Color(0, 128, 128)), randomElementX * 32 + 4.5, randomElementY * 32 + 4.5, 23, 23, 100, "poc"); break;
case 6: Bonuses_on_map[get_Num(randomElementY, randomElementX, WIDTH_MAP)] = new Pokemon(imageCreator("pocs/poc6.png", Color(0, 128, 128)), randomElementX * 32 + 5.5, randomElementY * 32 + 5.5, 21, 21, 100, "poc"); break;
case 7: Bonuses_on_map[get_Num(randomElementY, randomElementX, WIDTH_MAP)] = new Pokemon(imageCreator("pocs/poc7.png", Color(0, 128, 128)), randomElementX * 32 + 6.5, randomElementY * 32 + 5.5, 19, 21, 100, "poc"); break;
case 8: Bonuses_on_map[get_Num(randomElementY, randomElementX, WIDTH_MAP)] = new Pokemon(imageCreator("pocs/poc8.png", Color(0, 128, 128)), randomElementX * 32 + 6.5, randomElementY * 32 + 6, 19, 20, 100, "poc"); break;
}
std::cout << "BONUS CREATED!\n X: " << randomElementX << " Y: " << randomElementY << "\n\n";
bonus_last_I = randomElementY;
bonus_last_J = randomElementX;
}
};
void Bonus_deleter(int i, int j) {
delete Bonuses_on_map[get_Num(i, j, WIDTH_MAP)];
Bonuses_on_map[get_Num(i, j, WIDTH_MAP)] = NULL;
TileMap[i][j] = ' ';
}
void Bonus_deleter_byTimer(Simple_Bonus *b, float &timer, float time, float maxTime, int i, int j) {
if (timer >= maxTime) {
b->Score_reset();
Bonus_deleter(i, j);
timer = 0;
std::cout << "BONUS FAILD!\n\n";
}
else timer += time;
}
void Bonuses_update(RenderWindow *w, float time, Pokeball p) {
for (int i = 0; i < (HEIGHT_MAP* WIDTH_MAP); i++)if (Bonuses_on_map[i] != NULL)Bonuses_on_map[i]->update(time);
if (randomBonusFlag) {
createObjectForMapTimer += time;
if (createObjectForMapTimer > 6000) {
createObjectForMapTimer = 0;
randomBonusGenerate();
}
}
else
{
Bonus_deleter_byTimer(Bonuses_on_map[get_Num(bonus_last_I, bonus_last_J, WIDTH_MAP)], createObjectForMapTimer, time, 20000, bonus_last_I, bonus_last_J);
}
for (int i = 0; i < (HEIGHT_MAP* WIDTH_MAP); i++) {
if (Bonuses_on_map[i] != NULL) {
w->draw(Bonuses_on_map[i]->sprite);
if (Bonuses_on_map[i]->Name == "poc") {
w->draw(p.sprite);
w->draw(Bonuses_on_map[i]->text);
}
}
}
if (Bonuses_on_map[get_Num(bonus_last_I, bonus_last_J, WIDTH_MAP)] != NULL) w->draw(Bonuses_on_map[get_Num(bonus_last_I, bonus_last_J, WIDTH_MAP)]->sprite);
};
int get_Num(int i, int j, int max)
{
return i*max + j;
}
int RandomChoiser(int a1, int a2, int a3, int N) {
int A[3] = { a1,a2,a3 };
return A[rand() % N];
}
Image imageCreator(String Name, const sf::Color c ) {
Image bufer;
bufer.loadFromFile("images/" + Name);
if (c != c.Transparent)bufer.createMaskFromColor(c);
return bufer;
};