Advertisement
beefviper

XMLGameEngine - engine.cpp

Sep 26th, 2020 (edited)
1,422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. // engine.cpp
  2. // XML Game Engine
  3. // author: beefviper
  4. // date: Sept 18, 2020
  5.  
  6. #include "engine.h"
  7.  
  8. xge::Engine::Engine(xge::Game& game) :
  9.     game(game)
  10. {
  11.     xge::WindowDesc windowDesc = game.getWindowDesc();
  12.  
  13.     std::string name = windowDesc.name;
  14.  
  15.     int width = static_cast<int>(windowDesc.width);
  16.     int height = static_cast<int>(windowDesc.height);
  17.     sf::VideoMode videoMode(width, height);
  18.  
  19.     auto windowMode = (windowDesc.fullscreen == "true") ? sf::Style::Fullscreen : sf::Style::Default;
  20.  
  21.     window.create(videoMode, name, windowMode);
  22.     window.setFramerateLimit(windowDesc.framerate);
  23. }
  24.  
  25. sf::RenderWindow& xge::Engine::getWindow(void)
  26. {
  27.     return window;
  28. }
  29.  
  30. bool xge::Engine::isRunning(void)
  31. {
  32.     return window.isOpen();
  33. }
  34.  
  35. void xge::Engine::loop(void)
  36. {
  37.     while (window.isOpen())
  38.     {
  39.         sf::Event event;
  40.         while (window.pollEvent(event))
  41.         {
  42.             switch (event.type)
  43.             {
  44.             case sf::Event::Closed:
  45.                 window.close();
  46.                 break;
  47.  
  48.             case sf::Event::KeyPressed:
  49.                 handleKeyPressed(game, event);
  50.                 break;
  51.  
  52.             case sf::Event::KeyReleased:
  53.                 handleKeyReleased(game, event);
  54.                 break;
  55.  
  56.             default:
  57.                 break;
  58.             }
  59.         }
  60.  
  61.         game.updateObjects();
  62.  
  63.         window.clear(xge::sfmlColor(game.getWindowDesc().background));
  64.  
  65.         for (auto name : game.getCurrentState().show)
  66.         {
  67.             window.draw(*game.getObject(name).sprite);
  68.         }
  69.  
  70.         window.display();
  71.     }
  72. }
  73.  
  74. void xge::Engine::handleKeyPressed(xge::Game& game, sf::Event& event)
  75. {
  76.     if (!isKeyPressed[event.key.code])
  77.     {
  78.         isKeyPressed[event.key.code] = true;
  79.  
  80.         for (auto input : game.getCurrentState().input)
  81.         {
  82.             execute_action(game, event, input, true);
  83.         }
  84.     }
  85. }
  86.  
  87. void xge::Engine::handleKeyReleased(xge::Game& game, sf::Event& event)
  88. {
  89.  
  90.     isKeyPressed[event.key.code] = false;
  91.  
  92.     for (auto input : game.getCurrentState().input)
  93.     {
  94.         execute_action(game, event, input, false);
  95.     }
  96. }
  97.  
  98. void xge::Engine::execute_action(xge::Game& game, sf::Event& event, PairStringString& input, bool keyPressed)
  99. {
  100.     if (input.second == sfmlKeyToString(event.key.code))
  101.     {
  102.  
  103.         std::string aObject = getObjectFromInput(input);
  104.         std::string aCommand = getCommandFromInput(input);
  105.  
  106.         std::string aAction;
  107.  
  108.         if (aObject == "state")
  109.         {
  110.             if (keyPressed)
  111.             {
  112.                 game.setCurrentState(aCommand);
  113.             }
  114.         }
  115.         else if (&game.getObject(aObject) != nullptr)
  116.         {
  117.             aAction = game.getObject(aObject).action[aCommand];
  118.  
  119.             auto aStep = getValueFromAction(aAction);
  120.  
  121.             if (!keyPressed)
  122.             {
  123.                 aStep = -aStep;
  124.             }
  125.  
  126.             move(game.getObject(aObject), aCommand, aStep);
  127.         }
  128.     }
  129. }
  130.  
  131. void xge::Engine::move(xge::Object& object, Engine::Direction direction, float step)
  132. {
  133.     switch (direction)
  134.     {
  135.     case Direction::Up:
  136.         object.velocity.y -= step;
  137.         break;
  138.     case Direction::Down:
  139.         object.velocity.y += step;
  140.         break;
  141.     case Direction::Left:
  142.         object.velocity.x -= step;
  143.         break;
  144.     case Direction::Right:
  145.         object.velocity.x += step;
  146.         break;
  147.     }
  148. }
  149.  
  150. void xge::Engine::move(xge::Object& object, std::string direction, float step)
  151. {
  152.     move(object, mapDirection[direction], step);
  153. }
  154.  
  155. std::string xge::Engine::getObjectFromInput(PairStringString input)
  156. {
  157.     auto sStart = 0;
  158.     auto sEnd = input.first.find(".");
  159.     return input.first.substr(sStart, sEnd);
  160. }
  161.  
  162. std::string xge::Engine::getCommandFromInput(PairStringString input)
  163. {
  164.     auto sStart = input.first.rfind('.') + 1;
  165.     auto sEnd = input.first.length();
  166.     auto sLength = sEnd - sStart;
  167.  
  168.     return input.first.substr(sStart, sLength);
  169. }
  170.  
  171. float xge::Engine::getValueFromAction(std::string action)
  172. {
  173.     auto sStart = action.find("(") + 1;
  174.     auto sEnd = action.find(")");
  175.     auto sLength = sEnd - sStart;
  176.     auto sVar = action.substr(sStart, sLength);
  177.  
  178.     return game.getVariable(sVar);
  179. }
  180.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement