refactor(player): clean code => Lightweight

This commit is contained in:
2024-01-09 14:15:17 +01:00
parent 9b0a7a78bb
commit 8d542b6484
6 changed files with 33 additions and 31 deletions
+2 -2
View File
@@ -12,8 +12,8 @@ void Inputs::HandleInputs() {
if (Keyboard::isKeyPressed(Keyboard::Space)) { if (Keyboard::isKeyPressed(Keyboard::Space)) {
if (_clock.getElapsedTime().asMilliseconds() > 500) { if (_clock.getElapsedTime().asMilliseconds() > 500) {
const Vector2f playerPos = _player.PlayerShape.getPosition(); const Vector2f playerPos = _player.getShape().getPosition();
const float playerRadius = _player.PlayerShape.getRadius(); const float playerRadius = _player.getShape().getRadius();
Laser laser; Laser laser;
laser.setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius)); laser.setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius));
+11 -17
View File
@@ -1,25 +1,19 @@
#include "Player.h"; #include "Player.h";
void Player::drawPlayer() { Player::Player(int screenSize): _screenSize(screenSize) {
PlayerShape.setFillColor(Color(52, 252, 5)); _shape = CircleShape(20, 3);
PlayerShape.setPosition(Vector2f(_window.getSize().x / 2 + _x, _window.getSize().y - PlayerShape.getScale().y - 50)); _shape.setFillColor(Color(52, 252, 5));
_shape.setPosition(Vector2f(_screenSize / 2, _screenSize - _shape.getScale().y - 50));
_window.draw(PlayerShape);
} }
void Player::move(int direction) { 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; // Prevent leaving the window
int maxX = _window.getSize().x / 2 - ((int)PlayerShape.getRadius() * 2); if (_shape.getPosition().x + result < 0 || _shape.getPosition().x + result > max) {
result = 0;
}
if (result < minX) { _shape.move(result, 0);
result = minX;
}
else if (result > maxX) {
result = maxX;
}
else {
_x = result;
}
} }
+5 -7
View File
@@ -9,16 +9,14 @@ using namespace sf;
class Player class Player
{ {
public: public:
Player(RenderWindow& window): _window(window) {} Player(int screenSize);
CircleShape PlayerShape = CircleShape(20, 3); CircleShape getShape() { return _shape; };
void drawPlayer();
void move(int direction); void move(int direction);
private: private:
int _x = 0; CircleShape _shape;
int _velocity = 7;
RenderWindow& _window; int _screenSize;
}; };
+11
View File
@@ -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;
}
+3 -3
View File
@@ -2,11 +2,11 @@
int main() 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.setFramerateLimit(60);
window.setVerticalSyncEnabled(true); window.setVerticalSyncEnabled(true);
Player player(window); Player player(SCREEN_SIZE);
vector<RectangleShape> lasers; vector<RectangleShape> lasers;
Inputs inputs(player, lasers); Inputs inputs(player, lasers);
@@ -28,7 +28,7 @@ int main()
window.clear(Color::Black); window.clear(Color::Black);
// Draw everything // Draw everything
player.drawPlayer(); window.draw(player.getShape());
for (vector<RectangleShape>::iterator it = lasers.begin(); it < lasers.end();) { for (vector<RectangleShape>::iterator it = lasers.begin(); it < lasers.end();) {
window.draw(*it); window.draw(*it);
+1 -2
View File
@@ -11,5 +11,4 @@
using namespace std; using namespace std;
using namespace sf; using namespace sf;
const int SCREEN_WIDTH = 1024; const int SCREEN_SIZE = 1024;
const int SCREEN_HEIGHT = 1024;