From c424a67acaae1ce4addc8d34df8ad8425250bdd7 Mon Sep 17 00:00:00 2001 From: Darnagof Date: Sat, 18 Feb 2023 22:55:29 +0100 Subject: [PATCH 1/2] check 'class' instead of 'type' for objects (Tiled 1.9) --- src/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 08762bf..f7a3250 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -105,7 +105,7 @@ void repeat_sprites(vili::node& sprites, const std::string& base_id, vili::node create_game_object(const nlohmann::json::value_type& object, const std::unordered_map& objects_ids) { - const std::string object_type = object.at("type").get(); + const std::string object_type = object.at("class").get(); vili::node game_object = vili::object { { "type", object_type } }; game_object["Requires"] = vili::object{ { "x", object.at("x").get() }, @@ -245,7 +245,7 @@ vili::object export_obe_scene( { for (const auto& object : tmx_layer["objects"]) { - if (object.contains("type") && !object.at("type").get().empty()) + if (object.contains("class") && !object.at("class").get().empty()) { uint32_t object_id = object.at("id"); std::string game_object_id = object.at("name"); From 7426e3fc76287002ef7dfe62c980e8be5912c6e8 Mon Sep 17 00:00:00 2001 From: Darnagof Date: Sun, 19 Feb 2023 14:50:14 +0100 Subject: [PATCH 2/2] keep compatibility with version <= 1.9 --- src/main.cpp | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f7a3250..12509fe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,21 @@ struct TiledIntegrationArgs std::string cwd; }; +using Version = std::vector; +Version tiled_version; + +Version str_to_version(const std::string& str_version) +{ + Version version; + std::stringstream ss_version(str_version); + for (std::string str_number; std::getline(ss_version, str_number, '.');) + { + version.push_back(std::stoi(str_number)); + } + return version; +} + + std::string normalize_path(std::string path) { std::replace(path.begin(), path.end(), '\\', '/'); @@ -105,7 +120,12 @@ void repeat_sprites(vili::node& sprites, const std::string& base_id, vili::node create_game_object(const nlohmann::json::value_type& object, const std::unordered_map& objects_ids) { - const std::string object_type = object.at("class").get(); + std::string object_type_property = "class"; + if (tiled_version < str_to_version("1.9")) { + object_type_property = "type"; + } + + const std::string object_type = object.at(object_type_property).get(); vili::node game_object = vili::object { { "type", object_type } }; game_object["Requires"] = vili::object{ { "x", object.at("x").get() }, @@ -192,6 +212,13 @@ vili::object export_obe_scene( vili::object { { "x", 0.0 }, { "y", 0.0 }, { "unit", "SceneUnits" } } }, { "referential", "TopLeft" } }; + tiled_version = str_to_version(tmx_json["version"]); + std::string object_type_property = "class"; + if (tiled_version < str_to_version("1.9")) + { + object_type_property = "type"; + } + obe_scene["Tiles"] = vili::object {}; obe_scene["Tiles"]["tileWidth"] = tmx_json["tilewidth"].get(); @@ -245,7 +272,8 @@ vili::object export_obe_scene( { for (const auto& object : tmx_layer["objects"]) { - if (object.contains("class") && !object.at("class").get().empty()) + if (object.contains(object_type_property) + && !object.at(object_type_property).get().empty()) { uint32_t object_id = object.at("id"); std::string game_object_id = object.at("name");