Skip to content

Commit

Permalink
docs(scene): showcase relations
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Jan 30, 2024
1 parent ad0ecfb commit 0894ebe
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
8 changes: 6 additions & 2 deletions engine/samples/scene/assets/main.cubos
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
"Num": 0
},
"sub1.root": {
"Parent": "main"
"OwnedBy": "main",
"DistanceTo": {
"entity": "sub2.root",
"value": 5
}
},
"sub2.root": {
"Parent": "main"
"OwnedBy": "main"
}
}
}
2 changes: 1 addition & 1 deletion engine/samples/scene/assets/sub.cubos
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"child": {
"Num": 2,
"Parent": "root"
"OwnedBy": "root"
}
}
}
9 changes: 7 additions & 2 deletions engine/samples/scene/components.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
41 changes: 29 additions & 12 deletions engine/samples/scene/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ CUBOS_REFLECT_IMPL(Num)
return cubos::core::ecs::TypeBuilder<Num>("Num").withField("value", &Num::value).build();
}

CUBOS_REFLECT_IMPL(Parent)
CUBOS_REFLECT_IMPL(OwnedBy)
{
return cubos::core::ecs::TypeBuilder<Parent>("Parent").withField("entity", &Parent::entity).build();
return cubos::core::ecs::TypeBuilder<OwnedBy>("OwnedBy").tree().build();
}

CUBOS_REFLECT_IMPL(DistanceTo)
{
return cubos::core::ecs::TypeBuilder<DistanceTo>("DistanceTo")
.symmetric()
.withField("value", &DistanceTo::value)
.build();
}
/// [Component Refl]

Expand All @@ -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<Entity> query)
static void printStuff(Query<Entity, const Num&> numQuery, Query<const OwnedBy&, Entity> ownedByQuery,
Query<const DistanceTo&, Entity> 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]
Expand All @@ -76,7 +92,8 @@ int main(int argc, char** argv)
/// [Adding the plugin]

cubos.addComponent<Num>();
cubos.addComponent<Parent>();
cubos.addRelation<OwnedBy>();
cubos.addRelation<DistanceTo>();

cubos.startupSystem(settings).tagged("cubos.settings");

Expand Down

0 comments on commit 0894ebe

Please sign in to comment.