87 lines
1.8 KiB
C++
87 lines
1.8 KiB
C++
#include "item_tracker.h"
|
|
|
|
// Constructor
|
|
ItemTracker::ItemTracker()
|
|
{
|
|
}
|
|
|
|
// Destructor
|
|
ItemTracker::~ItemTracker()
|
|
{
|
|
list.clear();
|
|
}
|
|
|
|
// Comprueba si el objeto ya ha sido cogido
|
|
bool ItemTracker::hasBeenPicked(std::string name, SDL_Point pos)
|
|
{
|
|
bool success = false;
|
|
|
|
// Primero busca si ya hay una entrada con ese nombre
|
|
const int index = findByName(name);
|
|
if (index != -1)
|
|
{
|
|
// Luego busca si existe ya una entrada con esa posición
|
|
if (findByPos(index, pos) != -1)
|
|
{
|
|
success = true;
|
|
}
|
|
}
|
|
|
|
return success;
|
|
}
|
|
|
|
// Añade el objeto a la lista de objetos cogidos
|
|
void ItemTracker::addItem(std::string name, SDL_Point pos)
|
|
{
|
|
// Comprueba si el objeto no ha sido recogido con anterioridad
|
|
if (!hasBeenPicked(name, pos))
|
|
{
|
|
// Primero busca si ya hay una entrada con ese nombre
|
|
const int index = findByName(name);
|
|
if (index != -1)
|
|
{
|
|
list[index].pos.push_back(pos);
|
|
}
|
|
|
|
// 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 ItemTracker::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 ItemTracker::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;
|
|
} |