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 (_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));
+11 -17
View File
@@ -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);
}
+5 -7
View File
@@ -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;
};
+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()
{
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<RectangleShape> 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<RectangleShape>::iterator it = lasers.begin(); it < lasers.end();) {
window.draw(*it);
+1 -2
View File
@@ -11,5 +11,4 @@
using namespace std;
using namespace sf;
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 1024;
const int SCREEN_SIZE = 1024;