flecs_ecs 0.2.2

Rust API for the C/CPP flecs ECS library <https://github.com/SanderMertens/flecs>
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Tests from relationships.md

#![allow(unused_imports, unused_variables, dead_code, non_snake_case, path_statements, unreachable_code, unused_mut,clippy::print_stdout)]
#![cfg_attr(rustfmt, rustfmt_skip)]

use crate::common_test::*;

#[test]
fn relationships_introduction_01() {
    let world = World::new();
    let likes = world.entity();
    let bob = world.entity();
    let alice = world.entity();

    // bob likes alice
    bob.add((likes, alice));

    // bob likes alice no more
    bob.remove((likes, alice));
}

#[test]
fn relationships_introduction_02() {
    let world = World::new();

    let bob = world.entity();

    let eats = world.entity();
    let apples = world.entity();
    let pears = world.entity();

    bob.add((eats, apples));
    bob.add((eats, pears));

    bob.has((eats, apples)); // true
    bob.has((eats, pears)); // true
}

#[test]
fn relationships_introduction_03() {
    let world = World::new();
    let eats = world.entity();
    let apples = world.entity();
    world.component_named::<Eats>("Eats");
    world.component_named::<Apples>("Apples");
    // Find all entities that eat apples
    let q = world.query::<()>().expr("(Eats, Apples)").build();

    // Find all entities that eat anything
    let q = world.query::<()>().expr("(Eats, *)").build();

    // With the query builder API:
    let q = world.query::<()>().with((eats, apples)).build();

    // Or when using pair types, when both relationship & target are compile time types, they can be represented as a tuple:

    let q = world.query::<()>().with((Eats::id(), Apples::id())).build();
}

#[test]
fn relationships_relationship_queries_test_if_entity_has_a_relationship_pair_04() {
    let world = World::new();
    let bob = world.entity();
    let eats = world.entity();
    let apples = world.entity();
    bob.has((eats, apples));
}

#[test]
fn relationships_relationship_queries_test_if_entity_has_a_relationship_wildcard_05() {
    let world = World::new();
    let bob = world.entity();
    let eats = world.entity();
    bob.has((eats, flecs::Wildcard));
}

#[test]
fn relationships_relationship_queries_get_parent_for_entity_06() {
    let world = World::new();
    let bob = world.entity();
    let parent = bob.parent();
}

#[test]
fn relationships_relationship_queries_get_parent_for_entity_07() {
    let world = World::new();
    let bob = world.entity();
    let parent = bob.parent();
}

#[test]
fn relationships_relationship_queries_find_first_target_of_a_relationship_for_entity_08() {
    let world = World::new();
    let bob = world.entity();
    let eats = world.entity();
    let food = bob.target(eats, 0); // first target
}

#[test]
fn relationships_relationship_queries_find_all_targets_of_a_relationship_for_entity_09() {
    let world = World::new();
    let bob = world.entity();
    let eats = world.entity();
    let mut index = 0;
    while bob.target(eats, index).is_some() {
        index += 1;
    }
}

#[test]
fn relationships_relationship_queries_find_target_of_a_relationship_with_component_10() {
    let world = World::new();
    let bob = world.entity();
    let parent = bob.target_for(flecs::ChildOf, Position::id());
}

#[test]
fn relationships_relationship_queries_iterate_all_pairs_for_entity_11() {
    let world = World::new();
    let bob = world.entity();
    bob.each_component(|id| {
        if id.is_pair() {
            let first = id.first_id();
            let second = id.second_id();
        }
    });
}

#[test]
fn relationships_relationship_queries_find_all_entities_with_a_pair_12() {
    let world = World::new();
    let eats = world.entity();
    let apples = world.entity();
    world
        .query::<()>()
        .with((eats, apples))
        .build()
        .each_entity(|e, _| {
            // Iterate as usual
        });
}

#[test]
fn relationships_relationship_queries_find_all_entities_with_a_pair_wildcard_13() {
    let world = World::new();
    let eats = world.entity();
    world
        .query::<()>()
        .with((eats, flecs::Wildcard))
        .build()
        .each_iter(|it, i, _| {
            let food = it.pair(0).second_id(); // Apples, ...
            let e = it.entity(i);
            // Iterate as usual
        });
}

#[test]
fn relationships_relationship_queries_iterate_all_children_for_a_parent_14() {
    let world = World::new();
    let parent = world.entity();
    parent.each_child(|child| {
        // ...
    });
}

#[test]
fn relationships_relationship_components_15() {
    let world = World::new();
    // Empty types (types without members) are letmatically interpreted as tags

    #[derive(Component)]
    struct Begin;

    #[derive(Component)]
    struct End;

    #[derive(Component)]
    pub struct Eats {
        amount: u32,
    }

    // Tags
    let likes = world.entity();
    let apples = world.entity();

    let e = world.entity();

    // Both likes and Apples are tags, so (likes, Apples) is a tag
    e.add((likes, apples));

    // Eats is a type and Apples is a tag, so (Eats, Apples) has type Eats
    e.set_pair::<Eats, Apples>(Eats { amount: 1 });

    // Begin is a tags and Position is a type, so (Begin, Position) has type Position
    e.set_pair::<Begin, Position>(Position { x: 10.0, y: 20.0 });
    e.set_pair::<End, Position>(Position { x: 100.0, y: 20.0 }); // Same for End

    // ChildOf has the Tag property, so even though Position is a type, the pair
    // does not assume the Position type
    e.add((flecs::ChildOf, world.component_id::<Position>()));
    e.add((flecs::ChildOf, Position::id()));
}

#[test]
fn relationships_relationship_components_using_relationships_to_add_components_multiple_times_16() {
    let world = World::new();
    let e = world.entity();

    let first = world.entity();
    let second = world.entity();
    let third = world.entity();

    // Add component position 3 times, for 3 different objects
    e.set_first::<Position>(Position { x: 1.0, y: 2.0 }, first);
    e.set_first::<Position>(Position { x: 3.0, y: 4.0 }, second);
    e.set_first::<Position>(Position { x: 5.0, y: 6.0 }, third);
}

#[test]
fn relationships_relationship_wildcards_17() {
    let world = World::new();
    let likes = world.entity();
    let q = world
        .query::<()>()
        .with((likes, flecs::Wildcard))
        .build();

    q.each_iter(|it, i, _| {
        println!(
            "entity {} has relationship {} {}",
            it.entity(i),
            it.pair(0).first_id().name(),
            it.pair(0).second_id().name()
        );
    });
}

#[test]
fn relationships_relationship_wildcards_18() {
    let world = World::new();
    world.entity_named("likes");
    let q = world.query::<()>().expr("(likes, *)").build();
}

#[test]
fn relationships_inspecting_relationships_19() {
    let world = World::new();
    // bob eats apples and pears
    let bob = world.entity();

    let eats = world.entity();
    let apples = world.entity();
    let pears = world.entity();

    bob.add((eats, apples));
    bob.add((eats, pears));

    // Find all (Eats, *) relationships in bob's type
    bob.each_pair(eats, flecs::Wildcard, |id| {
        println!("bob eats {}", id.second_id().name());
    });

    // For target wildcard pairs, each_target_id() can be used:
    bob.each_target(eats, |entity| {
        println!("bob eats {}", entity.name());
    });
}

#[test]
fn relationships_builtin_relationships_the_isa_relationship_20() {
    let world = World::new();
    let apple = world.entity();
    let fruit = world.entity();

    apple.add((flecs::IsA::ID, fruit));
}

#[test]
fn relationships_builtin_relationships_the_isa_relationship_21() {
    let world = World::new();
    let apple = world.entity();
    let fruit = world.entity();
    apple.is_a(fruit);
}

#[test]
fn relationships_builtin_relationships_the_isa_relationship_22() {
    let world = World::new();
    let apple = world.entity();
    let granny_smith = world.entity();
    granny_smith.add((flecs::IsA::ID, apple));
}

#[test]
fn relationships_builtin_relationships_the_isa_relationship_component_sharing_23() {
    let world = World::new();

    world.component::<MaxSpeed>().add_trait::<(flecs::OnInstantiate, flecs::Inherit)>();

    let spaceship = world
        .entity()
        .set(MaxSpeed { value: 100 })
        .set(Defense { value: 50 });

    let frigate = world
        .entity()
        .is_a(spaceship) // shorthand for .add(flecs::IsA, Spaceship)
        .set(Defense { value: 75 });
}

#[test]
fn relationships_builtin_relationships_the_isa_relationship_component_sharing_24() {
    let world = World::new();
    world.component::<MaxSpeed>().add_trait::<(flecs::OnInstantiate, flecs::Inherit)>();
    world.component::<Defense>().add_trait::<(flecs::OnInstantiate, flecs::Inherit)>();
    let spaceship = world.entity().set(MaxSpeed { value: 100 }).set(Defense { value: 50 });
    let frigate = world.entity().is_a(spaceship).set(Defense { value: 75 });
    // Obtain the inherited component from Spaceship
    let is_100 = frigate.get::<&MaxSpeed>(|v| {
        v.value == 100 // True
    });
}

#[test]
fn relationships_builtin_relationships_the_isa_relationship_component_sharing_25() {
    let world = World::new();
    world.component::<MaxSpeed>().add_trait::<(flecs::OnInstantiate, flecs::Inherit)>();
    world.component::<Defense>().add_trait::<(flecs::OnInstantiate, flecs::Inherit)>();
    let spaceship = world.entity().set(MaxSpeed { value: 100 }).set(Defense { value: 50 });
    let frigate = world.entity().is_a(spaceship).set(Defense { value: 75 });
    // Obtain the overridden component from Frigate
    let is_75 = frigate.get::<&mut Defense>(|v| {
        v.value == 75 // True
    });
}

#[test]
fn relationships_builtin_relationships_the_isa_relationship_component_sharing_26() {
    let world = World::new();
    world.component::<MaxSpeed>().add_trait::<(flecs::OnInstantiate, flecs::Inherit)>();
    world.component::<Defense>().add_trait::<(flecs::OnInstantiate, flecs::Inherit)>();
    let spaceship = world.entity().set(MaxSpeed { value: 100 }).set(Defense { value: 50 });
    let frigate = world.entity().is_a(spaceship).set(Defense { value: 75 });
    let fast_frigate = world.entity().is_a(frigate).set(MaxSpeed { value: 200 });

    // Obtain the overridden component from FastFrigate
    let is_200 = fast_frigate.get::<&mut MaxSpeed>(|v| {
        v.value == 200 // True
    });

    // Obtain the inherited component from Frigate
    let is_75 = fast_frigate.get::<&Defense>(|v| {
        v.value == 75 // True
    });
}

#[test]
fn relationships_builtin_relationships_the_childof_relationship_27() {
    let world = World::new();
    let spaceship = world.entity();
    let cockpit = world.entity();
    cockpit.add((flecs::ChildOf, spaceship));
}

#[test]
fn relationships_builtin_relationships_the_childof_relationship_28() {
    let world = World::new();
    let cockpit = world.entity();
    let spaceship = world.entity();
    cockpit.child_of(spaceship);
}

#[test]
fn relationships_builtin_relationships_the_childof_relationship_namespacing_29() {
    let world = World::new();
    let parent = world.entity_named("Parent");
    let child = world.entity_named("Child").child_of(parent);

    assert!(child == world.lookup("Parent::Child")); // true
    assert!(child == parent.lookup("Child")); // true
}

#[test]
fn relationships_builtin_relationships_the_childof_relationship_scoping_30() {
    let world = World::new();
    let parent = world.entity();

    let prev = world.set_scope(parent);

    let child_a = world.entity();
    let child_b = world.entity();

    // Restore the previous scope
    world.set_scope(prev);

    child_a.has((flecs::ChildOf, parent)); // true
    child_b.has((flecs::ChildOf, parent)); // true
}

#[test]
fn relationships_builtin_relationships_the_childof_relationship_scoping_31() {
    let world = World::new();
    let parent = world.entity();
    parent.run_in_scope(|| {
        let child_a = world.entity();
        let child_b = world.entity();
        child_a.has((flecs::ChildOf, parent)); // true
        child_b.has((flecs::ChildOf, parent)); // true
    });
}