Advertisement
martin_santome

Refactor Game 80Casual (IA helper)

Mar 10th, 2024 (edited)
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.45 KB | Source Code | 0 0
  1. /*********************************************
  2. ** MAIN
  3. **********************************************/
  4.  
  5. #include <iostream>
  6. #include "Game.hpp"
  7.  
  8. int main() {
  9.     try {
  10.         Game game; // Create the game instance
  11.         game.run(); // Enter the main loop of the game
  12.         return 0; // Exit with success status
  13.     }
  14.     catch (const std::exception& e) {
  15.         std::cerr << "An exception has occurred: " << e.what() << std::endl;
  16.         return 1; // Exit with error status
  17.     }
  18.     catch (...) {
  19.         std::cerr << "An unknown exception has occurred." << std::endl;
  20.         return 2; // Exit with error status indicating an unknown exception
  21.     }
  22. }
  23.  
  24. /*********************************************
  25. ** GAME.HPP
  26. **********************************************/
  27.  
  28.  
  29. // Game.hpp
  30. #ifndef GAME_HPP
  31. #define GAME_HPP
  32.  
  33. #include <SFML/Graphics.hpp>
  34. #include "HUD.hpp"
  35. #include "Player.hpp"
  36. #include "AssetManager.hpp"
  37. #include "Utilities.hpp"
  38.  
  39. class Game {
  40. public:
  41.     Game();
  42.     void run();
  43.  
  44. private:
  45.     sf::RenderWindow window;
  46.     Player player;
  47.     HUD hud;
  48.     sf::View uiView, dyView, miniMap;
  49.     sf::Sprite bg;
  50.     sf::RectangleShape rectPl; // Placeholder for the player in the minimap
  51.     sf::Clock clock;
  52.  
  53.     void initialize();
  54.     void processEvents();
  55.     void update(sf::Time deltaTime);
  56.     void render();
  57.     void setupViews(const sf::Vector2u& windowSize);
  58.     void setupBackground();
  59.     void setupMiniMap();
  60. };
  61.  
  62. #endif // GAME_HPP
  63.  
  64. /*********************************************
  65. ** GAME.CPP
  66. **********************************************/
  67.  
  68. // Game.cpp
  69. #include "Game.hpp"
  70.  
  71. Game::Game() : window(sf::VideoMode(640, 480), "Testing") {
  72.     initialize();
  73. }
  74.  
  75. void Game::initialize() {
  76.     setupBackground();
  77.     setupViews(window.getSize());
  78.     setupMiniMap();
  79.     // Initialize player and hud if needed here
  80. }
  81.  
  82. void Game::setupViews(const sf::Vector2u& windowSize) {
  83.     uiView.setSize(sf::Vector2f(windowSize));
  84.     uiView.setViewport(sf::FloatRect(0.f, 0.f, 1.f, 1.f));
  85.  
  86.     dyView.setSize(sf::Vector2f(windowSize));
  87.     dyView.setViewport(sf::FloatRect(0.f, 0.f, 1.f, 1.f));
  88. }
  89.  
  90. void Game::setupBackground() {
  91.     bg.setTexture(AssetManager::GetTexture(Utils::GetWorkingDirectory() + "Media/Backgrounds/bg3-1.png"));
  92.     bg.setPosition(0, 0);
  93.     bg.setOrigin(0, 0);
  94. }
  95.  
  96. void Game::setupMiniMap() {
  97.     miniMap.setViewport(sf::FloatRect(0.75F, 0.f, 0.25F, 0.25F));
  98.     // Setup miniMap size based on the background size
  99.     miniMap.setSize(sf::Vector2f(bg.getLocalBounds().width, bg.getLocalBounds().height));
  100. }
  101.  
  102. void Game::run() {
  103.     sf::Clock clock;
  104.     while (window.isOpen()) {
  105.         sf::Time deltaTime = clock.restart();
  106.         processEvents();
  107.         update(deltaTime);
  108.         render();
  109.     }
  110. }
  111.  
  112. void Game::processEvents() {
  113.     sf::Event event;
  114.     while (window.pollEvent(event)) {
  115.         if (event.type == sf::Event::Closed) {
  116.             window.close();
  117.         }
  118.         // Add more event handling here as needed
  119.     }
  120.     // Additional real-time input handling could go here or inside Player::handleInput
  121. }
  122.  
  123. void Game::update(sf::Time deltaTime) {
  124.     // Update game state, player, HUD, etc.
  125.     hud.update(deltaTime); // Assuming hud.update needs deltaTime
  126.     player.update(deltaTime, window); // This is indicative; actual Player::update might differ
  127. }
  128.  
  129. void Game::render() {
  130.     window.clear();
  131.    
  132.     // Set view to default for drawing background and reset for UI components
  133.     window.setView(window.getDefaultView());
  134.     window.draw(bg);
  135.  
  136.     // Example of how to switch views
  137.     // if (player.getPosition().x < 320) { window.setView(window.getDefaultView()); }
  138.     // else { window.setView(dyView); }
  139.     // Update dyView center if necessary based on player position
  140.  
  141.     player.draw(window); // Delegate drawing the player
  142.     // Drawing minimap, UI elements, etc. could follow here
  143.  
  144.     window.setView(uiView);
  145.     hud.draw(window); // Delegate drawing HUD
  146.  
  147.     window.display();
  148. }
  149.  
  150.  
  151. /*********************************************
  152. ** HUD.HPP
  153. **********************************************/
  154.  
  155. // HUD.hpp
  156. #ifndef HUD_HPP
  157. #define HUD_HPP
  158.  
  159. #include <SFML/Graphics.hpp>
  160. #include "AssetManager.hpp"
  161. #include "Utilities.hpp"
  162.  
  163. class HUD {
  164. public:
  165.     HUD();
  166.  
  167.     void update(const sf::Time& deltaTime);
  168.     void draw(sf::RenderWindow& window);
  169.  
  170. private:
  171.     sf::Font font;
  172.     sf::Text oneUp, score, tDelta, tElapsed, tElapsed2;
  173.     sf::RectangleShape lifeBar;
  174.  
  175.     void setupText();
  176.     void setupLifeBar();
  177. };
  178.  
  179. #endif // HUD_HPP
  180.  
  181.  
  182. /*********************************************
  183. ** HUD.CPP
  184. **********************************************/
  185.  
  186. // HUD.cpp
  187. #include "HUD.hpp"
  188.  
  189. HUD::HUD() {
  190.     setupText();
  191.     setupLifeBar();
  192. }
  193.  
  194. void HUD::setupText() {
  195.     font = AssetManager::GetFont(Utils::GetWorkingDirectory() + "Media/Fonts/KOMIKAP_.ttf");
  196.  
  197.     oneUp.setFont(font);                  
  198.     oneUp.setString("1 UP");              
  199.     oneUp.setCharacterSize(30U);          
  200.     oneUp.setFillColor(sf::Color::White);  
  201.     oneUp.setPosition(sf::Vector2f(50,10));
  202.  
  203.     score.setFont(font);                  
  204.     score.setString("12345");              
  205.     score.setCharacterSize(20U);          
  206.     score.setFillColor(sf::Color::White);  
  207.     score.setPosition(sf::Vector2f(50,50));
  208.  
  209.     tDelta.setFont(font);                  
  210.     tDelta.setCharacterSize(10U);          
  211.     tDelta.setFillColor(sf::Color::Red);    
  212.     tDelta.setPosition(sf::Vector2f(50.f, 300.f));
  213.  
  214.     tElapsed.setFont(font);                
  215.     tElapsed.setCharacterSize(10U);        
  216.     tElapsed.setFillColor(sf::Color::Red);  
  217.     tElapsed.setPosition(sf::Vector2f(50.f,325.f));
  218.  
  219.     tElapsed2.setFont(font);                
  220.     tElapsed2.setCharacterSize(10U);        
  221.     tElapsed2.setFillColor(sf::Color::Red);
  222.     tElapsed2.setPosition(sf::Vector2f(50.f,350.f));
  223. }
  224.  
  225. void HUD::setupLifeBar() {
  226.     lifeBar.setSize(sf::Vector2f(96.f, 10.f));
  227.     lifeBar.setFillColor(sf::Color::Green);
  228.     lifeBar.setOrigin(lifeBar.getSize()*0.5F);
  229. }
  230.  
  231. void HUD::update(const sf::Time& deltaTime) {
  232.     // Update dynamic text (e.g., timers, scores) based on game state
  233.     // Example: Update deltaTime text
  234.     tDelta.setString("Delta: " + std::to_string(deltaTime.asSeconds()) + "s");
  235. }
  236.  
  237. void HUD::draw(sf::RenderWindow& window) {
  238.     // Draw all HUD elements
  239.     window.draw(oneUp);
  240.     window.draw(score);
  241.     window.draw(tDelta);
  242.     window.draw(tElapsed); // Note: You'll need to update tElapsed and tElapsed2 as needed
  243.     window.draw(tElapsed2);
  244.     window.draw(lifeBar); // Note: Position needs to be updated based on the game state
  245. }
  246.  
  247.  
  248. /*********************************************
  249. ** PLAYER.HPP
  250. **********************************************/
  251.  
  252. // Player.hpp
  253. #ifndef PLAYER_HPP
  254. #define PLAYER_HPP
  255.  
  256. #include <SFML/Graphics.hpp>
  257. #include "Animator.hpp"
  258.  
  259. class Player {
  260. public:
  261.     Player(const sf::Texture& texture);
  262.  
  263.     void handleInput();
  264.     void update(sf::Time deltaTime);
  265.     void draw(sf::RenderWindow& window);
  266.  
  267. private:
  268.     sf::Sprite sprite;
  269.     Animator animator;
  270.     sf::Vector2f velocity;
  271.  
  272.     void setupAnimations();
  273. };
  274.  
  275. #endif // PLAYER_HPP
  276.  
  277.  
  278. /*********************************************
  279. ** PLAYER.CPP
  280. **********************************************/
  281. // Player.cpp
  282. #include "Player.hpp"
  283.  
  284. Player::Player(const sf::Texture& texture) : sprite(texture), velocity(0.f, 0.f) {
  285.     setupAnimations();
  286. }
  287.  
  288. void Player::setupAnimations() {
  289.     // Assuming animations have been loaded into `animator`
  290.     // Example:
  291.     // Animation walkAnimation;
  292.     // walkAnimation.texture = &someTexture;
  293.     // walkAnimation.frames.push_back(sf::IntRect(...));
  294.     // animator.AddAnimation("Walk", walkAnimation);
  295. }
  296.  
  297. void Player::handleInput() {
  298.     // Handle player input and adjust velocity or switch animations accordingly
  299.     // Example:
  300.     // if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { ... }
  301. }
  302.  
  303. void Player::update(sf::Time deltaTime) {
  304.     // Update the player's position and animation
  305.     sprite.move(velocity * deltaTime.asSeconds());
  306.     animator.Update(deltaTime);
  307.     animator.ApplyToSprite(sprite);
  308. }
  309.  
  310. void Player::draw(sf::RenderWindow& window) {
  311.     window.draw(sprite);
  312. }
  313.  
  314. /*********************************************
  315. ** ANIMATOR.hPP
  316. **********************************************/
  317.  
  318. // Animator.hpp
  319. #ifndef ANIMATOR_HPP
  320. #define ANIMATOR_HPP
  321.  
  322. #include <SFML/Graphics.hpp>
  323. #include <map>
  324. #include <string>
  325. #include <vector>
  326.  
  327. class Animation {
  328. public:
  329.     std::vector<sf::IntRect> frames;
  330.     const sf::Texture* texture;
  331.     sf::Time duration;
  332.     bool loop;
  333.  
  334.     Animation() : texture(nullptr), duration(sf::seconds(1)), loop(true) {}
  335. };
  336.  
  337. class Animator {
  338. public:
  339.     Animator() : currentAnimation(nullptr) {}
  340.  
  341.     void AddAnimation(const std::string& name, const Animation& animation);
  342.     void SwitchAnimation(const std::string& name);
  343.     void Update(sf::Time deltaTime);
  344.     void ApplyToSprite(sf::Sprite& sprite) const;
  345.  
  346. private:
  347.     std::map<std::string, Animation> animations;
  348.     const Animation* currentAnimation;
  349.     sf::Time currentTime;
  350.     size_t currentFrame;
  351.  
  352.     void reset();
  353. };
  354.  
  355. #endif // ANIMATOR_HPP
  356.  
  357.  
  358.  
  359. /*********************************************
  360. ** ANIMATOR.cPP
  361. **********************************************/
  362.  
  363.  
  364. // Animator.cpp
  365. #include "Animator.hpp"
  366.  
  367. void Animator::AddAnimation(const std::string& name, const Animation& animation) {
  368.     animations[name] = animation;
  369. }
  370.  
  371. void Animator::SwitchAnimation(const std::string& name) {
  372.     auto it = animations.find(name);
  373.     if (it != animations.end()) {
  374.         currentAnimation = &it->second;
  375.         reset();
  376.     }
  377. }
  378.  
  379. void Animator::Update(sf::Time deltaTime) {
  380.     if (!currentAnimation) return;
  381.  
  382.     currentTime += deltaTime;
  383.  
  384.     float scaledTime = currentTime.asSeconds() / currentAnimation->duration.asSeconds();
  385.     size_t numFrames = currentAnimation->frames.size();
  386.     currentFrame = static_cast<size_t>(scaledTime * numFrames);
  387.  
  388.     if (currentAnimation->loop)
  389.         currentFrame %= numFrames;
  390.     else if (currentFrame >= numFrames)
  391.         currentFrame = numFrames - 1;
  392. }
  393.  
  394. void Animator::ApplyToSprite(sf::Sprite& sprite) const {
  395.     if (!currentAnimation) return;
  396.     sprite.setTexture(*currentAnimation->texture);
  397.     sprite.setTextureRect(currentAnimation->frames[currentFrame]);
  398. }
  399.  
  400. void Animator::reset() {
  401.     currentTime = sf::Time::Zero;
  402.     currentFrame = 0;
  403. }
  404.  
  405.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement