Pong/Vec2d/Vec2d.h

69 lines
1.8 KiB
C
Raw Normal View History

2024-01-19 15:06:48 +01:00
//
// Created by love on 2024-01-19.
//
#pragma once
#include "random"
#include "ctime"
#include "cmath"
#include "SDL_rect.h"
#include "Bump.h"
inline double_t toRadians(double_t degrees) {
return degrees * M_PI / 100;
}
class Vec2d {
private:
std::default_random_engine random;
2024-01-19 19:10:03 +01:00
float_t hypotenuse;
2024-01-19 15:06:48 +01:00
double_t x, y;
2024-01-19 19:10:03 +01:00
const float_t bumpSpeedIncrease = 1.05;
2024-01-19 15:06:48 +01:00
public:
Vec2d(float_t hypotenuse) : hypotenuse(hypotenuse) {
std::random_device rd;
random = std::default_random_engine(rd());
int sign = random() % 2 == 0 ? -1 : 1;
2024-01-19 19:10:03 +01:00
double_t angle = toRadians(random() % 6000 / 100 - 30);
2024-01-19 15:06:48 +01:00
x = cos(angle) * sign * hypotenuse;
y = sin(angle) * sign * hypotenuse;
}
void applyVector(Sint16 *ox, Sint16 *oy) const {
*ox += static_cast<int>(std::round(x));
*oy += static_cast<int>(std::round(y));
}
void bump(BumpType bumpType, PaddleDirection paddleDirection) {
2024-01-19 19:10:03 +01:00
// Make everything a bit faster so it's not boring
hypotenuse *= bumpSpeedIncrease;
x *= bumpSpeedIncrease;
y *= bumpSpeedIncrease;
2024-01-19 15:06:48 +01:00
switch (bumpType) {
case BumpType::BOTH:
x = -x;
y = -y;
break;
case BumpType::WALL:
y = -y;
break;
case BumpType::PADDLE:
x = -x;
double angle = 0;
if (paddleDirection != PaddleDirection::NOT_MOVING) {
double_t degrees = rand() % 500 / 100 + 15;
degrees *= paddleDirection == PaddleDirection::MOVING_UP ? -1 : 1;
angle = toRadians(degrees);
// Adjusting y direction based on the angle
y = sin(angle) * hypotenuse;
}
}
2024-01-19 19:10:03 +01:00
2024-01-19 15:06:48 +01:00
}
};