75 lines
1.8 KiB
C++
75 lines
1.8 KiB
C++
#include "game/gameplay/key_tracker.hpp"
|
|
|
|
// [SINGLETON]
|
|
KeyTracker* KeyTracker::key_tracker = nullptr;
|
|
|
|
// [SINGLETON] Crearemos el objeto con esta función estática
|
|
void KeyTracker::init() {
|
|
KeyTracker::key_tracker = new KeyTracker();
|
|
}
|
|
|
|
// [SINGLETON] Destruiremos el objeto con esta función estática
|
|
void KeyTracker::destroy() {
|
|
delete KeyTracker::key_tracker;
|
|
}
|
|
|
|
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
|
auto KeyTracker::get() -> KeyTracker* {
|
|
return KeyTracker::key_tracker;
|
|
}
|
|
|
|
// Comprueba si la llave ya ha sido cogida
|
|
auto KeyTracker::hasBeenPicked(const std::string& name, SDL_FPoint pos) -> bool {
|
|
if (const int INDEX = findByName(name); INDEX != NOT_FOUND) {
|
|
if (findByPos(INDEX, pos) != NOT_FOUND) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
// Añade la llave a la lista de llaves recogidas
|
|
void KeyTracker::addKey(const std::string& name, SDL_FPoint pos) {
|
|
if (!hasBeenPicked(name, pos)) {
|
|
if (const int INDEX = findByName(name); INDEX != NOT_FOUND) {
|
|
keys_.at(INDEX).pos.push_back(pos);
|
|
} else {
|
|
keys_.emplace_back(name, pos);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Vacía el tracker
|
|
void KeyTracker::clear() {
|
|
keys_.clear();
|
|
}
|
|
|
|
// Busca una entrada en la lista por nombre
|
|
auto KeyTracker::findByName(const std::string& name) -> int {
|
|
int i = 0;
|
|
|
|
for (const auto& key : keys_) {
|
|
if (key.name == name) {
|
|
return i;
|
|
}
|
|
i++;
|
|
}
|
|
|
|
return NOT_FOUND;
|
|
}
|
|
|
|
// Busca una entrada en la lista por posición
|
|
auto KeyTracker::findByPos(int index, SDL_FPoint pos) -> int {
|
|
int i = 0;
|
|
|
|
for (const auto& key : keys_[index].pos) {
|
|
if ((key.x == pos.x) && (key.y == pos.y)) {
|
|
return i;
|
|
}
|
|
i++;
|
|
}
|
|
|
|
return NOT_FOUND;
|
|
}
|