Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.89 KB | None | 0 0
  1. #include "Player.h"
  2. #include "Bullet.h"
  3. #include "Enemies.h"
  4. #include "Bar.h"
  5. #include "Asteroid.h"
  6. #include "shield.h"
  7. #include "aim.h"
  8. #include <memory>
  9.  
  10. namespace {
  11. const Vector2f MAP_WIDTH = {960, 2270};
  12. const Vector2f MAP_HEIGHT = {530, 2600};
  13. sf::View view;
  14. }
  15.  
  16. struct EnemiesContainer {
  17. std::vector<Object> easyOpponent;
  18. std::vector<Object> mediumOpponent;
  19. std::vector<Object> strongOpponent;
  20. };
  21.  
  22. struct ImageAssets {
  23. Image heroImage;
  24. Image easyEnemyImage;
  25. Image mediumEnemyImage;
  26. Image strongEnemyImage;
  27. Image bulletImage;
  28. Image rocketImage;
  29. Image smartRocketImage;
  30. Image enemyBulletImage;
  31. Image asteroid;
  32. };
  33.  
  34. struct Application {
  35. bool playerShieldIsActive = false;
  36. Clock clock;
  37. Level lvl;
  38. std::list<Entity *> entities;
  39. Bar bar;
  40. Shield shield;
  41. Aim aim;
  42. EnemiesHandler enemiesHandler;
  43. EnemiesContainer enemiesContainer;
  44. PlayerProperties playerProperties;
  45. ImageAssets imageAssets;
  46. MapObjects objects;
  47. Parameters parameters;
  48. };
  49.  
  50. struct PlayerPosition {
  51. Vector2f pos;
  52. };
  53.  
  54. bool IsCollide(Entity *a, Entity *b) {
  55. return (b->position.x - a->position.x) * (b->position.x - a->position.x) +
  56. (b->position.y - a->position.y) * (b->position.y - a->position.y) <
  57. (a->radius + b->radius) * (a->radius + b->radius);
  58. }
  59.  
  60. void getPlayerCoordinateForView(Vector2f position) {
  61. Vector2f centerPosition = {position.x, position.y};
  62. if (position.x < MAP_WIDTH.x) centerPosition.x = MAP_WIDTH.x;
  63. if (position.x > MAP_WIDTH.y) centerPosition.x = MAP_WIDTH.y;
  64. if (position.y < MAP_HEIGHT.x) centerPosition.y = MAP_HEIGHT.x;
  65. if (position.y > MAP_HEIGHT.y) centerPosition.y = MAP_HEIGHT.y;
  66. view.setCenter(centerPosition.x, centerPosition.y);
  67. }
  68.  
  69. float RunTimer(Application &application) {
  70. float time_ms = application.clock.getElapsedTime().asMicroseconds();
  71. application.clock.restart();
  72. time_ms /= 1000;
  73. return time_ms;
  74. }
  75.  
  76. void ProcessEvents(RenderWindow &window, Player &protagonist, PlayerPosition &playerPosition,
  77. Application &application) {
  78. sf::Event event;
  79. while (window.pollEvent(event)) {
  80. if (event.type == Event::Closed || event.key.code == sf::Keyboard::Escape) {
  81. window.close();
  82. }
  83. if (event.type == Event::KeyPressed && event.key.code == sf::Keyboard::R &&
  84. application.playerProperties.shield > 0) {
  85. application.entities.push_back(
  86. new Bullet(application.imageAssets.bulletImage, application.objects,
  87. protagonist.position,
  88. application.playerProperties.playerBullet.SIZE, playerPosition.pos, "Bullet"));
  89. application.playerProperties.shield -= 2;
  90. application.bar.UpdateProtagonist(static_cast<size_t >(protagonist.health),
  91. static_cast<size_t >(application.playerProperties.shield));
  92. }
  93. if (event.type == Event::KeyPressed && event.key.code == sf::Keyboard::F) {
  94. application.playerShieldIsActive = !application.playerShieldIsActive;
  95. }
  96. }
  97. }
  98.  
  99. void GetMousePosition(RenderWindow &window, PlayerPosition &playerPosition) {
  100. Vector2i pixelPos = Mouse::getPosition(window);
  101. playerPosition.pos = window.mapPixelToCoords(pixelPos);
  102. }
  103.  
  104. void InitializeImages(Application &application) {
  105. application.lvl.LoadFromFile("Assets/map1.tmx");
  106. application.imageAssets.bulletImage.loadFromFile("IMG/PlasmaBullet.png");
  107. application.imageAssets.rocketImage.loadFromFile("IMG/rocket1.png");
  108. application.imageAssets.smartRocketImage.loadFromFile("IMG/SmartRocket.png");
  109. application.imageAssets.enemyBulletImage.loadFromFile("IMG/RedPlasmaBullet.png");
  110. application.imageAssets.heroImage.loadFromFile("IMG/8888.png");
  111. application.imageAssets.easyEnemyImage.loadFromFile("IMG/EasyEnemyYellowThrust1.png");
  112. application.imageAssets.mediumEnemyImage.loadFromFile("IMG/MediumEnemyWithGreenThrust.png");
  113. application.imageAssets.strongEnemyImage.loadFromFile("IMG/StrongEnemyWithGreenThrust.png");
  114. application.imageAssets.asteroid.loadFromFile("IMG/Asteroids/strip_rock_type_D.png");
  115. }
  116.  
  117. Object InitializePlayer(Application &application) {
  118. Object player = application.lvl.GetObject("player");
  119. return player;
  120. }
  121.  
  122. bool IsAliveEntity(Entity *entity) {
  123. return !entity->alive;
  124. }
  125.  
  126. bool IsAggro(const Vector2f &protagonistPosition, const Vector2f &enemyPosition, const size_t &distance) {
  127. return ((abs(protagonistPosition.x - enemyPosition.x)) < distance &&
  128. (abs(protagonistPosition.y - enemyPosition.y)) < distance);
  129. }
  130.  
  131. void AppendEnemiesBullets(Application &application, Entity *it, Player &protagonist, float &localTime) {
  132. if ((it->name == "easyEnemy") &&
  133. IsAggro(protagonist.position, it->position, application.enemiesHandler.easyEnemy.AGGRO_DISTANCE) &&
  134. localTime >= 35000 && localTime <= 50000) {
  135. application.entities.push_back(
  136. new Bullet(application.imageAssets.enemyBulletImage, application.objects, it->position,
  137. application.enemiesHandler.easyEnemy.easyEnemyBullet.SIZE,
  138. protagonist.position, "EnemyBullet"));
  139. localTime += 15000;
  140. }
  141.  
  142. else if ((it->name == "mediumEnemy") &&
  143. IsAggro(protagonist.position, it->position, application.enemiesHandler.mediumEnemy.AGGRO_DISTANCE) &&
  144. localTime >= 70000 && localTime <= 86000) {
  145. application.entities.push_back(
  146. new Rocket(application.imageAssets.rocketImage, application.objects,
  147. it->position, application.enemiesHandler.mediumEnemy.simpleRocket.SIZE,
  148. protagonist.position, "EnemyRocket"));
  149. localTime += 16000;
  150. }
  151.  
  152. else if ((it->name == "strongEnemy") &&
  153. IsAggro(protagonist.position, it->position, application.enemiesHandler.hardEnemy.AGGRO_DISTANCE)) {
  154.  
  155. if (localTime >= 50000 && localTime <= 70000) {
  156. application.entities.push_back(
  157. new Bullet(application.imageAssets.enemyBulletImage, application.objects,
  158. it->position, application.enemiesHandler.easyEnemy.easyEnemyBullet.SIZE,
  159. protagonist.position, "EnemyBullet"));
  160. localTime += 20000;
  161. }
  162.  
  163. if (localTime >= 86000 && localTime <= 100000) {
  164. application.entities.push_back(
  165. new SmartRocket(application.imageAssets.smartRocketImage, application.objects,
  166. it->position, application.enemiesHandler.hardEnemy.smartRocket.SIZE,
  167. protagonist.position, "EnemySmartRocket"));
  168. localTime += 14000;
  169. }
  170.  
  171. }
  172.  
  173. if (localTime > 100000) {
  174. localTime = 0.f;
  175. }
  176.  
  177. }
  178.  
  179. void ProcessEntities(float &time_ms, Application &application, Player &protagonist) {
  180. auto new_end = std::remove_if(application.entities.begin(), application.entities.end(), IsAliveEntity);
  181. application.entities.erase(new_end, application.entities.end());
  182. static float localTime = 0.f;
  183. for (auto it : application.entities) {
  184. localTime += time_ms;
  185. AppendEnemiesBullets(application, it, protagonist, localTime);
  186. it->Update(time_ms, application.objects);
  187. }
  188. }
  189.  
  190. bool IsShieldActive(const Application &application) {
  191. return application.playerShieldIsActive && application.playerProperties.shield > 0;
  192. }
  193.  
  194. bool IsBullet(const string &name) {
  195. return (name == "Bullet" || name == "EnemyBullet" || name == "EnemyRocket" || name == "EnemySmartRocket");
  196. }
  197.  
  198. bool IsEnemy(const string &name) {
  199. return (name == "easyEnemy" || name == "mediumEnemy" /*|| name == "strongEnemy"*/);
  200. }
  201.  
  202. void ProcessDamage(Player &protagonist, Application &application) {
  203. for (auto it : application.entities) {
  204. for (auto at : application.entities) {
  205. if (it->RetRect().intersects(at->RetRect())) {
  206.  
  207. if (((at->name == "Bullet") &&
  208. (IsEnemy(it->name)))) {
  209. it->health -= application.playerProperties.playerBullet.DAMAGE;
  210. at->alive = false;
  211. application.bar.UpdateEnemy(static_cast<size_t >(it->health), it->name);
  212. }
  213.  
  214. else if (at->name == "Asteroid") {
  215. if (it->name != "Asteroid") {
  216. if (IsBullet(it->name)) {
  217. it->alive = false;
  218. }
  219. else {
  220. it->health -= 100;
  221. }
  222.  
  223. at->name = "explosion";
  224. }
  225. }
  226.  
  227. }
  228. if (IsCollide(it, at) /*&& ((it->name == "Bullet" && at->name == "strongEnemy") ||
  229. (at->name == "Bullet" && it->name == "strongEnemy"))*/) {
  230. if ((it->name == "Bullet" && at->name == "strongEnemy")) {
  231. at->health -= application.playerProperties.playerBullet.DAMAGE;
  232. it->alive = false;
  233. application.bar.UpdateEnemy(static_cast<size_t >(it->health), it->name);
  234. }
  235. else if ((at->name == "Bullet" && it->name == "strongEnemy")) {
  236.  
  237. it->health -= application.playerProperties.playerBullet.DAMAGE;
  238. at->alive = false;
  239. application.bar.UpdateEnemy(static_cast<size_t >(it->health), it->name);
  240. }
  241.  
  242. }
  243.  
  244. }
  245. if (it->RetRect().intersects(protagonist.RetRect())) {
  246. if (it->name == "EnemyBullet") {
  247. if (IsShieldActive(application)) {
  248. application.playerProperties.shield -=
  249. application.enemiesHandler.easyEnemy.easyEnemyBullet.DAMAGE / 2;
  250. }
  251. else {
  252. protagonist.health -= application.enemiesHandler.easyEnemy.easyEnemyBullet.DAMAGE;
  253. }
  254. it->alive = false;
  255. }
  256. else if (it->name == "EnemyRocket" || it->name == "EnemySmartRocket") {
  257. if (IsShieldActive(application)) {
  258. application.playerProperties.shield -=
  259. application.enemiesHandler.mediumEnemy.simpleRocket.DAMAGE / 2;
  260. }
  261. else {
  262. protagonist.health -= application.enemiesHandler.mediumEnemy.simpleRocket.DAMAGE;
  263. }
  264. it->name = "explosion";
  265. }
  266. else if (it->name == "easyEnemy" || it->name == "mediumEnemy" || it->name == "strongEnemy") {
  267. it->boost.x = 0;
  268. it->boost.y = 0;
  269. it->health -= (application.enemiesHandler.easyEnemy.COLLISION_DAMAGE +
  270. abs(static_cast<long>(it->velocity.x + it->velocity.y) / 2));
  271. if (application.playerProperties.shield > 0 && application.playerShieldIsActive) {
  272. application.playerProperties.shield -= (application.enemiesHandler.easyEnemy.COLLISION_DAMAGE +
  273. abs(static_cast<long>(it->velocity.x + it->velocity.y) /
  274. 2));
  275. }
  276. else {
  277. protagonist.health -= (application.enemiesHandler.easyEnemy.COLLISION_DAMAGE +
  278. abs(static_cast<long>(it->velocity.x + it->velocity.y) / 2));
  279. }
  280. application.bar.UpdateEnemy(static_cast<size_t>(it->health), it->name);
  281. }
  282. else if (it->name == "ShieldReward") {
  283. application.playerProperties.shield += 30;
  284. it->alive = false;
  285. }
  286. else if (it->name == "HealthReward") {
  287. protagonist.health += 30;
  288. it->alive = false;
  289. }
  290. application.bar.UpdateProtagonist(static_cast<size_t>(protagonist.health),
  291. static_cast<size_t>(application.playerProperties.shield));
  292. }
  293. }
  294. }
  295.  
  296. void AppendEnemies(PlayerPosition &playerPosition, Player &protagonist, Application &application) {
  297.  
  298. for (int i = 0; i < application.enemiesContainer.easyOpponent.size(); i++) {
  299. application.entities.push_back(
  300. new CEasyEnemy(application.imageAssets.easyEnemyImage, application.objects,
  301. {application.enemiesContainer.easyOpponent[i].rect.left,
  302. application.enemiesContainer.easyOpponent[i].rect.top},
  303. application.enemiesHandler.easyEnemy.SIZE, playerPosition.pos,
  304. "easyEnemy"));
  305. }
  306.  
  307. for (int i = 0; i < application.enemiesContainer.mediumOpponent.size(); i++) {
  308. application.entities.push_back(
  309. new CMediumEnemy(application.imageAssets.mediumEnemyImage, application.objects,
  310. {application.enemiesContainer.mediumOpponent[i].rect.left,
  311. application.enemiesContainer.mediumOpponent[i].rect.top},
  312. application.enemiesHandler.mediumEnemy.SIZE, playerPosition.pos,
  313. "mediumEnemy"));
  314. }
  315.  
  316. for (int i = 0; i < application.enemiesContainer.strongOpponent.size(); i++) {
  317. application.entities.push_back(
  318. new CStrongEnemy(application.imageAssets.strongEnemyImage, application.objects,
  319. {application.enemiesContainer.strongOpponent[i].rect.left,
  320. application.enemiesContainer.strongOpponent[i].rect.top},
  321. application.enemiesHandler.hardEnemy.SIZE, playerPosition.pos,
  322. "strongEnemy"));
  323. }
  324.  
  325. }
  326.  
  327. void CheckExistenceProtagonist(Player &protagonist, RenderWindow &window) {
  328. if (!protagonist.alive) {
  329. window.close();
  330. cout << "game over\n";
  331. }
  332.  
  333. }
  334.  
  335. void AppendAsteroids(size_t amount, Application &application) {
  336. for (size_t i = 0; i < amount; ++i) {
  337. application.entities.push_back(
  338. new Asteroid(application.imageAssets.asteroid,
  339. {rand() % (application.parameters.MAP_SIZE.first - 20 + 1) + 20,
  340. rand() % (application.parameters.MAP_SIZE.second - 10 + 1) + 10}, {65, 64},
  341. "Asteroid"));
  342. }
  343. }
  344.  
  345. void Draw(RenderWindow &window, Player &protagonist, Application &application) {
  346. window.clear();
  347. application.lvl.Draw(window);
  348. for (auto it : application.entities) {
  349. window.draw((it)->sprite);
  350. }
  351. window.draw(protagonist.sprite);
  352. if (application.playerShieldIsActive && application.playerProperties.shield > 0) {
  353. application.shield.Draw(window, protagonist.position);
  354. }
  355. application.bar.Draw(window);
  356. application.aim.Draw(window);
  357. window.display();
  358. }
  359.  
  360. int main() {
  361. Parameters parameters;
  362. Application application;
  363. sf::RenderWindow window(sf::VideoMode(parameters.WINDOW_SIZE.first, parameters.WINDOW_SIZE.second), "Game");
  364. window.setMouseCursorVisible(false);
  365. window.setFramerateLimit(60);
  366. window.setVerticalSyncEnabled(true);
  367. view.reset(sf::FloatRect(0, 0, parameters.WINDOW_SIZE.first, parameters.WINDOW_SIZE.second));
  368. PlayerPosition playerPosition;
  369. InitializeImages(application);
  370. Object player = InitializePlayer(application);
  371. AppendAsteroids(0, application);
  372. // application.enemiesContainer.easyOpponent = application.lvl.GetObjects("easyEnemy");
  373. // application.enemiesContainer.mediumOpponent = application.lvl.GetObjects("mediumEnemy");
  374. application.enemiesContainer.strongOpponent = application.lvl.GetObjects("hardEnemy");
  375. Player protagonist(application.imageAssets.heroImage, {player.rect.left, player.rect.top},
  376. application.playerProperties.SIZE, "player");
  377. application.entities.push_back(&protagonist);
  378. AppendEnemies(playerPosition, protagonist, application);
  379. while (window.isOpen()) {
  380. GetMousePosition(window, playerPosition);
  381. float time_ms = RunTimer(application);
  382. ProcessEvents(window, protagonist, playerPosition, application);
  383. protagonist.rotation_GG(playerPosition.pos);
  384. protagonist.Update(time_ms, application.objects);
  385. ProcessEntities(time_ms, application, protagonist);
  386. ProcessDamage(protagonist, application);
  387. CheckExistenceProtagonist(protagonist, window);
  388. window.setView(view);
  389. Draw(window, protagonist, application);
  390. }
  391. return 0;
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement