Properly link flecs library
This commit is contained in:
5
engine/libs/flecs/examples/cpp/relationships/basics/.gitignore
vendored
Normal file
5
engine/libs/flecs/examples/cpp/relationships/basics/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
.bake_cache
|
||||
.DS_Store
|
||||
.vscode
|
||||
gcov
|
||||
bin
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"id": "basics",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"author": "Jane Doe",
|
||||
"description": "A simple hello world flecs application",
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <basics.h>
|
||||
#include <iostream>
|
||||
|
||||
// Type used for Eats relationship
|
||||
struct Eats { };
|
||||
|
||||
int main(int, char*[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
// Entity used for Grows relationship
|
||||
flecs::entity grows = ecs.entity("Grows");
|
||||
|
||||
// Relationship objects
|
||||
flecs::entity apples = ecs.entity("Apples");
|
||||
flecs::entity pears = ecs.entity("Pears");
|
||||
|
||||
// Create an entity with 3 relationships. Relationships are like regular components,
|
||||
// but combine two types/identifiers into an (relationship, object) pair.
|
||||
flecs::entity bob = ecs.entity("Bob")
|
||||
// Pairs can be constructed from a type and entity
|
||||
.add<Eats>(apples)
|
||||
.add<Eats>(pears)
|
||||
// Pairs can also be constructed from two entity ids
|
||||
.add(grows, pears);
|
||||
|
||||
// Has can be used with relationships as well
|
||||
std::cout << "Bob eats apples? " << bob.has<Eats>(apples) << "\n";
|
||||
|
||||
// Wildcards can be used to match relationships
|
||||
std::cout << "Bob grows food? " << bob.has(grows, flecs::Wildcard) << "\n";
|
||||
|
||||
// Print the type of the entity. Should output:
|
||||
// (Identifier,Name),(Eats,Apples),(Eats,Pears),(Grows,Pears)
|
||||
std::cout << "Bob's type: [" << bob.type().str() << "]\n";
|
||||
|
||||
// Relationships can be iterated for an entity. This iterates (Eats, *):
|
||||
bob.each<Eats>([](flecs::entity second) {
|
||||
std::cout << "Bob eats " << second.name() << "\n";
|
||||
});
|
||||
|
||||
// Iterate by explicitly providing the pair. This iterates (*, Pears):
|
||||
bob.each(flecs::Wildcard, pears, [](flecs::id id) {
|
||||
std::cout << "Bob " << id.first().name() << " pears\n";
|
||||
});
|
||||
|
||||
// Get first target of relationship
|
||||
std::cout << "Bob eats " << bob.target<Eats>().name() << "\n";
|
||||
|
||||
// Get second target of relationship
|
||||
std::cout << "Bob also eats " << bob.target<Eats>(1).name() << "\n";
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef ENUM_RELATIONS_H
|
||||
#define ENUM_RELATIONS_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "enum_relations/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 ENUM_RELATIONS_BAKE_CONFIG_H
|
||||
#define ENUM_RELATIONS_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "enum_relations",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
#include <enum_relations.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// When an enumeration constant is added to an entity, it is added as a relationship
|
||||
// pair where the relationship is the enum type, and the target is the constant. For
|
||||
// example, this statement:
|
||||
// e.add(Color::Red)
|
||||
//
|
||||
// adds this relationship:
|
||||
// (Color, Color::Red)
|
||||
//
|
||||
// Enums are registered as exclusive relationships, which means that adding an
|
||||
// enum constant will replace the previous constant for that enumeration:
|
||||
// e.add(Color::Green)
|
||||
//
|
||||
// will replace Color::Red with Color::Green
|
||||
|
||||
// Regular enumerations are supported
|
||||
enum Tile {
|
||||
Grass,
|
||||
Sand,
|
||||
Stone
|
||||
};
|
||||
|
||||
// Enum classes are supported
|
||||
enum class TileStatus {
|
||||
Free,
|
||||
Occupied,
|
||||
};
|
||||
|
||||
int main(int, char *[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
// Create an entity with (Tile, Stone) and (TileStatus, Free) relationships
|
||||
flecs::entity tile = ecs.entity()
|
||||
.add(Tile::Stone)
|
||||
.add(TileStatus::Free);
|
||||
|
||||
// (Tile, Tile.Stone), (TileStatus, TileStatus.Free)
|
||||
printf("%s\n", tile.type().str().c_str());
|
||||
|
||||
// Replace (TileStatus, Free) with (TileStatus, Occupied)
|
||||
tile.add(TileStatus::Occupied);
|
||||
|
||||
// (Tile, Tile.Stone), (TileStatus, TileStatus.Occupied)
|
||||
printf("%s\n", tile.type().str().c_str());
|
||||
|
||||
// Check if the entity has the Tile relationship and the Tile::Stone pair
|
||||
printf("%s\n", tile.has<Tile>() ? "true" : "false"); // true
|
||||
printf("%s\n", tile.has(Tile::Stone) ? "true" : "false"); // true
|
||||
|
||||
// Get the current value of the enum
|
||||
const Tile* v = tile.get<Tile>();
|
||||
printf("%s\n", (v[0] == Tile::Stone) ? "true" : "false"); // true
|
||||
|
||||
// Create a few more entities that we can query
|
||||
ecs.entity().add(Tile::Grass).add(TileStatus::Free);
|
||||
ecs.entity().add(Tile::Sand).add(TileStatus::Occupied);
|
||||
|
||||
// Iterate all entities with a Tile relationship
|
||||
ecs.filter_builder()
|
||||
.with<Tile>(flecs::Wildcard)
|
||||
.build()
|
||||
.each([&](flecs::iter& it, size_t) {
|
||||
flecs::entity tile_constant = it.pair(1).second();
|
||||
printf("%s\n", tile_constant.path().c_str());
|
||||
});
|
||||
|
||||
// Outputs:
|
||||
// ::Tile::Stone
|
||||
// ::Tile::Grass
|
||||
// ::Tile::Sand
|
||||
|
||||
// Iterate only occupied tiles
|
||||
ecs.filter_builder()
|
||||
.with<Tile>(flecs::Wildcard)
|
||||
.with(TileStatus::Occupied)
|
||||
.build()
|
||||
.each([&](flecs::iter& it, size_t) {
|
||||
flecs::entity tile_constant = it.pair(1).second();
|
||||
printf("%s\n", tile_constant.path().c_str());
|
||||
});
|
||||
|
||||
// Outputs:
|
||||
// ::Tile::Stone
|
||||
// ::Tile::Sand
|
||||
|
||||
// Remove any instance of the TileStatus relationship
|
||||
tile.remove<TileStatus>();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef EXCLUSIVE_RELATIONS_H
|
||||
#define EXCLUSIVE_RELATIONS_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "exclusive_relations/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 EXCLUSIVE_RELATIONS_BAKE_CONFIG_H
|
||||
#define EXCLUSIVE_RELATIONS_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "exclusive_relations",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false,
|
||||
"language": "c++"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#include <exclusive_relations.h>
|
||||
#include <iostream>
|
||||
|
||||
// Type for Platoon relationship
|
||||
struct Platoon { };
|
||||
|
||||
int main(int, char*[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
// Register Platoon as exclusive relationship. This ensures that an entity
|
||||
// can only belong to a single Platoon.
|
||||
ecs.component<Platoon>()
|
||||
.add(flecs::Exclusive);
|
||||
|
||||
// Create two platoons
|
||||
flecs::entity platoon_1 = ecs.entity();
|
||||
flecs::entity platoon_2 = ecs.entity();
|
||||
|
||||
// Create a unit
|
||||
flecs::entity unit = ecs.entity();
|
||||
|
||||
// Add unit to platoon 1
|
||||
unit.add<Platoon>(platoon_1);
|
||||
|
||||
// Log platoon of unit
|
||||
std::cout << "Unit in platoon 1: " <<
|
||||
(unit.has<Platoon>(platoon_1) ? "true" : "false") << "\n";
|
||||
std::cout << "Unit in platoon 2: " <<
|
||||
(unit.has<Platoon>(platoon_2) ? "true" : "false") << "\n\n";
|
||||
|
||||
// Add unit to platoon 2. Because Platoon is an exclusive relationship, this
|
||||
// both removes (Platoon, platoon_1) and adds (Platoon, platoon_2) in a
|
||||
// single operation.
|
||||
unit.add<Platoon>(platoon_2);
|
||||
|
||||
std::cout << "Unit in platoon 1: " <<
|
||||
(unit.has<Platoon>(platoon_1) ? "true" : "false") << "\n";
|
||||
std::cout << "Unit in platoon 2: " <<
|
||||
(unit.has<Platoon>(platoon_2) ? "true" : "false") << "\n";
|
||||
|
||||
// Output:
|
||||
// Unit in platoon 1: true
|
||||
// Unit in platoon 2: false
|
||||
//
|
||||
// Unit in platoon 1: false
|
||||
// Unit in platoon 2: true
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef RELATION_COMPONENT_H
|
||||
#define RELATION_COMPONENT_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "relation_component/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 RELATION_COMPONENT_BAKE_CONFIG_H
|
||||
#define RELATION_COMPONENT_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "relation_component",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
#include <relation_component.h>
|
||||
#include <iostream>
|
||||
|
||||
// This example shows how relationships can be combined with components to attach
|
||||
// data to a relationship.
|
||||
|
||||
// Some demo components:
|
||||
|
||||
struct Requires {
|
||||
double amount;
|
||||
};
|
||||
|
||||
struct Gigawatts { };
|
||||
|
||||
struct Expires {
|
||||
double timeout;
|
||||
};
|
||||
|
||||
struct Position {
|
||||
double x;
|
||||
double y;
|
||||
};
|
||||
|
||||
struct MustHave { };
|
||||
|
||||
int main(int, char*[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
// When one element of a pair is a component and the other element is a tag,
|
||||
// the pair assumes the type of the component.
|
||||
flecs::entity e1 = ecs.entity().set<Requires, Gigawatts>({1.21});
|
||||
const Requires *r = e1.get<Requires, Gigawatts>();
|
||||
std::cout << "requires: " << r->amount << std::endl;
|
||||
|
||||
// The component can be either the first or second part of a pair:
|
||||
flecs::entity e2 = ecs.entity().set<Gigawatts, Requires>({1.21});
|
||||
r = e2.get<Gigawatts, Requires>();
|
||||
std::cout << "requires: " << r->amount << std::endl;
|
||||
|
||||
// Note that <Requires, Gigawatts> and <Gigawatts, Requires> are two
|
||||
// different pairs, and can be added to an entity at the same time.
|
||||
|
||||
// If both parts of a pair are components, the pair assumes the type of
|
||||
// the first element:
|
||||
flecs::entity e3 = ecs.entity().set<Expires, Position>({0.5});
|
||||
const Expires *e = e3.get<Expires, Position>();
|
||||
std::cout << "expires: " << e->timeout << std::endl;
|
||||
|
||||
// You can prevent a pair from assuming the type of a component by adding
|
||||
// the Tag property to a relationship:
|
||||
ecs.component<MustHave>().add(flecs::Tag);
|
||||
|
||||
// Even though Position is a component, <MustHave, Position> contains no
|
||||
// data because MustHave has the Tag property.
|
||||
ecs.entity().add<MustHave, Position>();
|
||||
|
||||
// The id::type_id method can be used to find the component type for a pair:
|
||||
std::cout << ecs.pair<Requires, Gigawatts>().type_id().path() << "\n";
|
||||
std::cout << ecs.pair<Gigawatts, Requires>().type_id().path() << "\n";
|
||||
std::cout << ecs.pair<Expires, Position>().type_id().path() << "\n";
|
||||
std::cout << ecs.pair<MustHave, Position>().type_id().path() << "\n";
|
||||
|
||||
// When querying for a relationship component, add the pair type as template
|
||||
// argument to the builder:
|
||||
flecs::query<Requires> q = ecs.query_builder<Requires>()
|
||||
.term_at(1).second<Gigawatts>() // set second part of pair for first term
|
||||
.build();
|
||||
|
||||
// When iterating, always use the pair type:
|
||||
q.each([](Requires& rq) {
|
||||
std::cout << "requires " << rq.amount << " gigawatts\n";
|
||||
});
|
||||
|
||||
// Output:
|
||||
// requires: 1.21
|
||||
// requires: 1.21
|
||||
// expires: 0.5
|
||||
// ::Requires
|
||||
// ::Requires
|
||||
// ::Expires
|
||||
// 0
|
||||
// requires 1.21 gigawatts
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef SYMMETRIC_RELATIONS_H
|
||||
#define SYMMETRIC_RELATIONS_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "symmetric_relations/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 SYMMETRIC_RELATIONS_BAKE_CONFIG_H
|
||||
#define SYMMETRIC_RELATIONS_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "symmetric_relations",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false,
|
||||
"language": "c++"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <symmetric_relations.h>
|
||||
#include <iostream>
|
||||
|
||||
// Type for TradesWith relationship
|
||||
struct TradesWith { };
|
||||
|
||||
int main(int, char*[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
// Register TradesWith as symmetric relationship. Symmetric relationships
|
||||
// go both ways, adding (R, B) to A will also add (R, A) to B.
|
||||
ecs.component<TradesWith>()
|
||||
.add(flecs::Symmetric);
|
||||
|
||||
// Create two players
|
||||
flecs::entity player_1 = ecs.entity();
|
||||
flecs::entity player_2 = ecs.entity();
|
||||
|
||||
// Add (TradesWith, player_2) to player_1. This also adds
|
||||
// (TradesWith, player_1) to player_2.
|
||||
player_1.add<TradesWith>(player_2);
|
||||
|
||||
// Log platoon of unit
|
||||
std::cout << "Player 1 trades with Player 2: " <<
|
||||
(player_1.has<TradesWith>(player_2) ? "true" : "false") << "\n";
|
||||
std::cout << "Player 2 trades with Player 1: " <<
|
||||
(player_2.has<TradesWith>(player_1) ? "true" : "false") << "\n";
|
||||
|
||||
// Output:
|
||||
// Player 1 trades with Player 2: true
|
||||
// Player 2 trades with Player 1: true
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef UNION_H
|
||||
#define UNION_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "union/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 UNION_BAKE_CONFIG_H
|
||||
#define UNION_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "union",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
#include <union.h>
|
||||
#include <iostream>
|
||||
|
||||
// This example shows how to use union relationships. Union relationships behave
|
||||
// much like exclusive relationships in that entities can have only one instance
|
||||
// and that adding an instance removes the previous instance.
|
||||
//
|
||||
// What makes union relationships stand out is that changing the relationship
|
||||
// target doesn't change the archetype of an entity. This allows for quick
|
||||
// switching of tags, which can be useful when encoding state machines in ECS.
|
||||
//
|
||||
// There is a tradeoff, and that is that because a single archetype can contain
|
||||
// entities with multiple targets, queries need to do a bit of extra work to
|
||||
// only return the requested target.
|
||||
//
|
||||
// This code uses enumeration relationships. See the enum_relations example for
|
||||
// more details.
|
||||
|
||||
enum Movement {
|
||||
Walking,
|
||||
Running
|
||||
};
|
||||
|
||||
enum Direction {
|
||||
Front,
|
||||
Back,
|
||||
Left,
|
||||
Right
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
flecs::world ecs(argc, argv);
|
||||
|
||||
ecs.component<Movement>().add(flecs::Union);
|
||||
ecs.component<Direction>().add(flecs::Union);
|
||||
|
||||
// Create a query that subscribes for all entities that have a Direction
|
||||
// and that are walking
|
||||
flecs::query<> q = ecs.query_builder()
|
||||
.with(Walking)
|
||||
.with<Direction>(flecs::Wildcard)
|
||||
.build();
|
||||
|
||||
// Create a few entities with various state combinations
|
||||
ecs.entity("e1")
|
||||
.add(Walking)
|
||||
.add(Front);
|
||||
|
||||
ecs.entity("e2")
|
||||
.add(Running)
|
||||
.add(Left);
|
||||
|
||||
flecs::entity e3 = ecs.entity("e3")
|
||||
.add(Running)
|
||||
.add(Back);
|
||||
|
||||
// Add Walking to e3. This will remove the Running case
|
||||
e3.add(Walking);
|
||||
|
||||
// Iterate the query
|
||||
q.iter([&](const flecs::iter& it) {
|
||||
// Get the column with direction states. This is stored as an array
|
||||
// with identifiers to the individual states
|
||||
auto movement = it.field<flecs::entity_t>(1);
|
||||
auto direction = it.field<flecs::entity_t>(2);
|
||||
|
||||
for (auto i : it) {
|
||||
// Movement will always be Walking, Direction can be any state
|
||||
std::cout << it.entity(i).name()
|
||||
<< ": Movement: "
|
||||
<< it.world().get_alive(movement[i]).name()
|
||||
<< ", Direction: "
|
||||
<< it.world().get_alive(direction[i]).name()
|
||||
<< std::endl;
|
||||
}
|
||||
});
|
||||
|
||||
// Output:
|
||||
// e3: Movement: Walking, Direction: Back
|
||||
// e1: Movement: Walking, Direction: Front
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user