Deconstructing Flecs Prefabs

Flecs Prefabs

A Tree

auto trunk = ecs.entity()
.set<Box>({0.4, 0.5, 0.4})
.set<Color>({0.25, 0.2, 0.1});
ecs.entity()
.add_instanceof(trunk)
.set<Position>({randf(10), 0, randf(10)});
auto tree = ecs.entity()
auto trunk = ecs.entity()
.add_childof(tree)
.set<Position>({0, 0.25, 0})
.set<Box>({0.4, 0.5, 0.4})
.set<Color>({0.25, 0.2, 0.1});
ecs.entity()
.add_instanceof(tree)
.set<Position>({randf(10), 0, randf(10)});
auto tree = ecs.prefab("PTree")
auto trunk = ecs.prefab()
.add_childof(tree)
.set<Position>({0, 0.25, 0})
.set<Box>({0.4, 0.5, 0.4})
.set<Color>({0.25, 0.2, 0.1});
auto tree = ecs.prefab("PTree")
auto trunk = ecs.prefab()
.add_childof(tree)
.set<Position>({0, 0.25, 0})
.set<Box>({0.4, 0.5, 0.4})
.set<Color>({0.25, 0.2, 0.1});
auto canopy = ecs.prefab()
.add_childof(tree)
.set<Position>({0, 0.9, 0})
.set<Box>({0.8, 0.8, 0.8})
.set<Color>({0.2, 0.3, 0.15});
ecs.system<Position, Box>("RandomizeCanopy")
.kind(flecs::OnSet)
.each([](flecs::entity e, Position &p, Box &b) {
float h = randf(1.0) + 0.8;
b.height = h;
p.y = h / 2.0 + 0.5;
});
struct Canopy { };
  auto canopy = ecs.prefab()
.add_childof(tree)
.add<Canopy>()
.set<Position>({0, 0.9, 0})
.set<Box>({0.8, 0.8, 0.8})
.set<Color>({0.2, 0.3, 0.15});
ecs.system<Position, Box, Canopy>("RandomizeCanopy")
.kind(flecs::OnSet)
.each([](flecs::entity e, Position &p, Box &b, Canopy) {
float h = randf(1.0) + 0.8;
b.height = h;
p.y = h / 2.0 + 0.5;
});
struct Canopy { };auto tree = ecs.prefab("PTree")
auto trunk = ecs.prefab()
.add_childof(tree)
.set<Position>({0, 0.25, 0})
.set<Box>({0.4, 0.5, 0.4})
.set<Color>({0.25, 0.2, 0.1});
auto canopy = ecs.prefab()
.add_childof(tree)
.add<Canopy>()
.set<Position>({0, 0.9, 0})
.set<Box>({0.8, 0.8, 0.8})
.set<Color>({0.2, 0.3, 0.15});
ecs.system<Position, Box, Canopy>("RandomizeCanopy")
.kind(flecs::OnSet)
.each([](flecs::entity e, Position &p, Box &b, Canopy) {
float h = randf(1.0) + 0.8;
b.height = h;
p.y = h / 2.0 + 0.5;
});

Pine trees

struct Canopy { };auto trunk = ecs.prefab("PTrunk")
.set<Position>({0, 0.25, 0})
.set<Box>({0.4, 0.5, 0.4})
.set<Color>({0.25, 0.2, 0.1});
auto canopy = ecs.prefab("PCanopy")
.add<Canopy>()
.set<Color>({0.2, 0.3, 0.15});
auto tree = ecs.prefab("PTree")
auto tree_trunk = ecs.prefab()
.add_childof(tree)
.add_instanceof(trunk);
auto tree_canopy = ecs.prefab()
.add_childof(tree)
.add_instanceof(canopy)
.set<Position>({0, 0.9, 0})
.set<Box>({0.8, 0.8, 0.8});
auto pine = ecs.prefab("PPine");
auto pine_trunk = ecs.prefab()
.add_childof(pine)
.add_instanceof(trunk);
auto pine_canopy_1 = ecs.prefab()
.add_childof(pine)
.add_instanceof(canopy)
.set<Position>({0, 0.6, 0})
.set<Box>({0.8, 0.4, 0.8});
auto pine_canopy_2 = ecs.prefab()
.add_childof(pine)
.add_instanceof(canopy)
.set<Position>({0, 1.0, 0})
.set<Box>({0.6, 0.4, 0.6});
auto pine_canopy_3 = ecs.prefab()
.add_childof(pine)
.add_instanceof(canopy)
.set<Position>({0, 1.4, 0})
.set<Box>({0.4, 0.4, 0.4});
ecs.system<Position, Box>("RandomizeCanopy", "ANY:Canopy")
.kind(flecs::OnSet)
.each([](flecs::entity e, Position &p, Box &b) {
float h = randf(1.0) + 0.8;
b.height = h;
p.y = h / 2.0 + 0.5;
});
// Canopy tag
struct Canopy { };
// Trunk and canopy prefabs
auto trunk = ecs.prefab("PTrunk")
.set<Position>({0, 0.25, 0})
.set<Box>({0.4, 0.5, 0.4})
.set<Color>({0.25, 0.2, 0.1});
auto canopy = ecs.prefab("PCanopy")
.add<Canopy>()
.set<Color>({0.2, 0.3, 0.15});
// Tree prefab
auto tree = ecs.prefab("PTree")
auto tree_trunk = ecs.prefab()
.add_childof(tree)
.add_instanceof(trunk);
auto tree_canopy = ecs.prefab()
.add_childof(tree)
.add_instanceof(canopy)
.set<Position>({0, 0.9, 0})
.set<Box>({0.8, 0.8, 0.8});
// Pine tree prefab
auto pine = ecs.prefab("PPine");
auto pine_trunk = ecs.prefab()
.add_childof(pine)
.add_instanceof(trunk);
auto pine_canopy_1 = ecs.prefab()
.add_childof(pine)
.add_instanceof(canopy)
.set<Position>({0, 0.6, 0})
.set<Box>({0.8, 0.4, 0.8});
auto pine_canopy_2 = ecs.prefab()
.add_childof(pine)
.add_instanceof(canopy)
.set<Position>({0, 1.0, 0})
.set<Box>({0.6, 0.4, 0.6});
auto pine_canopy_3 = ecs.prefab()
.add_childof(pine)
.add_instanceof(canopy)
.set<Position>({0, 1.4, 0})
.set<Box>({0.4, 0.4, 0.4});
// Randomize canopy system
ecs.system<Position, Box>("RandomizeCanopy", "ANY:Canopy,"
"PARENT:INSTANCEOF | PTree")
.kind(flecs::OnSet)
.each([](flecs::entity e, Position &p, Box &b) {
float h = randf(1.0) + 0.8;
b.height = h;
p.y = h / 2.0 + 0.5;
});

Fun stuff

canopy.set<Color>({0.35, 0.25, 0.0});
auto pine_canopy = ecs.prefab("PPineCanopy")
.add_instanceof(canopy)
.set<Color>({0.35, 0.25, 0.0});

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store