This commit is contained in:
2025-10-27 11:53:12 +01:00
parent 231dcd4b3b
commit 5d8811026d
69 changed files with 899 additions and 888 deletions

View File

@@ -17,19 +17,19 @@
// Calcula el cuadrado de la distancia entre dos puntos
double distanceSquared(int x1, int y1, int x2, int y2) {
const int deltaX = x2 - x1;
const int deltaY = y2 - y1;
return deltaX * deltaX + deltaY * deltaY;
const int DELTA_X = x2 - x1;
const int DELTA_Y = y2 - y1;
return (DELTA_X * DELTA_X) + (DELTA_Y * DELTA_Y);
}
// Detector de colisiones entre dos circulos
bool checkCollision(const Circle& a, const Circle& b) {
// Calcula el radio total al cuadrado
int totalRadiusSquared = a.r + b.r;
totalRadiusSquared = totalRadiusSquared * totalRadiusSquared;
int total_radius_squared = a.r + b.r;
total_radius_squared = total_radius_squared * total_radius_squared;
// Si la distancia entre el centro de los circulos es inferior a la suma de sus radios
if (distanceSquared(a.x, a.y, b.x, b.y) < (totalRadiusSquared)) {
if (distanceSquared(a.x, a.y, b.x, b.y) < (total_radius_squared)) {
// Los circulos han colisionado
return true;
}
@@ -42,7 +42,8 @@ bool checkCollision(const Circle& a, const Circle& b) {
bool checkCollision(const Circle& a, const SDL_FRect& rect) {
SDL_Rect b = toSDLRect(rect);
// Closest point on collision box
int cX, cY;
int cX;
int cY;
// Find closest x offset
if (a.x < b.x) {
@@ -77,31 +78,31 @@ bool checkCollision(const SDL_FRect& rect_a, const SDL_FRect& rect_b) {
SDL_Rect a = toSDLRect(rect_a);
SDL_Rect b = toSDLRect(rect_b);
// Calcula las caras del rectangulo a
const int leftA = a.x;
const int rightA = a.x + a.w;
const int topA = a.y;
const int bottomA = a.y + a.h;
const int LEFT_A = a.x;
const int RIGHT_A = a.x + a.w;
const int TOP_A = a.y;
const int BOTTOM_A = a.y + a.h;
// Calcula las caras del rectangulo b
const int leftB = b.x;
const int rightB = b.x + b.w;
const int topB = b.y;
const int bottomB = b.y + b.h;
const int LEFT_B = b.x;
const int RIGHT_B = b.x + b.w;
const int TOP_B = b.y;
const int BOTTOM_B = b.y + b.h;
// Si cualquiera de las caras de a está fuera de b
if (bottomA <= topB) {
if (BOTTOM_A <= TOP_B) {
return false;
}
if (topA >= bottomB) {
if (TOP_A >= BOTTOM_B) {
return false;
}
if (rightA <= leftB) {
if (RIGHT_A <= LEFT_B) {
return false;
}
if (leftA >= rightB) {
if (LEFT_A >= RIGHT_B) {
return false;
}
@@ -222,54 +223,54 @@ bool checkCollision(const LineHorizontal& l, const SDL_FPoint& point) {
// Detector de colisiones entre dos lineas
SDL_Point checkCollision(const Line& l1, const Line& l2) {
const float x1 = l1.x1;
const float y1 = l1.y1;
const float x2 = l1.x2;
const float y2 = l1.y2;
const float X1 = l1.x1;
const float Y1 = l1.y1;
const float X2 = l1.x2;
const float Y2 = l1.y2;
const float x3 = l2.x1;
const float y3 = l2.y1;
const float x4 = l2.x2;
const float y4 = l2.y2;
const float X3 = l2.x1;
const float Y3 = l2.y1;
const float X4 = l2.x2;
const float Y4 = l2.y2;
// calculate the direction of the lines
float uA = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1));
float uB = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1));
float u_a = ((X4 - X3) * (Y1 - Y3) - (Y4 - Y3) * (X1 - X3)) / ((Y4 - Y3) * (X2 - X1) - (X4 - X3) * (Y2 - Y1));
float u_b = ((X2 - X1) * (Y1 - Y3) - (Y2 - Y1) * (X1 - X3)) / ((Y4 - Y3) * (X2 - X1) - (X4 - X3) * (Y2 - Y1));
// if uA and uB are between 0-1, lines are colliding
if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {
if (u_a >= 0 && u_a <= 1 && u_b >= 0 && u_b <= 1) {
// Calcula la intersección
const float x = x1 + (uA * (x2 - x1));
const float y = y1 + (uA * (y2 - y1));
const float X = X1 + (u_a * (X2 - X1));
const float Y = Y1 + (u_a * (Y2 - Y1));
return {static_cast<int>(round(x)), static_cast<int>(round(y))};
return {static_cast<int>(round(X)), static_cast<int>(round(Y))};
}
return {-1, -1};
}
// Detector de colisiones entre dos lineas
SDL_Point checkCollision(const LineDiagonal& l1, const LineVertical& l2) {
const float x1 = l1.x1;
const float y1 = l1.y1;
const float x2 = l1.x2;
const float y2 = l1.y2;
const float X1 = l1.x1;
const float Y1 = l1.y1;
const float X2 = l1.x2;
const float Y2 = l1.y2;
const float x3 = l2.x;
const float y3 = l2.y1;
const float x4 = l2.x;
const float y4 = l2.y2;
const float X3 = l2.x;
const float Y3 = l2.y1;
const float X4 = l2.x;
const float Y4 = l2.y2;
// calculate the direction of the lines
float uA = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1));
float uB = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1));
float u_a = ((X4 - X3) * (Y1 - Y3) - (Y4 - Y3) * (X1 - X3)) / ((Y4 - Y3) * (X2 - X1) - (X4 - X3) * (Y2 - Y1));
float u_b = ((X2 - X1) * (Y1 - Y3) - (Y2 - Y1) * (X1 - X3)) / ((Y4 - Y3) * (X2 - X1) - (X4 - X3) * (Y2 - Y1));
// if uA and uB are between 0-1, lines are colliding
if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {
if (u_a >= 0 && u_a <= 1 && u_b >= 0 && u_b <= 1) {
// Calcula la intersección
const float x = x1 + (uA * (x2 - x1));
const float y = y1 + (uA * (y2 - y1));
const float X = X1 + (u_a * (X2 - X1));
const float Y = Y1 + (u_a * (Y2 - Y1));
return {static_cast<int>(x), static_cast<int>(y)};
return {static_cast<int>(X), static_cast<int>(Y)};
}
return {-1, -1};
}
@@ -279,12 +280,12 @@ void normalizeLine(LineDiagonal& l) {
// Las lineas diagonales van de izquierda a derecha
// x2 mayor que x1
if (l.x2 < l.x1) {
const int x = l.x1;
const int y = l.y1;
const int X = l.x1;
const int Y = l.y1;
l.x1 = l.x2;
l.y1 = l.y2;
l.x2 = x;
l.y2 = y;
l.x2 = X;
l.y2 = Y;
}
}
@@ -324,7 +325,7 @@ bool checkCollision(const SDL_FPoint& point, const LineDiagonal& l) {
// Convierte una cadena a un indice de la paleta
Uint8 stringToColor(const std::string& str) {
// Mapas de colores para cada paleta
static const std::unordered_map<std::string, Uint8> paletteMap = {
static const std::unordered_map<std::string, Uint8> PALETTE_MAP = {
{"black", 0},
{"bright_black", 1},
@@ -352,29 +353,27 @@ Uint8 stringToColor(const std::string& str) {
{"transparent", 255}};
// Busca el color en el mapa
auto it = paletteMap.find(str);
if (it != paletteMap.end()) {
auto it = PALETTE_MAP.find(str);
if (it != PALETTE_MAP.end()) {
return it->second;
} else {
// Si no se encuentra el color, devolvemos negro por defecto
return 0;
}
} // Si no se encuentra el color, devolvemos negro por defecto
return 0;
}
// Convierte una cadena a un entero de forma segura
int safeStoi(const std::string& value, int defaultValue) {
int safeStoi(const std::string& value, int default_value) {
try {
return std::stoi(value);
} catch (const std::exception&) {
return defaultValue;
return default_value;
}
}
// Convierte una cadena a un booleano
bool stringToBool(const std::string& str) {
std::string lowerStr = str;
std::transform(lowerStr.begin(), lowerStr.end(), lowerStr.begin(), ::tolower);
return (lowerStr == "true" || lowerStr == "1" || lowerStr == "yes" || lowerStr == "on");
std::string lower_str = str;
std::transform(lower_str.begin(), lower_str.end(), lower_str.begin(), ::tolower);
return (lower_str == "true" || lower_str == "1" || lower_str == "yes" || lower_str == "on");
}
// Convierte un booleano a una cadena
@@ -384,11 +383,11 @@ std::string boolToString(bool value) {
// Compara dos colores
bool colorAreEqual(Color color1, Color color2) {
const bool r = color1.r == color2.r;
const bool g = color1.g == color2.g;
const bool b = color1.b == color2.b;
const bool R = color1.r == color2.r;
const bool G = color1.g == color2.g;
const bool B = color1.b == color2.b;
return (r && g && b);
return (R && G && B);
}
// Función para convertir un string a minúsculas