Compare commits

..

10 Commits
v0.2 ... v0.4

12 changed files with 438 additions and 91 deletions

View File

@@ -1,4 +1,3 @@
id=1
name=Test Room
bg_color=black
tileset=room1.png
@@ -6,7 +5,10 @@ room_up=0
room_down=0
room_left=0
room_right=02.room
tilemap=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,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,41,41,41,41,41,41,41,41,41,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,41,41,41,41,41,0,0,0,0,0,0,0,0,0,81,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,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,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,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,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,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
[tilemap]
room1.tmx
[tilemap-end]
[enemy]
tileset=enemy01.png
@@ -34,4 +36,9 @@ y2=72
color=yellow
[enemy-end]
item=1,10,10
[item]
tileset=items.png
tile=1
x=10
y=10
[item-end]

View File

@@ -1,4 +1,3 @@
id=2
name=Test Room 2
bg_color=black
tileset=room1.png
@@ -6,7 +5,7 @@ room_up=0
room_down=0
room_left=01.room
room_right=0
tilemap=43,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,43,43,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,43,43,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,43,43,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,43,43,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,43,43,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,43,43,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,43,43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,43,43,43,43,43,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,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,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,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,0,63,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,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
enemy=1,0,0,0,1
enemy=2,10,0,0,1
item=1,10,10
[tilemap]
room2.tmx
[tilemap-end]

24
data/room/room1.tmx Normal file
View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.9" tiledversion="1.9.0" orientation="orthogonal" renderorder="right-down" width="32" height="16" tilewidth="8" tileheight="8" infinite="0" nextlayerid="2" nextobjectid="1">
<tileset firstgid="1" source="../../resources/tilesets/room1.tsx"/>
<layer id="1" name="Capa de patrones 1" width="32" height="16">
<data encoding="csv">
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,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,41,41,41,41,41,41,41,41,41,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,
41,0,0,41,41,41,41,41,0,0,0,0,0,0,0,0,0,81,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,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,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,
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,
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,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>
</layer>
</map>

24
data/room/room2.tmx Normal file
View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.9" tiledversion="1.9.0" orientation="orthogonal" renderorder="right-down" width="32" height="16" tilewidth="8" tileheight="8" infinite="0" nextlayerid="2" nextobjectid="1">
<tileset firstgid="1" source="../../resources/tilesets/room1.tsx"/>
<layer id="1" name="Capa de patrones 1" width="32" height="16">
<data encoding="csv">
43,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,43,
43,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,43,
43,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,43,
43,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,43,
43,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,43,
43,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,43,
43,0,0,0,0,0,0,0,0,0,0,83,83,83,0,0,0,0,0,0,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,43,43,43,43,43,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,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,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,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,
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
</data>
</layer>
</map>

View File

@@ -198,6 +198,8 @@ void Director::setFileList()
mAsset->add("/data/config.bin", data, false);
mAsset->add("/data/room/01.room", room);
mAsset->add("/data/room/02.room", room);
mAsset->add("/data/room/room1.tmx", room);
mAsset->add("/data/room/room2.tmx", room);
mAsset->add("/media/tilesets/room1.png", bitmap);
mAsset->add("/media/enemies/enemy01.png", bitmap);
mAsset->add("/media/player/player01.png", bitmap);

View File

@@ -113,9 +113,9 @@ void Game::update()
}
}
checkInput();
mRoom->update();
mPlayer->update();
checkPlayerAndWalls(); // Debe ir detras del player update, por si se ha metido en algun muro
checkPlayerOnBorder();
checkPlayerOnFloor();
}
@@ -125,9 +125,7 @@ void Game::update()
void Game::draw()
{
// Prepara para dibujar el frame
const color_t color = {0xAA, 0x55, 0x55};
mScreen->start();
mScreen->clean(color);
mScreen->clean(mRoom->getBGColor());
mRoom->drawMap();
@@ -135,16 +133,34 @@ void Game::draw()
mPlayer->draw();
// Texto en el centro de la pantalla
mText->writeCentered(GAMECANVAS_CENTER_X, 18 * 8, mRoom->getName());
SDL_Rect rect = {0, 16 * 8, PLAY_AREA_RIGHT, 8};
color_t color = stringToColor("light_black");
SDL_SetRenderDrawColor(mRenderer, color.r, color.g, color.b, 0xFF);
SDL_RenderFillRect(mRenderer, &rect);
mText->writeCentered(GAMECANVAS_CENTER_X, 16 * 8, mRoom->getName());
// Debug info
std::string text;
text = "status: " + std::to_string(mPlayer->status);
mText->write(0, 17 * 8, text);
text = "foot: " + std::to_string((int)mPlayer->getLeftFoot().y);
mText->write(0, 18 * 8, text);
const int a = (mPlayer->lastPosition.y + 16) / 8;
const int b = mPlayer->getLeftFoot().y / 8;
text = "tile: " + std::to_string(a) + " - " + std::to_string(b);
mText->write(0, 19 * 8, text);
// Actualiza la pantalla
mScreen->blit();
}
// Comprueba la entrada
/*
void Game::checkInput()
{
/*
if (mInput->checkInput(INPUT_UP, REPEAT_FALSE))
changeRoom(mRoom->getRoomUp());
@@ -156,8 +172,8 @@ void Game::checkInput()
if (mInput->checkInput(INPUT_RIGHT, REPEAT_FALSE))
changeRoom(mRoom->getRoomRight());
*/
}
*/
// Cambia de habitación
bool Game::changeRoom(std::string file)
@@ -176,9 +192,6 @@ bool Game::changeRoom(std::string file)
// Crea un objeto habitación nuevo a partir del fichero
mRoom = new Room(mAsset->get(file), mRenderer, mAsset);
// Comunica al jugador la nueva referencia para la habitación
mPlayer->setRoom(mRoom);
success = true;
}
@@ -190,21 +203,88 @@ void Game::checkPlayerOnBorder()
{
if (mPlayer->getOnBorder())
{
changeRoom(mRoom->getRoom(mPlayer->getBorder()));
if (changeRoom(mRoom->getRoom(mPlayer->getBorder())))
{
mPlayer->switchBorders();
}
}
}
// Comprueba si el jugador esta sobre el suelo
void Game::checkPlayerOnFloor()
{
// Comprueba ambos pies
if ((mRoom->isFloor(mPlayer->getLeftFoot())) || (mRoom->isFloor(mPlayer->getRightFoot())))
// Comprueba si tiene suelo bajo los pies solo cuando no hay velocidad de subida
// y solo cuando el pie este encima de un bloque, es decir, en multiplos de 8
// *** HAY UN POSIBLE PROBLEMA y es que caiga muy rapido y viaje a mas de un pixel de velocidad,
// con lo que se saltaria la comprobación
// *** POSIBLE SOLUCION. Comprobar si el tile actual del pie es diferente al tile del pie previo.
// Esto indica que se ha saltado la comprobacion cada 8 pixeles.
// En este caso habría que recolocar al jugador en el sitio
const int a = (mPlayer->lastPosition.y + 16) / 8;
const int b = mPlayer->getLeftFoot().y / 8;
const bool tile_change = a != b;
const bool is_not_going_up = mPlayer->getVelY() >= 0;
const bool is_tile_aligned = mPlayer->getLeftFoot().y % 8 == 0;
if (((is_not_going_up) && (is_tile_aligned)) || ((is_not_going_up) && (tile_change)))
{
mPlayer->setFalling(false);
}
else
bool test = false;
test |= (mRoom->getTile(mPlayer->getLeftFoot()) == TILE_SOLID);
test |= (mRoom->getTile(mPlayer->getRightFoot()) == TILE_SOLID);
test |= (mRoom->getTile(mPlayer->getLeftFoot()) == TILE_TRAVESSABLE);
test |= (mRoom->getTile(mPlayer->getRightFoot()) == TILE_TRAVESSABLE);
// Tiene uno de los pies sobre una superficie
if (test)
{
mPlayer->setFalling(true);
mPlayer->setStatus(STATUS_STANDING);
// Si ha habido un cambio de tile, hay que recolocarlo
if (tile_change)
{
int offset = (int)mPlayer->sprite->getPosY() % 8;
mPlayer->sprite->setPosY((int)mPlayer->sprite->getPosY() - offset);
}
}
// Tiene ambos pies sobre el vacío
else if (mPlayer->getStatus() != STATUS_JUMPING)
{
mPlayer->setStatus(STATUS_FALLING);
}
}
}
// Comprueba que el jugador no atraviese ninguna pared
void Game::checkPlayerAndWalls()
{
// Comprueba las cuatro esquinas de los dos tiles del jugador
const SDL_Rect rect = mPlayer->getRect();
const SDL_Point p1 = {rect.x, rect.y};
const SDL_Point p2 = {rect.x + 7, rect.y};
const SDL_Point p3 = {rect.x + 7, rect.y + 7};
const SDL_Point p4 = {rect.x, rect.y + 7};
const SDL_Point p5 = {rect.x, rect.y + 8};
const SDL_Point p6 = {rect.x + 7, rect.y + 8};
const SDL_Point p7 = {rect.x + 7, rect.y + 15};
const SDL_Point p8 = {rect.x, rect.y + 15};
bool test = false;
test |= (mRoom->getTile(p1) == TILE_SOLID);
test |= (mRoom->getTile(p2) == TILE_SOLID);
test |= (mRoom->getTile(p3) == TILE_SOLID);
test |= (mRoom->getTile(p4) == TILE_SOLID);
test |= (mRoom->getTile(p5) == TILE_SOLID);
test |= (mRoom->getTile(p6) == TILE_SOLID);
test |= (mRoom->getTile(p7) == TILE_SOLID);
test |= (mRoom->getTile(p8) == TILE_SOLID);
if (test)
{
// Si hay colisión, deshace el movimiento y lo pone en modo caída
mPlayer->undoLastMove();
mPlayer->setStatus(STATUS_FALLING);
}
}

View File

@@ -51,7 +51,7 @@ private:
void draw();
// Comprueba la entrada y actua
void checkInput();
//void checkInput();
// Cambia de habitación
bool changeRoom(std::string file);
@@ -62,6 +62,9 @@ private:
// Comprueba si el jugador esta sobre el suelo
void checkPlayerOnFloor();
// Comprueba que el jugador no atraviese ninguna pared
void checkPlayerAndWalls();
public:
// Constructor
Game(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Lang *lang, Input *input);

View File

@@ -2,6 +2,8 @@
#include <fstream>
#include <sstream>
// CAUTION!!!!! si no se gasta al final, quitar la referencia a la habitación
// Constructor
Player::Player(std::string _tileset, SDL_Renderer *_renderer, Asset *_asset, Input *_input, Room *_room)
{
@@ -9,7 +11,6 @@ Player::Player(std::string _tileset, SDL_Renderer *_renderer, Asset *_asset, Inp
asset = _asset;
renderer = _renderer;
input = _input;
room = _room;
// Crea objetos
texture = new LTexture();
@@ -23,6 +24,9 @@ Player::Player(std::string _tileset, SDL_Renderer *_renderer, Asset *_asset, Inp
onBorder = false;
border = BORDER_TOP;
jump_ini = 0;
status = STATUS_STANDING;
sprite->setPosX(2 * 8);
sprite->setPosX(10 * 8);
sprite->setPosY(12 * 8);
@@ -41,6 +45,8 @@ Player::Player(std::string _tileset, SDL_Renderer *_renderer, Asset *_asset, Inp
sprite->setAnimationFrames(0, 2, 8 * 2, 0, 8, 16);
sprite->setAnimationFrames(0, 3, 8 * 3, 0, 8, 16);
sprite->setSpriteClip(sprite->getAnimationClip(0, 0));
lastPosition = getRect();
}
// Destructor
@@ -65,38 +71,38 @@ void Player::draw()
// Actualiza las variables del objeto
void Player::update()
{
checkInput();
sprite->update();
// sprite->animate(0);
checkBorders();
setLastPosition(); // Guarda la posición actual en la variable lastPosition
checkInput(); // Comprueba las entradas y modifica variables
move(); // Recalcula la posición del jugador y su animación
checkBorders(); // Comprueba si está situado en alguno de los cuatro bordes de la habitación
applyGravity(); // Aplica gravedad al jugador
checkJump(); // Comprueba si ha finalizado el salto
}
// Comprueba las entradas y modifica variables
void Player::checkInput()
{
// if (mInput->checkInput(INPUT_UP, REPEAT_FALSE))
// changeRoom(mRoom->getRoomUp());
//
// if (mInput->checkInput(INPUT_DOWN, REPEAT_FALSE))
// changeRoom(mRoom->getRoomDown());
if (input->checkInput(INPUT_LEFT, REPEAT_TRUE))
// Solo comprueba las entradas de dirección cuando está de pie
if ((input->checkInput(INPUT_LEFT, REPEAT_TRUE)) && (status == STATUS_STANDING))
{
sprite->setVelX(-VX);
sprite->animate(0);
sprite->setVelX(-0.6f);
sprite->setFlip(SDL_FLIP_HORIZONTAL);
}
else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE))
else if ((input->checkInput(INPUT_RIGHT, REPEAT_TRUE)) && (status == STATUS_STANDING))
{
sprite->setVelX(VX);
sprite->animate(0);
sprite->setVelX(0.6f);
sprite->setFlip(SDL_FLIP_NONE);
}
else
else if (status == STATUS_STANDING)
{
sprite->setVelX(0);
}
if (input->checkInput(INPUT_UP, REPEAT_FALSE))
{
setStatus(STATUS_JUMPING);
}
}
// Indica si el jugador esta en uno de los cuatro bordes de la pantalla
@@ -111,7 +117,7 @@ int Player::getBorder()
return border;
}
// Comprueba si está situado en alguno de los cuatro bordes
// Comprueba si está situado en alguno de los cuatro bordes de la habitación
void Player::checkBorders()
{
if (sprite->getPosX() < PLAY_AREA_LEFT)
@@ -163,12 +169,6 @@ void Player::switchBorders()
onBorder = false;
}
// Establece el valor de la variable
void Player::setRoom(Room *_room)
{
room = _room;
}
// Obtiene el valor del pixel inferior izquierdo del jugador
SDL_Point Player::getLeftFoot()
{
@@ -179,19 +179,99 @@ SDL_Point Player::getLeftFoot()
// Obtiene el valor del pixel inferior derecho del jugador
SDL_Point Player::getRightFoot()
{
SDL_Point point = {(int)sprite->getPosX() + sprite->getWidth(), (int)sprite->getPosY() + sprite->getHeight()};
SDL_Point point = {(int)sprite->getPosX() + sprite->getWidth() - 1, (int)sprite->getPosY() + sprite->getHeight()};
return point;
}
// Establece la velocidad en el eje Y al jugador
void Player::setFalling(bool value)
// Cambia el estado del jugador
void Player::setStatus(int value)
{
if (value)
// Si quiere cambiar a saltando, ha de ser desde quieto
if ((value == STATUS_JUMPING) && (status == STATUS_STANDING))
{
sprite->setVelY(0.5f);
status = STATUS_JUMPING;
sprite->setVelY(-MAX_VY);
jump_ini = sprite->getPosY();
}
else
// Modifica el estado a 'cayendo'
if (value == STATUS_FALLING)
{
status = STATUS_FALLING;
sprite->setVelY(MAX_VY);
sprite->setVelX(0);
}
// Modifica el estado a 'de pie'
if (value == STATUS_STANDING)
{
status = STATUS_STANDING;
sprite->setVelY(0.0f);
}
}
// Obtiene el estado del jugador
int Player::getStatus()
{
return status;
}
// Obtiene la velocidad en el eje Y del jugador
float Player::getVelY()
{
return sprite->getVelY();
}
// Aplica gravedad al jugador
void Player::applyGravity()
{
if (status == STATUS_JUMPING)
{
sprite->setVelY(sprite->getVelY() + GRAVITY);
if (sprite->getVelY() > MAX_VY)
{
sprite->setVelY(MAX_VY);
}
}
}
// Obtiene el rectangulo que delimita al jugador
SDL_Rect Player::getRect()
{
SDL_Rect rect = {(int)sprite->getPosX(), (int)sprite->getPosY(), sprite->getWidth(), sprite->getHeight()};
return rect;
}
// Guarda la posición actual en la variable lastPosition
void Player::setLastPosition()
{
lastPosition = getRect();
}
// Deshace el ultimo movimiento
void Player::undoLastMove()
{
sprite->setPosX(lastPosition.x);
sprite->setPosY(lastPosition.y);
}
// Recalcula la posición del jugador y su animación
void Player::move()
{
sprite->update();
if (sprite->getVelX() != 0)
{
sprite->animate(0);
}
}
// Comprueba si ha finalizado el salto
void Player::checkJump()
{
if (status == STATUS_JUMPING)
if (sprite->getVelY() > 0)
if (sprite->getPosY() > jump_ini)
{
status = STATUS_FALLING;
}
}

View File

@@ -11,16 +11,20 @@
#ifndef PLAYER_H
#define PLAYER_H
#define VX 0.6
//#define VX 0.6
#define STATUS_STANDING 0
#define STATUS_JUMPING 1
#define STATUS_FALLING 2
#define GRAVITY 0.035f
#define MAX_VY 1.2f
// Clase Player
class Player
{
private:
LTexture *texture; // Textura con los graficos del enemigo
AnimatedSprite *sprite; // Sprite del enemigo
Input *input; // Objeto para gestionar la entrada
Room *room; // Objeto con el mapa del juego
SDL_Renderer *renderer; // El renderizador de la ventana
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
@@ -35,7 +39,27 @@ private:
// Comprueba si se halla en alguno de los cuatro bordes
void checkBorders();
// Asigna velocidad negativa en el eje Y al jugador
void jump();
// Aplica gravedad al jugador
void applyGravity();
// Guarda la posición actual en la variable lastPosition
void setLastPosition();
// Recalcula la posición del jugador y su animación
void move();
// Comprueba si ha finalizado el salto
void checkJump();
public:
AnimatedSprite *sprite; // Sprite del enemigo
SDL_Rect lastPosition; // Contiene la ultima posición del jugador, por si hay que deshacer algun movimiento
int jump_ini; // Valor del eje Y en el que se inicia el salto
int status; // Estado en el que se encuentra el jugador. Util apara saber si está saltando o cayendo
// Constructor
Player(std::string _tileset, SDL_Renderer *_renderer, Asset *_asset, Input *_input, Room *_room);
@@ -57,17 +81,26 @@ public:
// Cambia al jugador de un borde al opuesto. Util para el cambio de pantalla
void switchBorders();
// Establece el valor de la variable
void setRoom(Room *_room);
// Obtiene el valor del pixel inferior izquierdo del jugador
SDL_Point getLeftFoot();
// Obtiene el valor del pixel inferior derecho del jugador
SDL_Point getRightFoot();
// Establece la velocidad en el eje Y al jugador
void setFalling(bool value);
// Cambia el estado del jugador
void setStatus(int value);
// Obtiene el estado del jugador
int getStatus();
// Obtiene la velocidad en el eje Y del jugador
float getVelY();
// Obtiene el rectangulo que delimita al jugador
SDL_Rect getRect();
// Deshace el ultimo movimiento
void undoLastMove();
};
#endif

View File

@@ -56,7 +56,7 @@ bool Room::load(std::string _file_path)
printf("Reading file %s\n", filename.c_str());
while (std::getline(file, line))
{
// Si la linea contiene el texto [enemy] se realiza un proceso distinto
// Si la linea contiene el texto [enemy] se realiza el proceso de carga de un enemigo
if (line == "[enemy]")
{
enemy_t enemy;
@@ -80,6 +80,84 @@ bool Room::load(std::string _file_path)
// Añade el enemigo al vector de enemigos
enemy_list.push_back(new Enemy(enemy));
}
// Si la linea contiene el texto [tilemap] se realiza el proceso de carga del fichero tmx
else if (line == "[tilemap]")
{
//printf("Loading tilemap...\n");
do
{
std::getline(file, line);
if (line.find(".tmx") != std::string::npos)
{
//printf("Reading file %s\n", asset->get(line).c_str());
std::ifstream file2(asset->get(line)); // Abre el fichero tmx
if (file2.good())
{
bool data_read = false;
while (std::getline(file2, line)) // Lee el fichero linea a linea
{
if (!data_read)
{ // Lee lineas hasta que encuentre donde empiezan los datos del mapa
int pos = 0;
do
{
std::getline(file2, line);
//printf("parsing: %s\n", line.c_str());
pos = line.find("data encoding");
//printf("pos: %i\n", pos);
} while (pos == std::string::npos);
do
{ // Se introducen los valores separados por comas en un vector
data_read = true;
std::getline(file2, line);
if (line != "</data>")
{
//printf("data: %s\n", line.c_str());
std::stringstream ss(line);
std::string tmp;
while (getline(ss, tmp, ','))
{
//printf("tile: %s\n", tmp.c_str());
tilemap.push_back(std::stoi(tmp));
}
}
} while (line != "</data>");
}
}
}
}
} while (line != "[tilemap-end]");
}
// Si la linea contiene el texto [item] se realiza el proceso de carga de un item
else if (line == "[item]")
{
// enemy_t enemy;
// enemy.asset = asset;
// enemy.renderer = renderer;
do
{
std::getline(file, line);
// Encuentra la posición del caracter '='
int pos = line.find("=");
// Procesa las dos subcadenas
// if (!setEnemy(&enemy, line.substr(0, pos), line.substr(pos + 1, line.length())))
//{
// printf("Warning: file %s\n, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
// success = false;
//}
} while (line != "[item-end]");
// Añade el enemigo al vector de enemigos
// enemy_list.push_back(new Enemy(enemy));
}
// En caso contrario se parsea el fichero para buscar las variables y los valores
else
{
@@ -114,11 +192,7 @@ bool Room::setVars(std::string _var, std::string _value)
// Indicador de éxito en la asignación
bool success = true;
if (_var == "id")
{
id = _value;
}
else if (_var == "name")
if (_var == "name")
{
name = _value;
}
@@ -156,6 +230,9 @@ bool Room::setVars(std::string _var, std::string _value)
tilemap.push_back(std::stoi(tmp));
}
}
else if (_var == "")
{
}
else
{
success = false;
@@ -210,6 +287,9 @@ bool Room::setEnemy(enemy_t *enemy, std::string _var, std::string _value)
{
enemy->color = stringToColor(_value);
}
else if (_var == "[enemy-end]")
{
}
else
{
success = false;
@@ -308,18 +388,26 @@ std::string Room::getRoom(int border)
return "";
}
// Indica si el tile al que pertenece el pixel es sólido o no
bool Room::isFloor(SDL_Point point)
// Devuelve el tipo de tile que hay en ese pixel
int Room::getTile(SDL_Point point)
{
int tile = ((point.y / 8) * 32) + (point.x / 8);
int pos = ((point.y / 8) * 32) + (point.x / 8);
int tile = TILE_EMPTY;
if (tile < 512)
if (pos < 512)
{
if (tilemap[tile] != 0)
// Los tiles entre el 1 y el 80 son solidos
if ((tilemap[pos] > 0) && (tilemap[pos] < 81))
{
return true;
return TILE_SOLID;
}
// Los tiles mayores de 80 son atravesables
if (tilemap[pos] > 80)
{
return TILE_TRAVESSABLE;
}
}
return false;
return tile;
}

View File

@@ -10,6 +10,10 @@
#ifndef ROOM_H
#define ROOM_H
#define TILE_EMPTY 0
#define TILE_SOLID 1
#define TILE_TRAVESSABLE 2
/*
Cada habitación se crea y destruye cada vez que se entra o sale de la misma
Cada habitacion si que tendra lo siguiente:
@@ -28,7 +32,6 @@ LISTADO DE ITEMS (tipo, posicion)
class Room
{
private:
std::string id; // Identificador
std::string name; // Nombre de la habitación
color_t bg_color; // Color de fondo de la habitación
std::string room_up; // Identificador de la habitación que se encuentra arriba
@@ -81,8 +84,8 @@ public:
// Devuelve la cadena del fichero de la habitación contigua segun el borde
std::string getRoom(int border);
// Indica si el tile al que pertenece el pixel es sólido o no
bool isFloor(SDL_Point point);
// Devuelve el tipo de tile que hay en ese pixel
int getTile(SDL_Point point);
};
#endif

4
todo.txt Normal file
View File

@@ -0,0 +1,4 @@
[x] Hacer que deje de poder moverse tras el salto al alcanzar la misma posicion en altura que tenia cuando saltó
[x] Arreglar que no atraviese tiles atravaseables al caer muy rapido
[x] Leer los mapas directamente del archivo tmx
[ ] Crear la clase item