From 8d542b64845d9974d71500ce8111be49b0997b32 Mon Sep 17 00:00:00 2001 From: UnEpicier Date: Tue, 9 Jan 2024 14:15:17 +0100 Subject: [PATCH] refactor(player): clean code => Lightweight --- Inputs.cpp | 4 ++-- Player.cpp | 28 +++++++++++----------------- Player.h | 12 +++++------- Shield.cpp | 11 +++++++++++ main.cpp | 6 +++--- main.h | 3 +-- 6 files changed, 33 insertions(+), 31 deletions(-) create mode 100644 Shield.cpp diff --git a/Inputs.cpp b/Inputs.cpp index f3dab5d..be0de1a 100644 --- a/Inputs.cpp +++ b/Inputs.cpp @@ -12,8 +12,8 @@ void Inputs::HandleInputs() { if (Keyboard::isKeyPressed(Keyboard::Space)) { if (_clock.getElapsedTime().asMilliseconds() > 500) { - const Vector2f playerPos = _player.PlayerShape.getPosition(); - const float playerRadius = _player.PlayerShape.getRadius(); + const Vector2f playerPos = _player.getShape().getPosition(); + const float playerRadius = _player.getShape().getRadius(); Laser laser; laser.setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius)); diff --git a/Player.cpp b/Player.cpp index cfb68af..9523b88 100644 --- a/Player.cpp +++ b/Player.cpp @@ -1,25 +1,19 @@ #include "Player.h"; -void Player::drawPlayer() { - PlayerShape.setFillColor(Color(52, 252, 5)); - PlayerShape.setPosition(Vector2f(_window.getSize().x / 2 + _x, _window.getSize().y - PlayerShape.getScale().y - 50)); - - _window.draw(PlayerShape); +Player::Player(int screenSize): _screenSize(screenSize) { + _shape = CircleShape(20, 3); + _shape.setFillColor(Color(52, 252, 5)); + _shape.setPosition(Vector2f(_screenSize / 2, _screenSize - _shape.getScale().y - 50)); } void Player::move(int direction) { - int result = _x + _velocity * direction; + float max = _screenSize - (_shape.getRadius() * 2); + float result = 5 * direction; - int minX = _window.getSize().x / 2 * -1; - int maxX = _window.getSize().x / 2 - ((int)PlayerShape.getRadius() * 2); + // Prevent leaving the window + if (_shape.getPosition().x + result < 0 || _shape.getPosition().x + result > max) { + result = 0; + } - if (result < minX) { - result = minX; - } - else if (result > maxX) { - result = maxX; - } - else { - _x = result; - } + _shape.move(result, 0); } \ No newline at end of file diff --git a/Player.h b/Player.h index cd6da16..c980131 100644 --- a/Player.h +++ b/Player.h @@ -9,16 +9,14 @@ using namespace sf; class Player { public: - Player(RenderWindow& window): _window(window) {} + Player(int screenSize); - CircleShape PlayerShape = CircleShape(20, 3); - - void drawPlayer(); + CircleShape getShape() { return _shape; }; void move(int direction); private: - int _x = 0; - int _velocity = 7; - RenderWindow& _window; + CircleShape _shape; + + int _screenSize; }; diff --git a/Shield.cpp b/Shield.cpp new file mode 100644 index 0000000..7b3e4bb --- /dev/null +++ b/Shield.cpp @@ -0,0 +1,11 @@ +#include "Shield.h" + +Shield::Shield() { + _shield = RectangleShape(Vector2f(30, 20)); + _shield.setFillColor(Color(52, 252, 5)); +} + +void Shield::decreaseLife() +{ + _life -= 1; +} diff --git a/main.cpp b/main.cpp index ba15fd8..5a26bf2 100644 --- a/main.cpp +++ b/main.cpp @@ -2,11 +2,11 @@ int main() { - RenderWindow window(VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Space Invader", Style::Titlebar | Style::Close); + RenderWindow window(VideoMode(SCREEN_SIZE, SCREEN_SIZE), "Space Invader", Style::Titlebar | Style::Close); window.setFramerateLimit(60); window.setVerticalSyncEnabled(true); - Player player(window); + Player player(SCREEN_SIZE); vector lasers; Inputs inputs(player, lasers); @@ -28,7 +28,7 @@ int main() window.clear(Color::Black); // Draw everything - player.drawPlayer(); + window.draw(player.getShape()); for (vector::iterator it = lasers.begin(); it < lasers.end();) { window.draw(*it); diff --git a/main.h b/main.h index eb7a5e8..928763b 100644 --- a/main.h +++ b/main.h @@ -11,5 +11,4 @@ using namespace std; using namespace sf; -const int SCREEN_WIDTH = 1024; -const int SCREEN_HEIGHT = 1024; \ No newline at end of file +const int SCREEN_SIZE = 1024;