forked from jaildesigner-jailgames/jaildoctors_dilemma
Singleton de ItemTracker
Arreglos menors
This commit is contained in:
@@ -1,50 +1,57 @@
|
||||
#include "item_tracker.h"
|
||||
|
||||
// Destructor
|
||||
ItemTracker::~ItemTracker()
|
||||
// [SINGLETON]
|
||||
ItemTracker *ItemTracker::item_tracker_ = nullptr;
|
||||
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
void ItemTracker::init()
|
||||
{
|
||||
list.clear();
|
||||
ItemTracker::item_tracker_ = new ItemTracker();
|
||||
}
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
void ItemTracker::destroy()
|
||||
{
|
||||
delete ItemTracker::item_tracker_;
|
||||
}
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
ItemTracker *ItemTracker::get()
|
||||
{
|
||||
return ItemTracker::item_tracker_;
|
||||
}
|
||||
|
||||
// Comprueba si el objeto ya ha sido cogido
|
||||
bool ItemTracker::hasBeenPicked(std::string name, SDL_Point pos)
|
||||
bool ItemTracker::hasBeenPicked(const 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)
|
||||
if (const int index = findByName(name); index != -1)
|
||||
{
|
||||
// Luego busca si existe ya una entrada con esa posición
|
||||
if (findByPos(index, pos) != -1)
|
||||
{
|
||||
success = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return success;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Añade el objeto a la lista de objetos cogidos
|
||||
void ItemTracker::addItem(std::string name, SDL_Point pos)
|
||||
void ItemTracker::addItem(const 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)
|
||||
if (const int index = findByName(name); index != -1)
|
||||
{
|
||||
list[index].pos.push_back(pos);
|
||||
list.at(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);
|
||||
list.emplace_back(name, pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -54,7 +61,7 @@ int ItemTracker::findByName(std::string name)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
for (auto l : list)
|
||||
for (const auto &l : list)
|
||||
{
|
||||
if (l.name == name)
|
||||
{
|
||||
@@ -71,7 +78,7 @@ int ItemTracker::findByPos(int index, SDL_Point pos)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
for (auto l : list[index].pos)
|
||||
for (const auto &l : list[index].pos)
|
||||
{
|
||||
if ((l.x == pos.x) && (l.y == pos.y))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user