-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
438 lines (363 loc) · 9.88 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
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
#include "Game.h"
Coord_Object::Coord_Object() {}
Coord_Object::Coord_Object(float new_x, float new_y, int new_weight, int new_height, float* current_time) :
coord(new_x, new_y),
speed_x_(0),
speed_y_(0),
weight_(new_weight),
height_(new_height),
current_time_(current_time),
action_(TypeAction::Stay)
{}
//use ChangeActionTo instead!!!
void Coord_Object::ChangeAction(TypeAction new_action)
{
action_ = new_action;
switch (action_)
{
case TypeAction::Stay:
speed_x_ = 0.0;
speed_y_ = 0.0;
break;
case TypeAction::MoveRight:
speed_x_ = SPEED_RIGHT;
speed_y_ = 0;
break;
case TypeAction::MoveLeft:
speed_x_ = -1 * SPEED_RIGHT;
speed_y_ = 0;
break;
case TypeAction::Jump:
speed_x_ = 0;
speed_y_ = -SPEED_UP;
break;
case TypeAction::JumpLeft:
speed_x_ = -SPEED_RIGHT;
speed_y_ = -SPEED_UP;
break;
case TypeAction::JumpRight:
speed_x_ = SPEED_RIGHT;
speed_y_ = -SPEED_UP;
break;
case TypeAction::Fall:
speed_x_ = 0;
speed_y_ = SPEED_UP;
break;
case TypeAction::FallRight:
speed_x_ = SPEED_RIGHT;
speed_y_ = SPEED_UP;
break;
case TypeAction::FallLeft:
speed_x_ = -SPEED_RIGHT;
speed_y_ = SPEED_UP;
break;
case TypeAction::FallEnd:
speed_x_ = 0;
speed_y_ = 0;
break;
case TypeAction::ClimbUp:
break;
case TypeAction::ClimbDown:
break;
case TypeAction::Attack:
break;
default:
break;
}
}
//Attention!!!
void Coord_Object::DisplaceCoordinates()
{
coord.x += speed_x_ * (*current_time_);
coord.y += speed_y_ * (*current_time_);
}
//Should be in M:: space I think and easy to get using only Coord_Hero*----
bool Coord_Object::IsEmptyRight(const M::Map& map)
{
//x_ and y_ are the right-up coordinates
int current_y = int(coord.y);
int current_x = int(coord.x + weight_);
//constants in Constants.h
for (int i = 0; i < height_ / BLOCK_Y; ++i) {
if (!map.isSoft(current_x, current_y))
return false;
current_y += BLOCK_Y;
}
//checking for the opposite boundary of hero
return true;
}
bool Coord_Object::IsEmptyLeft(const M::Map& map)
{
int current_y = int(coord.y);
for (int i = 0; i < height_ / BLOCK_Y; ++i) {
if (!map.isSoft(int(coord.x), current_y))
return false;
current_y += BLOCK_Y;
}
return true;
}
bool Coord_Object::IsEmptyUp(const M::Map& map)
{
int current_x = int(coord.x);
for (int i = 0; i < weight_ / BLOCK_X; ++i) {
if (!map.isSoft(current_x, int(coord.y)))
return false;
current_x += BLOCK_X;
}
return true;
}
bool Coord_Object::IsEmptyDown(const M::Map& map)
{
int current_x = int(coord.x);
int current_y = int(coord.y + height_);
for (int i = 0; i < weight_ / BLOCK_X; ++i) {
if (!map.isSoft(current_x, current_y))
return false;
current_x += BLOCK_X;
}
return true;
}
//------------------------------------------------------------------------
TypeAction Coord_Object::CheckOportunityAction(TypeAction new_action, const M::Map& map)
{
if (IsEmptyDown(map)) {
if (new_action == TypeAction::MoveRight || new_action == TypeAction::FallRight) {
if (!IsEmptyRight(map))
return TypeAction::Fall;
return TypeAction::FallRight;
}
else if (new_action == TypeAction::MoveLeft || new_action == TypeAction::FallLeft) {
if (!IsEmptyLeft(map))
return TypeAction::Fall;
return TypeAction::FallLeft;
}
else {
return TypeAction::Fall;
}
}
switch (new_action)
{
case TypeAction::MoveRight:
if (IsEmptyRight(map))
return TypeAction::MoveRight;
else
return TypeAction::Stay;
break;
case TypeAction::MoveLeft:
if (IsEmptyLeft(map))
return TypeAction::MoveLeft;
else
return TypeAction::Stay;
break;
case TypeAction::Jump:
if (IsEmptyUp(map))
return TypeAction::Jump;
else
return TypeAction::Stay;
break;
case TypeAction::JumpRight:
if (IsEmptyRight(map) && IsEmptyUp(map))
return TypeAction::JumpRight;
else
return TypeAction::Stay;
break;
case TypeAction::JumpLeft:
if (IsEmptyLeft(map) && IsEmptyUp(map))
return TypeAction::JumpLeft;
else
return TypeAction::Stay;
break;
case TypeAction::ClimbUp:
break;
case TypeAction::ClimbDown:
break;
case TypeAction::Attack:
break;
}
return TypeAction::Stay;
}
Image_Object::Image_Object(std::string file, float new_x, float new_y, int weight, int height, float* current_time) :
Coord_Object(new_x, new_y, weight, height, current_time),
image_(),
texture_(),
sprite_(),
number_sprite_(0),
current_frame_(0)
{
image_.loadFromFile(file);
image_.createMaskFromColor(sf::Color(255, 255, 255));
texture_.loadFromImage(image_);
sprite_.setTexture(texture_);
sprite_.setPosition(new_x, new_y);
}
void Image_Object::ChangeSprite(TypeAction new_action)
{
current_frame_ = 0;
number_sprite_ = FindSprite(new_action);
sprite_.setTextureRect(Sprites_Hero[number_sprite_][0]);
}
void Image_Object::ChangeImage(TypeAction new_action)
{
number_sprite_ = FindSprite(new_action);
sprite_.setTextureRect(Sprites_Hero[number_sprite_][0]);
}
void Image_Object::ChangeActionTo(TypeAction new_action)
{
ChangeAction(new_action);
ChangeSprite(new_action);
}
void Image_Object::UpdateSprite()
{
current_frame_ += SPEED_FRAME * (*current_time_);
if ((this->action_ == TypeAction::Jump || action_ == TypeAction::JumpLeft || action_ == TypeAction::JumpRight) && current_frame_ > TIME_JUMPING) {
ChangeActionTo(TypeAction::Fall);
return;
}
if (this->action_ == TypeAction::FallEnd && current_frame_ > TIME_LANDING) {
ChangeActionTo(TypeAction::Stay);
return;
}
if ((int)current_frame_ > COUNT_SPRITES - 1)
current_frame_ -= COUNT_SPRITES - 1;
sprite_.setTextureRect(Sprites_Hero[number_sprite_][(int)current_frame_]);
}
void Image_Object::DisplaceSprite()
{
sprite_.setPosition(this->coord.x, this->coord.y);
}
const int Image_Object::FindSprite(TypeAction new_action) const
{
switch (new_action)
{
case TypeAction::Stay:
return Sprites_Hero_Stay;
break;
case TypeAction::MoveRight:
return Sprites_Hero_MoveRight;
break;
case TypeAction::MoveLeft:
return Sprites_Hero_MoveLeft;
break;
case TypeAction::Jump:
return Sprites_Hero_Jump;
break;
case TypeAction::JumpLeft:
return Sprites_Hero_JumpLeft;
break;
case TypeAction::JumpRight:
return Sprites_Hero_JumpRight;
break;
case TypeAction::Fall:
return Sprites_Hero_Fall;
break;
case TypeAction::FallLeft:
return Sprites_Hero_FallLeft;
break;
case TypeAction::FallRight:
return Sprites_Hero_FallRight;
break;
case TypeAction::FallEnd:
return Sprites_Hero_FallEnd;
break;
case TypeAction::ClimbUp:
break;
case TypeAction::ClimbDown:
break;
case TypeAction::Attack:
break;
default:
break;
}
std::cerr << "Action wasn't found!\n";
return 0;
}
void Image_Object::Zoom(float delta)
{
float zoom_coff = 1 + delta / ZOOMING_COOF;
weight_ *= zoom_coff;
height_ *= zoom_coff;
sprite_.scale(zoom_coff, zoom_coff);
}
void Image_Object::Draw(sf::RenderWindow& window)
{
window.draw(sprite_);
}
Hero::Hero(std::string file, float new_x, float new_y, int weight_, int height_, float* current_time) :
Image_Object(file, new_x, new_y, weight_, height_, current_time),
health_(static_cast<int>(TypeMaxHealth::HERO_START)),
weapon_(new Weapon)
{}
Hero::~Hero()
{
delete weapon_;
}
void Hero::DoAction(TypeAction new_action)
{
if (this->action_ != new_action) {
ChangeActionTo(new_action);
}
else {
this->UpdateSprite();
}
DisplaceCoordinates();
DisplaceSprite();
}
void Hero::ContinueAction()
{
this->UpdateSprite();
this->DisplaceSprite();
this->DisplaceCoordinates();
}
void Hero::CorrectAction(TypeAction new_action)
{
ChangeAction(new_action);
ChangeImage(new_action);
}
void Hero::ChangeWeapon(TypeWeapon typeweapon)
{
switch (typeweapon)
{
case TypeWeapon::Nothing:
weapon_ = new (weapon_) Weapon;
break;
case TypeWeapon::WoodSword:
weapon_ = new (weapon_) WoodSword;
break;
case TypeWeapon::ElderSword:
weapon_ = new (weapon_) ElderSword;
break;
case TypeWeapon::SilverSword:
weapon_ = new (weapon_) SilverSword;
break;
default:
break;
}
}
/*
Animal::Animal(std::string file, float new_x, float new_y, int weight_, int height_):
Image_Object(file, new_x, new_y, weight_, height_)
{}
//NOT DONE!!!!!---------------------------------------------------------------------
Bison::Bison(std::string file, float new_x, float new_y, int weight_, int height_):
Animal(file, new_x, new_y, weight_, height_),
health_(static_cast<int>(TypeMaxHealth::BISON)),
action_(TypeAction::Stay)
{}
int Bison::GetDamage()
{
return damage_;
}
bool Bison::ReceiveDamage(int delivered_damage)
{
health_ -= delivered_damage;
if (health_ < 0)
return false;
return true;
}
void Bison::ChangeAction(TypeAction new_action)
{
//êàêàÿ-òî ïðîâåðêà â map
this->action_ = new_action;
}
//UNTIL THIS-------------------------------------------------------------------------
*/