Skip to content

Commit 6f424b3

Browse files
Merge pull request #67 from Strife-AI/tiled-properties
Add tilemap object and property deserialization
2 parents c51c425 + fc0d456 commit 6f424b3

File tree

8 files changed

+100
-30
lines changed

8 files changed

+100
-30
lines changed

src/Resource/TilemapResource.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,32 @@ void LoadObjectGroup(tmx::Map& map, tmx::ObjectGroup& layer, MapSegmentDto* mapS
407407
return;
408408
}
409409
}
410+
411+
for (const auto& object : layer.getObjects())
412+
{
413+
auto newEntityInstanceDto = EntityInstanceDto();
414+
415+
newEntityInstanceDto.properties.emplace("name", object.getName());
416+
newEntityInstanceDto.properties.emplace("type", object.getType());
417+
418+
{
419+
float x = object.getPosition().x;
420+
float y = object.getPosition().y;
421+
422+
auto positionValue = std::to_string(x) + " " + std::to_string(y);
423+
424+
newEntityInstanceDto.properties.emplace("position", positionValue);
425+
}
426+
427+
newEntityInstanceDto.properties.emplace("rotation", std::to_string(object.getRotation()));
428+
429+
for (const auto& prop : object.getProperties())
430+
{
431+
newEntityInstanceDto.properties.emplace(prop.getName(),GetTmxPropertyValue(prop));
432+
}
433+
434+
mapSegmentDto->entities.push_back(std::move(newEntityInstanceDto));
435+
}
410436
}
411437

412438
MapSegmentDto ProcessMap(const std::string& path)
@@ -549,6 +575,17 @@ void DtoToSegment(MapSegment* segment, MapSegmentDto& segmentDto)
549575
layerDto.tileSize,
550576
layerDto.layerName);
551577
}
578+
579+
for (auto& entity : segmentDto.entities)
580+
{
581+
if (!entity.properties.contains("type")) continue;
582+
583+
EntityInstance entityInstance;
584+
entityInstance.type = StringId(entity.properties["type"]);
585+
entityInstance.properties = std::move(entity.properties);
586+
587+
segment->entities.push_back(entityInstance);
588+
}
552589
}
553590

554591
bool TilemapResource::LoadFromFile(const ResourceSettings& settings)

src/Scene/EntitySerializer.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <nlohmann/json.hpp>
77
#include <Engine.hpp>
8+
#include <robin_hood.h>
89

910
enum class EntitySerializerMode
1011
{
@@ -49,5 +50,5 @@ struct EntitySerializer
4950
}
5051

5152
EntitySerializerMode mode;
52-
std::unordered_map<std::string, std::string> properties;
53+
robin_hood::unordered_flat_map<std::string, std::string> properties;
5354
};

src/Scene/MapSegment.hpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ struct MapLayer
4646

4747
struct EntityInstance
4848
{
49-
int totalProperties;
49+
StringId type;
50+
robin_hood::unordered_flat_map<std::string, std::string> properties;
5051
};
5152

5253
struct MapSegment
@@ -59,12 +60,5 @@ struct MapSegment
5960
std::vector<MapLayer> layers;
6061
std::vector<EntityInstance> entities;
6162
std::vector<TileProperties*> tileProperties;
62-
63-
64-
// TODO: deprecate
65-
Vector2 startMarker;
66-
Vector2 endMarker;
67-
68-
private:
6963
};
7064

src/Scene/Scene.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ void Scene::LoadMapSegment(const MapSegment& segment)
202202
{
203203
auto tileMap = CreateEntity<TilemapEntity>(Vector2(0, 0 ));
204204
tileMap->SetMapSegment(segment);
205-
206205
}
207206

208207
void Scene::StartTimer(float timeSeconds, const std::function<void()>& callback)

src/Scene/Scene.hpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,8 @@ TEntity* Scene::CreateEntity(EntitySerializer& serializer, Args&& ... constructo
286286
auto oldEntityUnderConstruction = entityUnderConstruction;
287287

288288
TEntity* entity = (TEntity*)AllocateMemory(sizeof(TEntity));
289-
290-
entity->scene = this;
291-
292289
entityUnderConstruction = entity;
290+
293291
new(entity) TEntity(std::forward<Args>(constructorArgs) ...);
294292

295293
Vector2 position;
@@ -299,16 +297,16 @@ TEntity* Scene::CreateEntity(EntitySerializer& serializer, Args&& ... constructo
299297
.Add("position", position)
300298
.Add("rotation", rotation);
301299

302-
entity->_position = position;
303-
entity->_rotation = rotation;
304-
entity->type = TEntity::Type;
305-
entity->scene = this;
300+
entity->_position = position;
301+
entity->_rotation = rotation;
302+
entity->type = TEntity::Type;
303+
entity->scene = this;
306304

307-
RegisterEntity(entity);
308-
((Entity*)entity)->DoSerialize(serializer);
305+
entity->DoSerialize(serializer);
309306

310-
((Entity*)entity)->OnAdded();
311-
entityUnderConstruction = oldEntityUnderConstruction;
307+
RegisterEntity(entity);
308+
((Entity*)entity)->OnAdded();
309+
entityUnderConstruction = oldEntityUnderConstruction;
312310

313311
return entity;
314312
}
@@ -349,6 +347,7 @@ EntityList<TEntity> Scene::GetEntitiesOfType()
349347
return _entityManager.GetEntitiesOfType<TEntity>();
350348
}
351349

350+
// TODO: Deprecate
352351
template<typename TEntity>
353352
TEntity* Scene::GetFirstNamedEntityOfType(StringId name)
354353
{

src/Scene/TilemapEntity.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ void TilemapEntity::SetMapSegment(const MapSegment& mapSegment)
7070
scene->isometricSettings.tileSize = Vector2(64, 32);
7171
}
7272

73+
for (auto& entityDictionary : mapSegment.entities)
74+
{
75+
EntitySerializer entitySerializer;
76+
entitySerializer.properties = entityDictionary.properties;
77+
entitySerializer.mode = EntitySerializerMode::Read;
78+
79+
scene->CreateEntity(entityDictionary.type, entitySerializer);
80+
}
81+
7382
_mapSegment = &mapSegment;
7483

7584
scene->isometricSettings.BuildFromMapSegment(mapSegment, pathFinder, this);

src/System/Serialization.hpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <map>
44
#include <vector>
55
#include <string>
6+
#include <robin_hood.h>
67

78
#include "Math/Rectangle.hpp"
89
#include "Math/Vector2.hpp"
@@ -38,19 +39,20 @@ void ReadVector(BinaryStreamReader& reader, std::vector<T>& outV)
3839
}
3940

4041
template<typename TKey, typename TValue>
41-
void WriteMap(BinaryStreamWriter& writer, const std::map<TKey, TValue> m)
42+
void WriteMap(BinaryStreamWriter& writer, const std::map<TKey, TValue>& map)
4243
{
43-
writer.WriteInt(m.size());
44+
writer.WriteInt(map.size());
4445

45-
for (auto& item : m)
46+
for (auto& pair
47+
: map)
4648
{
47-
Write(writer, item.first);
48-
Write(writer, item.second);
49+
Write(writer, pair.first);
50+
Write(writer, pair.second);
4951
}
5052
}
5153

5254
template<typename TKey, typename TValue>
53-
void ReadMap(BinaryStreamReader& reader, std::map<TKey, TValue>& m)
55+
void ReadMap(BinaryStreamReader& reader, std::map<TKey, TValue>& map)
5456
{
5557
int size = reader.ReadInt();
5658
for(int i = 0; i < size; ++i)
@@ -61,7 +63,35 @@ void ReadMap(BinaryStreamReader& reader, std::map<TKey, TValue>& m)
6163
Read(reader, key);
6264
Read(reader, value);
6365

64-
m.insert({key, value});
66+
map.insert({ key, value});
67+
}
68+
}
69+
70+
template<typename TKey, typename TValue>
71+
void WriteMap(BinaryStreamWriter& writer, const robin_hood::unordered_flat_map<TKey, TValue>& map)
72+
{
73+
writer.WriteInt(map.size());
74+
75+
for (auto& pair : map)
76+
{
77+
Write(writer, pair.first);
78+
Write(writer, pair.second);
79+
}
80+
}
81+
82+
template<typename TKey, typename TValue>
83+
void ReadMap(BinaryStreamReader& reader, robin_hood::unordered_flat_map<TKey, TValue>& map)
84+
{
85+
auto size = reader.ReadInt();
86+
for (int i = 0; i < size; ++i)
87+
{
88+
TKey key;
89+
TValue value;
90+
91+
Read(reader, key);
92+
Read(reader, value);
93+
94+
map.insert({key, value});
6595
}
6696
}
6797

src/System/TileMapSerialization.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <string>
55
#include <map>
66
#include <Math/Polygon.hpp>
7+
#include <robin_hood.h>
78

89
#include "BinaryStreamReader.hpp"
910
#include "Math/Rectangle.hpp"
@@ -77,13 +78,13 @@ struct EntityInstanceDto
7778

7879
}
7980

80-
EntityInstanceDto(const std::map<std::string, std::string>& properties_)
81+
EntityInstanceDto(const robin_hood::unordered_flat_map<std::string, std::string>& properties_)
8182
: properties(properties_)
8283
{
8384

8485
}
8586

86-
std::map<std::string, std::string> properties;
87+
robin_hood::unordered_flat_map<std::string, std::string> properties;
8788
};
8889

8990
struct MapSegmentDto

0 commit comments

Comments
 (0)