Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Player.h"
- #include "Bullet.h"
- #include "Enemies.h"
- #include "Bar.h"
- #include "Asteroid.h"
- #include "shield.h"
- #include "aim.h"
- #include <memory>
- namespace {
- const Vector2f MAP_WIDTH = {960, 2270};
- const Vector2f MAP_HEIGHT = {530, 2600};
- sf::View view;
- }
- struct EnemiesContainer {
- std::vector<Object> easyOpponent;
- std::vector<Object> mediumOpponent;
- std::vector<Object> strongOpponent;
- };
- struct ImageAssets {
- Image heroImage;
- Image easyEnemyImage;
- Image mediumEnemyImage;
- Image strongEnemyImage;
- Image bulletImage;
- Image rocketImage;
- Image smartRocketImage;
- Image enemyBulletImage;
- Image asteroid;
- };
- struct Application {
- bool playerShieldIsActive = false;
- Clock clock;
- Level lvl;
- std::list<Entity *> entities;
- Bar bar;
- Shield shield;
- Aim aim;
- EnemiesHandler enemiesHandler;
- EnemiesContainer enemiesContainer;
- PlayerProperties playerProperties;
- ImageAssets imageAssets;
- MapObjects objects;
- Parameters parameters;
- };
- struct PlayerPosition {
- Vector2f pos;
- };
- bool IsCollide(Entity *a, Entity *b) {
- return (b->position.x - a->position.x) * (b->position.x - a->position.x) +
- (b->position.y - a->position.y) * (b->position.y - a->position.y) <
- (a->radius + b->radius) * (a->radius + b->radius);
- }
- void getPlayerCoordinateForView(Vector2f position) {
- Vector2f centerPosition = {position.x, position.y};
- if (position.x < MAP_WIDTH.x) centerPosition.x = MAP_WIDTH.x;
- if (position.x > MAP_WIDTH.y) centerPosition.x = MAP_WIDTH.y;
- if (position.y < MAP_HEIGHT.x) centerPosition.y = MAP_HEIGHT.x;
- if (position.y > MAP_HEIGHT.y) centerPosition.y = MAP_HEIGHT.y;
- view.setCenter(centerPosition.x, centerPosition.y);
- }
- float RunTimer(Application &application) {
- float time_ms = application.clock.getElapsedTime().asMicroseconds();
- application.clock.restart();
- time_ms /= 1000;
- return time_ms;
- }
- void ProcessEvents(RenderWindow &window, Player &protagonist, PlayerPosition &playerPosition,
- Application &application) {
- sf::Event event;
- while (window.pollEvent(event)) {
- if (event.type == Event::Closed || event.key.code == sf::Keyboard::Escape) {
- window.close();
- }
- if (event.type == Event::KeyPressed && event.key.code == sf::Keyboard::R &&
- application.playerProperties.shield > 0) {
- application.entities.push_back(
- new Bullet(application.imageAssets.bulletImage, application.objects,
- protagonist.position,
- application.playerProperties.playerBullet.SIZE, playerPosition.pos, "Bullet"));
- application.playerProperties.shield -= 2;
- application.bar.UpdateProtagonist(static_cast<size_t >(protagonist.health),
- static_cast<size_t >(application.playerProperties.shield));
- }
- if (event.type == Event::KeyPressed && event.key.code == sf::Keyboard::F) {
- application.playerShieldIsActive = !application.playerShieldIsActive;
- }
- }
- }
- void GetMousePosition(RenderWindow &window, PlayerPosition &playerPosition) {
- Vector2i pixelPos = Mouse::getPosition(window);
- playerPosition.pos = window.mapPixelToCoords(pixelPos);
- }
- void InitializeImages(Application &application) {
- application.lvl.LoadFromFile("Assets/map1.tmx");
- application.imageAssets.bulletImage.loadFromFile("IMG/PlasmaBullet.png");
- application.imageAssets.rocketImage.loadFromFile("IMG/rocket1.png");
- application.imageAssets.smartRocketImage.loadFromFile("IMG/SmartRocket.png");
- application.imageAssets.enemyBulletImage.loadFromFile("IMG/RedPlasmaBullet.png");
- application.imageAssets.heroImage.loadFromFile("IMG/8888.png");
- application.imageAssets.easyEnemyImage.loadFromFile("IMG/EasyEnemyYellowThrust1.png");
- application.imageAssets.mediumEnemyImage.loadFromFile("IMG/MediumEnemyWithGreenThrust.png");
- application.imageAssets.strongEnemyImage.loadFromFile("IMG/StrongEnemyWithGreenThrust.png");
- application.imageAssets.asteroid.loadFromFile("IMG/Asteroids/strip_rock_type_D.png");
- }
- Object InitializePlayer(Application &application) {
- Object player = application.lvl.GetObject("player");
- return player;
- }
- bool IsAliveEntity(Entity *entity) {
- return !entity->alive;
- }
- bool IsAggro(const Vector2f &protagonistPosition, const Vector2f &enemyPosition, const size_t &distance) {
- return ((abs(protagonistPosition.x - enemyPosition.x)) < distance &&
- (abs(protagonistPosition.y - enemyPosition.y)) < distance);
- }
- void AppendEnemiesBullets(Application &application, Entity *it, Player &protagonist, float &localTime) {
- if ((it->name == "easyEnemy") &&
- IsAggro(protagonist.position, it->position, application.enemiesHandler.easyEnemy.AGGRO_DISTANCE) &&
- localTime >= 35000 && localTime <= 50000) {
- application.entities.push_back(
- new Bullet(application.imageAssets.enemyBulletImage, application.objects, it->position,
- application.enemiesHandler.easyEnemy.easyEnemyBullet.SIZE,
- protagonist.position, "EnemyBullet"));
- localTime += 15000;
- }
- else if ((it->name == "mediumEnemy") &&
- IsAggro(protagonist.position, it->position, application.enemiesHandler.mediumEnemy.AGGRO_DISTANCE) &&
- localTime >= 70000 && localTime <= 86000) {
- application.entities.push_back(
- new Rocket(application.imageAssets.rocketImage, application.objects,
- it->position, application.enemiesHandler.mediumEnemy.simpleRocket.SIZE,
- protagonist.position, "EnemyRocket"));
- localTime += 16000;
- }
- else if ((it->name == "strongEnemy") &&
- IsAggro(protagonist.position, it->position, application.enemiesHandler.hardEnemy.AGGRO_DISTANCE)) {
- if (localTime >= 50000 && localTime <= 70000) {
- application.entities.push_back(
- new Bullet(application.imageAssets.enemyBulletImage, application.objects,
- it->position, application.enemiesHandler.easyEnemy.easyEnemyBullet.SIZE,
- protagonist.position, "EnemyBullet"));
- localTime += 20000;
- }
- if (localTime >= 86000 && localTime <= 100000) {
- application.entities.push_back(
- new SmartRocket(application.imageAssets.smartRocketImage, application.objects,
- it->position, application.enemiesHandler.hardEnemy.smartRocket.SIZE,
- protagonist.position, "EnemySmartRocket"));
- localTime += 14000;
- }
- }
- if (localTime > 100000) {
- localTime = 0.f;
- }
- }
- void ProcessEntities(float &time_ms, Application &application, Player &protagonist) {
- auto new_end = std::remove_if(application.entities.begin(), application.entities.end(), IsAliveEntity);
- application.entities.erase(new_end, application.entities.end());
- static float localTime = 0.f;
- for (auto it : application.entities) {
- localTime += time_ms;
- AppendEnemiesBullets(application, it, protagonist, localTime);
- it->Update(time_ms, application.objects);
- }
- }
- bool IsShieldActive(const Application &application) {
- return application.playerShieldIsActive && application.playerProperties.shield > 0;
- }
- bool IsBullet(const string &name) {
- return (name == "Bullet" || name == "EnemyBullet" || name == "EnemyRocket" || name == "EnemySmartRocket");
- }
- bool IsEnemy(const string &name) {
- return (name == "easyEnemy" || name == "mediumEnemy" /*|| name == "strongEnemy"*/);
- }
- void ProcessDamage(Player &protagonist, Application &application) {
- for (auto it : application.entities) {
- for (auto at : application.entities) {
- if (it->RetRect().intersects(at->RetRect())) {
- if (((at->name == "Bullet") &&
- (IsEnemy(it->name)))) {
- it->health -= application.playerProperties.playerBullet.DAMAGE;
- at->alive = false;
- application.bar.UpdateEnemy(static_cast<size_t >(it->health), it->name);
- }
- else if (at->name == "Asteroid") {
- if (it->name != "Asteroid") {
- if (IsBullet(it->name)) {
- it->alive = false;
- }
- else {
- it->health -= 100;
- }
- at->name = "explosion";
- }
- }
- }
- if (IsCollide(it, at) /*&& ((it->name == "Bullet" && at->name == "strongEnemy") ||
- (at->name == "Bullet" && it->name == "strongEnemy"))*/) {
- if ((it->name == "Bullet" && at->name == "strongEnemy")) {
- at->health -= application.playerProperties.playerBullet.DAMAGE;
- it->alive = false;
- application.bar.UpdateEnemy(static_cast<size_t >(it->health), it->name);
- }
- else if ((at->name == "Bullet" && it->name == "strongEnemy")) {
- it->health -= application.playerProperties.playerBullet.DAMAGE;
- at->alive = false;
- application.bar.UpdateEnemy(static_cast<size_t >(it->health), it->name);
- }
- }
- }
- if (it->RetRect().intersects(protagonist.RetRect())) {
- if (it->name == "EnemyBullet") {
- if (IsShieldActive(application)) {
- application.playerProperties.shield -=
- application.enemiesHandler.easyEnemy.easyEnemyBullet.DAMAGE / 2;
- }
- else {
- protagonist.health -= application.enemiesHandler.easyEnemy.easyEnemyBullet.DAMAGE;
- }
- it->alive = false;
- }
- else if (it->name == "EnemyRocket" || it->name == "EnemySmartRocket") {
- if (IsShieldActive(application)) {
- application.playerProperties.shield -=
- application.enemiesHandler.mediumEnemy.simpleRocket.DAMAGE / 2;
- }
- else {
- protagonist.health -= application.enemiesHandler.mediumEnemy.simpleRocket.DAMAGE;
- }
- it->name = "explosion";
- }
- else if (it->name == "easyEnemy" || it->name == "mediumEnemy" || it->name == "strongEnemy") {
- it->boost.x = 0;
- it->boost.y = 0;
- it->health -= (application.enemiesHandler.easyEnemy.COLLISION_DAMAGE +
- abs(static_cast<long>(it->velocity.x + it->velocity.y) / 2));
- if (application.playerProperties.shield > 0 && application.playerShieldIsActive) {
- application.playerProperties.shield -= (application.enemiesHandler.easyEnemy.COLLISION_DAMAGE +
- abs(static_cast<long>(it->velocity.x + it->velocity.y) /
- 2));
- }
- else {
- protagonist.health -= (application.enemiesHandler.easyEnemy.COLLISION_DAMAGE +
- abs(static_cast<long>(it->velocity.x + it->velocity.y) / 2));
- }
- application.bar.UpdateEnemy(static_cast<size_t>(it->health), it->name);
- }
- else if (it->name == "ShieldReward") {
- application.playerProperties.shield += 30;
- it->alive = false;
- }
- else if (it->name == "HealthReward") {
- protagonist.health += 30;
- it->alive = false;
- }
- application.bar.UpdateProtagonist(static_cast<size_t>(protagonist.health),
- static_cast<size_t>(application.playerProperties.shield));
- }
- }
- }
- void AppendEnemies(PlayerPosition &playerPosition, Player &protagonist, Application &application) {
- for (int i = 0; i < application.enemiesContainer.easyOpponent.size(); i++) {
- application.entities.push_back(
- new CEasyEnemy(application.imageAssets.easyEnemyImage, application.objects,
- {application.enemiesContainer.easyOpponent[i].rect.left,
- application.enemiesContainer.easyOpponent[i].rect.top},
- application.enemiesHandler.easyEnemy.SIZE, playerPosition.pos,
- "easyEnemy"));
- }
- for (int i = 0; i < application.enemiesContainer.mediumOpponent.size(); i++) {
- application.entities.push_back(
- new CMediumEnemy(application.imageAssets.mediumEnemyImage, application.objects,
- {application.enemiesContainer.mediumOpponent[i].rect.left,
- application.enemiesContainer.mediumOpponent[i].rect.top},
- application.enemiesHandler.mediumEnemy.SIZE, playerPosition.pos,
- "mediumEnemy"));
- }
- for (int i = 0; i < application.enemiesContainer.strongOpponent.size(); i++) {
- application.entities.push_back(
- new CStrongEnemy(application.imageAssets.strongEnemyImage, application.objects,
- {application.enemiesContainer.strongOpponent[i].rect.left,
- application.enemiesContainer.strongOpponent[i].rect.top},
- application.enemiesHandler.hardEnemy.SIZE, playerPosition.pos,
- "strongEnemy"));
- }
- }
- void CheckExistenceProtagonist(Player &protagonist, RenderWindow &window) {
- if (!protagonist.alive) {
- window.close();
- cout << "game over\n";
- }
- }
- void AppendAsteroids(size_t amount, Application &application) {
- for (size_t i = 0; i < amount; ++i) {
- application.entities.push_back(
- new Asteroid(application.imageAssets.asteroid,
- {rand() % (application.parameters.MAP_SIZE.first - 20 + 1) + 20,
- rand() % (application.parameters.MAP_SIZE.second - 10 + 1) + 10}, {65, 64},
- "Asteroid"));
- }
- }
- void Draw(RenderWindow &window, Player &protagonist, Application &application) {
- window.clear();
- application.lvl.Draw(window);
- for (auto it : application.entities) {
- window.draw((it)->sprite);
- }
- window.draw(protagonist.sprite);
- if (application.playerShieldIsActive && application.playerProperties.shield > 0) {
- application.shield.Draw(window, protagonist.position);
- }
- application.bar.Draw(window);
- application.aim.Draw(window);
- window.display();
- }
- int main() {
- Parameters parameters;
- Application application;
- sf::RenderWindow window(sf::VideoMode(parameters.WINDOW_SIZE.first, parameters.WINDOW_SIZE.second), "Game");
- window.setMouseCursorVisible(false);
- window.setFramerateLimit(60);
- window.setVerticalSyncEnabled(true);
- view.reset(sf::FloatRect(0, 0, parameters.WINDOW_SIZE.first, parameters.WINDOW_SIZE.second));
- PlayerPosition playerPosition;
- InitializeImages(application);
- Object player = InitializePlayer(application);
- AppendAsteroids(0, application);
- // application.enemiesContainer.easyOpponent = application.lvl.GetObjects("easyEnemy");
- // application.enemiesContainer.mediumOpponent = application.lvl.GetObjects("mediumEnemy");
- application.enemiesContainer.strongOpponent = application.lvl.GetObjects("hardEnemy");
- Player protagonist(application.imageAssets.heroImage, {player.rect.left, player.rect.top},
- application.playerProperties.SIZE, "player");
- application.entities.push_back(&protagonist);
- AppendEnemies(playerPosition, protagonist, application);
- while (window.isOpen()) {
- GetMousePosition(window, playerPosition);
- float time_ms = RunTimer(application);
- ProcessEvents(window, protagonist, playerPosition, application);
- protagonist.rotation_GG(playerPosition.pos);
- protagonist.Update(time_ms, application.objects);
- ProcessEntities(time_ms, application, protagonist);
- ProcessDamage(protagonist, application);
- CheckExistenceProtagonist(protagonist, window);
- window.setView(view);
- Draw(window, protagonist, application);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement