1#include <system_ctx.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <math.h>
5
6
7
8
9
10
11typedef struct {
12 double x, y;
13} Position;
14
15typedef struct {
16 double value;
17} Radius;
18
19double sqr(double value) {
20 return value * value;
21}
22
23double distance_sqr(const Position *p1, const Position *p2) {
24 return sqr(p2->x - p1->x) + sqr(p2->y - p1->y);
25}
26
29 const Position *p1 =
ecs_field(it, Position, 0);
30 const Radius *r1 =
ecs_field(it, Radius, 1);
31
32 for (
int i = 0; i < it->
count; i ++) {
34
35
38 const Position *p2 =
ecs_field(&qit, Position, 0);
39 const Radius *r2 =
ecs_field(&qit, Radius, 1);
40 for (
int j = 0; j < qit.
count; j ++) {
42 if (e1 == e2) {
43
44 continue;
45 }
46
47 if (e2 > e1) {
48
49
50 continue;
51 }
52
53
54 double d_sqr = distance_sqr(&p1[i], &p2[j]);
55 double r_sqr = sqr(r1[i].value + r2[j].value);
56 if (r_sqr > d_sqr) {
57 printf("%u and %u collided!\n", (uint32_t)e1, (uint32_t)e2);
58 }
59 }
60 }
61 }
62}
63
64int main(int argc, char *argv[]) {
66
69
70
71
73 .terms = {
76 }
77 });
78
79
81 .query.terms = {
84 },
85 .callback = Collide,
86 .ctx = q_position
87 });
88
89
90 for (int i = 0; i < 10; i ++) {
92 ecs_set(ecs, e, Position, { .x = rand() % 100, .y = rand() % 100 });
93 ecs_set(ecs, e, Radius, { rand() % 10 + 1 });
94 }
95
96
97 ecs_run(ecs, collide, 0.0f, NULL);
98
100
101
102
103
104
105
106}
FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
Component ID for EcsDocDescription.
#define ecs_system(world,...)
Shorthand for creating a system with ecs_system_init().
FLECS_API ecs_entity_t ecs_run(ecs_world_t *world, ecs_entity_t system, ecs_ftime_t delta_time, void *param)
Run a specific system manually.
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.
ecs_entity_t ecs_new(ecs_world_t *world)
Create new entity ID.
#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.
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.
@ EcsIn
Term is only read.
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.
void * ctx
System context.
int32_t count
Number of entities to iterate.
const ecs_entity_t * entities
Entity identifiers.
Queries are lists of constraints (terms) that match entities.