Skip to content

Commit ae19084

Browse files
committed
resolve entity references
1 parent 0696136 commit ae19084

5 files changed

Lines changed: 45 additions & 5 deletions

File tree

include/LDtkLoader/DataTypes.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,17 @@ namespace ldtk {
111111

112112
bool operator==(const IID& lhs, const IID& rhs);
113113

114+
class Entity;
114115
struct EntityRef {
116+
EntityRef(IID ent, IID layer, IID level, IID world);
115117
IID entity_iid;
116118
IID layer_iid;
117119
IID level_iid;
118120
IID world_iid;
121+
auto operator->() const -> const Entity*;
122+
private:
123+
friend class Project;
124+
const Entity* ref = nullptr;
119125
};
120126
}
121127

include/LDtkLoader/containers/FieldsContainer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ namespace ldtk {
4646
void addArrayField(const std::string& name, const ArrayField<T>& field);
4747

4848
private:
49+
friend class Project;
4950
std::vector<std::unique_ptr<IField>> m_gc;
5051
std::unordered_map<std::string, IField*> m_fields;
5152
std::unordered_map<std::string, IField*> m_array_fields;
53+
static std::vector<EntityRef*> tmp_entity_refs_vector;
5254
};
5355

5456
template <FieldType T>

src/DataTypes.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ bool ldtk::operator==(const IID& lhs, const IID& rhs) {
6868
return lhs.str() == rhs.str();
6969
}
7070

71+
EntityRef::EntityRef(IID ent, IID layer, IID level, IID world) :
72+
entity_iid(std::move(ent)),
73+
layer_iid(std::move(layer)),
74+
level_iid(std::move(level)),
75+
world_iid(std::move(world))
76+
{}
77+
78+
auto EntityRef::operator->() const -> const Entity* {
79+
return ref;
80+
}
81+
7182
auto operator<<(std::ostream& os, const ldtk::Color& col) -> std::ostream& {
7283
os << "rgb(" << (int)col.r << ", " << (int)col.g << ", " << (int)col.b << ")";
7384
return os;

src/FieldsContainer.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
using namespace ldtk;
77

8+
std::vector<EntityRef*> FieldsContainer::tmp_entity_refs_vector;
9+
810
FieldsContainer::FieldsContainer(const nlohmann::json& j, const World* w) {
911
parseFields(j, w);
1012
}
@@ -100,16 +102,24 @@ void FieldsContainer::parseFields(const nlohmann::json& j, const World* w) {
100102
}
101103
else if (field_type == "Array<EntityRef>") {
102104
std::vector<Field<EntityRef>> values;
105+
values.reserve(field_value.size());
103106
for (const auto& v : field_value) {
104-
if (v.is_null())
107+
if (v.is_null()) {
105108
values.emplace_back(null);
106-
else
109+
}
110+
else {
107111
values.emplace_back(EntityRef{
108112
IID(v["entityIid"].get<std::string>()), IID(v["layerIid"].get<std::string>()),
109113
IID(v["levelIid"].get<std::string>()), IID(v["worldIid"].get<std::string>())
110114
});
115+
}
111116
}
112117
addArrayField(field_name, values);
118+
auto& this_field = *dynamic_cast<ArrayField<EntityRef>*>(m_array_fields.at(field_name));
119+
for (auto& ent_ref : this_field) {
120+
tmp_entity_refs_vector.emplace_back(&ent_ref.value());
121+
}
122+
113123
}
114124
}
115125
// simple fields
@@ -163,13 +173,17 @@ void FieldsContainer::parseFields(const nlohmann::json& j, const World* w) {
163173
addField<FilePath>(field_name, field_value.get<std::string>());
164174
}
165175
else if (field_type == "EntityRef") {
166-
if (field_value.is_null())
176+
if (field_value.is_null()) {
167177
addField<EntityRef>(field_name, null);
168-
else
178+
}
179+
else {
169180
addField<EntityRef>(field_name, {IID(field_value["entityIid"].get<std::string>()),
170181
IID(field_value["layerIid"].get<std::string>()),
171182
IID(field_value["levelIid"].get<std::string>()),
172183
IID(field_value["worldIid"].get<std::string>())});
184+
auto& this_field = *dynamic_cast<Field<EntityRef>*>(m_fields.at(field_name));
185+
tmp_entity_refs_vector.emplace_back(&this_field.value());
186+
}
173187
}
174188
}
175189
}

src/Project.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,14 @@ void Project::loadFromFile(const std::string& filepath) {
7272
}
7373
}
7474

75-
// TODO : resolve EntityRefs using iids
75+
// resolve all EntityRefs in the project
76+
for (auto* ref : FieldsContainer::tmp_entity_refs_vector) {
77+
auto& world = (m_worlds.size() == 1 ? getWorld() : getWorld(ref->world_iid));
78+
ref->ref = &world.getLevel(ref->level_iid)
79+
.getLayer(ref->layer_iid)
80+
.getEntity(ref->entity_iid);
81+
}
82+
FieldsContainer::tmp_entity_refs_vector.clear();
7683
}
7784

7885
auto Project::getFilePath() const -> const FilePath& {

0 commit comments

Comments
 (0)