60 lines
1.6 KiB
C++
60 lines
1.6 KiB
C++
#include "clue_db.h"
|
|
#include "core/io/resource_loader.h"
|
|
#include "you_done_it/clue_data.h"
|
|
#include "you_done_it/ydi_networking.h"
|
|
|
|
Ref<ClueDB> ClueDB::singleton_instance{ nullptr };
|
|
|
|
void ClueDB::_bind_methods() {
|
|
BIND_HPROPERTY(Variant::ARRAY, clues, PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:ClueData", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE));
|
|
}
|
|
|
|
void ClueDB::ensure_data_valid() {
|
|
this->clues.resize_initialized(this->clues.capacity());
|
|
for (int i{ 0 }; i < NetworkData::CLUE_MAX; ++i) {
|
|
if (!this->clues[i].is_valid()) {
|
|
this->clues[i].instantiate();
|
|
}
|
|
this->clues[i]->set_id((NetworkData::ClueID)i);
|
|
}
|
|
}
|
|
|
|
Ref<ClueDB> &ClueDB::get_singleton() {
|
|
if (!singleton_instance.is_valid()) {
|
|
singleton_instance = Ref<ClueDB>(ResourceLoader::load("res://clue_db.tres"));
|
|
}
|
|
return singleton_instance;
|
|
}
|
|
|
|
bool ClueDB::has_singleton() {
|
|
return singleton_instance.is_valid();
|
|
}
|
|
|
|
void ClueDB::set_clues(Array data) {
|
|
for (Variant value : data) {
|
|
Ref<ClueData> clue{ Object::cast_to<ClueData>(value) };
|
|
if (clue.is_valid()) {
|
|
NetworkData::ClueID id{ clue->get_id() };
|
|
if (id >= 0 && id < NetworkData::CLUE_MAX) {
|
|
this->clues[clue->get_id()] = clue;
|
|
} else {
|
|
print_error(vformat("attempt to insert clue of id %s into db (min 0 max %s)", id, NetworkData::CLUE_MAX));
|
|
}
|
|
}
|
|
}
|
|
ensure_data_valid();
|
|
}
|
|
|
|
Array ClueDB::get_clues() {
|
|
ensure_data_valid();
|
|
Array data{};
|
|
for (int i{ 0 }; i < NetworkData::CLUE_MAX; ++i) {
|
|
data.push_back(this->clues[i]);
|
|
}
|
|
return data;
|
|
}
|
|
|
|
Ref<ClueData> ClueDB::get_clue(NetworkData::ClueID id) {
|
|
ensure_data_valid();
|
|
return this->clues[id];
|
|
}
|