forked from jaildesigner-jailgames/jaildoctors_dilemma
Creadas las colisiones con los enemigos
This commit is contained in:
@@ -12,11 +12,11 @@
|
|||||||
41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,
|
41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,
|
||||||
41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,41,
|
41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,41,
|
||||||
41,41,41,0,0,0,0,0,0,0,0,0,0,0,41,41,41,41,41,41,41,41,0,0,0,0,0,0,0,0,0,41,
|
41,41,41,0,0,0,0,0,0,0,0,0,0,0,41,41,41,41,41,41,41,41,0,0,0,0,0,0,0,0,0,41,
|
||||||
|
41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,81,0,0,0,0,0,0,41,
|
||||||
41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,
|
41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,
|
||||||
41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,0,0,0,0,0,41,
|
|
||||||
41,0,0,0,0,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,0,0,0,0,0,41,
|
41,0,0,0,0,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,0,0,0,0,0,41,
|
||||||
0,0,0,0,0,0,0,0,0,81,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
41,0,0,0,0,0,0,0,0,81,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,
|
41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,
|
||||||
41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41
|
41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41
|
||||||
</data>
|
</data>
|
||||||
|
|||||||
@@ -15,8 +15,8 @@
|
|||||||
43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,43,
|
43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,43,
|
||||||
43,0,0,0,0,0,0,0,0,83,0,0,0,0,0,0,0,0,0,3,0,0,0,0,83,83,0,0,0,0,0,43,
|
43,0,0,0,0,0,0,0,0,83,0,0,0,0,0,0,0,0,0,3,0,0,0,0,83,83,0,0,0,0,0,43,
|
||||||
43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,43,
|
43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,43,
|
||||||
0,0,0,0,0,0,0,83,83,83,83,83,83,0,0,0,0,0,0,3,0,0,0,0,83,83,0,0,0,0,0,0,
|
0,0,0,0,0,0,0,83,83,83,83,83,83,0,0,0,0,0,0,3,0,0,0,0,83,83,0,0,0,0,0,43,
|
||||||
0,0,0,0,0,83,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,
|
0,0,0,0,0,83,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,43,
|
||||||
63,43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,43,43,43,43,43,43,43,43,43,43,43,63,
|
63,43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,43,43,43,43,43,43,43,43,43,43,43,63,
|
||||||
63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63
|
63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63
|
||||||
</data>
|
</data>
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ Enemy::Enemy(enemy_t enemy)
|
|||||||
sprite->setAnimationFrames(0, 2, 16 * 2, 0, 16, 16);
|
sprite->setAnimationFrames(0, 2, 16 * 2, 0, 16, 16);
|
||||||
sprite->setAnimationFrames(0, 3, 16 * 3, 0, 16, 16);
|
sprite->setAnimationFrames(0, 3, 16 * 3, 0, 16, 16);
|
||||||
sprite->setSpriteClip(sprite->getAnimationClip(0, 0));
|
sprite->setSpriteClip(sprite->getAnimationClip(0, 0));
|
||||||
|
|
||||||
|
collider = getRect();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
@@ -67,6 +69,7 @@ void Enemy::update()
|
|||||||
sprite->update();
|
sprite->update();
|
||||||
sprite->animate(0);
|
sprite->animate(0);
|
||||||
checkPath();
|
checkPath();
|
||||||
|
collider = getRect();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Comprueba si ha llegado al limite del recorrido para darse media vuelta
|
// Comprueba si ha llegado al limite del recorrido para darse media vuelta
|
||||||
@@ -74,13 +77,25 @@ void Enemy::checkPath()
|
|||||||
{
|
{
|
||||||
if (sprite->getPosX() > x2 || sprite->getPosX() < x1)
|
if (sprite->getPosX() > x2 || sprite->getPosX() < x1)
|
||||||
{
|
{
|
||||||
//sprite->setPosX(sprite->getPosX() - sprite->getVelX());
|
// sprite->setPosX(sprite->getPosX() - sprite->getVelX());
|
||||||
sprite->setVelX(sprite->getVelX() * (-1));
|
sprite->setVelX(sprite->getVelX() * (-1));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sprite->getPosY() > y2 || sprite->getPosY() < y1)
|
if (sprite->getPosY() > y2 || sprite->getPosY() < y1)
|
||||||
{
|
{
|
||||||
//sprite->setPosY(sprite->getPosY() - sprite->getVelY());
|
// sprite->setPosY(sprite->getPosY() - sprite->getVelY());
|
||||||
sprite->setVelY(sprite->getVelY() * (-1));
|
sprite->setVelY(sprite->getVelY() * (-1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Devuelve el rectangulo que contiene al enemigo
|
||||||
|
SDL_Rect Enemy::getRect()
|
||||||
|
{
|
||||||
|
return sprite->getRect();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el rectangulo de colision del enemigo
|
||||||
|
SDL_Rect &Enemy::getCollider()
|
||||||
|
{
|
||||||
|
return collider;
|
||||||
|
}
|
||||||
@@ -54,6 +54,7 @@ private:
|
|||||||
int x2; // Limite derecho de la ruta en el eje X
|
int x2; // Limite derecho de la ruta en el eje X
|
||||||
int y1; // Limite superior de la ruta en el eje Y
|
int y1; // Limite superior de la ruta en el eje Y
|
||||||
int y2; // Limite inferior de la ruta en el eje Y
|
int y2; // Limite inferior de la ruta en el eje Y
|
||||||
|
SDL_Rect collider; // Caja de colisión
|
||||||
|
|
||||||
// Comprueba si ha llegado al limite del recorrido para darse media vuelta
|
// Comprueba si ha llegado al limite del recorrido para darse media vuelta
|
||||||
void checkPath();
|
void checkPath();
|
||||||
@@ -70,6 +71,12 @@ public:
|
|||||||
|
|
||||||
// Actualiza las variables del objeto
|
// Actualiza las variables del objeto
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
|
// Devuelve el rectangulo que contiene al enemigo
|
||||||
|
SDL_Rect getRect();
|
||||||
|
|
||||||
|
// Obtiene el rectangulo de colision del enemigo
|
||||||
|
SDL_Rect &getCollider();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -153,6 +153,10 @@ void Game::draw()
|
|||||||
text = "tile: " + std::to_string(a) + " - " + std::to_string(b);
|
text = "tile: " + std::to_string(a) + " - " + std::to_string(b);
|
||||||
mText->write(0, 19 * 8, text);
|
mText->write(0, 19 * 8, text);
|
||||||
|
|
||||||
|
const bool collision = checkPlayerAndEnemies();
|
||||||
|
text = "collision: " + std::to_string(collision);
|
||||||
|
mText->write(0, 20 * 8, text);
|
||||||
|
|
||||||
// Actualiza la pantalla
|
// Actualiza la pantalla
|
||||||
mScreen->blit();
|
mScreen->blit();
|
||||||
}
|
}
|
||||||
@@ -288,3 +292,9 @@ void Game::checkPlayerAndWalls()
|
|||||||
mPlayer->setStatus(STATUS_FALLING);
|
mPlayer->setStatus(STATUS_FALLING);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Comprueba las colisiones del jugador con los enemigos
|
||||||
|
bool Game::checkPlayerAndEnemies()
|
||||||
|
{
|
||||||
|
return mRoom->enemyCollision(mPlayer->getCollider());
|
||||||
|
}
|
||||||
@@ -65,6 +65,9 @@ private:
|
|||||||
// Comprueba que el jugador no atraviese ninguna pared
|
// Comprueba que el jugador no atraviese ninguna pared
|
||||||
void checkPlayerAndWalls();
|
void checkPlayerAndWalls();
|
||||||
|
|
||||||
|
// Comprueba las colisiones del jugador con los enemigos
|
||||||
|
bool checkPlayerAndEnemies();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
Game(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Lang *lang, Input *input);
|
Game(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Lang *lang, Input *input);
|
||||||
|
|||||||
@@ -293,3 +293,10 @@ void MovingSprite::setFlip(SDL_RendererFlip flip)
|
|||||||
{
|
{
|
||||||
mFlip = flip;
|
mFlip = flip;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Devuelve el rectangulo donde está el sprite
|
||||||
|
SDL_Rect MovingSprite::getRect()
|
||||||
|
{
|
||||||
|
SDL_Rect rect = {(int)getPosX(), (int)getPosY(), getWidth(), getHeight()};
|
||||||
|
return rect;
|
||||||
|
}
|
||||||
@@ -137,6 +137,9 @@ public:
|
|||||||
|
|
||||||
// Establece el valor de la variable
|
// Establece el valor de la variable
|
||||||
void setFlip(SDL_RendererFlip flip);
|
void setFlip(SDL_RendererFlip flip);
|
||||||
|
|
||||||
|
// Devuelve el rectangulo donde está el sprite
|
||||||
|
SDL_Rect getRect();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ Player::Player(std::string _tileset, SDL_Renderer *_renderer, Asset *_asset, Inp
|
|||||||
sprite->setSpriteClip(sprite->getAnimationClip(0, 0));
|
sprite->setSpriteClip(sprite->getAnimationClip(0, 0));
|
||||||
|
|
||||||
lastPosition = getRect();
|
lastPosition = getRect();
|
||||||
|
collider = getRect();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
@@ -78,6 +79,7 @@ void Player::update()
|
|||||||
checkBorders(); // Comprueba si está situado en alguno de los cuatro bordes de la habitación
|
checkBorders(); // Comprueba si está situado en alguno de los cuatro bordes de la habitación
|
||||||
applyGravity(); // Aplica gravedad al jugador
|
applyGravity(); // Aplica gravedad al jugador
|
||||||
checkJump(); // Comprueba si ha finalizado el salto
|
checkJump(); // Comprueba si ha finalizado el salto
|
||||||
|
collider = getRect();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Comprueba las entradas y modifica variables
|
// Comprueba las entradas y modifica variables
|
||||||
@@ -238,8 +240,13 @@ void Player::applyGravity()
|
|||||||
// Obtiene el rectangulo que delimita al jugador
|
// Obtiene el rectangulo que delimita al jugador
|
||||||
SDL_Rect Player::getRect()
|
SDL_Rect Player::getRect()
|
||||||
{
|
{
|
||||||
SDL_Rect rect = {(int)sprite->getPosX(), (int)sprite->getPosY(), sprite->getWidth(), sprite->getHeight()};
|
return sprite->getRect();
|
||||||
return rect;
|
}
|
||||||
|
|
||||||
|
// Obtiene el rectangulo de colision del jugador
|
||||||
|
SDL_Rect &Player::getCollider()
|
||||||
|
{
|
||||||
|
return collider;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Guarda la posición actual en la variable lastPosition
|
// Guarda la posición actual en la variable lastPosition
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ private:
|
|||||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||||
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
|
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
|
||||||
color_t color; // Color del jugador
|
color_t color; // Color del jugador
|
||||||
|
SDL_Rect collider; // Caja de colisión
|
||||||
|
|
||||||
bool onBorder; // Indica si el jugador esta en uno de los cuatro bordes de la pantalla
|
bool onBorder; // Indica si el jugador esta en uno de los cuatro bordes de la pantalla
|
||||||
int border; // Indica en cual de los cuatro bordes se encuentra
|
int border; // Indica en cual de los cuatro bordes se encuentra
|
||||||
@@ -99,6 +100,9 @@ public:
|
|||||||
// Obtiene el rectangulo que delimita al jugador
|
// Obtiene el rectangulo que delimita al jugador
|
||||||
SDL_Rect getRect();
|
SDL_Rect getRect();
|
||||||
|
|
||||||
|
// Obtiene el rectangulo de colision del jugador
|
||||||
|
SDL_Rect &getCollider();
|
||||||
|
|
||||||
// Deshace el ultimo movimiento
|
// Deshace el ultimo movimiento
|
||||||
void undoLastMove();
|
void undoLastMove();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -84,13 +84,13 @@ bool Room::load(std::string _file_path)
|
|||||||
// Si la linea contiene el texto [tilemap] se realiza el proceso de carga del fichero tmx
|
// Si la linea contiene el texto [tilemap] se realiza el proceso de carga del fichero tmx
|
||||||
else if (line == "[tilemap]")
|
else if (line == "[tilemap]")
|
||||||
{
|
{
|
||||||
//printf("Loading tilemap...\n");
|
// printf("Loading tilemap...\n");
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
std::getline(file, line);
|
std::getline(file, line);
|
||||||
if (line.find(".tmx") != std::string::npos)
|
if (line.find(".tmx") != std::string::npos)
|
||||||
{
|
{
|
||||||
//printf("Reading file %s\n", asset->get(line).c_str());
|
// printf("Reading file %s\n", asset->get(line).c_str());
|
||||||
std::ifstream file2(asset->get(line)); // Abre el fichero tmx
|
std::ifstream file2(asset->get(line)); // Abre el fichero tmx
|
||||||
if (file2.good())
|
if (file2.good())
|
||||||
{
|
{
|
||||||
@@ -103,10 +103,10 @@ bool Room::load(std::string _file_path)
|
|||||||
do
|
do
|
||||||
{
|
{
|
||||||
std::getline(file2, line);
|
std::getline(file2, line);
|
||||||
//printf("parsing: %s\n", line.c_str());
|
// printf("parsing: %s\n", line.c_str());
|
||||||
|
|
||||||
pos = line.find("data encoding");
|
pos = line.find("data encoding");
|
||||||
//printf("pos: %i\n", pos);
|
// printf("pos: %i\n", pos);
|
||||||
|
|
||||||
} while (pos == std::string::npos);
|
} while (pos == std::string::npos);
|
||||||
|
|
||||||
@@ -116,12 +116,12 @@ bool Room::load(std::string _file_path)
|
|||||||
std::getline(file2, line);
|
std::getline(file2, line);
|
||||||
if (line != "</data>")
|
if (line != "</data>")
|
||||||
{
|
{
|
||||||
//printf("data: %s\n", line.c_str());
|
// printf("data: %s\n", line.c_str());
|
||||||
std::stringstream ss(line);
|
std::stringstream ss(line);
|
||||||
std::string tmp;
|
std::string tmp;
|
||||||
while (getline(ss, tmp, ','))
|
while (getline(ss, tmp, ','))
|
||||||
{
|
{
|
||||||
//printf("tile: %s\n", tmp.c_str());
|
// printf("tile: %s\n", tmp.c_str());
|
||||||
tilemap.push_back(std::stoi(tmp));
|
tilemap.push_back(std::stoi(tmp));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -411,3 +411,16 @@ int Room::getTile(SDL_Point point)
|
|||||||
|
|
||||||
return tile;
|
return tile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Indica si hay colision con un enemigo a partir de un rectangulo
|
||||||
|
bool Room::enemyCollision(SDL_Rect &rect)
|
||||||
|
{
|
||||||
|
bool collision = false;
|
||||||
|
|
||||||
|
for (auto enemy : enemy_list)
|
||||||
|
{
|
||||||
|
collision |= checkCollision(rect, enemy->getCollider());
|
||||||
|
}
|
||||||
|
|
||||||
|
return collision;
|
||||||
|
}
|
||||||
@@ -86,6 +86,9 @@ public:
|
|||||||
|
|
||||||
// Devuelve el tipo de tile que hay en ese pixel
|
// Devuelve el tipo de tile que hay en ese pixel
|
||||||
int getTile(SDL_Point point);
|
int getTile(SDL_Point point);
|
||||||
|
|
||||||
|
// Indica si hay colision con un enemigo a partir de un rectangulo
|
||||||
|
bool enemyCollision(SDL_Rect &rect);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ bool checkCollision(circle_t &a, circle_t &b);
|
|||||||
bool checkCollision(circle_t &a, SDL_Rect &b);
|
bool checkCollision(circle_t &a, SDL_Rect &b);
|
||||||
|
|
||||||
// Detector de colisiones entre un dos rectangulos
|
// Detector de colisiones entre un dos rectangulos
|
||||||
bool checkCollision(SDL_Rect a, SDL_Rect b);
|
bool checkCollision(SDL_Rect &a, SDL_Rect &b);
|
||||||
|
|
||||||
// Carga un archivo de imagen en una textura
|
// Carga un archivo de imagen en una textura
|
||||||
bool loadTextureFromFile(LTexture *texture, std::string path, SDL_Renderer *renderer);
|
bool loadTextureFromFile(LTexture *texture, std::string path, SDL_Renderer *renderer);
|
||||||
|
|||||||
3
todo.txt
3
todo.txt
@@ -2,3 +2,6 @@
|
|||||||
[x] Arreglar que no atraviese tiles atravaseables al caer muy rapido
|
[x] Arreglar que no atraviese tiles atravaseables al caer muy rapido
|
||||||
[x] Leer los mapas directamente del archivo tmx
|
[x] Leer los mapas directamente del archivo tmx
|
||||||
[ ] Crear la clase item
|
[ ] Crear la clase item
|
||||||
|
[ ] Colisiones con los enemigos
|
||||||
|
[ ] Decidir un diseño para qué sucede en caso de morir: Recordar el punto por donde se entró al mapa y la velocidad en el eje X/Y que llevaba el personaje, crear puntos de reaparicion en las habitaciones, etc
|
||||||
|
En el Jet Set Willy el juego recuerda la posicion y el momento. En las Tres Luces de Glaurung solo la posición. Se va a optar por seguir el diseño del Jet Set Willy
|
||||||
Reference in New Issue
Block a user