BiggerRadius

my Code

Apr 23rd, 2025 (edited)
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.78 KB | None | 0 0
  1. //main.cpp
  2. #include "gameManager.h"
  3. /*cmake -G "MinGW Makefiles" ..*/
  4. int main() {
  5.     GameManager gameManager;
  6.     if (!gameManager.initialize()) {
  7.         return 1;
  8.     }
  9.     gameManager.runGameLoop();
  10.     return 0;
  11. }
  12.  
  13. //entityImplementation.cpp
  14. #include <SFML/Graphics.hpp>
  15. #include "entity.h"
  16.  
  17.     Entity::Entity(sf::Texture &texture,sf::Vector2f position):sprite(texture){
  18.         sprite.setPosition(position);
  19.     }
  20.     void Entity::setPos(sf::Vector2f position){
  21.         sprite.setPosition(position);
  22.     }
  23.     sf::Vector2f Entity::getPos(){
  24.         return sprite.getPosition();
  25.     }
  26.     void Entity::setTexture(sf::Texture& texture){
  27.         sprite.setTexture(texture);
  28.     }
  29.     void Entity::draw(sf::RenderWindow& window) {
  30.         window.draw(this->sprite);
  31.     }
  32.     sf::Vector2f Entity::getSpeed(){
  33.         return speed;
  34.     }
  35.     void Entity::setSpeed(sf::Vector2f newSpeed){
  36.         speed = newSpeed;
  37.     }
  38.      Entity::~Entity(){
  39.      };
  40. //player.cpp
  41. #include "entity.h"
  42. #include "player.h"
  43. #include <SFML/Graphics.hpp>
  44.  
  45. Player::Player(sf::Texture& texture, sf::Vector2f position) : Entity(texture, position){
  46.     setSpeed({1.0,1.0});
  47. }
  48. //world.cpp
  49. #include "world.h"
  50. #include "player.h"
  51. #include "memory.h"
  52.  
  53. World::World(std::unique_ptr<Entity> player):player(std::move(player)){
  54. }
  55.  
  56. World::World() : player(nullptr) {}
  57.  
  58. Entity* World::getPlayer() const {
  59.     return player.get();
  60. }
  61.  
  62. void World::update(float dtSeconds,sf::Vector2f movementInput){
  63.     Entity* player = this->getPlayer();
  64.     player->setPos((player->getPos() + movementInput));
  65. }
  66. void World::draw(sf::RenderWindow& window){
  67.     player->draw(window);
  68. }
  69. //entityManager.cpp
  70. #include <string>
  71. #include "entity.h"
  72. #include <SFML/Graphics.hpp>
  73. #include "entityManager.h"
  74. #include <memory>
  75. #include <iostream>
  76. #include "player.h"
  77.  
  78. sf::Texture* EntityManager::loadTexture(std::string path) {
  79.         auto it = textureMap.find(path);
  80.         if (it != textureMap.end()) {
  81.             return &it->second;
  82.         } else {
  83.             sf::Texture newTexture;
  84.             if (newTexture.loadFromFile(path)) {
  85.                 textureMap[path] = newTexture;
  86.                 return &textureMap[path];
  87.             } else {
  88.                 throw std::runtime_error("Error loading texture: " + path);
  89.             }
  90.         }
  91.     }
  92.     std::unique_ptr<Entity> EntityManager::entityFactory(std::string entityType,std::string path,sf::Vector2f position){
  93.         sf::Texture* texture = loadTexture(path);
  94.         if (entityType == "Player"){
  95.             return std::make_unique<Player>(*texture,position);
  96.         }
  97.         throw std::runtime_error("Error loading texture: " + path + " of type: " + entityType);
  98.     }    
  99. //gameManager.cpp
  100. #include "gameManager.h"
  101. #include <iostream>
  102. #include <stdexcept>
  103. #include <SFML/Graphics.hpp>
  104. #include <cmath>        
  105. #include <map>
  106. #include <string>
  107. GameManager::GameManager() : initialization_failed(false), window(sf::VideoMode({1920u, 1080u}), "Game") {}
  108. GameManager::~GameManager() {}
  109.  
  110. bool GameManager::initialize() {
  111.     initialization_failed = false;
  112.     try {
  113.         player = entityManager.entityFactory("Player", "build/assets/texture/cube.png", sf::Vector2f(250, 250));
  114.         world = World(std::move(player));
  115.     } catch (const std::runtime_error& error) {
  116.         std::cerr << "Error creating game object: " << error.what() << std::endl;
  117.         initialization_failed = true;
  118.     }
  119.     if (initialization_failed) {
  120.         std::cerr << "Game initialization failed. Exiting." << std::endl;
  121.         return false;
  122.     }
  123.     window.setFramerateLimit(144);
  124.     return true;
  125. }
  126.  
  127. sf::RenderWindow& GameManager::getWindow() {
  128.     return window;
  129. }
  130. void GameManager::runGameLoop() {
  131.     auto player = world.getPlayer();
  132.     std::map <std::string,bool> inputs;
  133.     sf::Vector2f movementInput;
  134.     auto InputToMovementDirection = [=] (std::map<std::string,bool> inputs){
  135.         sf::Vector2f direction;
  136.         for (auto input: inputs){
  137.             if(input.second){
  138.                 if(input.first == "pressingW"){
  139.                     direction.y -= player->getSpeed().y;
  140.                 }
  141.                 if(input.first == "pressingA"){
  142.                     direction.x -= player->getSpeed().x;
  143.                 }
  144.                 if(input.first == "pressingS"){
  145.                     direction.y += player->getSpeed().y;
  146.                 }
  147.                 if(input.first == "pressingD"){
  148.                     direction.x += player->getSpeed().x;
  149.                 }
  150.                 if(input.first == "pressingLeftMouse"){
  151.                     //reserved
  152.                 }
  153.                 if(input.first == "pressingRightMouse"){
  154.                     //reserved
  155.                 }
  156.             }  
  157.         }
  158.         return direction;
  159.     };
  160.     while (window.isOpen()) {
  161.         while (const std::optional event = window.pollEvent()) {
  162.             if (event->is<sf::Event::Closed>()) {
  163.                 window.close();
  164.             }
  165.         }
  166.         inputs["pressingW"] =  sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W);
  167.         inputs["pressingA"] = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A);
  168.         inputs["pressingS"] =  sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S);
  169.         inputs["pressingD"] = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D);
  170.         inputs["pressingLeftMouse"] = sf::Mouse::isButtonPressed(sf::Mouse::Button::Left);
  171.         inputs["pressingRightMouse"] = sf::Mouse::isButtonPressed(sf::Mouse::Button::Right);
  172.         movementInput = InputToMovementDirection(inputs);
  173.         sf::Time deltaTime = clock.restart();
  174.         float dtSeconds = deltaTime.asSeconds();
  175.         world.update(dtSeconds, movementInput);
  176.         window.clear();
  177.         world.draw(window);
  178.         window.display();
  179.     }
  180. }
  181.  
  182.  
Advertisement
Add Comment
Please, Sign In to add comment