mirror of
https://github.com/lov3b/Pong.git
synced 2025-01-18 20:50:12 +01:00
cleanup
This commit is contained in:
parent
a86a3c170a
commit
62e7eb1e93
@ -22,14 +22,9 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Ball(const SDL_Point *screen, const PlayerPaddle *leftPaddle, const PlayerPaddle *rightPaddle,
|
explicit Ball(const SDL_Point *screen, const PlayerPaddle *leftPaddle, const PlayerPaddle *rightPaddle,
|
||||||
Score *score) {
|
Score *score) :
|
||||||
this->score = score;
|
score(score), screen(screen), leftPaddle(leftPaddle), rightPaddle(rightPaddle), x(screen->x / 2),
|
||||||
this->screen = screen;
|
y(screen->y / 2), vec2d(new Vec2d(6)) {
|
||||||
this->leftPaddle = leftPaddle;
|
|
||||||
this->rightPaddle = rightPaddle;
|
|
||||||
this->x = screen->x / 2;
|
|
||||||
this->y = screen->y / 2;
|
|
||||||
vec2d = new Vec2d(6);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void resetPosition() {
|
void resetPosition() {
|
||||||
@ -66,11 +61,11 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool collidedScreenEdgeVertical() {
|
[[nodiscard]]bool collidedScreenEdgeVertical() const {
|
||||||
return y - RADIUS <= 0 || y + RADIUS >= screen->y;
|
return y - RADIUS <= 0 || y + RADIUS >= screen->y;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<Side> collidedScreenEdgeHorizontal() {
|
[[nodiscard]] std::optional<Side> collidedScreenEdgeHorizontal() const {
|
||||||
if (x + RADIUS >= screen->x)
|
if (x + RADIUS >= screen->x)
|
||||||
return Side::RIGHT;
|
return Side::RIGHT;
|
||||||
else if (x - RADIUS <= 0)
|
else if (x - RADIUS <= 0)
|
||||||
@ -78,7 +73,7 @@ private:
|
|||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<Side> collidedPaddle() {
|
[[nodiscard]] std::optional<Side> collidedPaddle() const {
|
||||||
// Right paddle
|
// Right paddle
|
||||||
if (x + RADIUS >= rightPaddle->x &&
|
if (x + RADIUS >= rightPaddle->x &&
|
||||||
y >= rightPaddle->y &&
|
y >= rightPaddle->y &&
|
||||||
|
@ -44,13 +44,13 @@ public:
|
|||||||
SDL_RenderFillRect(renderer, this);
|
SDL_RenderFillRect(renderer, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void startMoving(bool up) {
|
void startMoving(const bool up) {
|
||||||
if (up)
|
if (up)
|
||||||
movingUp = true;
|
movingUp = true;
|
||||||
else movingDown = true;
|
else movingDown = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void stopMoving(bool up) {
|
void stopMoving(const bool up) {
|
||||||
if (up)
|
if (up)
|
||||||
movingUp = false;
|
movingUp = false;
|
||||||
else movingDown = false;
|
else movingDown = false;
|
||||||
@ -68,11 +68,11 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool canMoveDown() {
|
[[nodiscard]] bool canMoveDown() const {
|
||||||
return y + h < screen->y;
|
return y + h < screen->y;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool canMoveUp() {
|
[[nodiscard]] bool canMoveUp() const {
|
||||||
return y > 0;
|
return y > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ private:
|
|||||||
const int shadowOffset = 3;
|
const int shadowOffset = 3;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Score(uint8_t max_score, SDL_Point *screenSize, const std::function<void(Side)> whenWon) : MAX_SCORE(
|
explicit Score(uint8_t max_score, SDL_Point *screenSize, const std::function<void(Side)> &whenWon) : MAX_SCORE(
|
||||||
max_score), whenWon(whenWon) {
|
max_score), whenWon(whenWon) {
|
||||||
resetScore();
|
resetScore();
|
||||||
if (defaultFontPath == nullptr) {
|
if (defaultFontPath == nullptr) {
|
||||||
@ -57,16 +57,13 @@ public:
|
|||||||
~Score() {
|
~Score() {
|
||||||
if (font)
|
if (font)
|
||||||
TTF_CloseFont(font);
|
TTF_CloseFont(font);
|
||||||
if (surface)
|
|
||||||
SDL_FreeSurface(surface);
|
SDL_FreeSurface(surface);
|
||||||
}
|
}
|
||||||
|
|
||||||
void update() {
|
void update() {
|
||||||
if (!hasIncremented && surface != nullptr && shadowSurface != nullptr) return;
|
if (!hasIncremented && surface != nullptr && shadowSurface != nullptr) return;
|
||||||
|
|
||||||
if (surface != nullptr)
|
|
||||||
SDL_FreeSurface(surface);
|
SDL_FreeSurface(surface);
|
||||||
if (shadowSurface != nullptr)
|
|
||||||
SDL_FreeSurface(shadowSurface);
|
SDL_FreeSurface(shadowSurface);
|
||||||
|
|
||||||
hasIncremented = false;
|
hasIncremented = false;
|
||||||
@ -112,7 +109,7 @@ public:
|
|||||||
leftScore = rightScore = 0;
|
leftScore = rightScore = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void incrementScore(Side side) {
|
void incrementScore(const Side side) {
|
||||||
hasIncremented = true;
|
hasIncremented = true;
|
||||||
uint8_t temp;
|
uint8_t temp;
|
||||||
switch (side) {
|
switch (side) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user