diff --git a/data/room/01.room b/data/room/01.room index 9566b00..1f0b360 100644 --- a/data/room/01.room +++ b/data/room/01.room @@ -12,27 +12,27 @@ room1.tmx [enemy] tileset=enemy01.png -x=8 +x=1 y=0 vx=0 vy=0.3 -x1=8 -x2=8 +x1=1 y1=0 -y2=48 +x2=1 +y2=6 color=red [enemy-end] [enemy] tileset=enemy01.png -x=40 -y=72 -vx=0.3 +x=5 +y=9 +vx=0.6 vy=0 -x1=40 -x2=184 -y1=72 -y2=72 +x1=5 +y1=9 +x2=22 +y2=9 color=yellow [enemy-end] diff --git a/data/room/02.room b/data/room/02.room index 20fb3e9..0743b32 100644 --- a/data/room/02.room +++ b/data/room/02.room @@ -12,13 +12,20 @@ room2.tmx [enemy] tileset=enemy01.png -x=120 -y=8 +x=14 +y=0 vx=0 vy=1 -x1=120 -x2=120 -y1=8 -y2=104 +x1=14 +y1=0 +x2=14 +y2=13 color=purple -[enemy-end] \ No newline at end of file +[enemy-end] + +[item] +tileset=items.png +tile=0 +x=19 +y=6 +[item-end] \ No newline at end of file diff --git a/data/room/room2.tmx b/data/room/room2.tmx index 139bdcc..8911cae 100644 --- a/data/room/room2.tmx +++ b/data/room/room2.tmx @@ -9,7 +9,7 @@ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23, -0,0,0,0,0,0,0,0,0,0,0,203,203,203,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23, +0,0,0,0,0,0,0,0,0,0,0,203,203,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,23,23,23,23,0,0,0,0,0,0,0,0,0,23, 23,23,23,23,23,23,0,0,0,203,0,0,0,0,0,0,0,0,0,263,0,0,0,0,203,203,0,0,0,0,0,23, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,263,0,0,0,0,0,0,0,0,0,0,0,23, diff --git a/source/director.cpp b/source/director.cpp index 3db0cc9..db51a0a 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -98,6 +98,7 @@ void Director::init(Uint8 name) mInput->bindKey(INPUT_ACCEPT, SDL_SCANCODE_RETURN); mInput->bindKey(INPUT_CANCEL, SDL_SCANCODE_ESCAPE); mInput->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_SPACE); + mInput->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_D); mInput->bindKey(INPUT_BUTTON_PAUSE, SDL_SCANCODE_ESCAPE); mInput->bindKey(INPUT_BUTTON_ESCAPE, SDL_SCANCODE_ESCAPE); diff --git a/source/game.cpp b/source/game.cpp index 42fee61..5962e74 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -6,6 +6,7 @@ Game::Game(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Lang *lang, // Inicia variables mCurrentRoom = "01.room"; mSpawnPoint = {2 * 8, 12 * 8, 0, 0, 0, STATUS_STANDING, SDL_FLIP_NONE}; + mDebug = false; // Copia los punteros mRenderer = renderer; @@ -122,6 +123,7 @@ void Game::update() checkPlayerAndWalls(); // Debe ir detras del player update, por si se ha metido en algun muro checkPlayerOnBorder(); checkPlayerOnFloor(); + checkPlayerAndItems(); if (checkPlayerAndEnemies()) { // Destruye la habitacion y el jugador @@ -132,6 +134,8 @@ void Game::update() mRoom = new Room(mAsset->get(mCurrentRoom), mRenderer, mAsset); mPlayer = new Player(mSpawnPoint, mAsset->get("player01.png"), mRenderer, mAsset, mInput, mRoom); } + + checkInput(); } } @@ -156,43 +160,50 @@ void Game::draw() 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); + if (mDebug) + { + 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); + 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); + 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); - const bool collision = checkPlayerAndEnemies(); - text = "collision: " + std::to_string(collision); - mText->write(0, 20 * 8, text); + const bool collision = checkPlayerAndEnemies(); + text = "collision: " + std::to_string(collision); + mText->write(0, 20 * 8, text); + } // Actualiza la pantalla mScreen->blit(); } // Comprueba la entrada -/* + void Game::checkInput() { -if (mInput->checkInput(INPUT_UP, REPEAT_FALSE)) - changeRoom(mRoom->getRoomUp()); + /* + if (mInput->checkInput(INPUT_UP, REPEAT_FALSE)) + changeRoom(mRoom->getRoomUp()); -if (mInput->checkInput(INPUT_DOWN, REPEAT_FALSE)) - changeRoom(mRoom->getRoomDown()); + if (mInput->checkInput(INPUT_DOWN, REPEAT_FALSE)) + changeRoom(mRoom->getRoomDown()); -if (mInput->checkInput(INPUT_LEFT, REPEAT_FALSE)) - changeRoom(mRoom->getRoomLeft()); + if (mInput->checkInput(INPUT_LEFT, REPEAT_FALSE)) + changeRoom(mRoom->getRoomLeft()); -if (mInput->checkInput(INPUT_RIGHT, REPEAT_FALSE)) - changeRoom(mRoom->getRoomRight()); + if (mInput->checkInput(INPUT_RIGHT, REPEAT_FALSE)) + changeRoom(mRoom->getRoomRight()); + */ + + if (mInput->checkInput(INPUT_BUTTON_2, REPEAT_FALSE)) + mDebug = !mDebug; } -*/ // Cambia de habitación bool Game::changeRoom(std::string file) @@ -246,7 +257,7 @@ void Game::checkPlayerOnFloor() // En este caso habría que recolocar al jugador en el sitio // *** PARECE RESUELTO - + const int a = (mPlayer->lastPosition.y + 16) / 8; const int b = mPlayer->getLeftFoot().y / 8; const bool tile_change = a != b; @@ -318,4 +329,10 @@ void Game::checkPlayerAndWalls() bool Game::checkPlayerAndEnemies() { return mRoom->enemyCollision(mPlayer->getCollider()); +} + +// Comprueba las colisiones del jugador con los objetos +void Game::checkPlayerAndItems() +{ + mRoom->itemCollision(mPlayer->getCollider()); } \ No newline at end of file diff --git a/source/game.h b/source/game.h index 2b1318b..7e87656 100644 --- a/source/game.h +++ b/source/game.h @@ -39,7 +39,8 @@ private: section_t mSection; // Seccion actual dentro del juego std::string mCurrentRoom; // Fichero de la habitación actual player_t mSpawnPoint; // Lugar de la habitación donde aparece el jugador - + bool mDebug; // Indica si el modo debug está activo + // Inicializa las variables void init(); @@ -53,7 +54,7 @@ private: void draw(); // Comprueba la entrada y actua - // void checkInput(); + void checkInput(); // Cambia de habitación bool changeRoom(std::string file); @@ -70,6 +71,9 @@ private: // Comprueba las colisiones del jugador con los enemigos bool checkPlayerAndEnemies(); + // Comprueba las colisiones del jugador con los objetos + void checkPlayerAndItems(); + public: // Constructor Game(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Lang *lang, Input *input); diff --git a/source/item.cpp b/source/item.cpp index 10e634c..d777772 100644 --- a/source/item.cpp +++ b/source/item.cpp @@ -18,6 +18,7 @@ Item::Item(item_t item) // Inicia variables sprite->setSpriteClip(item.tile * 8, 0, 8, 8); + collider = sprite->getRect(); // Inicializa los colores color_t c = stringToColor("blue"); @@ -50,13 +51,29 @@ Item::~Item() sprite = nullptr; } -// Pinta el enemigo en pantalla +// Pinta el objeto en pantalla void Item::draw() { + const int index = (counter / 2) % color.size(); + sprite->getTexture()->setColor(color[index].r, color[index].g, color[index].b); sprite->render(); + sprite->getTexture()->setColor(255, 255, 255); } // Actualiza las variables del objeto void Item::update() { + counter++; } + +// Coge el objeto +void Item::pick() +{ + sprite->setEnabled(false); +} + +// Obtiene el rectangulo de colision del objeto +SDL_Rect &Item::getCollider() +{ + return collider; +} \ No newline at end of file diff --git a/source/item.h b/source/item.h index d77911e..102f199 100644 --- a/source/item.h +++ b/source/item.h @@ -34,6 +34,8 @@ private: SDL_Renderer *renderer; // El renderizador de la ventana Asset *asset; // Objeto con la ruta a todos los ficheros de recursos std::vector color; // Vector con los colores del objeto + int counter; // Contador interno + SDL_Rect collider; // Rectangulo de colisión // Comprueba si ha llegado al limite del recorrido para darse media vuelta void checkPath(); @@ -50,6 +52,12 @@ public: // Actualiza las variables del objeto void update(); + + // Coge el item + void pick(); + + // Obtiene el rectangulo de colision del objeto + SDL_Rect &getCollider(); }; #endif diff --git a/source/room.cpp b/source/room.cpp index a207869..187b6c0 100644 --- a/source/room.cpp +++ b/source/room.cpp @@ -259,11 +259,11 @@ bool Room::setEnemy(enemy_t *enemy, std::string _var, std::string _value) } else if (_var == "x") { - enemy->x = std::stof(_value); + enemy->x = std::stof(_value) * BLOCK; } else if (_var == "y") { - enemy->y = std::stof(_value); + enemy->y = std::stof(_value) * BLOCK; } else if (_var == "vx") { @@ -275,19 +275,19 @@ bool Room::setEnemy(enemy_t *enemy, std::string _var, std::string _value) } else if (_var == "x1") { - enemy->x1 = std::stoi(_value); + enemy->x1 = std::stoi(_value) * BLOCK; } else if (_var == "x2") { - enemy->x2 = std::stoi(_value); + enemy->x2 = std::stoi(_value) * BLOCK; } else if (_var == "y1") { - enemy->y1 = std::stoi(_value); + enemy->y1 = std::stoi(_value) * BLOCK; } else if (_var == "y2") { - enemy->y2 = std::stoi(_value); + enemy->y2 = std::stoi(_value) * BLOCK; } else if (_var == "color") { @@ -407,6 +407,11 @@ void Room::update() { enemy->update(); } + + for (auto item : item_list) + { + item->update(); + } } // Devuelve la cadena del fichero de la habitación contigua segun el borde @@ -470,5 +475,22 @@ bool Room::enemyCollision(SDL_Rect &rect) collision |= checkCollision(rect, enemy->getCollider()); } + return collision; +} + +// Indica si hay colision con un objeto a partir de un rectangulo +bool Room::itemCollision(SDL_Rect &rect) +{ + bool collision = false; + + for (auto item : item_list) + { + collision |= checkCollision(rect, item->getCollider()); + if (collision) + { + item->pick(); + } + } + return collision; } \ No newline at end of file diff --git a/source/room.h b/source/room.h index d3114a3..2d52a7a 100644 --- a/source/room.h +++ b/source/room.h @@ -96,6 +96,9 @@ public: // Indica si hay colision con un enemigo a partir de un rectangulo bool enemyCollision(SDL_Rect &rect); + + // Indica si hay colision con un objeto a partir de un rectangulo + bool itemCollision(SDL_Rect &rect); }; #endif diff --git a/source/sprite.cpp b/source/sprite.cpp index d8ba1b3..8a31493 100644 --- a/source/sprite.cpp +++ b/source/sprite.cpp @@ -160,4 +160,11 @@ void Sprite::setEnabled(bool value) bool Sprite::isEnabled() { return mEnabled; +} + +// Devuelve el rectangulo donde está el sprite +SDL_Rect Sprite::getRect() +{ + SDL_Rect rect = {getPosX(), getPosY(), getWidth(), getHeight()}; + return rect; } \ No newline at end of file diff --git a/source/sprite.h b/source/sprite.h index e89f10e..b2b1fe4 100644 --- a/source/sprite.h +++ b/source/sprite.h @@ -78,6 +78,9 @@ public: // Comprueba si el objeto está habilitado bool isEnabled(); + + // Devuelve el rectangulo donde está el sprite + SDL_Rect getRect(); }; #endif