-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
398 lines (376 loc) · 12.5 KB
/
Game.cpp
File metadata and controls
398 lines (376 loc) · 12.5 KB
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
#include "Game.h"
Game* Game::instance = nullptr; //initializez singleton pattern
Game::Game()
: window(sf::VideoMode::getDesktopMode(), "EscapeRoom: Conquer"),
safeZone(sf::Vector2f({ 850.f,1720.f })),
chest(sf::Vector2f(150.f, 370.f))
{
try
{
if (!font.openFromFile("BASKVILL.TTF"))
{
throw FileLoadingException();
}
playButton = std::make_unique<sf::Text>(font);
gameOverText = std::make_unique<sf::Text>(font);
gameMessage = std::make_unique<sf::Text>(font);
playButton->setString("Play");
gameOverText->setString("Game Over");
gameMessage->setString("HINT: Don't forget the sword~");
gameMessage->setCharacterSize(40);
gameMessage->setFillColor(sf::Color::White);
gameMessage->setPosition(sf::Vector2f(10.f, 20.f));
}
catch (const FileLoadingException& e)
{
std::cerr << e.what() << std::endl;
exit(EXIT_FAILURE);
}
int firstKnight = 0;
hero = std::make_unique<Hero>(sf::Vector2f(2730.f, 790.f));
king = std::make_unique<King>(sf::Vector2f(150.f, 780.f));
addKnights();
for (auto& knight : knights)
{
if (!firstKnight)
{
knight = std::make_unique<Knight>(sf::Vector2f(300.f, 500.f));
firstKnight++;
}
else
{
knight = std::make_unique<Knight>(sf::Vector2f(300.f, 1100.f));
}
}
Hero* heroPtr = dynamic_cast<Hero*>(hero.get());
King* kingPtr = dynamic_cast<King*>(king.get());
initializeWalls();
initializeChest();
initializeSafeZone();
initializeBackground();
initializeUI();
}
//adaug cavaleri in vectorul de entitati
void Game::addKnights()
{
knights.push_back(std::make_unique<Knight>(sf::Vector2f(300.f, 500.f)));
knights.push_back(knights[0]->clone());
}
std::string Game::getGameMessage() const
{
if (gameStage == 1) return "One more to go!";
if (gameStage == 2) return "What is happening? Oh, no...the King arises!";
if (gameStage == 3) return "Yohooo! Finally I can say Game Over!";
return "";
}
//functia care ruleaza jocul
void Game::run()
{
playBackgroundMusic();
while (window.isOpen())
{
handleEvents();
update();
render();
}
}
void Game::initializeWalls()
{
walls.setPointCount(6);
walls.setPoint(0, { 500.f, 40.f });
walls.setPoint(1, { 2850.f, 40.f });
walls.setPoint(2, { 2850.f, 1760.f });
walls.setPoint(3, { 500.f, 1760.f });
walls.setPoint(4, { 40.f, 1409.f });
walls.setPoint(5, { 40.f, 350.f });
walls.setFillColor(sf::Color::Transparent);
walls.setOutlineThickness(3.f);
walls.setOutlineColor(sf::Color(0, 25, 51));
}
void Game::initializeChest()
{
chest.setFillColor(sf::Color::Transparent);
chest.setOutlineThickness(3.f);
chest.setOutlineColor(sf::Color::Magenta);
chest.setPosition(sf::Vector2f(2700.0f, 1150.0f));
}
void Game::initializeSafeZone()
{
safeZone.setPosition(sf::Vector2f({ 2200.f, 40.f }));
safeZone.setOutlineColor(sf::Color::Black);
}
void Game::initializeBackground()
{
if (!backgroundTexture.loadFromFile("backgroundGame.jpeg"))
{
std::cerr << "Error: Could not load background image!\n";
}
else
{ //ma asigur ca imaginea mea de fundal se afla pe toata dimensiunea ferestrei
backgroundSprite = std::make_unique<sf::Sprite>(backgroundTexture);
backgroundSprite->setScale(sf::Vector2f(
static_cast<float>(window.getSize().x) / backgroundTexture.getSize().x,
static_cast<float>(window.getSize().y) / backgroundTexture.getSize().y)
);
}
}
//initializez ecranele de start si game over
void Game::initializeUI()
{
startScreen.setSize(sf::Vector2f(3000.f, 2000.f));
startScreen.setFillColor(sf::Color(0,0,0,200)); // Semi-transparent for blur effect
playButton->setFont(font);
playButton->setString("Play");
playButton->setCharacterSize(100);
playButton->setPosition(sf::Vector2f(1350.f, 800.f));
playButton->setFillColor(sf::Color::Green);
blurEffect.setSize(sf::Vector2f(3000.f, 2000.f));
blurEffect.setFillColor(sf::Color(0, 0, 0, 200)); // Semi-transparent overlay
gameOverText->setFont(font);
gameOverText->setString("Game Over");
gameOverText->setCharacterSize(100);
gameOverText->setPosition(sf::Vector2f(1200.f, 800.f));
gameOverText->setFillColor(sf::Color::Red);
}
//afisez in UI mesajul corespunzator stadiului jocului
void Game::updateGameMessage()
{
if (gameStage == 1)
{
gameMessage->setString("One more to go!");
}
else if (gameStage == 2)
{
gameMessage->setString("What is happening? Oh no... The King arises!");
}
else if (gameStage == 3)
{
gameMessage->setString("Yohooo! Finally, I can say Game Over!");
}
}
void Game::handleEvents()
{
while (const std::optional event = window.pollEvent())
{
if (event->is<sf::Event::Closed>())
{
window.close();
}
//trebuie sa apese cuvantul play de pe ecran ca sea treaca in stadiul Playing
if (gameState == GameState::Menu)
{ //am grija ca ecranul de pornire sa se suprapuna peste fundalul jocului
if (const auto* resizedEvent = event->getIf<sf::Event::Resized>()) {
sf::Vector2u newSize = resizedEvent->size;
sf::FloatRect visibleArea(sf::Vector2f(0.f, 0.f), sf::Vector2f(static_cast<float>(newSize.x), static_cast<float>(newSize.y)));
window.setView(sf::View(visibleArea));
backgroundSprite->setScale(sf::Vector2f(
static_cast<float>(newSize.x) / backgroundTexture.getSize().x,
static_cast<float>(newSize.y) / backgroundTexture.getSize().y)
);
}
if (const auto* mousePressed = event->getIf<sf::Event::MouseButtonPressed>())
{
if (playButton->getGlobalBounds().contains(sf::Vector2f(mousePressed->position)))
{
gameState = GameState::Playing;
}
}
}
}
}
bool Game::isInSafeZone(const sf::RectangleShape& zone, const sf::RectangleShape& player)
{
return zone.getGlobalBounds().contains(player.getPosition());
}
//am modificat-o in Game.h pentru a implementa un template
//void Game::changeTexture(sf::RectangleShape& player, const std::string& newTexture)
//{
// auto* newSkin = new sf::Texture();
// if (!newSkin->loadFromFile(newTexture))
// {
// std::cerr << "Error loading texture: " << newTexture << std::endl;
// delete newSkin;
// return;
// }
//
// player.setTexture(newSkin);
//
// sf::IntRect textureRect(sf::Vector2i(0, 0), sf::Vector2i(static_cast<int>(newSkin->getSize().x), static_cast<int>(newSkin->getSize().y)));
// player.setTextureRect(textureRect);
//}
//pentru a evita suprapunerea celor doi cavaleri, am grija sa nu se creeze o coliziune
void Game::betweenKnights(sf::RectangleShape& knight1, const sf::RectangleShape& knight2, const sf::ConvexShape& walls)
{
sf::Vector2f posK1 = knight1.getPosition(), posK2 = knight2.getPosition();
sf::Vector2f direction = posK1 - posK2;
float length = std::sqrt(std::pow(direction.x, 2) + std::pow(direction.y, 2));
if (length > 0)
{
direction /= length;
if (length < 200.f)
{
sf::Vector2f newPos = posK1 + (direction * (200.f - length));
if (walls.getGlobalBounds().contains(newPos)) {
knight1.move(direction * (200.f - length));
}
}
}
}
//aici se intampla magia jocului
void Game::update()
{
if (gameState != GameState::Playing) return;
updateGameMessage();
updateGameHealth();
bool ok = 0, kingOk = 0;
float m_delta_t = clock.restart().asSeconds(); //am nevoie pentru a crea atacuri la un anumit interval de timp pentru a nu fi continue
hero->moveEntity(walls, m_delta_t, *hero);
hero->moveHPBar(hero->getEntity(), hero->getHPBar(), walls, m_delta_t, 400.f);
for (auto& knight : knights)
{
knight->moveHPBar(knight->getEntity(), knight->getHPBar(), walls, m_delta_t, 250.f);
}
king->moveHPBar(king->getEntity(), king->getHPBar(), walls, m_delta_t, 250.f);
//cat timp este in safe zone, cavalerii nu il mai urmaresc/ataca pe erou
if (!isInSafeZone(safeZone, hero->getEntity()))
{
if (Hero* heroPtr = dynamic_cast<Hero*>(hero.get()))
{
for (auto& knight : knights)
{
knight->moveEntity(walls, m_delta_t, *knight, heroPtr);
}
}
}
betweenKnights(knights[0]->getEntity(), knights[1]->getEntity(), walls);
//daca eroul a dat hover pe cufar, isi ia sabia( schimb textura eroului)
if (isInSafeZone(chest, hero->getEntity()) && !ok)
{
ok = 1;
changeTexture(hero->getEntity(),"heroWithSword.png");
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space))
{
for (auto& knight : knights)
{
if (knight->getHP() > 0.f)
{
hero->attack(hero->getEntity(), knight->getEntity(), 200.f, knight->getHP(), heroAttackTimer);
}
}
if (gameStage == 2)
{
hero->attack(hero->getEntity(), king->getEntity(), 200.f, king->getHP(), heroAttackTimer);
}
}
if (knights[0]->getHP() <= 0.f)
{
knights[0]->getEntity().setPosition(sf::Vector2f(-1000.f, -1000.f));
gameStage = 1;
knightAlive = 1;
}
else if (knights[1]->getHP() <= 0.f)
{
knights[1]->getEntity().setPosition(sf::Vector2f(-1000.f, -1000.f));
gameStage = 1;
knightAlive = 0;
}
if (knights[0]->getHP() <= 0.f && knights[1]->getHP() <= 0.f)
{
knights[knightAlive]->getEntity().setPosition(sf::Vector2f(-1000.f, -1000.f));
gameStage = 2;
}
//daca ambii cavaleri au murit in batalie, regele se va apara singur si il va ataca pe erou
if (gameStage == 2)
{
if (King* kingPtr = dynamic_cast<King*>(king.get()))
{
king->moveEntity(walls, m_delta_t, *kingPtr, dynamic_cast<Hero*>(hero.get()));
if (!kingOk)
{
kingOk = 1;
changeTexture(king->getEntity(), "kingWithSword.png");
}
}
}
if (king->getHP() <= 5.f)
{
king->getEntity().setPosition(sf::Vector2f(-1000.f, -1000.f));
gameStage = 3;
}
//daca moare eroul, va aparea pe ecran Game Over
if (hero->getHP() <= 5.f)
{
hero->getEntity().setPosition(sf::Vector2f(5000.f, 780.f));
gameState = GameState::GameOver;
}
for (auto& knight : knights)
{
knight->attack(hero->getEntity(), knight->getEntity(), 100.f, hero->getHP(), enemyAttackTimer);
knight->updateHPBar(knight->getHP(), knight->getHPBar(), 200.f);
}
king->attack(hero->getEntity(), king->getEntity(), 180.f, hero->getHP(), enemyAttackTimer);
King* kingPtr = dynamic_cast<King*>(king.get());
if (kingPtr)
{
kingPtr->dodgeAttack(king->getEntity(), hero->getEntity(), walls);
//puterea speciala a regelui, se poate teleporta oriunde pentru a deruta adversarul si pentru a da dodge la atacurile eroului
}
hero->updateHPBar(hero->getHP(), hero->getHPBar(), 200.f);
king->updateHPBar(king->getHP(), king->getHPBar(), 200.f);
}
//controlez ce se va afisa pe ecran
void Game::render()
{
window.clear();
if (gameState == GameState::Menu)
{
renderMenu();
}
else if (gameState == GameState::Playing)
{
window.draw(walls);
window.draw(chest);
window.draw(safeZone);
if (backgroundSprite)
{
window.draw(*backgroundSprite);
}
hero->draw(window);
for (auto& knight : knights)
{
knight->draw(window);
}
king->draw(window);
window.draw(*gameMessage);
}
else if (gameState == GameState::GameOver)
{
renderGameOver();
}
window.display();
}
void Game::renderGameOver()
{
if (backgroundSprite)
{
window.draw(*backgroundSprite);
}
window.draw(blurEffect);
window.draw(*gameOverText);
}
void Game::renderMenu()
{
if (backgroundSprite)
{
window.draw(*backgroundSprite);
}
window.draw(startScreen);
window.draw(*playButton);
}
//suprascriu operatorul de scriere pentru mesajele din consola in timpul jocului
std::ostream& operator<<(std::ostream& os, const Game& game)
{
os << game.getGameMessage();
return os;
}