1#include <variant.h>
2#include <stdio.h>
3
4
5
6
7
8typedef struct { double value; } Attack;
9typedef struct { double value; } Defense;
10typedef struct { double value; } FreightCapacity;
11typedef struct { double value; } ImpulseSpeed;
12
13typedef struct {
14 double x;
15 double y;
16} Position;
17
18int main(int argc, char *argv[]) {
20
26
27
30 ecs_set(ecs, SpaceShip, ImpulseSpeed, {50});
31 ecs_set(ecs, SpaceShip, Defense, {25});
32
33
37 ecs_set(ecs, Freighter, FreightCapacity, {100});
38 ecs_set(ecs, Freighter, Defense, {50});
39
40
44 ecs_set(ecs, MammothFreighter, FreightCapacity, {500});
45
46
50 ecs_set(ecs, Frigate, Attack, {100});
51 ecs_set(ecs, Frigate, Defense, {75});
52 ecs_set(ecs, Frigate, ImpulseSpeed, {125});
53
54
55
56
57
60
61
62 ecs_set(ecs, inst, Position, {10, 20});
63
64
65
66 ecs_set(ecs, inst, Defense, {100});
67
68
70 .terms = {
71 { .id =
ecs_id(Position) },
72 { .id =
ecs_id(ImpulseSpeed) },
74 { .id =
ecs_id(FreightCapacity) }
75 }
76 });
77
80 Position *p =
ecs_field(&it, Position, 0);
81 ImpulseSpeed *s =
ecs_field(&it, ImpulseSpeed, 1);
83 FreightCapacity *c =
ecs_field(&it, FreightCapacity, 3);
84
85 for (int i = 0; i < it.count; i ++) {
87 printf(" - position: %f, %f\n", p[i].x, p[i].y);
88 printf(" - impulse speed: %f\n", s[i].value);
89 printf(" - defense: %f\n", d[i].value);
90 printf(" - capacity: %f\n", c[i].value);
91 }
92 }
93
94
95
96
97
98
99
100
102}
void ecs_add_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Add a (component) ID to an entity.
FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
Component ID for EcsDocDescription.
ecs_id_t ecs_entity_t
An entity identifier.
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
#define ecs_add_pair(world, subject, first, second)
Add a pair to an entity.
#define ecs_entity(world,...)
Shorthand for creating an entity with ecs_entity_init().
#define ECS_COMPONENT(world, id)
Declare and define a component.
#define ecs_query(world,...)
Shorthand for creating a query with ecs_query_init().
#define ecs_set(world, entity, component,...)
Set a component value.
#define ecs_field(it, T, index)
Get field data for a component.
const char * ecs_get_name(const ecs_world_t *world, ecs_entity_t entity)
Get the name of an entity.
bool ecs_query_next(ecs_iter_t *it)
Progress a query iterator.
ecs_iter_t ecs_query_iter(const ecs_world_t *world, const ecs_query_t *query)
Create a query iterator.
int ecs_fini(ecs_world_t *world)
Delete a world.
ecs_world_t * ecs_init_w_args(int argc, char *argv[])
Create a new world with arguments.
Queries are lists of constraints (terms) that match entities.