rename prefab "parent" property to "prefab"

This commit is contained in:
BlackMATov
2020-04-24 04:05:59 +07:00
parent e659663954
commit b82b743f76
4 changed files with 68 additions and 49 deletions

View File

@@ -25,7 +25,7 @@ namespace
"additionalProperties" : false, "additionalProperties" : false,
"properties" : { "properties" : {
"uuid" : { "$ref": "#/common_definitions/uuid" }, "uuid" : { "$ref": "#/common_definitions/uuid" },
"parent" : { "$ref": "#/common_definitions/address" }, "prefab" : { "$ref": "#/common_definitions/address" },
"children" : { "$ref": "#/definitions/children" }, "children" : { "$ref": "#/definitions/children" },
"mod_children" : { "$ref": "#/definitions/mod_children" }, "mod_children" : { "$ref": "#/definitions/mod_children" },
"components" : { "type" : "object" } "components" : { "type" : "object" }
@@ -41,7 +41,7 @@ namespace
"additionalProperties" : false, "additionalProperties" : false,
"properties" : { "properties" : {
"uuid" : { "$ref": "#/common_definitions/uuid" }, "uuid" : { "$ref": "#/common_definitions/uuid" },
"parent" : { "$ref": "#/common_definitions/address" }, "prefab" : { "$ref": "#/common_definitions/address" },
"children" : { "$ref": "#/definitions/children" }, "children" : { "$ref": "#/definitions/children" },
"mod_children" : { "$ref": "#/definitions/mod_children" }, "mod_children" : { "$ref": "#/definitions/mod_children" },
"components" : { "type" : "object" } "components" : { "type" : "object" }
@@ -88,9 +88,9 @@ namespace
const rapidjson::Value& root, const rapidjson::Value& root,
asset_dependencies& dependencies) asset_dependencies& dependencies)
{ {
if ( root.HasMember("parent") ) { if ( root.HasMember("prefab") ) {
dependencies.add_dependency<prefab_asset>( dependencies.add_dependency<prefab_asset>(
path::combine(parent_address, root["parent"].GetString())); path::combine(parent_address, root["prefab"].GetString()));
} }
if ( root.HasMember("children") ) { if ( root.HasMember("children") ) {
@@ -147,14 +147,14 @@ namespace
return dependencies.load_async(library); return dependencies.load_async(library);
} }
prefab* find_prefab_child(prefab& parent, str_view child_uuid) noexcept { prefab* find_prefab_child(prefab& root, str_view child_uuid) noexcept {
for ( prefab& child : parent.children() ) { for ( prefab& child : root.children() ) {
if ( child.uuid() == child_uuid ) { if ( child.uuid() == child_uuid ) {
return &child; return &child;
} }
} }
for ( prefab& child : parent.children() ) { for ( prefab& child : root.children() ) {
if ( prefab* sub_child = find_prefab_child(child, child_uuid) ) { if ( prefab* sub_child = find_prefab_child(child, child_uuid) ) {
return sub_child; return sub_child;
} }
@@ -178,15 +178,15 @@ namespace
content.set_uuid(std::move(uuid)); content.set_uuid(std::move(uuid));
} }
if ( root.HasMember("parent") ) { if ( root.HasMember("prefab") ) {
auto proto_res = dependencies.find_asset<prefab_asset>( auto proto_res = dependencies.find_asset<prefab_asset>(
path::combine(parent_address, root["parent"].GetString())); path::combine(parent_address, root["prefab"].GetString()));
if ( !proto_res ) { if ( !proto_res ) {
the<debug>().error("PREFAB: Dependency 'parent' is not found:\n" the<debug>().error("PREFAB: Dependency 'prefab' is not found:\n"
"--> Parent address: %0\n" "--> Parent address: %0\n"
"--> Dependency address: %1", "--> Dependency address: %1",
parent_address, parent_address,
root["parent"].GetString()); root["prefab"].GetString());
throw prefab_asset_loading_exception(); throw prefab_asset_loading_exception();
} }
content.set_children(proto_res->content().children()); content.set_children(proto_res->content().children());
@@ -217,14 +217,22 @@ namespace
for ( rapidjson::SizeType i = 0; i < mod_children_root.Size(); ++i ) { for ( rapidjson::SizeType i = 0; i < mod_children_root.Size(); ++i ) {
const rapidjson::Value& mod_child_root = mod_children_root[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 ) { if ( !child_prefab ) {
the<debug>().error("PREFAB: Modifiable child is not found:\n" the<debug>().error("PREFAB: Modifiable child is not found:\n"
"--> Child UUID: %0", "--> Child UUID: %0",
mod_child_root["uuid"].GetString()); mod_child_root["uuid"].GetString());
throw prefab_asset_loading_exception(); throw prefab_asset_loading_exception();
} }
parse_prefab_inplace( parse_prefab_inplace(
*child_prefab, *child_prefab,
parent_address, parent_address,
@@ -243,10 +251,12 @@ namespace
str(parent_address), str(parent_address),
component_root->value, component_root->value,
dependencies); dependencies);
const bool success = the<factory>().fill_prototype( const bool success = the<factory>().fill_prototype(
component_root->name.GetString(), component_root->name.GetString(),
content.prototype(), content.prototype(),
ctx); ctx);
if ( !success ) { if ( !success ) {
throw prefab_asset_loading_exception(); throw prefab_asset_loading_exception();
} }
@@ -260,7 +270,10 @@ namespace
const asset_group& dependencies) const asset_group& dependencies)
{ {
prefab content; prefab content;
parse_prefab_inplace(content, parent_address, root, dependencies); parse_prefab_inplace(
content,
parent_address,
root, dependencies);
return content; return content;
} }
} }

View File

@@ -1,8 +1,8 @@
{ {
"parent" : "parent.json", "prefab" : "prefab_root.json",
"components" : { "components" : {
"named" : { "named" : {
"name" : "prefab" "name" : "child"
}, },
"rect_collider" : { "rect_collider" : {
"size" : [1,2], "size" : [1,2],

View File

@@ -2,7 +2,7 @@
"uuid" : "73740BC4-CE9F-4A7F-A029-4AB65027A8AE", "uuid" : "73740BC4-CE9F-4A7F-A029-4AB65027A8AE",
"components" : { "components" : {
"named" : { "named" : {
"name" : "parent" "name" : "root"
}, },
"circle_collider" : { "circle_collider" : {
"radius" : 5, "radius" : 5,

View File

@@ -27,23 +27,26 @@ TEST_CASE("prefab"){
safe_starter_initializer initializer; safe_starter_initializer initializer;
library& l = the<library>(); library& l = the<library>();
{ {
auto parent_res = l.load_asset<prefab_asset>("parent.json"); auto prefab_root_res = l.load_asset<prefab_asset>("prefab_root.json");
REQUIRE(parent_res); REQUIRE(prefab_root_res);
REQUIRE(parent_res->content().uuid() == "73740BC4-CE9F-4A7F-A029-4AB65027A8AE");
REQUIRE(parent_res->content().children().size() == 2u); const prefab& prefab_root = prefab_root_res->content();
REQUIRE(parent_res->content().children()[0].uuid() == "4A93547E-4635-4C2F-9C59-3546E11B1722"); REQUIRE(prefab_root.uuid() == "73740BC4-CE9F-4A7F-A029-4AB65027A8AE");
REQUIRE(parent_res->content().children()[1].uuid() == "58063213-9FC1-457C-B773-B826BE1BE6D7");
auto go = the<world>().instantiate(parent_res->content()); REQUIRE(prefab_root.children().size() == 2u);
REQUIRE(go); 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<named>()); auto go = the<world>().instantiate(prefab_root);
REQUIRE(go.component<named>()->name() == "parent");
REQUIRE(go.component<circle_collider>()); {
REQUIRE(math::approximately(go.component<circle_collider>()->radius(), 5.f)); REQUIRE(go.component<named>());
REQUIRE(go.component<circle_collider>()->offset() == v2f(4.f,2.f)); REQUIRE(go.component<named>()->name() == "root");
REQUIRE(go.component<circle_collider>());
REQUIRE(go.component<circle_collider>()->radius() == Approx(5.f));
REQUIRE(go.component<circle_collider>()->offset() == v2f(4.f,2.f));
}
{ {
node_iptr go_node = go.component<actor>() node_iptr go_node = go.component<actor>()
@@ -75,31 +78,34 @@ TEST_CASE("prefab"){
} }
{ {
auto prefab_res = l.load_asset<prefab_asset>("prefab.json"); auto prefab_child_res = l.load_asset<prefab_asset>("prefab_child.json");
REQUIRE(prefab_res); REQUIRE(prefab_child_res);
REQUIRE(prefab_res->content().uuid().empty());
REQUIRE(prefab_res->content().children().size() == 2u); const prefab& prefab_child = prefab_child_res->content();
REQUIRE(prefab_res->content().children()[0].uuid() == "4A93547E-4635-4C2F-9C59-3546E11B1722"); REQUIRE(prefab_child.uuid().empty());
REQUIRE(prefab_res->content().children()[1].uuid() == "58063213-9FC1-457C-B773-B826BE1BE6D7");
auto go = the<world>().instantiate(prefab_res->content()); REQUIRE(prefab_child.children().size() == 2u);
REQUIRE(go); 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<named>()); auto go = the<world>().instantiate(prefab_child);
REQUIRE(go.component<named>()->name() == "prefab");
REQUIRE(go.component<rect_collider>()); {
REQUIRE(go.component<rect_collider>()->size() == v2f(1.f,2.f)); REQUIRE(go.component<named>());
REQUIRE(go.component<rect_collider>()->offset() == v2f(2.f,4.f)); REQUIRE(go.component<named>()->name() == "child");
REQUIRE(go.component<circle_collider>()); REQUIRE(go.component<rect_collider>());
REQUIRE(math::approximately(go.component<circle_collider>()->radius(), 5.f)); REQUIRE(go.component<rect_collider>()->size() == v2f(1.f,2.f));
REQUIRE(go.component<circle_collider>()->offset() == v2f(4.f,2.f)); REQUIRE(go.component<rect_collider>()->offset() == v2f(2.f,4.f));
REQUIRE(go.component<polygon_collider>()); REQUIRE(go.component<circle_collider>());
REQUIRE(go.component<polygon_collider>()->points() == vector<v2f>{{1,2},{2,3},{3,4}}); REQUIRE(go.component<circle_collider>()->radius() == Approx(5.f));
REQUIRE(go.component<polygon_collider>()->offset() == v2f(8.f,4.f)); REQUIRE(go.component<circle_collider>()->offset() == v2f(4.f,2.f));
REQUIRE(go.component<polygon_collider>());
REQUIRE(go.component<polygon_collider>()->points() == vector<v2f>{{1,2},{2,3},{3,4}});
REQUIRE(go.component<polygon_collider>()->offset() == v2f(8.f,4.f));
}
{ {
node_iptr go_node = go.component<actor>() node_iptr go_node = go.component<actor>()