diff --git a/data/room/room2.tmx b/data/room/room2.tmx index 8911cae..55452af 100644 --- a/data/room/room2.tmx +++ b/data/room/room2.tmx @@ -3,7 +3,7 @@ -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,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,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, diff --git a/source/item.cpp b/source/item.cpp index d777772..b155ad1 100644 --- a/source/item.cpp +++ b/source/item.cpp @@ -76,4 +76,11 @@ void Item::pick() SDL_Rect &Item::getCollider() { return collider; +} + +// Obtiene su ubicación +SDL_Point Item::getPos() +{ + const SDL_Point p = {sprite->getPosX(), sprite->getPosY()}; + return p; } \ No newline at end of file diff --git a/source/item.h b/source/item.h index 102f199..f7e0174 100644 --- a/source/item.h +++ b/source/item.h @@ -58,6 +58,9 @@ public: // Obtiene el rectangulo de colision del objeto SDL_Rect &getCollider(); + + // Obtiene su ubicación + SDL_Point getPos(); }; #endif diff --git a/source/item_tracker.cpp b/source/item_tracker.cpp new file mode 100644 index 0000000..46dc2cf --- /dev/null +++ b/source/item_tracker.cpp @@ -0,0 +1,67 @@ +#include "item_tracker.h" + +// Constructor +Item_tracker::Item_tracker() +{ +} + +// Destructor +Item_tracker::~Item_tracker() +{ +} + +// Comprueba si el objeto ya ha sido cogido +bool Item_tracker::hasBeenPicked(std::string name, SDL_Point pos) +{ +} + +// Añade el objeto a la lista de objetos cogidos +void Item_tracker::addItem(std::string name, SDL_Point pos) +{ + // Primero busca si ya hay una entrada con ese nombre + const int index = findByName(name); + if (index != -1) + { + } + + // En caso contrario crea la entrada + else + { + item_tracker_t item; + item.name = name; + item.pos.push_back(pos); + list.push_back(item); + } +} + +// Busca una entrada en la lista por nombre +int Item_tracker::findByName(std::string name) +{ + const int c = -1; + + for (int i = 0; i < list.size(); i++) + { + if (list[i].name == name) + { + return i; + } + } + + return c; +} + +// Busca una entrada en la lista por posición +int Item_tracker::findByPos(int index, SDL_Point pos) +{ + const int c = -1; + + for (int i = 0; i < list[index].pos.size(); i++) + { + if ((list[index].pos[i].x == pos.x) && (list[index].pos[i].y == pos.y)) + { + return i; + } + } + + return c; +} \ No newline at end of file diff --git a/source/item_tracker.h b/source/item_tracker.h new file mode 100644 index 0000000..2072f09 --- /dev/null +++ b/source/item_tracker.h @@ -0,0 +1,42 @@ +#pragma once +#include "ifdefs.h" +#include "utils.h" +#include +#include + +#ifndef ITEM_TRACKER_H +#define ITEM_TRACKER_H + +struct item_tracker_t +{ + std::string name; // Nombre de la habitación donde se encuentra el objeto + std::vector pos; // Lista de objetos cogidos de la habitación +}; + +// Clase Item_tracker +class Item_tracker +{ +private: + std::vector list; // Lista con todos los objetos recogidos + + // Busca una entrada en la lista por nombre + int findByName(std::string name); + + // Busca una entrada en la lista por posición + int findByPos(int index, SDL_Point pos); + +public: + // Constructor + Item_tracker(); + + // Destructor + ~Item_tracker(); + + // Comprueba si el objeto ya ha sido cogido + bool hasBeenPicked(std::string name, SDL_Point pos); + + // Añade el objeto a la lista de objetos cogidos + void addItem(std::string name, SDL_Point pos); +}; + +#endif diff --git a/source/room.cpp b/source/room.cpp index 187b6c0..8db6bab 100644 --- a/source/room.cpp +++ b/source/room.cpp @@ -479,18 +479,17 @@ bool Room::enemyCollision(SDL_Rect &rect) } // Indica si hay colision con un objeto a partir de un rectangulo -bool Room::itemCollision(SDL_Rect &rect) +SDL_Point Room::itemCollision(SDL_Rect &rect) { - bool collision = false; - + SDL_Point p = {-1, -1}; for (auto item : item_list) { - collision |= checkCollision(rect, item->getCollider()); - if (collision) + if (checkCollision(rect, item->getCollider())) { item->pick(); + p = item->getPos(); } } - return collision; + return p; } \ No newline at end of file diff --git a/source/room.h b/source/room.h index 2d52a7a..5390540 100644 --- a/source/room.h +++ b/source/room.h @@ -98,7 +98,7 @@ public: bool enemyCollision(SDL_Rect &rect); // Indica si hay colision con un objeto a partir de un rectangulo - bool itemCollision(SDL_Rect &rect); + SDL_Point itemCollision(SDL_Rect &rect); }; #endif