From 15c745d7c084b177eeddedf5de18e5c1e5fc30f1 Mon Sep 17 00:00:00 2001 From: UnEpicier Date: Tue, 9 Jan 2024 16:10:24 +0100 Subject: [PATCH] refactor: re-organize code --- Inputs.cpp | 27 --------------- Inputs.h | 24 ------------- Shield.cpp | 11 ------ Space-Invader.vcxproj | 15 ++++---- Space-Invader.vcxproj.filters | 15 ++++---- Space-Invader.vcxproj.user | 2 +- include/GameManager.h | 28 +++++++++++++++ Laser.h => include/Laser.h | 2 +- Player.h => include/Player.h | 4 +-- lib/GameManager.cpp | 65 +++++++++++++++++++++++++++++++++++ Laser.cpp => lib/Laser.cpp | 2 +- Player.cpp => lib/Player.cpp | 10 +++--- main.cpp | 53 +++++----------------------- main.h | 14 -------- 14 files changed, 126 insertions(+), 146 deletions(-) delete mode 100644 Inputs.cpp delete mode 100644 Inputs.h delete mode 100644 Shield.cpp create mode 100644 include/GameManager.h rename Laser.h => include/Laser.h (82%) rename Player.h => include/Player.h (82%) create mode 100644 lib/GameManager.cpp rename Laser.cpp => lib/Laser.cpp (83%) rename Player.cpp => lib/Player.cpp (53%) delete mode 100644 main.h diff --git a/Inputs.cpp b/Inputs.cpp deleted file mode 100644 index be0de1a..0000000 --- a/Inputs.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "Inputs.h" - -void Inputs::HandleInputs() { - // Player movements - if (Keyboard::isKeyPressed(Keyboard::Left) || Keyboard::isKeyPressed(Keyboard::Q)) { - _player.move(-1); - } - - if (Keyboard::isKeyPressed(Keyboard::Right) || Keyboard::isKeyPressed(Keyboard::D)) { - _player.move(1); - } - - if (Keyboard::isKeyPressed(Keyboard::Space)) { - if (_clock.getElapsedTime().asMilliseconds() > 500) { - const Vector2f playerPos = _player.getShape().getPosition(); - const float playerRadius = _player.getShape().getRadius(); - - Laser laser; - laser.setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius)); - - _lasers.push_back(laser.getLaser()); - - _clock.restart(); - } - - } -} \ No newline at end of file diff --git a/Inputs.h b/Inputs.h deleted file mode 100644 index 0d874b6..0000000 --- a/Inputs.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include -#include -#include -#include "Player.h"; -#include "Laser.h"; - -using namespace std; -using namespace sf; - -class Inputs -{ -public: - Inputs(Player& player, vector& lasers) : _player(player), _lasers(lasers) {}; - - void HandleInputs(); - -private: - Player& _player; - Clock _clock; - vector& _lasers; -}; - diff --git a/Shield.cpp b/Shield.cpp deleted file mode 100644 index 7b3e4bb..0000000 --- a/Shield.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "Shield.h" - -Shield::Shield() { - _shield = RectangleShape(Vector2f(30, 20)); - _shield.setFillColor(Color(52, 252, 5)); -} - -void Shield::decreaseLife() -{ - _life -= 1; -} diff --git a/Space-Invader.vcxproj b/Space-Invader.vcxproj index 1517bb8..0813c20 100644 --- a/Space-Invader.vcxproj +++ b/Space-Invader.vcxproj @@ -107,6 +107,7 @@ true _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true + include;%(AdditionalIncludeDirectories) Console @@ -121,6 +122,7 @@ true NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true + include;%(AdditionalIncludeDirectories) Console @@ -133,16 +135,15 @@ - - + + - + - - - - + + + diff --git a/Space-Invader.vcxproj.filters b/Space-Invader.vcxproj.filters index bf5a3a1..1ac28a3 100644 --- a/Space-Invader.vcxproj.filters +++ b/Space-Invader.vcxproj.filters @@ -21,27 +21,24 @@ Fichiers sources - + Fichiers sources - + Fichiers sources - + Fichiers sources - + Fichiers d%27en-tête - + Fichiers d%27en-tête - - Fichiers d%27en-tête - - + Fichiers d%27en-tête diff --git a/Space-Invader.vcxproj.user b/Space-Invader.vcxproj.user index 5df420f..966b4ff 100644 --- a/Space-Invader.vcxproj.user +++ b/Space-Invader.vcxproj.user @@ -1,6 +1,6 @@  - false + true \ No newline at end of file diff --git a/include/GameManager.h b/include/GameManager.h new file mode 100644 index 0000000..5c1cab0 --- /dev/null +++ b/include/GameManager.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include +#include "Player.h" +#include "Laser.h" + +using namespace std; +using namespace sf; + +class GameManager +{ +public: + GameManager(); + + void run(); + void handleInputs(); + +private: + Clock _clock; + + RenderWindow _window{ VideoMode::getFullscreenModes()[0], "Space Invader", Style::Fullscreen }; + + Player _player = { _window.getSize() }; + + vector _lasers; +}; + diff --git a/Laser.h b/include/Laser.h similarity index 82% rename from Laser.h rename to include/Laser.h index 2051b6a..6dc164c 100644 --- a/Laser.h +++ b/include/Laser.h @@ -11,7 +11,7 @@ class Laser public: Laser(); - RectangleShape getLaser() { return _laser; } + RectangleShape& getShape() { return _laser; } void setInitialPosition(Vector2f position); diff --git a/Player.h b/include/Player.h similarity index 82% rename from Player.h rename to include/Player.h index c980131..59019e8 100644 --- a/Player.h +++ b/include/Player.h @@ -9,7 +9,7 @@ using namespace sf; class Player { public: - Player(int screenSize); + Player(Vector2u screenSize); CircleShape getShape() { return _shape; }; void move(int direction); @@ -17,6 +17,6 @@ public: private: CircleShape _shape; - int _screenSize; + int _screenWidth; }; diff --git a/lib/GameManager.cpp b/lib/GameManager.cpp new file mode 100644 index 0000000..23bd138 --- /dev/null +++ b/lib/GameManager.cpp @@ -0,0 +1,65 @@ +#include "GameManager.h"; + +GameManager::GameManager() { + _window.setVerticalSyncEnabled(true); +} + +void GameManager::run() { + while (_window.isOpen()) { + + Event event; + while (_window.pollEvent(event)) { + if (event.type == Event::Closed) { + _window.close(); + } + } + + // Handle user's inputs + handleInputs(); + + _window.clear(Color::Black); + + // Draw everything + _window.draw(_player.getShape()); + + for (vector::iterator it = _lasers.begin(); it < _lasers.end();) { + _window.draw((*it)->getShape()); + + (*it)->getShape().move(0, -5); + + if ((*it)->getShape().getPosition().y < -(*it)->getShape().getScale().y) { + it = _lasers.erase(it); + continue; + } + + ++it; + } + + _window.display(); + } +} + +void GameManager::handleInputs() { + // Player movements + if (Keyboard::isKeyPressed(Keyboard::Left) || Keyboard::isKeyPressed(Keyboard::Q)) { + _player.move(-1); + } + + if (Keyboard::isKeyPressed(Keyboard::Right) || Keyboard::isKeyPressed(Keyboard::D)) { + _player.move(1); + } + + if (Keyboard::isKeyPressed(Keyboard::Space)) { + if (_clock.getElapsedTime().asMilliseconds() > 500) { + const Vector2f playerPos = _player.getShape().getPosition(); + const float playerRadius = _player.getShape().getRadius(); + + Laser* laser = new Laser(); + laser->setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius)); + + _lasers.push_back(laser); + + _clock.restart(); + } + } +} \ No newline at end of file diff --git a/Laser.cpp b/lib/Laser.cpp similarity index 83% rename from Laser.cpp rename to lib/Laser.cpp index 0c1132b..8d287b4 100644 --- a/Laser.cpp +++ b/lib/Laser.cpp @@ -2,7 +2,7 @@ Laser::Laser() { - _laser = RectangleShape(Vector2f(2, 20)); + _laser = RectangleShape(Vector2f(4, 30)); _laser.setFillColor(Color::White); } diff --git a/Player.cpp b/lib/Player.cpp similarity index 53% rename from Player.cpp rename to lib/Player.cpp index 9523b88..235e078 100644 --- a/Player.cpp +++ b/lib/Player.cpp @@ -1,18 +1,18 @@ #include "Player.h"; -Player::Player(int screenSize): _screenSize(screenSize) { - _shape = CircleShape(20, 3); +Player::Player(Vector2u screenSize): _screenWidth(screenSize.x) { + _shape = CircleShape(40, 3); _shape.setFillColor(Color(52, 252, 5)); - _shape.setPosition(Vector2f(_screenSize / 2, _screenSize - _shape.getScale().y - 50)); + _shape.setPosition(_screenWidth / 2.f, screenSize.y - _shape.getScale().y - 50); } void Player::move(int direction) { - float max = _screenSize - (_shape.getRadius() * 2); + float max = _screenWidth - (_shape.getRadius() * 2); float result = 5 * direction; // Prevent leaving the window if (_shape.getPosition().x + result < 0 || _shape.getPosition().x + result > max) { - result = 0; + result = 0; } _shape.move(result, 0); diff --git a/main.cpp b/main.cpp index 5a26bf2..9376abc 100644 --- a/main.cpp +++ b/main.cpp @@ -1,51 +1,16 @@ -#include "main.h"; +using namespace std; + +#include + +#include "GameManager.h" int main() { - RenderWindow window(VideoMode(SCREEN_SIZE, SCREEN_SIZE), "Space Invader", Style::Titlebar | Style::Close); - window.setFramerateLimit(60); - window.setVerticalSyncEnabled(true); + cout << "Starting game..." << endl; + GameManager gameManager; - Player player(SCREEN_SIZE); - vector lasers; - - Inputs inputs(player, lasers); - - while (window.isOpen()) - { - Event event; - while (window.pollEvent(event)) - { - if (event.type == sf::Event::Closed) - { - window.close(); - } - } - - // Handle user's inputs - inputs.HandleInputs(); - - window.clear(Color::Black); - - // Draw everything - window.draw(player.getShape()); - - for (vector::iterator it = lasers.begin(); it < lasers.end();) { - window.draw(*it); - it->move(0, -5); - - if (it->getPosition().y < -it->getScale().y) { - it = lasers.erase(it); - } - else { - lasers[distance(lasers.begin(), it)] = *it; - ++it; - } - } - - // Display window - window.display(); - } + cout << "Game started." << endl; + gameManager.run(); return 0; } \ No newline at end of file diff --git a/main.h b/main.h deleted file mode 100644 index 928763b..0000000 --- a/main.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "Player.h"; -#include "Laser.h"; -#include "Inputs.h"; - -using namespace std; -using namespace sf; - -const int SCREEN_SIZE = 1024;