diff --git a/CMakeLists.txt b/CMakeLists.txt index 9fd5ecf..d396cb8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -108,7 +108,7 @@ endif() ############################################################################### # NOTE: update executable name in .github/workflows/cmake.yml:25 when changing name here -add_executable(${PROJECT_NAME} main.cpp doodle_jump/game/Game.cpp doodle_jump/platform/Platform.cpp doodle_jump/platform/PlatformGenerator.cpp doodle_jump/player/Player.cpp doodle_jump/screen/GameScreen.cpp doodle_jump/screen/MainMenu.cpp doodle_jump/screen/PlayScreen.cpp doodle_jump/screen/GameOver.cpp) +add_executable(${PROJECT_NAME} main.cpp doodle_jump/game/Game.cpp doodle_jump/platform/Platform.cpp doodle_jump/player/Player.cpp doodle_jump/screen/GameScreen.cpp doodle_jump/screen/MainMenu.cpp doodle_jump/screen/PlayScreen.cpp doodle_jump/screen/GameOver.cpp) ############################################################################### diff --git a/doodle_jump/game/Game.cpp b/doodle_jump/game/Game.cpp index 4a9e74c..6fccedb 100644 --- a/doodle_jump/game/Game.cpp +++ b/doodle_jump/game/Game.cpp @@ -4,7 +4,6 @@ #include "./../screen/GameOver.h" #include #include -#include "./../platform/PlatformGenerator.h" const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 600; @@ -16,11 +15,12 @@ Game::Game() { maxScore = 0; player = new Player(); platforms = std::vector(); + lastPlatform = new Platform(); for (int i = 0; i < 15; i++) { auto platform = new Platform(); - platform->useGenerator(platformGenerator.getLastPlatformCoordinates()); - platformGenerator.setLastPlatform(platform); + platform->useGenerator(lastPlatform->getSprite().getPosition()); + lastPlatform = platform; platforms.push_back(platform); } @@ -44,6 +44,8 @@ Game::~Game() { for (auto& platform : platforms) { delete platform; } + delete lastPlatform; + delete scoreText; } std::ostream& operator<<(std::ostream& out, const Game& game) { @@ -61,12 +63,12 @@ void Game::reset() { } platforms.clear(); - platformGenerator.setLastPlatform(new Platform()); + lastPlatform = new Platform(); for (int i = 0; i < 15; i++) { auto platform = new Platform(); - platform->useGenerator(platformGenerator.getLastPlatformCoordinates()); - platformGenerator.setLastPlatform(platform); + platform->useGenerator(lastPlatform->getSprite().getPosition()); + lastPlatform = platform; platforms.push_back(platform); } score = 0; @@ -162,8 +164,8 @@ void Game::checkCollision() { if (platform->getType() == PlatformType::BREAKABLE) { delete platform; auto plat = new Platform(); - plat->useGenerator(platformGenerator.getLastPlatformCoordinates()); - platformGenerator.setLastPlatform(plat); + plat->useGenerator(lastPlatform->getSprite().getPosition()); + lastPlatform = plat; platform = plat; } else if (platform->getType() == PlatformType::BOOST) { @@ -202,8 +204,8 @@ void Game::play() { if (platform->getSprite().getPosition().y > 750.0f) { delete platform; auto plat = new Platform(); - plat->useGenerator(platformGenerator.getLastPlatformCoordinates()); - platformGenerator.setLastPlatform(plat); + plat->useGenerator(lastPlatform->getSprite().getPosition()); + lastPlatform = plat; platform = plat; } @@ -237,14 +239,6 @@ void Game::displayScore() { window.draw(*scoreText); } -// int Game::getScore() const { -// return score; -// } - -// void Game::setScore(int score_) { -// score = score_; -// } - void Game::changeScreen(ScreenType screenType) { currentScreen = screenType; } \ No newline at end of file diff --git a/doodle_jump/game/Game.h b/doodle_jump/game/Game.h index 85babb4..dc8cbdd 100644 --- a/doodle_jump/game/Game.h +++ b/doodle_jump/game/Game.h @@ -1,5 +1,4 @@ #include "./../platform/Platform.h" -#include "./../platform/PlatformGenerator.h" #include "./../player/Player.h" #include "./../screen/GameScreen.h" @@ -18,16 +17,12 @@ class Game { sf::Text *scoreText; Player *player; std::vector platforms; - PlatformGenerator platformGenerator = PlatformGenerator(); + Platform* lastPlatform; public: Game(); ~Game(); - Game& operator=(const Game& game_); - Game(const Game& game_); friend std::ostream& operator<<(std::ostream& out, const Game& game); void run(); - // int getScore() const; - // void setScore(int score); void changeScreen(ScreenType screenType); void play(); void reset(); diff --git a/doodle_jump/platform/Platform.cpp b/doodle_jump/platform/Platform.cpp index 274d90d..953f042 100644 --- a/doodle_jump/platform/Platform.cpp +++ b/doodle_jump/platform/Platform.cpp @@ -15,6 +15,29 @@ Platform::Platform() { sprite.setPosition((float)x, (float)y); } +Platform::~Platform() { + std::cout << "Platform destructor called\n"; +} + +Platform::Platform(const Platform& platform) { + std::cout << "Platform copy constructor called\n"; + type = platform.type; + texture = platform.texture; + sprite = platform.sprite; + updateCount = platform.updateCount; +} + +Platform& Platform::operator=(const Platform& platform) { + std::cout << "Platform copy assignment operator called\n"; + if (this != &platform) { + type = platform.type; + texture = platform.texture; + sprite = platform.sprite; + updateCount = platform.updateCount; + } + return *this; +} + void Platform::useGenerator(const sf::Vector2f& lastPlatformCoordinates) { std::cout << "Platform constructor called\n"; diff --git a/doodle_jump/platform/Platform.h b/doodle_jump/platform/Platform.h index ac8f71e..4a6618a 100644 --- a/doodle_jump/platform/Platform.h +++ b/doodle_jump/platform/Platform.h @@ -19,6 +19,9 @@ class Platform { int updateCount; public: Platform(); + ~Platform(); + Platform(const Platform& platform); + Platform& operator=(const Platform& platform); void useGenerator(const sf::Vector2f& lastPlatformCoordinates); friend std::ostream& operator<<(std::ostream& os, const Platform& platform); PlatformType getType() const; diff --git a/doodle_jump/platform/PlatformGenerator.cpp b/doodle_jump/platform/PlatformGenerator.cpp deleted file mode 100644 index b80b740..0000000 --- a/doodle_jump/platform/PlatformGenerator.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include "PlatformGenerator.h" -#include "Platform.h" - -PlatformGenerator::PlatformGenerator() { - std::cout << "PlatformGenerator constructor called\n"; - - lastPlatform = new Platform(); -} - -PlatformGenerator::~PlatformGenerator() { - std::cout << "PlatformGenerator destructor called\n"; -} - -PlatformGenerator& PlatformGenerator::operator=(const PlatformGenerator& platformGenerator_) { - std::cout << "PlatformGenerator copy assignment called\n"; - if (this != &platformGenerator_) { - lastPlatform = platformGenerator_.lastPlatform; - } - return *this; -} - -PlatformGenerator::PlatformGenerator(const PlatformGenerator& platformGenerator_) { - std::cout << "PlatformGenerator copy constructor called\n"; - lastPlatform = new Platform(); - *lastPlatform = *platformGenerator_.lastPlatform; -} - -// Platform PlatformGenerator::getLastPlatform() { -// return lastPlatform; -// } - -sf::Vector2f PlatformGenerator::getLastPlatformCoordinates() { - return lastPlatform->getSprite().getPosition(); -} - -void PlatformGenerator::setLastPlatform(Platform* lastPlatform_) { - lastPlatform = lastPlatform_; -} \ No newline at end of file diff --git a/doodle_jump/platform/PlatformGenerator.h b/doodle_jump/platform/PlatformGenerator.h deleted file mode 100644 index 5486a1e..0000000 --- a/doodle_jump/platform/PlatformGenerator.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include -#include -#include "./Platform.h" - -class PlatformGenerator { -private: - Platform* lastPlatform; -public: - PlatformGenerator(); - ~PlatformGenerator(); - PlatformGenerator& operator=(const PlatformGenerator& platformGenerator_); - PlatformGenerator(const PlatformGenerator& platformGenerator_); - sf::Vector2f getLastPlatformCoordinates(); - void setLastPlatform(Platform* lastPlatform_); -}; \ No newline at end of file