Skip to content

Instantly share code, notes, and snippets.

@SanderMertens
Created June 9, 2020 02:07
Show Gist options
  • Save SanderMertens/501476607dec0ff6c6ab992fe73f8aae to your computer and use it in GitHub Desktop.
Save SanderMertens/501476607dec0ff6c6ab992fe73f8aae to your computer and use it in GitHub Desktop.
struct BulletCount {
int32_t count;
};
struct Damage {
float value;
};
int main(int argc, char * argv[]) {
flecs::world world(argc, argv);
// Declare components
flecs::component<BulletCount>(world, "BulletCount");
flecs::component<Damage>(world, "Damage");
// System that assigns random value to Damage upon
// adding the Damage component
flecs::system<Damage>(world)
.kind(flecs::OnAdd)
.each([](flecs::entity e, Damage &dmg) {
dmg.value = ((float)rand() / (float)RAND_MAX) * 3 + 20;
});
// Gun template with bullet count of 6
auto Gun = flecs::prefab(world, "Gun")
.set<BulletCount>({6});
// The fun part: create a reusable type that
// contains both the gun and the damage component.
// Each gun created with this type will have
// 6 bullets and a random damage value
auto RandomDmgGun = flecs::type(world, "RandomDmgGun")
.add_instanceof(Gun)
.add<Damage>();
// Instantiate the random gun type
auto my_gun = flecs::entity(world)
.add(RandomDmgGun);
// Outputs 6
BulletCount* bullets = my_gun.get_ptr<BulletCount>();
std::cout << bullets->count << std::endl;
// Outputs random value
Damage* damage = my_gun.get_ptr<Damage>();
std::cout << damage->value << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment