Files
jaildoctors_dilemma/source/utils.cpp

315 lines
6.0 KiB
C++

#include "utils.h"
// 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;
}
// Detector de colisiones entre dos circulos
bool checkCollision(circle_t &a, circle_t &b)
{
// Calcula el radio total al cuadrado
int totalRadiusSquared = a.r + b.r;
totalRadiusSquared = totalRadiusSquared * totalRadiusSquared;
// 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))
{
// Los circulos han colisionado
return true;
}
// En caso contrario
return false;
}
// Detector de colisiones entre un circulo y un rectangulo
bool checkCollision(circle_t &a, SDL_Rect &b)
{
// Closest point on collision box
int cX, cY;
// Find closest x offset
if (a.x < b.x)
{
cX = b.x;
}
else if (a.x > b.x + b.w)
{
cX = b.x + b.w;
}
else
{
cX = a.x;
}
// Find closest y offset
if (a.y < b.y)
{
cY = b.y;
}
else if (a.y > b.y + b.h)
{
cY = b.y + b.h;
}
else
{
cY = a.y;
}
// If the closest point is inside the circle_t
if (distanceSquared(a.x, a.y, cX, cY) < a.r * a.r)
{
// This box and the circle_t have collided
return true;
}
// If the shapes have not collided
return false;
}
// Detector de colisiones entre dos rectangulos
bool checkCollision(SDL_Rect &a, SDL_Rect &b)
{
// Calculate the sides of rect 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;
// Calculate the sides of rect 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;
// If any of the sides from A are outside of B
if (bottomA <= topB)
{
return false;
}
if (topA >= bottomB)
{
return false;
}
if (rightA <= leftB)
{
return false;
}
if (leftA >= rightB)
{
return false;
}
// If none of the sides from A are outside B
return true;
}
// Detector de colisiones entre un punto y u rectangulo
bool checkCollision(SDL_Point &p, SDL_Rect &r)
{
// Comprueba si el punto está fuera del rectangulo en el eje X
if (p.x < r.x)
{
return false;
}
if (p.x > r.x + r.w)
{
return false;
}
// Comprueba si el punto está fuera del rectangulo en el eje Y
if (p.y < r.y)
{
return false;
}
if (p.y > r.y + r.h)
{
return false;
}
// Si ha llegado hasta aquí, es que está dentro
return true;
}
// Devuelve un color_t a partir de un string
color_t stringToColor(std::string str)
{
const std::string palette = "spectrum";
if (palette = "spectrum")
{
if (str == "black")
{
return {0x00, 0x00, 0x00};
}
else if (str == "light_black")
{
return {0x00, 0x00, 0x00};
}
else if (str == "blue")
{
return {0x00, 0x00, 0xFF};
}
else if (str == "light_blue")
{
return {0x00, 0x00, 0xEE};
}
else if (str == "red")
{
return {0xFF, 0x00, 0x00};
}
else if (str == "light_red")
{
return {0xEE, 0x00, 0x00};
}
else if (str == "purple")
{
return {0xFF, 0x00, 0xFF};
}
else if (str == "light_purple")
{
return {0xEE, 0x00, 0xEE};
}
else if (str == "green")
{
return {0x00, 0xFF, 0x00};
}
else if (str == "light_green")
{
return {0x00, 0xEE, 0x00};
}
else if (str == "cyan")
{
return {0x00, 0xFF, 0xFF};
}
else if (str == "light_cyan")
{
return {0x00, 0xEE, 0xEE};
}
else if (str == "yellow")
{
return {0xFF, 0xFF, 0x00};
}
else if (str == "light_yellow")
{
return {0xEE, 0xEE, 0x00};
}
else if (str == "white")
{
return {0xEE, 0xEE, 0xEE};
}
else if (str == "light_white")
{
return {0xFF, 0xFF, 0xFF};
}
}
else
{
if (str == "black")
{
return {0x00, 0x00, 0x00};
}
else if (str == "light_black")
{
return {0x3C, 0x35, 0x1F};
}
else if (str == "blue")
{
return {0x31, 0x33, 0x90};
}
else if (str == "light_blue")
{
return {0x15, 0x59, 0xDB};
}
else if (str == "red")
{
return {0xA7, 0x32, 0x11};
}
else if (str == "light_red")
{
return {0xD8, 0x55, 0x25};
}
else if (str == "purple")
{
return {0xA1, 0x55, 0x89};
}
else if (str == "light_purple")
{
return {0xCD, 0x7A, 0x50};
}
else if (str == "green")
{
return {0x62, 0x9A, 0x31};
}
else if (str == "light_green")
{
return {0x9C, 0xD3, 0x3C};
}
else if (str == "cyan")
{
return {0x28, 0xA4, 0xCB};
}
else if (str == "light_cyan")
{
return {0x65, 0xDC, 0xD6};
}
else if (str == "yellow")
{
return {0xE8, 0xBC, 0x50};
}
else if (str == "light_yellow")
{
return {0xF1, 0xE7, 0x82};
}
else if (str == "white")
{
return {0xBF, 0xBF, 0xBD};
}
else if (str == "light_white")
{
return {0xF2, 0xF1, 0xED};
}
}
return {0x00, 0x00, 0x00};
}