From 0894ebeef918c81a633a462fabe1abe13614dfb4 Mon Sep 17 00:00:00 2001 From: Ricardo Antunes Date: Tue, 30 Jan 2024 12:21:30 +0000 Subject: [PATCH] docs(scene): showcase relations --- engine/samples/scene/assets/main.cubos | 8 +++-- engine/samples/scene/assets/sub.cubos | 2 +- engine/samples/scene/components.hpp | 9 ++++-- engine/samples/scene/main.cpp | 41 ++++++++++++++++++-------- 4 files changed, 43 insertions(+), 17 deletions(-) diff --git a/engine/samples/scene/assets/main.cubos b/engine/samples/scene/assets/main.cubos index 72386ebc50..1b3fda4a0a 100644 --- a/engine/samples/scene/assets/main.cubos +++ b/engine/samples/scene/assets/main.cubos @@ -8,10 +8,14 @@ "Num": 0 }, "sub1.root": { - "Parent": "main" + "OwnedBy": "main", + "DistanceTo": { + "entity": "sub2.root", + "value": 5 + } }, "sub2.root": { - "Parent": "main" + "OwnedBy": "main" } } } diff --git a/engine/samples/scene/assets/sub.cubos b/engine/samples/scene/assets/sub.cubos index 12bb64ae5e..ce337a0b33 100644 --- a/engine/samples/scene/assets/sub.cubos +++ b/engine/samples/scene/assets/sub.cubos @@ -6,7 +6,7 @@ }, "child": { "Num": 2, - "Parent": "root" + "OwnedBy": "root" } } } diff --git a/engine/samples/scene/components.hpp b/engine/samples/scene/components.hpp index 45ddc33dbc..b739f1cdc3 100644 --- a/engine/samples/scene/components.hpp +++ b/engine/samples/scene/components.hpp @@ -10,9 +10,14 @@ struct Num int value; }; -struct Parent +struct OwnedBy { CUBOS_REFLECT; +}; - cubos::core::ecs::Entity entity; +struct DistanceTo +{ + CUBOS_REFLECT; + + int value; }; diff --git a/engine/samples/scene/main.cpp b/engine/samples/scene/main.cpp index 5921665047..62aff8eabd 100644 --- a/engine/samples/scene/main.cpp +++ b/engine/samples/scene/main.cpp @@ -20,9 +20,17 @@ CUBOS_REFLECT_IMPL(Num) return cubos::core::ecs::TypeBuilder("Num").withField("value", &Num::value).build(); } -CUBOS_REFLECT_IMPL(Parent) +CUBOS_REFLECT_IMPL(OwnedBy) { - return cubos::core::ecs::TypeBuilder("Parent").withField("entity", &Parent::entity).build(); + return cubos::core::ecs::TypeBuilder("OwnedBy").tree().build(); +} + +CUBOS_REFLECT_IMPL(DistanceTo) +{ + return cubos::core::ecs::TypeBuilder("DistanceTo") + .symmetric() + .withField("value", &DistanceTo::value) + .build(); } /// [Component Refl] @@ -44,25 +52,33 @@ static void spawnScene(Commands commands, const Assets& assets) /// [Spawning the scene] /// [Displaying the scene] -static void printStuff(const World& world, Query query) +static void printStuff(Query numQuery, Query ownedByQuery, + Query distanceToQuery) { using cubos::core::data::DebugSerializer; using cubos::core::memory::Stream; DebugSerializer ser{Stream::stdOut}; - for (auto [entity] : query) + for (auto [entity, num] : numQuery) { - Stream::stdOut.printf("Entity "); + Stream::stdOut.print("Entity "); ser.write(entity); - Stream::stdOut.put('\n'); - for (auto [type, value] : world.components(entity)) + Stream::stdOut.printf(":\n- Num = {}\n", num.value); + + for (auto [distanceTo, what] : distanceToQuery.pin(0, entity)) + { + Stream::stdOut.print("- DistanceTo("); + ser.write(what); + Stream::stdOut.printf(") = {}\n", distanceTo.value); + } + + for (auto [ownedBy, owner] : ownedByQuery.pin(0, entity)) { - Stream::stdOut.printf("- {}: ", type->name()); - ser.write(*type, value); - Stream::stdOut.put('\n'); + Stream::stdOut.print("- OwnedBy("); + ser.write(owner); + Stream::stdOut.print(")\n"); } - Stream::stdOut.put('\n'); } } /// [Displaying the scene] @@ -76,7 +92,8 @@ int main(int argc, char** argv) /// [Adding the plugin] cubos.addComponent(); - cubos.addComponent(); + cubos.addRelation(); + cubos.addRelation(); cubos.startupSystem(settings).tagged("cubos.settings");