Properly link flecs library
This commit is contained in:
16
engine/libs/flecs/examples/c/rules/basics/include/basics.h
Normal file
16
engine/libs/flecs/examples/c/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
|
||||
|
||||
10
engine/libs/flecs/examples/c/rules/basics/project.json
Normal file
10
engine/libs/flecs/examples/c/rules/basics/project.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"id": "basics",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
77
engine/libs/flecs/examples/c/rules/basics/src/main.c
Normal file
77
engine/libs/flecs/examples/c/rules/basics/src/main.c
Normal file
@@ -0,0 +1,77 @@
|
||||
#include <basics.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
ECS_TAG(ecs, Eats);
|
||||
ECS_TAG(ecs, Healthy);
|
||||
|
||||
ecs_entity_t Apples = ecs_new_entity(ecs, "Apples");
|
||||
ecs_entity_t Salad = ecs_new_entity(ecs, "Salad");
|
||||
ecs_entity_t Burgers = ecs_new_entity(ecs, "Burgers");
|
||||
ecs_entity_t Pizza = ecs_new_entity(ecs, "Pizza");
|
||||
ecs_entity_t Chocolate = ecs_new_entity(ecs, "Chocolate");
|
||||
|
||||
ecs_add(ecs, Apples, Healthy);
|
||||
ecs_add(ecs, Salad, Healthy);
|
||||
|
||||
ecs_entity_t bob = ecs_new_entity(ecs, "Bob");
|
||||
ecs_add_pair(ecs, bob, Eats, Apples);
|
||||
ecs_add_pair(ecs, bob, Eats, Burgers);
|
||||
ecs_add_pair(ecs, bob, Eats, Pizza);
|
||||
|
||||
ecs_entity_t alice = ecs_new_entity(ecs, "Alice");
|
||||
ecs_add_pair(ecs, alice, Eats, Salad);
|
||||
ecs_add_pair(ecs, alice, Eats, Chocolate);
|
||||
ecs_add_pair(ecs, alice, 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.
|
||||
ecs_rule_t *r = ecs_rule(ecs, {
|
||||
.terms = {
|
||||
// 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.
|
||||
{ .first.id = Eats, .second = {
|
||||
.name = "Food", .flags = EcsIsVariable },
|
||||
},
|
||||
{ .first.id = Healthy, .src = {
|
||||
.name = "Food", .flags = EcsIsVariable }
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Lookup the index of the variable. This will let us quickly lookup its
|
||||
// value while we're iterating.
|
||||
int food_var = ecs_rule_find_var(r, "Food");
|
||||
|
||||
// Iterate the rule
|
||||
ecs_iter_t it = ecs_rule_iter(ecs, r);
|
||||
while (ecs_rule_next(&it)) {
|
||||
ecs_entity_t food = ecs_iter_get_var(&it, food_var);
|
||||
for (int i = 0; i < it.count; i ++) {
|
||||
printf("%s eats %s\n", ecs_get_name(ecs, it.entities[i]),
|
||||
ecs_get_name(ecs, food));
|
||||
}
|
||||
}
|
||||
|
||||
// Rules need to be explicitly deleted
|
||||
ecs_rule_fini(r);
|
||||
|
||||
// Output
|
||||
// Bob eats Apples
|
||||
// Alice eats Apples
|
||||
// Alice eats Salad
|
||||
|
||||
return ecs_fini(ecs);
|
||||
}
|
||||
@@ -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,10 @@
|
||||
{
|
||||
"id": "component_inheritance",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
#include <component_inheritance.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// Use convenience macros to create simple hierarchy of unit types.
|
||||
// This macro call:
|
||||
// ECS_ENTITY(ecs, CombatUnit, (IsA, Unit))
|
||||
//
|
||||
// is the same as these C statements:
|
||||
// ecs_entity_t CombatUnit = ecs_new_entity("CombatUnit");
|
||||
// ecs_add_pair(ecs, CombatUnit, EcsIsA, Unit);
|
||||
|
||||
ECS_TAG(ecs, Unit);
|
||||
ECS_ENTITY(ecs, CombatUnit, (IsA, Unit));
|
||||
ECS_ENTITY(ecs, MeleeUnit, (IsA, CombatUnit));
|
||||
ECS_ENTITY(ecs, RangedUnit, (IsA, CombatUnit));
|
||||
|
||||
ECS_ENTITY(ecs, Warrior, (IsA, MeleeUnit));
|
||||
ECS_ENTITY(ecs, Wizard, (IsA, RangedUnit));
|
||||
ECS_ENTITY(ecs, Marksman, (IsA, RangedUnit));
|
||||
ECS_ENTITY(ecs, Builder, (IsA, Unit));
|
||||
|
||||
// Create a few units
|
||||
ecs_entity_t warrior_1 = ecs_new_entity(ecs, "warrior_1");
|
||||
ecs_add(ecs, warrior_1, Warrior);
|
||||
ecs_entity_t warrior_2 = ecs_new_entity(ecs, "warrior_2");
|
||||
ecs_add(ecs, warrior_2, Warrior);
|
||||
|
||||
ecs_entity_t marksman_1 = ecs_new_entity(ecs, "marksman_1");
|
||||
ecs_add(ecs, marksman_1, Marksman);
|
||||
ecs_entity_t marksman_2 = ecs_new_entity(ecs, "marksman_2");
|
||||
ecs_add(ecs, marksman_2, Marksman);
|
||||
|
||||
ecs_entity_t wizard_1 = ecs_new_entity(ecs, "wizard_1");
|
||||
ecs_add(ecs, wizard_1, Wizard);
|
||||
ecs_entity_t wizard_2 = ecs_new_entity(ecs, "wizard_2");
|
||||
ecs_add(ecs, wizard_2, Wizard);
|
||||
|
||||
ecs_entity_t builder_1 = ecs_new_entity(ecs, "builder_1");
|
||||
ecs_add(ecs, builder_1, Builder);
|
||||
ecs_entity_t builder_2 = ecs_new_entity(ecs, "builder_2");
|
||||
ecs_add(ecs, builder_2, Builder);
|
||||
|
||||
// Create a rule to find all ranged units
|
||||
ecs_rule_t *r = ecs_rule(ecs, {
|
||||
.terms = {{ .id = RangedUnit }}
|
||||
});
|
||||
|
||||
// Iterate the rule
|
||||
ecs_iter_t it = ecs_rule_iter(ecs, r);
|
||||
while (ecs_rule_next(&it)) {
|
||||
for (int i = 0; i < it.count; i ++) {
|
||||
printf("Unit %s found\n", ecs_get_name(ecs, it.entities[i]));
|
||||
}
|
||||
}
|
||||
|
||||
ecs_rule_fini(r);
|
||||
|
||||
// Output
|
||||
// Unit wizard_1 found
|
||||
// Unit wizard_2 found
|
||||
// Unit marksman_1 found
|
||||
// Unit marksman_2 found
|
||||
|
||||
return ecs_fini(ecs);
|
||||
}
|
||||
5
engine/libs/flecs/examples/c/rules/cyclic_variables/.gitignore
vendored
Normal file
5
engine/libs/flecs/examples/c/rules/cyclic_variables/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
.bake_cache
|
||||
.DS_Store
|
||||
.vscode
|
||||
gcov
|
||||
bin
|
||||
@@ -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,9 @@
|
||||
{
|
||||
"id": "cyclic_variables",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
#include <cyclic_variables.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// This example shows how a rule may have terms with cyclic dependencies on
|
||||
// variables.
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
ECS_TAG(ecs, Likes); // Likes relationship
|
||||
|
||||
ecs_entity_t bob = ecs_new_entity(ecs, "Bob");
|
||||
ecs_entity_t alice = ecs_new_entity(ecs, "Alice");
|
||||
ecs_entity_t john = ecs_new_entity(ecs, "John");
|
||||
ecs_entity_t jane = ecs_new_entity(ecs, "Jane");
|
||||
|
||||
ecs_add_pair(ecs, bob, Likes, alice);
|
||||
ecs_add_pair(ecs, alice, Likes, bob);
|
||||
ecs_add_pair(ecs, john, Likes, jane);
|
||||
ecs_add_pair(ecs, jane, Likes, john);
|
||||
ecs_add_pair(ecs, bob, 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.
|
||||
// Create a rule to find all ranged units
|
||||
ecs_rule_t *r = ecs_rule(ecs, {
|
||||
.terms = {
|
||||
{
|
||||
.first.id = Likes,
|
||||
.src = { .name = "X", .flags = EcsIsVariable },
|
||||
.second = { .name = "Y", .flags = EcsIsVariable }
|
||||
},
|
||||
{
|
||||
.first.id = Likes,
|
||||
.src = { .name = "Y", .flags = EcsIsVariable },
|
||||
.second = { .name = "X", .flags = EcsIsVariable }
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Lookup the index of the variables. This will let us quickly lookup their
|
||||
// values while we're iterating.
|
||||
int x_var = ecs_rule_find_var(r, "X");
|
||||
int y_var = ecs_rule_find_var(r, "Y");
|
||||
|
||||
// Iterate the rule
|
||||
ecs_iter_t it = ecs_rule_iter(ecs, r);
|
||||
while (ecs_rule_next(&it)) {
|
||||
ecs_entity_t x = ecs_iter_get_var(&it, x_var);
|
||||
ecs_entity_t y = ecs_iter_get_var(&it, y_var);
|
||||
printf("%s likes %s\n", ecs_get_name(ecs, x), ecs_get_name(ecs, y));
|
||||
}
|
||||
|
||||
ecs_rule_fini(r);
|
||||
|
||||
// Output
|
||||
// Bob likes Alice
|
||||
// Jane likes John
|
||||
// John likes Jane
|
||||
// Alice likes Bob
|
||||
|
||||
// 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.
|
||||
|
||||
return ecs_fini(ecs);
|
||||
}
|
||||
16
engine/libs/flecs/examples/c/rules/facts/include/facts.h
Normal file
16
engine/libs/flecs/examples/c/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/c/rules/facts/project.json
Normal file
10
engine/libs/flecs/examples/c/rules/facts/project.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"id": "facts",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
104
engine/libs/flecs/examples/c/rules/facts/src/main.c
Normal file
104
engine/libs/flecs/examples/c/rules/facts/src/main.c
Normal file
@@ -0,0 +1,104 @@
|
||||
#include <facts.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// 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).
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
ECS_TAG(ecs, Likes); // Likes relationship
|
||||
|
||||
ecs_entity_t bob = ecs_new_entity(ecs, "Bob");
|
||||
ecs_entity_t alice = ecs_new_entity(ecs, "Alice");
|
||||
ecs_entity_t john = ecs_new_entity(ecs, "John");
|
||||
ecs_entity_t jane = ecs_new_entity(ecs, "Jane");
|
||||
|
||||
ecs_add_pair(ecs, bob, Likes, alice);
|
||||
ecs_add_pair(ecs, alice, Likes, bob);
|
||||
ecs_add_pair(ecs, john, Likes, jane);
|
||||
ecs_add_pair(ecs, jane, Likes, john);
|
||||
ecs_add_pair(ecs, bob, Likes, jane); // inserting a 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 of this 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.
|
||||
//
|
||||
// See the setting_variables example for more details
|
||||
ecs_rule_t *friends = ecs_rule(ecs, {
|
||||
.terms = {
|
||||
{
|
||||
.first.id = Likes,
|
||||
.src = { .name = "X", .flags = EcsIsVariable },
|
||||
.second = { .name = "Y", .flags = EcsIsVariable }
|
||||
},
|
||||
{
|
||||
.first.id = Likes,
|
||||
.src = { .name = "Y", .flags = EcsIsVariable },
|
||||
.second = { .name = "X", .flags = EcsIsVariable }
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Lookup the index of the variables.
|
||||
int x_var = ecs_rule_find_var(friends, "X");
|
||||
int y_var = ecs_rule_find_var(friends, "Y");
|
||||
|
||||
ecs_iter_t it = ecs_rule_iter(ecs, friends);
|
||||
ecs_iter_set_var(&it, x_var, bob);
|
||||
ecs_iter_set_var(&it, y_var, alice);
|
||||
printf("Are Bob and Alice friends? %s\n",
|
||||
ecs_iter_is_true(&it) ? "Yes" : "No");
|
||||
|
||||
it = ecs_rule_iter(ecs, friends);
|
||||
ecs_iter_set_var(&it, x_var, bob);
|
||||
ecs_iter_set_var(&it, y_var, john);
|
||||
printf("Are Bob and John friends? %s\n",
|
||||
ecs_iter_is_true(&it) ? "Yes" : "No");
|
||||
|
||||
it = ecs_rule_iter(ecs, friends);
|
||||
ecs_iter_set_var(&it, x_var, jane);
|
||||
ecs_iter_set_var(&it, y_var, john);
|
||||
printf("Are Jane and John friends? %s\n",
|
||||
ecs_iter_is_true(&it) ? "Yes" : "No");
|
||||
|
||||
// It doesn't matter who we assign to X or Y. After the variables are
|
||||
// substituted, either yields a fact that is true.
|
||||
it = ecs_rule_iter(ecs, friends);
|
||||
ecs_iter_set_var(&it, x_var, john);
|
||||
ecs_iter_set_var(&it, y_var, jane);
|
||||
printf("Are John and Jane friends? %s\n",
|
||||
ecs_iter_is_true(&it) ? "Yes" : "No");
|
||||
|
||||
ecs_rule_fini(friends);
|
||||
|
||||
// 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
|
||||
|
||||
return ecs_fini(ecs);
|
||||
}
|
||||
5
engine/libs/flecs/examples/c/rules/setting_variables/.gitignore
vendored
Normal file
5
engine/libs/flecs/examples/c/rules/setting_variables/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
.bake_cache
|
||||
.DS_Store
|
||||
.vscode
|
||||
gcov
|
||||
bin
|
||||
@@ -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,9 @@
|
||||
{
|
||||
"id": "setting_variables",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
]
|
||||
}
|
||||
}
|
||||
127
engine/libs/flecs/examples/c/rules/setting_variables/src/main.c
Normal file
127
engine/libs/flecs/examples/c/rules/setting_variables/src/main.c
Normal file
@@ -0,0 +1,127 @@
|
||||
#include <setting_variables.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// 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.
|
||||
|
||||
static const int PlayerCount = 100;
|
||||
static const int PlatoonsPerPlayer = 3;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// Unit datamodel - see component_inheritance example
|
||||
ECS_TAG(ecs, Unit);
|
||||
ECS_ENTITY(ecs, CombatUnit, (IsA, Unit));
|
||||
ECS_ENTITY(ecs, MeleeUnit, (IsA, CombatUnit));
|
||||
ECS_ENTITY(ecs, RangedUnit, (IsA, CombatUnit));
|
||||
|
||||
ECS_ENTITY(ecs, Warrior, (IsA, MeleeUnit));
|
||||
ECS_ENTITY(ecs, Wizard, (IsA, RangedUnit));
|
||||
ECS_ENTITY(ecs, Marksman, (IsA, RangedUnit));
|
||||
ECS_ENTITY(ecs, Builder, (IsA, Unit));
|
||||
|
||||
// Player/Platoon tags
|
||||
ECS_TAG(ecs, Player);
|
||||
ECS_TAG(ecs, Platoon);
|
||||
|
||||
// Populate store with players and platoons
|
||||
for (int p = 0; p < PlayerCount; p ++) {
|
||||
ecs_entity_t player;
|
||||
|
||||
// Give first player a name so we can look it up later
|
||||
if (p == 0) {
|
||||
player = ecs_new_entity(ecs, "MyPlayer");
|
||||
} else {
|
||||
player = ecs_new_id(ecs);
|
||||
}
|
||||
|
||||
// Add player tag so we can query for all players if we want to
|
||||
ecs_add(ecs, player, Player);
|
||||
|
||||
for (int pl = 0; pl < PlatoonsPerPlayer; pl ++) {
|
||||
ecs_entity_t platoon = ecs_new_w_pair(ecs, Player, player);
|
||||
|
||||
// Add platoon tag so we can query for all platoons if we want to
|
||||
ecs_add(ecs, platoon, Platoon);
|
||||
|
||||
// Add warriors, wizards and marksmen to the platoon
|
||||
ecs_entity_t warrior = ecs_new(ecs, Warrior);
|
||||
ecs_add_pair(ecs, warrior, Platoon, platoon);
|
||||
|
||||
ecs_entity_t marksman = ecs_new(ecs, Marksman);
|
||||
ecs_add_pair(ecs, marksman, Platoon, platoon);
|
||||
|
||||
ecs_entity_t wizard = ecs_new(ecs, Wizard);
|
||||
ecs_add_pair(ecs, wizard, 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
|
||||
ecs_rule_t *r = ecs_rule(ecs, {
|
||||
.terms = {
|
||||
{ .first.id = RangedUnit },
|
||||
{
|
||||
.first.id = Platoon,
|
||||
.second = { .name = "Platoon", .flags = EcsIsVariable },
|
||||
},
|
||||
{
|
||||
.first.id = Player,
|
||||
.src = { .name = "Platoon", .flags = EcsIsVariable },
|
||||
.second = { .name = "Player", .flags = EcsIsVariable },
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 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 = ecs_rule_find_var(r, "Player");
|
||||
int platoon_var = ecs_rule_find_var(r, "Platoon");
|
||||
|
||||
// Iterate rule, limit the results to units of MyPlayer
|
||||
ecs_iter_t it = ecs_rule_iter(ecs, r);
|
||||
ecs_iter_set_var(&it, player_var, ecs_lookup(ecs, "MyPlayer"));
|
||||
|
||||
while (ecs_rule_next(&it)) {
|
||||
ecs_entity_t player = ecs_iter_get_var(&it, player_var);
|
||||
ecs_entity_t platoon = ecs_iter_get_var(&it, platoon_var);
|
||||
char *platoon_str = ecs_get_fullpath(ecs, platoon);
|
||||
char *class_str = ecs_id_str(ecs, ecs_field_id(&it, 1));
|
||||
|
||||
for (int i = 0; i < it.count; i ++) {
|
||||
char *unit_str = ecs_get_fullpath(ecs, it.entities[i]);
|
||||
printf("Unit %s of class %s in platoon %s for player %s\n",
|
||||
unit_str, class_str, platoon_str,
|
||||
ecs_get_name(ecs, player));
|
||||
ecs_os_free(unit_str);
|
||||
}
|
||||
|
||||
ecs_os_free(class_str);
|
||||
ecs_os_free(platoon_str);
|
||||
}
|
||||
|
||||
ecs_rule_fini(r);
|
||||
|
||||
// Output
|
||||
// Unit 513 of class Wizard in platoon 510 for player MyPlayer
|
||||
// Unit 517 of class Wizard in platoon 514 for player MyPlayer
|
||||
// Unit 521 of class Wizard in platoon 518 for player MyPlayer
|
||||
// Unit 512 of class Marksman in platoon 510 for player MyPlayer
|
||||
// Unit 516 of class Marksman in platoon 514 for player MyPlayer
|
||||
// Unit 520 of class Marksman in platoon 518 for player MyPlayer
|
||||
|
||||
return ecs_fini(ecs);
|
||||
}
|
||||
@@ -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,10 @@
|
||||
{
|
||||
"id": "transitive_queries",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
137
engine/libs/flecs/examples/c/rules/transitive_queries/src/main.c
Normal file
137
engine/libs/flecs/examples/c/rules/transitive_queries/src/main.c
Normal file
@@ -0,0 +1,137 @@
|
||||
#include <transitive_queries.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// 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.
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// LocatedIn relationship
|
||||
ecs_entity_t LocatedIn = ecs_new_id(ecs);
|
||||
ecs_add_id(ecs, LocatedIn, EcsTransitive);
|
||||
|
||||
// Tags
|
||||
ECS_TAG(ecs, Planet);
|
||||
ECS_TAG(ecs, Continent);
|
||||
ECS_TAG(ecs, Country);
|
||||
ECS_TAG(ecs, State);
|
||||
ECS_TAG(ecs, City);
|
||||
ECS_TAG(ecs, Person);
|
||||
|
||||
// Populate the store with locations
|
||||
ecs_entity_t earth = ecs_new_entity(ecs, "Earth");
|
||||
ecs_add(ecs, earth, Planet);
|
||||
|
||||
// Continents
|
||||
ecs_entity_t north_america = ecs_new_entity(ecs, "NorthAmerica");
|
||||
ecs_add(ecs, north_america, Continent);
|
||||
ecs_add_pair(ecs, north_america, LocatedIn, earth);
|
||||
|
||||
ecs_entity_t europe = ecs_new_entity(ecs, "Europe");
|
||||
ecs_add(ecs, europe, Continent);
|
||||
ecs_add_pair(ecs, europe, LocatedIn, earth);
|
||||
|
||||
// Countries
|
||||
ecs_entity_t united_states = ecs_new_entity(ecs, "UnitedStates");
|
||||
ecs_add(ecs, united_states, Country);
|
||||
ecs_add_pair(ecs, united_states, LocatedIn, north_america);
|
||||
|
||||
ecs_entity_t netherlands = ecs_new_entity(ecs, "Netherlands");
|
||||
ecs_add(ecs, netherlands, Country);
|
||||
ecs_add_pair(ecs, netherlands, LocatedIn, europe);
|
||||
|
||||
// States
|
||||
ecs_entity_t california = ecs_new_entity(ecs, "California");
|
||||
ecs_add(ecs, california, State);
|
||||
ecs_add_pair(ecs, california, LocatedIn, united_states);
|
||||
|
||||
ecs_entity_t washington = ecs_new_entity(ecs, "Washington");
|
||||
ecs_add(ecs, washington, State);
|
||||
ecs_add_pair(ecs, washington, LocatedIn, united_states);
|
||||
|
||||
ecs_entity_t noord_holland = ecs_new_entity(ecs, "NoordHolland");
|
||||
ecs_add(ecs, noord_holland, State);
|
||||
ecs_add_pair(ecs, noord_holland, LocatedIn, netherlands);
|
||||
|
||||
// Cities
|
||||
ecs_entity_t san_francisco = ecs_new_entity(ecs, "SanFrancisco");
|
||||
ecs_add(ecs, san_francisco, City);
|
||||
ecs_add_pair(ecs, san_francisco, LocatedIn, california);
|
||||
|
||||
ecs_entity_t seattle = ecs_new_entity(ecs, "Seattle");
|
||||
ecs_add(ecs, seattle, City);
|
||||
ecs_add_pair(ecs, seattle, LocatedIn, washington);
|
||||
|
||||
ecs_entity_t amsterdam = ecs_new_entity(ecs, "Amsterdam");
|
||||
ecs_add(ecs, amsterdam, City);
|
||||
ecs_add_pair(ecs, amsterdam, LocatedIn, noord_holland);
|
||||
|
||||
// Inhabitants
|
||||
ecs_entity_t bob = ecs_new_entity(ecs, "Bob");
|
||||
ecs_add(ecs, bob, Person);
|
||||
ecs_add_pair(ecs, bob, LocatedIn, san_francisco);
|
||||
|
||||
ecs_entity_t alice = ecs_new_entity(ecs, "Alice");
|
||||
ecs_add(ecs, alice, Person);
|
||||
ecs_add_pair(ecs, alice, LocatedIn, seattle);
|
||||
|
||||
ecs_entity_t job = ecs_new_entity(ecs, "Job");
|
||||
ecs_add(ecs, job, Person);
|
||||
ecs_add_pair(ecs, job, 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)
|
||||
ecs_rule_t *r = ecs_rule(ecs, {
|
||||
.terms = {
|
||||
{ .id = Person },
|
||||
{
|
||||
.first.id = LocatedIn,
|
||||
.second = { .name = "Location", .flags = EcsIsVariable },
|
||||
},
|
||||
{
|
||||
.first.id = Country,
|
||||
.src = { .name = "Location", .flags = EcsIsVariable },
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
// Lookup the index of the variable. This will let us quickly lookup its
|
||||
// value while we're iterating.
|
||||
int location_var = ecs_rule_find_var(r, "Location");
|
||||
|
||||
// Iterate the rule
|
||||
ecs_iter_t it = ecs_rule_iter(ecs, r);
|
||||
while (ecs_rule_next(&it)) {
|
||||
ecs_entity_t location = ecs_iter_get_var(&it, location_var);
|
||||
for (int i = 0; i < it.count; i ++) {
|
||||
printf("%s lives in %s\n",
|
||||
ecs_get_name(ecs, it.entities[i]),
|
||||
ecs_get_name(ecs, location));
|
||||
}
|
||||
}
|
||||
|
||||
ecs_rule_fini(r);
|
||||
|
||||
// Output:
|
||||
// Bob lives in UnitedStates
|
||||
// Alice lives in UnitedStates
|
||||
// Job lives in Netherlands
|
||||
|
||||
return ecs_fini(ecs);
|
||||
}
|
||||
Reference in New Issue
Block a user