Properly link flecs library
This commit is contained in:
16
engine/libs/flecs/examples/cpp/rules/basics/include/basics.h
Normal file
16
engine/libs/flecs/examples/cpp/rules/basics/include/basics.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef BASICS_H
|
||||
#define BASICS_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "basics/bake_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
)
|
||||
(.)
|
||||
.|.
|
||||
| |
|
||||
_.--| |--._
|
||||
.-'; ;`-'& ; `&.
|
||||
\ & ; & &_/
|
||||
|"""---...---"""|
|
||||
\ | | | | | | | /
|
||||
`---.|.|.|.---'
|
||||
|
||||
* This file is generated by bake.lang.c for your convenience. Headers of
|
||||
* dependencies will automatically show up in this file. Include bake_config.h
|
||||
* in your main project file. Do not edit! */
|
||||
|
||||
#ifndef BASICS_BAKE_CONFIG_H
|
||||
#define BASICS_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
11
engine/libs/flecs/examples/cpp/rules/basics/project.json
Normal file
11
engine/libs/flecs/examples/cpp/rules/basics/project.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "basics",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
65
engine/libs/flecs/examples/cpp/rules/basics/src/main.cpp
Normal file
65
engine/libs/flecs/examples/cpp/rules/basics/src/main.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#include <basics.h>
|
||||
#include <iostream>
|
||||
|
||||
struct Eats { };
|
||||
struct Healthy { };
|
||||
|
||||
int main(int, char *[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
flecs::entity Apples = ecs.entity("Apples").add<Healthy>();
|
||||
flecs::entity Salad = ecs.entity("Salad").add<Healthy>();
|
||||
flecs::entity Burgers = ecs.entity("Burgers");
|
||||
flecs::entity Pizza = ecs.entity("Pizza");
|
||||
flecs::entity Chocolate = ecs.entity("Chocolate");
|
||||
|
||||
ecs.entity("Bob")
|
||||
.add<Eats>(Apples)
|
||||
.add<Eats>(Burgers)
|
||||
.add<Eats>(Pizza);
|
||||
|
||||
ecs.entity("Alice")
|
||||
.add<Eats>(Salad)
|
||||
.add<Eats>(Chocolate)
|
||||
.add<Eats>(Apples);
|
||||
|
||||
// Here we're creating a rule that in the query DSL would look like this:
|
||||
// Eats($This, $Food), Healthy($Food)
|
||||
//
|
||||
// Rules are similar to queries, but support more advanced features. This
|
||||
// example shows how the basics of how to use rules & variables.
|
||||
flecs::rule<> r = ecs.rule_builder()
|
||||
// Identifiers that start with _ are query variables. Query variables
|
||||
// are like wildcards, but enforce that the entity substituted by the
|
||||
// wildcard is the same across terms.
|
||||
//
|
||||
// For example, in this query it is not guaranteed that the entity
|
||||
// substituted by the * for Eats is the same as for Healthy:
|
||||
// (Eats, *), Healthy(*)
|
||||
//
|
||||
// By replacing * with _Food, both terms are constrained to use the
|
||||
// same entity.
|
||||
.with<Eats>("$Food")
|
||||
.with<Healthy>().src("$Food")
|
||||
.build();
|
||||
|
||||
// Lookup the index of the variable. This will let us quickly lookup its
|
||||
// value while we're iterating.
|
||||
int food_var = r.find_var("Food");
|
||||
|
||||
// Iterate the rule
|
||||
r.each([&](flecs::iter& it, size_t index) {
|
||||
std::cout
|
||||
<< it.entity(index).name()
|
||||
<< " eats " << it.get_var(food_var).name()
|
||||
<< "\n";
|
||||
});
|
||||
|
||||
// Rules need to be explicitly deleted.
|
||||
r.destruct();
|
||||
|
||||
// Output:
|
||||
// Bob eats Apples
|
||||
// Alice eats Apples
|
||||
// Alice eats Salad
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef COMPONENT_INHERITANCE_H
|
||||
#define COMPONENT_INHERITANCE_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "component_inheritance/bake_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
)
|
||||
(.)
|
||||
.|.
|
||||
| |
|
||||
_.--| |--._
|
||||
.-'; ;`-'& ; `&.
|
||||
\ & ; & &_/
|
||||
|"""---...---"""|
|
||||
\ | | | | | | | /
|
||||
`---.|.|.|.---'
|
||||
|
||||
* This file is generated by bake.lang.c for your convenience. Headers of
|
||||
* dependencies will automatically show up in this file. Include bake_config.h
|
||||
* in your main project file. Do not edit! */
|
||||
|
||||
#ifndef COMPONENT_INHERITANCE_BAKE_CONFIG_H
|
||||
#define COMPONENT_INHERITANCE_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "component_inheritance",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
#include <component_inheritance.h>
|
||||
#include <iostream>
|
||||
|
||||
// This example shows how rules can be used to match simple inheritance trees.
|
||||
|
||||
struct Unit { };
|
||||
struct CombatUnit : Unit { };
|
||||
struct MeleeUnit : CombatUnit { };
|
||||
struct RangedUnit : CombatUnit { };
|
||||
|
||||
struct Warrior : MeleeUnit { };
|
||||
struct Wizard : RangedUnit { };
|
||||
struct Marksman : RangedUnit { };
|
||||
struct Builder : Unit { };
|
||||
|
||||
int main(int, char *[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
// Make the ECS aware of the inheritance relationships. Note that IsA
|
||||
// relationship used here is the same as in the prefab example.
|
||||
ecs.component<CombatUnit>().is_a<Unit>();
|
||||
ecs.component<MeleeUnit>().is_a<CombatUnit>();
|
||||
ecs.component<RangedUnit>().is_a<CombatUnit>();
|
||||
|
||||
ecs.component<Warrior>().is_a<MeleeUnit>();
|
||||
ecs.component<Wizard>().is_a<RangedUnit>();
|
||||
ecs.component<Marksman>().is_a<RangedUnit>();
|
||||
ecs.component<Builder>().is_a<Unit>();
|
||||
|
||||
// Create a few units
|
||||
ecs.entity("warrior_1").add<Warrior>();
|
||||
ecs.entity("warrior_2").add<Warrior>();
|
||||
|
||||
ecs.entity("marksman_1").add<Marksman>();
|
||||
ecs.entity("marksman_2").add<Marksman>();
|
||||
|
||||
ecs.entity("wizard_1").add<Wizard>();
|
||||
ecs.entity("wizard_2").add<Wizard>();
|
||||
|
||||
ecs.entity("builder_1").add<Builder>();
|
||||
ecs.entity("builder_2").add<Builder>();
|
||||
|
||||
// Create a rule to find all ranged units
|
||||
flecs::rule<RangedUnit> r = ecs.rule<RangedUnit>();
|
||||
|
||||
// Iterate the rule
|
||||
r.each([](flecs::entity e, RangedUnit) {
|
||||
std::cout << "Unit " << e.name() << " found\n";
|
||||
});
|
||||
|
||||
r.destruct();
|
||||
|
||||
// Output:
|
||||
// Unit wizard_1 found
|
||||
// Unit wizard_2 found
|
||||
// Unit marksman_1 found
|
||||
// Unit marksman_2 found
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef CYCLIC_VARIABLES_H
|
||||
#define CYCLIC_VARIABLES_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "cyclic_variables/bake_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
)
|
||||
(.)
|
||||
.|.
|
||||
| |
|
||||
_.--| |--._
|
||||
.-'; ;`-'& ; `&.
|
||||
\ & ; & &_/
|
||||
|"""---...---"""|
|
||||
\ | | | | | | | /
|
||||
`---.|.|.|.---'
|
||||
|
||||
* This file is generated by bake.lang.c for your convenience. Headers of
|
||||
* dependencies will automatically show up in this file. Include bake_config.h
|
||||
* in your main project file. Do not edit! */
|
||||
|
||||
#ifndef CYCLIC_VARIABLES_BAKE_CONFIG_H
|
||||
#define CYCLIC_VARIABLES_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "cyclic_variables",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
#include <cyclic_variables.h>
|
||||
#include <iostream>
|
||||
|
||||
// This example shows how a rule may have terms with cyclic dependencies on
|
||||
// variables.
|
||||
|
||||
struct Likes { };
|
||||
|
||||
int main(int, char *[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
flecs::entity bob = ecs.entity("Bob");
|
||||
flecs::entity alice = ecs.entity("Alice");
|
||||
flecs::entity john = ecs.entity("John");
|
||||
flecs::entity jane = ecs.entity("Jane");
|
||||
|
||||
bob.add<Likes>(alice);
|
||||
alice.add<Likes>(bob);
|
||||
john.add<Likes>(jane);
|
||||
jane.add<Likes>(john);
|
||||
bob.add<Likes>(jane); // inserting a bit of drama
|
||||
|
||||
// The following rule will only return entities that have a cyclic Likes
|
||||
// relationship- that is they must both like each other.
|
||||
//
|
||||
// The equivalent query in the DSL is:
|
||||
// Likes($X, $Y), Likes($Y, $X)
|
||||
//
|
||||
// This is also an example of a query where all sources are variables. By
|
||||
// default queries use the builtin "This" variable as subject, which is what
|
||||
// populates the entities array in the query result (accessed by the
|
||||
// iter::entity function).
|
||||
//
|
||||
// Because this query does not use This at all, the entities array will not
|
||||
// be populated, and it.count() will always be 0.
|
||||
flecs::rule<> r = ecs.rule_builder()
|
||||
.with<Likes>("$Y").src("$X")
|
||||
.with<Likes>("$X").src("$Y")
|
||||
.build();
|
||||
|
||||
// Lookup the index of the variables. This will let us quickly lookup their
|
||||
// values while we're iterating.
|
||||
int x_var = r.find_var("X");
|
||||
int y_var = r.find_var("Y");
|
||||
|
||||
// Because the query doesn't use the This variable we cannot use "each"
|
||||
// which iterates the entities array. Instead we can use iter like this:
|
||||
r.iter([&](flecs::iter& it) {
|
||||
flecs::entity x = it.get_var(x_var);
|
||||
flecs::entity y = it.get_var(y_var);
|
||||
std::cout << x.name() << " likes " << y.name() << "\n";
|
||||
});
|
||||
|
||||
// Output
|
||||
// Alice likes Bob
|
||||
// Bob likes Alice
|
||||
// Jane likes John
|
||||
// John likes Jane
|
||||
|
||||
// Note that the rule returns each pair twice. The reason for this is that
|
||||
// the goal of the rule engine is to return all "facts" that are true
|
||||
// within the given constraints. Since we did not give it any constraints
|
||||
// that would favor a person being matched by X or Y, the rule engine
|
||||
// returns both.
|
||||
|
||||
r.destruct();
|
||||
}
|
||||
5
engine/libs/flecs/examples/cpp/rules/facts/.gitignore
vendored
Normal file
5
engine/libs/flecs/examples/cpp/rules/facts/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
.bake_cache
|
||||
.DS_Store
|
||||
.vscode
|
||||
gcov
|
||||
bin
|
||||
16
engine/libs/flecs/examples/cpp/rules/facts/include/facts.h
Normal file
16
engine/libs/flecs/examples/cpp/rules/facts/include/facts.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef FACTS_H
|
||||
#define FACTS_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "facts/bake_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
)
|
||||
(.)
|
||||
.|.
|
||||
| |
|
||||
_.--| |--._
|
||||
.-'; ;`-'& ; `&.
|
||||
\ & ; & &_/
|
||||
|"""---...---"""|
|
||||
\ | | | | | | | /
|
||||
`---.|.|.|.---'
|
||||
|
||||
* This file is generated by bake.lang.c for your convenience. Headers of
|
||||
* dependencies will automatically show up in this file. Include bake_config.h
|
||||
* in your main project file. Do not edit! */
|
||||
|
||||
#ifndef FACTS_BAKE_CONFIG_H
|
||||
#define FACTS_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
10
engine/libs/flecs/examples/cpp/rules/facts/project.json
Normal file
10
engine/libs/flecs/examples/cpp/rules/facts/project.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"id": "facts",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++"
|
||||
}
|
||||
}
|
||||
90
engine/libs/flecs/examples/cpp/rules/facts/src/main.cpp
Normal file
90
engine/libs/flecs/examples/cpp/rules/facts/src/main.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#include <facts.h>
|
||||
#include <iostream>
|
||||
|
||||
// This example shows how to use rules for testing facts. A fact is a query that
|
||||
// has no variable elements. Consider a regular ECS query like this:
|
||||
// Position, Velocity
|
||||
//
|
||||
// When written out in full, this query looks like:
|
||||
// Position($This), Velocity($This)
|
||||
//
|
||||
// "This" is a (builtin) query variable that is unknown before we evaluate the
|
||||
// query. Therefore this query does not test a fact, we can't know which values
|
||||
// This will assume.
|
||||
//
|
||||
// An example of a fact-checking query is:
|
||||
// IsA(Cat, Animal)
|
||||
//
|
||||
// This is a fact: the query has no elements that are unknown before evaluating
|
||||
// the query. A rule that checks a fact does not return entities, but will
|
||||
// instead return the reasons why a fact is true (if it is true).
|
||||
|
||||
struct Likes { };
|
||||
|
||||
int main(int, char *[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
flecs::entity bob = ecs.entity("Bob");
|
||||
flecs::entity alice = ecs.entity("Alice");
|
||||
flecs::entity jane = ecs.entity("Jane");
|
||||
flecs::entity john = ecs.entity("John");
|
||||
|
||||
bob.add<Likes>(alice);
|
||||
alice.add<Likes>(bob);
|
||||
jane.add<Likes>(john);
|
||||
john.add<Likes>(jane);
|
||||
|
||||
bob.add<Likes>(john); // bit of drama
|
||||
|
||||
// Create a rule that checks if two entities like each other. By itself this
|
||||
// rule is not a fact, but we can use it to check facts by populating both
|
||||
// of its variables.
|
||||
//
|
||||
// The equivalent query in the DSL is:
|
||||
// Likes($X, $Y), Likes($Y, $X)
|
||||
//
|
||||
// Instead of using variables we could have created a rule that referred the
|
||||
// entities directly, but then we would have to create a rule for each
|
||||
// fact, vs reusing a single rule for multiple facts.
|
||||
flecs::rule<> friends = ecs.rule_builder()
|
||||
.with<Likes>("$Y").src("$X")
|
||||
.with<Likes>("$X").src("$Y")
|
||||
.build();
|
||||
|
||||
int x_var = friends.find_var("X");
|
||||
int y_var = friends.find_var("Y");
|
||||
|
||||
// Check a few facts
|
||||
|
||||
std::cout << "Are Bob and Alice friends? " <<
|
||||
(friends.iter()
|
||||
.set_var(x_var, bob)
|
||||
.set_var(y_var, alice)
|
||||
.is_true() ? "Yes\n" : "No\n");
|
||||
|
||||
std::cout << "Are Bob and John friends? " <<
|
||||
(friends.iter()
|
||||
.set_var(x_var, bob)
|
||||
.set_var(y_var, john)
|
||||
.is_true() ? "Yes\n" : "No\n");
|
||||
|
||||
std::cout << "Are Jane and John friends? " <<
|
||||
(friends.iter()
|
||||
.set_var(x_var, jane)
|
||||
.set_var(y_var, john)
|
||||
.is_true() ? "Yes\n" : "No\n");
|
||||
|
||||
// It doesn't matter who we assign to X or Y. After the variables are
|
||||
// substituted, either yields a fact that is true.
|
||||
std::cout << "Are John and Jane friends? " <<
|
||||
(friends.iter()
|
||||
.set_var(x_var, john)
|
||||
.set_var(y_var, jane)
|
||||
.is_true() ? "Yes\n" : "No\n");
|
||||
|
||||
// Output
|
||||
// Are Bob and Alice friends? Yes
|
||||
// Are Bob and John friends? No
|
||||
// Are Jane and John friends? Yes
|
||||
// Are John and Jane friends? Yes
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef SETTING_VARIABLES_H
|
||||
#define SETTING_VARIABLES_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "setting_variables/bake_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
)
|
||||
(.)
|
||||
.|.
|
||||
| |
|
||||
_.--| |--._
|
||||
.-'; ;`-'& ; `&.
|
||||
\ & ; & &_/
|
||||
|"""---...---"""|
|
||||
\ | | | | | | | /
|
||||
`---.|.|.|.---'
|
||||
|
||||
* This file is generated by bake.lang.c for your convenience. Headers of
|
||||
* dependencies will automatically show up in this file. Include bake_config.h
|
||||
* in your main project file. Do not edit! */
|
||||
|
||||
#ifndef SETTING_VARIABLES_BAKE_CONFIG_H
|
||||
#define SETTING_VARIABLES_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "setting_variables",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
#include <setting_variables.h>
|
||||
#include <iostream>
|
||||
|
||||
// This example extends the component_inheritance example, and shows how
|
||||
// we can use a single rule to match units from different players and platoons
|
||||
// by setting query variables before we iterate.
|
||||
//
|
||||
// The units in this example belong to a platoon, with the platoons belonging
|
||||
// to a player.
|
||||
|
||||
// Unit datamodel
|
||||
struct Unit { };
|
||||
struct CombatUnit : Unit { };
|
||||
struct MeleeUnit : CombatUnit { };
|
||||
struct RangedUnit : CombatUnit { };
|
||||
struct Warrior : MeleeUnit { };
|
||||
struct Wizard : RangedUnit { };
|
||||
struct Marksman : RangedUnit { };
|
||||
|
||||
// Player/Platoon tags
|
||||
struct Player { };
|
||||
struct Platoon { };
|
||||
|
||||
static const int PlayerCount = 100;
|
||||
static const int PlatoonsPerPlayer = 3;
|
||||
|
||||
int main(int, char *[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
// See component_inheritance example
|
||||
ecs.component<CombatUnit>().is_a<Unit>();
|
||||
ecs.component<MeleeUnit>().is_a<CombatUnit>();
|
||||
ecs.component<RangedUnit>().is_a<CombatUnit>();
|
||||
|
||||
ecs.component<Warrior>().is_a<MeleeUnit>();
|
||||
ecs.component<Wizard>().is_a<RangedUnit>();
|
||||
ecs.component<Marksman>().is_a<RangedUnit>();
|
||||
|
||||
// Populate store with players and platoons
|
||||
for (int p = 0; p < PlayerCount; p ++) {
|
||||
flecs::entity player;
|
||||
|
||||
// Give first player a name so we can look it up later
|
||||
if (p == 0) {
|
||||
player = ecs.entity("MyPlayer");
|
||||
} else {
|
||||
player = ecs.entity();
|
||||
}
|
||||
|
||||
// Add player tag so we can query for all players if we want to
|
||||
player.add<Player>();
|
||||
|
||||
for (int pl = 0; pl < PlatoonsPerPlayer; pl ++) {
|
||||
flecs::entity platoon = ecs.entity().add<Player>(player);
|
||||
|
||||
// Add platoon tag so we can query for all platoons if we want to
|
||||
platoon.add<Platoon>();
|
||||
|
||||
// Add warriors, wizards and marksmen to the platoon
|
||||
ecs.entity().add<Warrior>().add<Platoon>(platoon);
|
||||
ecs.entity().add<Marksman>().add<Platoon>(platoon);
|
||||
ecs.entity().add<Wizard>().add<Platoon>(platoon);
|
||||
}
|
||||
}
|
||||
|
||||
// Create a rule to find all RangedUnits for a platoon/player. The
|
||||
// equivalent query in the query DSL would look like this:
|
||||
// (Platoon, $Platoon), Player($Platoon, $Player)
|
||||
//
|
||||
// The way to read how this query is evaluated is:
|
||||
// - find all entities with (Platoon, *), store * in _Platoon
|
||||
// - check if _Platoon has (Player, *), store * in _Player
|
||||
flecs::rule<RangedUnit> r = ecs.rule_builder<RangedUnit>()
|
||||
.with<Platoon>().second("$Platoon")
|
||||
.with<Player>("$Player").src("$Platoon")
|
||||
.build();
|
||||
|
||||
// If we would iterate this rule it would return all ranged units for all
|
||||
// platoons & for all players. We can limit the results to just a single
|
||||
// platoon or a single player setting a variable beforehand. In this example
|
||||
// we'll just find all platoons & ranged units for a single player.
|
||||
|
||||
int player_var = r.find_var("Player");
|
||||
int platoon_var = r.find_var("Platoon");
|
||||
|
||||
// Iterate rule, limit the results to units of MyPlayer
|
||||
r.iter().set_var(player_var, ecs.lookup("MyPlayer"))
|
||||
.each([&](flecs::iter& it, size_t index, RangedUnit) {
|
||||
flecs::entity unit = it.entity(index);
|
||||
std::cout << "Unit " << unit.path() << " of class "
|
||||
<< it.id(1).str() << " in platoon "
|
||||
<< it.get_var(platoon_var).path() << " for player "
|
||||
<< it.get_var(player_var).path() << "\n";
|
||||
});
|
||||
|
||||
r.destruct();
|
||||
|
||||
// Output:
|
||||
// Unit ::499 of class Wizard in platoon ::496 for player ::MyPlayer
|
||||
// Unit ::503 of class Wizard in platoon ::500 for player ::MyPlayer
|
||||
// Unit ::507 of class Wizard in platoon ::504 for player ::MyPlayer
|
||||
// Unit ::498 of class Marksman in platoon ::496 for player ::MyPlayer
|
||||
// Unit ::502 of class Marksman in platoon ::500 for player ::MyPlayer
|
||||
// Unit ::506 of class Marksman in platoon ::504 for player ::MyPlayer
|
||||
|
||||
// Try removing the set_var call, this will cause the iterator to return
|
||||
// all units in all platoons for all players.
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef TRANSITIVE_QUERIES_H
|
||||
#define TRANSITIVE_QUERIES_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "transitive_queries/bake_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
)
|
||||
(.)
|
||||
.|.
|
||||
| |
|
||||
_.--| |--._
|
||||
.-'; ;`-'& ; `&.
|
||||
\ & ; & &_/
|
||||
|"""---...---"""|
|
||||
\ | | | | | | | /
|
||||
`---.|.|.|.---'
|
||||
|
||||
* This file is generated by bake.lang.c for your convenience. Headers of
|
||||
* dependencies will automatically show up in this file. Include bake_config.h
|
||||
* in your main project file. Do not edit! */
|
||||
|
||||
#ifndef TRANSITIVE_QUERIES_BAKE_CONFIG_H
|
||||
#define TRANSITIVE_QUERIES_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "transitive_queries",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
#include <transitive_queries.h>
|
||||
#include <iostream>
|
||||
|
||||
// Transitive relationships make it possible to tell the ECS that if an entity
|
||||
// has a relationship (R, X) and X has relationship (R, Y), the entity should be
|
||||
// treated as if it also has (R, Y). In practice this is useful for expressing
|
||||
// things like:
|
||||
//
|
||||
// Bob lives in SanFrancisco
|
||||
// San Francisco is located in the United States
|
||||
// Therefore Bob also lives in the United States.
|
||||
//
|
||||
// An example of transitivity can be seen in the component_inheritance example.
|
||||
// This example uses the builtin IsA relationship, which is transitive. This
|
||||
// example shows how to achieve similar behavior with a user-defined relationship.
|
||||
|
||||
struct LocatedIn { };
|
||||
struct Planet { };
|
||||
struct Continent { };
|
||||
struct Country { };
|
||||
struct State { };
|
||||
struct City { };
|
||||
struct Person { };
|
||||
|
||||
int main(int, char *[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
// Register the LocatedIn relationship as transitive
|
||||
ecs.component<LocatedIn>().add(flecs::Transitive);
|
||||
|
||||
// Populate the store with locations
|
||||
flecs::entity earth = ecs.entity("Earth")
|
||||
.add<Planet>();
|
||||
|
||||
// Continents
|
||||
flecs::entity north_america = ecs.entity("NorthAmerica")
|
||||
.add<Continent>()
|
||||
.add<LocatedIn>(earth);
|
||||
|
||||
flecs::entity europe = ecs.entity("Europe")
|
||||
.add<Continent>()
|
||||
.add<LocatedIn>(earth);
|
||||
|
||||
// Countries
|
||||
flecs::entity united_states = ecs.entity("UnitedStates")
|
||||
.add<Country>()
|
||||
.add<LocatedIn>(north_america);
|
||||
|
||||
flecs::entity netherlands = ecs.entity("Netherlands")
|
||||
.add<Country>()
|
||||
.add<LocatedIn>(europe);
|
||||
|
||||
// States
|
||||
flecs::entity california = ecs.entity("California")
|
||||
.add<State>()
|
||||
.add<LocatedIn>(united_states);
|
||||
|
||||
flecs::entity washington = ecs.entity("Washington")
|
||||
.add<State>()
|
||||
.add<LocatedIn>(united_states);
|
||||
|
||||
flecs::entity noord_holland = ecs.entity("NoordHolland")
|
||||
.add<State>()
|
||||
.add<LocatedIn>(netherlands);
|
||||
|
||||
// Cities
|
||||
flecs::entity san_francisco = ecs.entity("SanFrancisco")
|
||||
.add<City>()
|
||||
.add<LocatedIn>(california);
|
||||
|
||||
flecs::entity seattle = ecs.entity("Seattle")
|
||||
.add<City>()
|
||||
.add<LocatedIn>(washington);
|
||||
|
||||
flecs::entity amsterdam = ecs.entity("Amsterdam")
|
||||
.add<City>()
|
||||
.add<LocatedIn>(noord_holland);
|
||||
|
||||
// Inhabitants
|
||||
ecs.entity("Bob")
|
||||
.add<Person>()
|
||||
.add<LocatedIn>(san_francisco);
|
||||
|
||||
ecs.entity("Alice")
|
||||
.add<Person>()
|
||||
.add<LocatedIn>(seattle);
|
||||
|
||||
ecs.entity("Job")
|
||||
.add<Person>()
|
||||
.add<LocatedIn>(amsterdam);
|
||||
|
||||
// Create a query that finds the countries persons live in. Note that these
|
||||
// have not been explicitly added to the Person entities, but because the
|
||||
// LocatedIn is transitive, the rule engine will traverse the relationship
|
||||
// until it found something that is a country.
|
||||
//
|
||||
// The equivalent of this query in the DSL is:
|
||||
// Person, (LocatedIn, $Location), Country($Location)
|
||||
flecs::rule<> r = ecs.rule_builder()
|
||||
.with<Person>()
|
||||
.with<LocatedIn>("$Location")
|
||||
.with<Country>().src("$Location")
|
||||
.build();
|
||||
|
||||
// Lookup the index of the variable. This will let us quickly lookup its
|
||||
// value while we're iterating.
|
||||
int location_var = r.find_var("Location");
|
||||
|
||||
// Iterate the rule
|
||||
r.each([&](flecs::iter& it, size_t index) {
|
||||
std::cout
|
||||
<< it.entity(index).name()
|
||||
<< " lives in " << it.get_var(location_var).name()
|
||||
<< "\n";
|
||||
});
|
||||
|
||||
// Rules need to be explicitly deleted.
|
||||
r.destruct();
|
||||
|
||||
// Output:
|
||||
// Bob lives in UnitedStates
|
||||
// Alice lives in UnitedStates
|
||||
// Job lives in Netherlands
|
||||
}
|
||||
Reference in New Issue
Block a user