BiggerRadius

Untitled

May 15th, 2025 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.53 KB | None | 0 0
  1. //main.cpp
  2. #include "game_manager.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. //game_manager.cpp
  13. #include "game_manager.h"
  14. #include <iostream>
  15. #include <stdexcept>
  16. #include <SFML/Graphics.hpp>
  17. #include <cmath>
  18. #include <map>
  19. #include <string>
  20.  
  21. enum class GameManager::GameStates{Play,Pause,Menu};
  22. void GameManager::changeState(GameManager::GameStates newState){currentState = newState;}
  23. GameManager::GameStates GameManager::getCurrentState(){return currentState;}
  24. GameManager::GameManager() : initialization_failed(false), window(sf::VideoMode({1920u, 1080u}), "Game") {
  25. instance = this;
  26. }
  27. GameManager::~GameManager() {}
  28.  
  29. bool GameManager::initialize() {
  30. window.setVerticalSyncEnabled(true);
  31. menu.setupMenu();
  32. changeState(GameStates::Menu);
  33. initialization_failed = false;
  34. try {
  35. player = entityManager.entityFactory("Player", "build/assets/texture/cube.png", sf::Vector2f(250, 250));
  36. world = World(std::move(player));
  37. } catch (const std::runtime_error& error) {
  38. std::cerr << "Error creating game object: " << error.what() << std::endl;
  39. initialization_failed = true;
  40. }
  41. if (initialization_failed) {
  42. std::cerr << "Game initialization failed. Exiting." << std::endl;
  43. return false;
  44. }
  45. window.setFramerateLimit(144);
  46. return true;
  47. }
  48.  
  49. sf::RenderWindow& GameManager::getWindow() {
  50. return window;
  51. }
  52.  
  53. void GameManager::menuLoop(){
  54. while (getCurrentState() == GameStates::Menu){//Menu state
  55. window.display();
  56. world.draw(window,World::DrawContext::Menu);
  57. const auto& buttons = menu.getButtons();
  58. if (!buttons.empty() && buttons[0]->isClicked(sf::Mouse::getPosition() , sf::Mouse::Button::Left)){
  59. changeState(GameStates::Play);
  60. }
  61. }
  62. }
  63.  
  64. void GameManager::runGameLoop() {
  65. menuLoop();
  66. auto player = world.getPlayer();
  67. std::map <std::string,bool> inputs;
  68. sf::Vector2f normalizedMovementVector;
  69. auto InputToMovementDirection = [&player,this] (std::map<std::string,bool> inputs){
  70. sf::Vector2f direction;
  71. for (auto input: inputs){
  72. if(input.second){
  73. if(input.first == "pressingEnter"){
  74. //reserved
  75. }
  76. if(input.first == "pressingW"){
  77. direction.y -= player->getSpeed().y;
  78. }
  79. if(input.first == "pressingA"){
  80. direction.x -= player->getSpeed().x;
  81. }
  82. if(input.first == "pressingS"){
  83. direction.y += player->getSpeed().y;
  84. }
  85. if(input.first == "pressingD"){
  86. direction.x += player->getSpeed().x;
  87. }
  88. if(input.first == "pressingLeftMouse"){
  89. //reserved
  90. }
  91. if(input.first == "pressingRightMouse"){
  92. //reserved
  93. }
  94. }
  95. }
  96. return direction;
  97. };
  98. while (window.isOpen()){
  99. while (const std::optional event = window.pollEvent()) {
  100. if (event->is<sf::Event::Closed>()) {
  101. window.close();
  102. }//this line of code is going to be changed
  103. if(const auto* keyPressed = event->getIf<sf::Event::KeyPressed>()){
  104. if (keyPressed->scancode == sf::Keyboard::Scancode::Escape){
  105. if(getCurrentState() != GameStates::Menu){
  106. if(getCurrentState() == GameStates::Pause){
  107. changeState(GameStates::Play);
  108. }else if (getCurrentState() == GameStates::Play){
  109. changeState(GameStates::Pause);
  110. }
  111. }
  112. }
  113. }
  114. }
  115. using namespace sf::Keyboard;
  116. using namespace sf::Mouse;
  117. inputs["pressingEnter"] = isKeyPressed(Key::Enter);
  118. inputs["pressingW"] = isKeyPressed(Key::W);
  119. inputs["pressingA"] = isKeyPressed(Key::A);
  120. inputs["pressingS"] = isKeyPressed(Key::S);
  121. inputs["pressingD"] = isKeyPressed(Key::D);
  122. inputs["pressingLeftMouse"] = isButtonPressed(sf::Mouse::Button::Left);
  123. inputs["pressingRightMouse"] = isButtonPressed(sf::Mouse::Button::Right);
  124. normalizedMovementVector = InputToMovementDirection(inputs);
  125. sf::Time deltaTime = clock.restart();
  126. window.clear();
  127. if(getCurrentState() == GameStates::Play){
  128. float dtSeconds = deltaTime.asSeconds();
  129. world.update(dtSeconds, normalizedMovementVector);
  130. world.draw(window,World::DrawContext::Game);
  131. } else if (getCurrentState() == GameStates::Pause){
  132. pause.setupPause();
  133. world.draw(window,World::DrawContext::Pause);
  134. const auto& buttons = pause.getButtons();
  135. if (!buttons.empty() && buttons[0]->isClicked(sf::Mouse::getPosition() , sf::Mouse::Button::Left)){
  136. changeState(GameStates::Menu);
  137. menuLoop();
  138.  
  139. }
  140. //paused state
  141. }
  142. window.display();
  143. }
  144. }
  145.  
  146. const Menu& GameManager::getMenu(){
  147. return this->menu;
  148. }
  149.  
  150. const Pause& GameManager::getPause(){
  151. return this->pause;
  152. }
  153. //world.cpp
  154. #include "world.h"
  155. #include "player.h"
  156. #include <memory>
  157. #include <iostream>
  158. #include "game_manager.h"
  159.  
  160. World::World(std::shared_ptr<Entity> player):player(player){
  161. }
  162.  
  163. World::World() : player(nullptr) {}
  164.  
  165. Entity* World::getPlayer() const {
  166. return player.get();
  167. }
  168.  
  169. void World::update(float dtSeconds,sf::Vector2f movementInput){
  170. Entity* player = this->getPlayer();
  171. player->setPos(player->getPos() + movementInput);
  172. }
  173. void World::draw(sf::RenderWindow& window,World::DrawContext DrawContext){
  174. const auto gameManager = GameManager::instance;
  175. if(DrawContext == DrawContext::Game){
  176. player->draw(window);
  177. }
  178. if (DrawContext == DrawContext::Menu){
  179. gameManager->getMenu().draw(window);
  180. }
  181. if (DrawContext == DrawContext::Pause){
  182. gameManager->getPause().draw(window);
  183. }
  184. }
  185. //menu.cpp
  186. #include "button_manager.h"
  187. #include "button.h"
  188. #include <SFML/Graphics.hpp>
  189. #include "menu.h"
  190.  
  191. void Menu::setupMenu(){
  192. buttons.push_back(buttonManager.ButtonFactory("build/assets/texture/play.png",sf::Vector2f{500,500}));
  193. }
  194.  
  195. void Menu::draw(sf::RenderWindow& window)const{
  196. for(auto& button: buttons){
  197. window.draw(button.get()->getSprite());
  198. }
  199. }
  200.  
  201. std::vector<std::shared_ptr<Button>> Menu::getButtons(){
  202. return this->buttonManager.getButtons();
  203. }
  204. //pause.cpp
  205. #include "button_manager.h"
  206. #include "button.h"
  207. #include <SFML/Graphics.hpp>
  208. #include "pause.h"
  209.  
  210. void Pause::setupPause(){
  211. buttons.push_back(buttonManager.ButtonFactory("build/assets/texture/Menu.png",sf::Vector2f{500,500}));
  212. }
  213.  
  214. void Pause::draw(sf::RenderWindow& window)const{
  215. for(auto& button: buttons){
  216. window.draw(button.get()->getSprite());
  217. }
  218. }
  219.  
  220. std::vector<std::shared_ptr<Button>> Pause::getButtons(){
  221. return this->buttonManager.getButtons();
  222. }
  223. //button_manager.cpp
  224. #include "button_manager.h"
  225. #include <functional>
  226. #include <memory>
  227. #include "button.h"
  228. std::shared_ptr<Button> ButtonManager::ButtonFactory(std::string path,sf::Vector2f position){
  229. sf::Texture* texture = textureManager.loadTexture(path);
  230. auto button = std::make_shared<Button>(*texture,position);
  231. buttons.push_back(button);
  232. return button;
  233. };
  234. std::vector<std::shared_ptr<Button>> ButtonManager::getButtons(){
  235. return buttons;
  236. }
  237. //button_implementation.cpp
  238. #include "button.h"
  239. #include <functional>
  240. #include <SFML/Graphics.hpp>
  241.  
  242. Button::Button(sf::Texture& texture,sf::Vector2f position):effect(effect),sprite(texture){
  243. sprite.setPosition(position);
  244. }
  245.  
  246. sf::Sprite Button::getSprite(){
  247. return this->sprite;
  248. }
  249.  
  250. bool Button::isClicked(sf::Vector2i mousePosition, sf::Mouse::Button buttonType){
  251. if(!clicked){
  252. clicked = true;
  253. return sprite.getGlobalBounds().contains(sf::Vector2f(mousePosition)) && sf::Mouse::isButtonPressed(buttonType);
  254. }
  255. if(!sf::Mouse::isButtonPressed(buttonType)){
  256. clicked = false;
  257. }
  258. return false;
  259. }
  260. //entity_manager.cpp
  261. #include <string>
  262. #include "entity.h"
  263. #include <SFML/Graphics.hpp>
  264. #include "entity_manager.h"
  265. #include <memory>
  266. #include <iostream>
  267. #include "player.h"
  268. #include "texture_manager.h"
  269.  
  270. std::shared_ptr<Entity> EntityManager::entityFactory(std::string entityType,std::string path,sf::Vector2f position){
  271. sf::Texture* texture = textureManager.loadTexture(path);
  272. if (entityType == "Player"){
  273. return std::make_shared<Player>(*texture,position);
  274. }
  275. throw std::runtime_error("Error loading texture: " + path + " of type: " + entityType);
  276. }
  277. //player.cpp
  278. #include "entity.h"
  279. #include "player.h"
  280. #include <SFML/Graphics.hpp>
  281.  
  282. Player::Player(sf::Texture& texture, sf::Vector2f position) : Entity(texture, position){
  283. setSpeed({1.0,1.0});
  284. }
  285. //entity_implementation.cpp
  286. #include <SFML/Graphics.hpp>
  287. #include "entity.h"
  288.  
  289. Entity::Entity(sf::Texture &texture,sf::Vector2f position):sprite(texture){
  290. sprite.setPosition(position);
  291. }
  292. void Entity::setPos(sf::Vector2f position){
  293. sprite.setPosition(position);
  294. }
  295. sf::Vector2f Entity::getPos(){
  296. return sprite.getPosition();
  297. }
  298. void Entity::setTexture(sf::Texture& texture){
  299. sprite.setTexture(texture);
  300. }
  301. void Entity::draw(sf::RenderWindow& window) {
  302. window.draw(this->sprite);
  303. }
  304. sf::Vector2f Entity::getSpeed(){
  305. return speed;
  306. }
  307. void Entity::setSpeed(sf::Vector2f newSpeed){
  308. speed = newSpeed;
  309. }
  310. Entity::~Entity(){
  311. };
  312. //texture_manager.cpp
  313. #include <SFML/Graphics.hpp>
  314. #include <string>
  315. #include <map>
  316. #include "texture_manager.h"
  317.  
  318. sf::Texture* TextureManager::loadTexture(std::string path) {
  319. auto it = textureMap.find(path);
  320. if (it != textureMap.end()) {
  321. return &it->second;
  322. } else {
  323. sf::Texture newTexture;
  324. if (newTexture.loadFromFile(path)) {
  325. textureMap[path] = newTexture;
  326. return &textureMap[path];
  327. } else {
  328. throw std::runtime_error("Error loading texture: " + path);
  329. }
  330. }
  331. }
  332.  
Advertisement
Add Comment
Please, Sign In to add comment