diff --git a/sources/enduro2d/high/assets/prefab_asset.cpp b/sources/enduro2d/high/assets/prefab_asset.cpp index 45d964f9..3270616c 100644 --- a/sources/enduro2d/high/assets/prefab_asset.cpp +++ b/sources/enduro2d/high/assets/prefab_asset.cpp @@ -25,7 +25,7 @@ namespace "additionalProperties" : false, "properties" : { "uuid" : { "$ref": "#/common_definitions/uuid" }, - "parent" : { "$ref": "#/common_definitions/address" }, + "prefab" : { "$ref": "#/common_definitions/address" }, "children" : { "$ref": "#/definitions/children" }, "mod_children" : { "$ref": "#/definitions/mod_children" }, "components" : { "type" : "object" } @@ -41,7 +41,7 @@ namespace "additionalProperties" : false, "properties" : { "uuid" : { "$ref": "#/common_definitions/uuid" }, - "parent" : { "$ref": "#/common_definitions/address" }, + "prefab" : { "$ref": "#/common_definitions/address" }, "children" : { "$ref": "#/definitions/children" }, "mod_children" : { "$ref": "#/definitions/mod_children" }, "components" : { "type" : "object" } @@ -88,9 +88,9 @@ namespace const rapidjson::Value& root, asset_dependencies& dependencies) { - if ( root.HasMember("parent") ) { + if ( root.HasMember("prefab") ) { dependencies.add_dependency( - path::combine(parent_address, root["parent"].GetString())); + path::combine(parent_address, root["prefab"].GetString())); } if ( root.HasMember("children") ) { @@ -147,14 +147,14 @@ namespace return dependencies.load_async(library); } - prefab* find_prefab_child(prefab& parent, str_view child_uuid) noexcept { - for ( prefab& child : parent.children() ) { + prefab* find_prefab_child(prefab& root, str_view child_uuid) noexcept { + for ( prefab& child : root.children() ) { if ( child.uuid() == child_uuid ) { return &child; } } - for ( prefab& child : parent.children() ) { + for ( prefab& child : root.children() ) { if ( prefab* sub_child = find_prefab_child(child, child_uuid) ) { return sub_child; } @@ -178,15 +178,15 @@ namespace content.set_uuid(std::move(uuid)); } - if ( root.HasMember("parent") ) { + if ( root.HasMember("prefab") ) { auto proto_res = dependencies.find_asset( - path::combine(parent_address, root["parent"].GetString())); + path::combine(parent_address, root["prefab"].GetString())); if ( !proto_res ) { - the().error("PREFAB: Dependency 'parent' is not found:\n" + the().error("PREFAB: Dependency 'prefab' is not found:\n" "--> Parent address: %0\n" "--> Dependency address: %1", parent_address, - root["parent"].GetString()); + root["prefab"].GetString()); throw prefab_asset_loading_exception(); } content.set_children(proto_res->content().children()); @@ -217,14 +217,22 @@ namespace for ( rapidjson::SizeType i = 0; i < mod_children_root.Size(); ++i ) { const rapidjson::Value& mod_child_root = mod_children_root[i]; - E2D_ASSERT(mod_child_root.HasMember("uuid") && mod_child_root["uuid"].IsString()); - prefab* child_prefab = find_prefab_child(content, mod_child_root["uuid"].GetString()); + + E2D_ASSERT( + mod_child_root.HasMember("uuid") && + mod_child_root["uuid"].IsString()); + + prefab* const child_prefab = find_prefab_child( + content, + mod_child_root["uuid"].GetString()); + if ( !child_prefab ) { the().error("PREFAB: Modifiable child is not found:\n" "--> Child UUID: %0", mod_child_root["uuid"].GetString()); throw prefab_asset_loading_exception(); } + parse_prefab_inplace( *child_prefab, parent_address, @@ -243,10 +251,12 @@ namespace str(parent_address), component_root->value, dependencies); + const bool success = the().fill_prototype( component_root->name.GetString(), content.prototype(), ctx); + if ( !success ) { throw prefab_asset_loading_exception(); } @@ -260,7 +270,10 @@ namespace const asset_group& dependencies) { prefab content; - parse_prefab_inplace(content, parent_address, root, dependencies); + parse_prefab_inplace( + content, + parent_address, + root, dependencies); return content; } } diff --git a/untests/bin/library/prefab.json b/untests/bin/library/prefab_child.json similarity index 90% rename from untests/bin/library/prefab.json rename to untests/bin/library/prefab_child.json index 801cc8e8..6972a937 100644 --- a/untests/bin/library/prefab.json +++ b/untests/bin/library/prefab_child.json @@ -1,8 +1,8 @@ { - "parent" : "parent.json", + "prefab" : "prefab_root.json", "components" : { "named" : { - "name" : "prefab" + "name" : "child" }, "rect_collider" : { "size" : [1,2], diff --git a/untests/bin/library/parent.json b/untests/bin/library/prefab_root.json similarity index 95% rename from untests/bin/library/parent.json rename to untests/bin/library/prefab_root.json index afc24701..2435de1c 100644 --- a/untests/bin/library/parent.json +++ b/untests/bin/library/prefab_root.json @@ -2,7 +2,7 @@ "uuid" : "73740BC4-CE9F-4A7F-A029-4AB65027A8AE", "components" : { "named" : { - "name" : "parent" + "name" : "root" }, "circle_collider" : { "radius" : 5, diff --git a/untests/sources/untests_high/prefab.cpp b/untests/sources/untests_high/prefab.cpp index 693c642c..2987115f 100644 --- a/untests/sources/untests_high/prefab.cpp +++ b/untests/sources/untests_high/prefab.cpp @@ -27,23 +27,26 @@ TEST_CASE("prefab"){ safe_starter_initializer initializer; library& l = the(); { - auto parent_res = l.load_asset("parent.json"); - REQUIRE(parent_res); - REQUIRE(parent_res->content().uuid() == "73740BC4-CE9F-4A7F-A029-4AB65027A8AE"); + auto prefab_root_res = l.load_asset("prefab_root.json"); + REQUIRE(prefab_root_res); - REQUIRE(parent_res->content().children().size() == 2u); - REQUIRE(parent_res->content().children()[0].uuid() == "4A93547E-4635-4C2F-9C59-3546E11B1722"); - REQUIRE(parent_res->content().children()[1].uuid() == "58063213-9FC1-457C-B773-B826BE1BE6D7"); + const prefab& prefab_root = prefab_root_res->content(); + REQUIRE(prefab_root.uuid() == "73740BC4-CE9F-4A7F-A029-4AB65027A8AE"); - auto go = the().instantiate(parent_res->content()); - REQUIRE(go); + REQUIRE(prefab_root.children().size() == 2u); + REQUIRE(prefab_root.children()[0].uuid() == "4A93547E-4635-4C2F-9C59-3546E11B1722"); + REQUIRE(prefab_root.children()[1].uuid() == "58063213-9FC1-457C-B773-B826BE1BE6D7"); - REQUIRE(go.component()); - REQUIRE(go.component()->name() == "parent"); + auto go = the().instantiate(prefab_root); - REQUIRE(go.component()); - REQUIRE(math::approximately(go.component()->radius(), 5.f)); - REQUIRE(go.component()->offset() == v2f(4.f,2.f)); + { + REQUIRE(go.component()); + REQUIRE(go.component()->name() == "root"); + + REQUIRE(go.component()); + REQUIRE(go.component()->radius() == Approx(5.f)); + REQUIRE(go.component()->offset() == v2f(4.f,2.f)); + } { node_iptr go_node = go.component() @@ -75,31 +78,34 @@ TEST_CASE("prefab"){ } { - auto prefab_res = l.load_asset("prefab.json"); - REQUIRE(prefab_res); - REQUIRE(prefab_res->content().uuid().empty()); + auto prefab_child_res = l.load_asset("prefab_child.json"); + REQUIRE(prefab_child_res); - REQUIRE(prefab_res->content().children().size() == 2u); - REQUIRE(prefab_res->content().children()[0].uuid() == "4A93547E-4635-4C2F-9C59-3546E11B1722"); - REQUIRE(prefab_res->content().children()[1].uuid() == "58063213-9FC1-457C-B773-B826BE1BE6D7"); + const prefab& prefab_child = prefab_child_res->content(); + REQUIRE(prefab_child.uuid().empty()); - auto go = the().instantiate(prefab_res->content()); - REQUIRE(go); + REQUIRE(prefab_child.children().size() == 2u); + REQUIRE(prefab_child.children()[0].uuid() == "4A93547E-4635-4C2F-9C59-3546E11B1722"); + REQUIRE(prefab_child.children()[1].uuid() == "58063213-9FC1-457C-B773-B826BE1BE6D7"); - REQUIRE(go.component()); - REQUIRE(go.component()->name() == "prefab"); + auto go = the().instantiate(prefab_child); - REQUIRE(go.component()); - REQUIRE(go.component()->size() == v2f(1.f,2.f)); - REQUIRE(go.component()->offset() == v2f(2.f,4.f)); + { + REQUIRE(go.component()); + REQUIRE(go.component()->name() == "child"); - REQUIRE(go.component()); - REQUIRE(math::approximately(go.component()->radius(), 5.f)); - REQUIRE(go.component()->offset() == v2f(4.f,2.f)); + REQUIRE(go.component()); + REQUIRE(go.component()->size() == v2f(1.f,2.f)); + REQUIRE(go.component()->offset() == v2f(2.f,4.f)); - REQUIRE(go.component()); - REQUIRE(go.component()->points() == vector{{1,2},{2,3},{3,4}}); - REQUIRE(go.component()->offset() == v2f(8.f,4.f)); + REQUIRE(go.component()); + REQUIRE(go.component()->radius() == Approx(5.f)); + REQUIRE(go.component()->offset() == v2f(4.f,2.f)); + + REQUIRE(go.component()); + REQUIRE(go.component()->points() == vector{{1,2},{2,3},{3,4}}); + REQUIRE(go.component()->offset() == v2f(8.f,4.f)); + } { node_iptr go_node = go.component()