mirror of
https://github.com/lov3b/Pong.git
synced 2025-06-28 07:50:22 +02:00
physics input works
This commit is contained in:
83
VisibleObjects/Ball.h
Normal file
83
VisibleObjects/Ball.h
Normal file
@ -0,0 +1,83 @@
|
||||
//
|
||||
// Created by love on 2024-01-18.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include "../utils.h"
|
||||
#include "../Vec2d/Vec2d.h"
|
||||
#include "PlayerPaddle.h"
|
||||
#include <SDL2/SDL2_gfxPrimitives.h>
|
||||
#include <iostream>
|
||||
#include "optional"
|
||||
|
||||
class Ball {
|
||||
private:
|
||||
static const uint8_t RADIUS = 15;
|
||||
const SDL_Point *screen;
|
||||
Sint16 x, y;
|
||||
Vec2d *vec2d;
|
||||
static const uint32_t color = 0xCD5C5CFF;
|
||||
const PlayerPaddle *leftPaddle, *rightPaddle;
|
||||
|
||||
public:
|
||||
explicit Ball(const SDL_Point *screen, const PlayerPaddle *leftPaddle, const PlayerPaddle *rightPaddle) {
|
||||
this->screen = screen;
|
||||
this->x = screen->x / 2;
|
||||
this->y = screen->y / 2;
|
||||
this->vec2d = new Vec2d(2.5);
|
||||
this->leftPaddle = leftPaddle;
|
||||
this->rightPaddle = rightPaddle;
|
||||
}
|
||||
|
||||
void draw(SDL_Renderer *renderer) const {
|
||||
filledCircleColor(renderer, x, y, RADIUS, color);
|
||||
}
|
||||
|
||||
void update() {
|
||||
std::cout << "Ball x: " << x << ", y: " << y << std::endl;
|
||||
std::optional<Side> paddleSide = collidedPaddle();
|
||||
bool screenEdgeVertical = collidedScreenEdgeVertical();
|
||||
if (screenEdgeVertical && paddleSide.has_value()) {
|
||||
vec2d->bump(BumpType::BOTH, PaddleDirection::NONE);
|
||||
} else if (screenEdgeVertical) {
|
||||
vec2d->bump(BumpType::WALL, PaddleDirection::NONE);
|
||||
} else if (collidedScreenEdgeHorizontal()) {
|
||||
std::cout << "Player won" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
if (paddleSide.has_value()) {
|
||||
const PlayerPaddle *paddle = paddleSide.value() == Side::LEFT ? leftPaddle : rightPaddle;
|
||||
vec2d->bump(BumpType::PADDLE, paddle->getPaddleDirection());
|
||||
}
|
||||
vec2d->applyVector(&x, &y);
|
||||
}
|
||||
|
||||
private:
|
||||
bool collidedScreenEdgeVertical() {
|
||||
return y - RADIUS <= 0 || y + RADIUS >= screen->y;
|
||||
}
|
||||
|
||||
bool collidedScreenEdgeHorizontal() {
|
||||
return x + RADIUS >= screen->x || x - RADIUS <= 0;
|
||||
}
|
||||
|
||||
std::optional<Side> collidedPaddle() {
|
||||
// Check collision with right paddle
|
||||
if (x + RADIUS >= rightPaddle->x &&
|
||||
y >= rightPaddle->y &&
|
||||
y <= rightPaddle->y + rightPaddle->h) {
|
||||
return Side::RIGHT;
|
||||
}
|
||||
// Check collision with left paddle
|
||||
else if (x - RADIUS <= leftPaddle->x + leftPaddle->w &&
|
||||
y >= leftPaddle->y &&
|
||||
y <= leftPaddle->y + leftPaddle->h) {
|
||||
return Side::LEFT;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
79
VisibleObjects/PlayerPaddle.h
Normal file
79
VisibleObjects/PlayerPaddle.h
Normal file
@ -0,0 +1,79 @@
|
||||
//
|
||||
// Created by love on 2024-01-19.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
#include "Side.h"
|
||||
#include "../Vec2d/Bump.h"
|
||||
|
||||
class PlayerPaddle : public SDL_Rect {
|
||||
private:
|
||||
static const int MOVE_PER_TICK = 5;
|
||||
const SDL_Point *screen;
|
||||
bool movingUp, movingDown;
|
||||
uint8_t color[4]{};
|
||||
|
||||
public:
|
||||
PlayerPaddle(const SDL_Point *screen, const Side side) : SDL_Rect() {
|
||||
w = 20;
|
||||
h = 80;
|
||||
x = side == Side::LEFT ? 0 : screen->x - w;
|
||||
y = (screen->y - h) / 2;
|
||||
movingUp = false;
|
||||
movingDown = false;
|
||||
this->screen = screen;
|
||||
|
||||
color[0] = 255;
|
||||
color[1] = 234;
|
||||
color[2] = 0;
|
||||
color[3] = 255;
|
||||
}
|
||||
|
||||
PaddleDirection getPaddleDirection() const {
|
||||
if (movingUp != movingDown)
|
||||
return PaddleDirection::NOT_MOVING;
|
||||
else if (movingUp)
|
||||
return PaddleDirection::MOVING_UP;
|
||||
return PaddleDirection::MOVING_DOWN;
|
||||
}
|
||||
|
||||
void draw(SDL_Renderer *renderer) {
|
||||
SDL_SetRenderDrawColor(renderer, color[0], color[1], color[2], color[3]);
|
||||
SDL_RenderFillRect(renderer, this);
|
||||
}
|
||||
|
||||
void startMoving(bool up) {
|
||||
if (up)
|
||||
movingUp = true;
|
||||
else movingDown = true;
|
||||
}
|
||||
|
||||
void stopMoving(bool up) {
|
||||
if (up)
|
||||
movingUp = false;
|
||||
else movingDown = false;
|
||||
}
|
||||
|
||||
void update() {
|
||||
// We cannot move up and down
|
||||
if (movingUp == movingDown)
|
||||
return;
|
||||
|
||||
if (movingUp && canMoveUp())
|
||||
y -= MOVE_PER_TICK;
|
||||
else if (movingDown && canMoveDown())
|
||||
y += MOVE_PER_TICK;
|
||||
}
|
||||
|
||||
private:
|
||||
bool canMoveDown() {
|
||||
return y + h < screen->y;
|
||||
}
|
||||
|
||||
bool canMoveUp() {
|
||||
return y > 0;
|
||||
}
|
||||
|
||||
};
|
9
VisibleObjects/Side.h
Normal file
9
VisibleObjects/Side.h
Normal file
@ -0,0 +1,9 @@
|
||||
//
|
||||
// Created by love on 2024-01-19.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
enum class Side {
|
||||
LEFT, RIGHT
|
||||
};
|
Reference in New Issue
Block a user