forked from GianlucaBotteri/CPPVideogame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
347 lines (283 loc) · 10.6 KB
/
Copy pathmain.cpp
File metadata and controls
347 lines (283 loc) · 10.6 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
#include <iostream>
#include <vector>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "TileMap.h"
#include "Hero.h"
#include "Mob.h"
#include "NPC.h"
#include "Projectile.h"
#include "Settings.h"
#include "StatusBar.h"
#include "Strategy.h"
#include "Game.h"
int main() {
enum Gamestate {
Initialize, Pause, Playing
};
Gamestate gameState = Initialize;
Game game;
Settings settings;
Strategy strategy;
// Screen dimesions
sf::Vector2u screenDimension(800, 600);
// Create the window
sf::RenderWindow window(sf::VideoMode(screenDimension.x, screenDimension.y), "Videogame", sf::Style::None);
// Center the window
sf::Vector2i positionWindow((sf::VideoMode::getDesktopMode().width / 2) - screenDimension.x / 2,
(sf::VideoMode::getDesktopMode().height / 2) - screenDimension.y / 2);
window.setPosition(positionWindow);
window.setFramerateLimit(60);
int choosenCharacter = 0;
game.setFont();
sf::Text name;
name.setColor(sf::Color::Black);
std::string str;
int chooseNPC = 0;
if (gameState == Initialize) {
settings.showSplash(window);
window.clear();
settings.showOptions(window, choosenCharacter);
settings.inputName(str, name, window, game.font, choosenCharacter);
name.setCharacterSize(18);
name.setPosition(12, 180);
window.clear();
settings.chooseNPC(window, chooseNPC);
gameState = Playing;
}
// center camera
sf::View view;
view.reset(sf::FloatRect(0, 0, screenDimension.x, screenDimension.y));
view.setViewport(sf::FloatRect(0, 0, 1.0f, 1.0f));
sf::Vector2f position(screenDimension.x / 2, screenDimension.y / 2);
sf::Vector2f viewSize = view.getSize();
game.loadTextures(choosenCharacter, chooseNPC);
// Load music & sound effects
game.setMusic();
// Set Hero
Hero hero(8, 100, str, 200, 0);
hero.sprite.setTexture(game.texturePlayer);
// Enemies
std::vector<Item> items;
game.setItems();
// NPC
NPC buddy(100, 1);
buddy.rect.setPosition(200, 400);
buddy.sprite.setTexture(game.textureNPC);
// Vector of enemies
std::vector<Mob> enemies;
game.setEnemies();
enemies.push_back(game.darkLord);
enemies.push_back(game.assassin);
enemies.push_back(game.zombie);
enemies.push_back(game.chimera);
enemies.push_back(game.chaosKnight);
// Vector of projectiles
std::vector<Projectile> projectileArray;
Projectile projectile;
projectile.sprite.setTexture(game.textureFireBall);
// Personalize Hero
game.setSprites(choosenCharacter);
game.setHeroText(hero);
game.personalizeHero(hero, choosenCharacter);
game.setHeroBar(hero);
// Mappa Background
TileMap mapBackground;
if (!mapBackground.load("TileMap.png", sf::Vector2u(48, 48), game.levelBackground, 19, 13))
return -1;
// Mappa Visibile
TileMap mapVisible;
if (!mapVisible.load("TileMap.png", sf::Vector2u(48, 48), game.levelVisible, 19, 13))
return -1;
// Inizializza gli observer
ExperienceBar experienceBar(&hero);
HealthBar healthBar(&hero);
MoneyBar moneyBar(&hero);
WeaponBar weaponBar(&hero);
PotionBar potionBar(&hero);
// Inizializza valori bool per animazione spada
bool isAnimating = false;
bool startAnimation = false;
bool halfAnimation = false;
// MAIN LOOP
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
window.close();
}
// Setting View
if (hero.rect.getPosition().x + 24 > screenDimension.x / 2)
position.x = hero.rect.getPosition().x + 24;
if (hero.rect.getPosition().y + 24 > screenDimension.y / 2)
position.y = hero.rect.getPosition().y + 24;
if (hero.rect.getPosition().x + viewSize.x / 2 > 888)
position.x = 912 - (viewSize.x / 2);
if (hero.rect.getPosition().y + viewSize.y / 2 > 600)
position.y = 624 - (viewSize.y / 2);
view.setCenter(position);
window.setView(view);
window.clear();
// Draw the map
window.draw(mapBackground);
window.draw(mapVisible);
sf::Time elapsedProjectile = game.clockProjectile.getElapsedTime();
sf::Time elapsedAngry = game.clockAngry.getElapsedTime();
sf::Time elapsedNPC = game.clockNPC.getElapsedTime();
int enemyCounter = 0;
// Weapon Animation
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
startAnimation = true;
}
// Game in pause
if (sf::Keyboard::isKeyPressed(sf::Keyboard::P)) {
if (gameState == Playing) {
gameState = Pause;
} else if (gameState == Pause) {
gameState = Playing;
}
}
// Use potions
if (sf::Keyboard::isKeyPressed(sf::Keyboard::R) && hero.getPotion()->getUseTime() > 0) {
hero.setHealth(hero.getHealth() + hero.getPotion()->getRecovery());
if (hero.getHealth() > 8)
hero.setHealth(8);
hero.getPotion()->setUseTime(hero.getPotion()->getUseTime() - 1);
hero.notify();
}
// Weapon Animation
if (gameState == Playing) {
if (startAnimation) {
if (!isAnimating) {
hero.useWeapon();
isAnimating = true;
halfAnimation = true;
} else {
if (halfAnimation) {
hero.hit();
halfAnimation = false;
isAnimating = false;
startAnimation = false;
}
}
}
// Enemies attack
strategy.EnemyAttack(enemies, elapsedAngry, game.clockAngry, projectile, hero, game.soundShot,
projectileArray);
hero.update();
hero.moveSprite(game.levelVisible);
if (elapsedProjectile.asSeconds() >= 0.25) {
game.clockProjectile.restart();
strategy.HeroShot(hero, game.soundShot, projectileArray, projectile);
}
// NPC attack
strategy.NPCAttack(buddy, elapsedNPC, game.clockNPC, projectile, enemies, game.soundShot, projectileArray);
// Check projectile-enemy collision
strategy.Projectile_EnemyCollision(enemies, projectile, projectileArray, items, game.coinItem);
// Check weapons' collisions
strategy.WeaponsCollisions(enemies, hero, isAnimating, items, game.coinItem, game.swordItem, game.stickItem,
game.axeItem);
// Check Hero - Item collisions
strategy.Hero_ItemsCollisions(hero, items);
// Collisions between NPC & enemies
strategy.NPC_EnemiesCollisions(buddy, enemies);
//Collisions between enemies
strategy.EnemiesCollisions(enemies);
// Hero - enemy projectile collisions
strategy.Hero_ProjectileCollisions(projectileArray, hero);
// Change weapon
if (sf::Keyboard::isKeyPressed(sf::Keyboard::B) && hero.isChangeToSword())
hero.setWeapon(&(game.sword));
if (sf::Keyboard::isKeyPressed(sf::Keyboard::N) && hero.isChangeToStick())
hero.setWeapon(&(game.stick));
if (sf::Keyboard::isKeyPressed(sf::Keyboard::M) && hero.isChangeToAxe())
hero.setWeapon(&(game.axe));
// Update enemies
for (auto itr = enemies.begin(); itr != enemies.end(); itr++) {
enemies[enemyCounter].update();
enemies[enemyCounter].moveSprite(game.levelVisible);
enemyCounter++;
}
// Update NPC
buddy.update();
buddy.moveSprite(game.levelVisible);
}
// Draw Player
window.draw(hero.sprite);
// Draw FireBalls
int counter = 0;
for (auto itr = projectileArray.begin(); itr != projectileArray.end(); itr++) {
projectileArray[counter].update();
window.draw(projectileArray[counter].sprite);
counter++;
}
// Draw Enemies
enemyCounter = 0;
for (auto itr = enemies.begin(); itr != enemies.end(); itr++) {
window.draw(enemies[enemyCounter].sprite);
enemyCounter++;
}
// Draws items
counter = 0;
for (auto itr = items.begin(); itr != items.end(); itr++) {
items[counter].sprite.setPosition(items[counter].rect.getPosition());
window.draw(items[counter].sprite);
counter++;
}
// Draw NPC
window.draw(buddy.sprite);
// Delete dead enemies
counter = 0;
for (auto itr = enemies.begin(); itr != enemies.end(); itr++) {
if (!enemies[counter].isAlive()) {
enemies.erase(itr);
hero.setExp(hero.getExp() + 1);
if (hero.getExp() > 19) {
hero.setExp(0);
}
hero.notify();
break;
}
counter++;
}
// Delete tooken items
counter = 0;
for (auto itr = items.begin(); itr != items.end(); itr++) {
if (items[counter].isTooken()) {
items.erase(itr);
hero.setMoney(hero.getMoney() + items[counter].getValue());
hero.notify();
break;
}
counter++;
}
window.setView(window.getDefaultView());
// Draw Status Bar
window.draw(game.headSprite);
window.draw(game.nameSprite);
window.draw(hero.getHeartsSprite());
window.draw(hero.getExpSprite());
window.draw(game.moneySprite);
window.draw(hero.getWeaponSprite());
window.draw(hero.getPotionsSprite());
window.draw(game.scrollsSprite);
window.draw(name);
window.draw(hero.text);
// Delete projectiles
counter = 0;
for (auto itr = projectileArray.begin(); itr != projectileArray.end(); itr++) {
if (projectileArray[counter].isDestroy()) {
projectileArray.erase(itr);
break;
}
counter++;
}
window.display();
// GAME OVER
if (hero.getHealth() <= 0) {
settings.GameOver(window);
return 0;
}
}
return 0;
}