flecs_ecs 0.2.0

Rust API for the C/CPP flecs ECS library <https://github.com/SanderMertens/flecs>
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Tests from componenttraits.md

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

use crate::common_test::*;

#[test]
fn component_traits_cantoggle_trait_01() {
    let world = World::new();
    world
    .component::<Position>()
    .add_trait::<flecs::CanToggle>();

    let e = world.entity().set(Position { x: 10.0, y: 20.0 });

    e.disable(Position::id()); // Disable component
    assert!(!e.is_enabled(Position::id()));

    e.enable(Position::id()); // Enable component
    assert!(e.is_enabled(Position::id()));
}

#[test]
fn component_traits_cleanup_traits_02() {
    let world = World::new();
    let parent = world.entity();
    let e = world.entity();
    #[derive(Component)]
    struct MyComponent {
        e: Entity, // Not covered by cleanup traits
    }

    e.child_of(parent); // Covered by cleanup traits
}

#[test]
fn component_traits_cleanup_traits_03() {
    let world = World::new();
    let archer = world.entity();
    world.remove_all(archer); //entity
    world.remove_all(Archer); //type

}

#[test]
fn component_traits_cleanup_traits_04() {
    let world = World::new();
    let archer = world.entity();
    world.remove_all(archer);
    world.remove_all((archer, flecs::Wildcard));
    world.remove_all((flecs::Wildcard, archer));
}

#[test]
fn component_traits_cleanup_traits_examples_ondelete_remove_05() {
    let world = World::new();
    // Remove Archer from entities when Archer is deleted
    world
        .component::<Archer>()
        .add_trait::<(flecs::OnDelete, flecs::Remove)>();

    let e = world.entity().add(Archer::id());
}

#[test]
fn component_traits_cleanup_traits_examples_ondelete_delete_06() {
    let world = World::new();
    // Remove Archer from entities when Archer is deleted
    world
        .component::<Archer>()
        .add_trait::<(flecs::OnDelete, flecs::Remove)>();

    let e = world.entity().add(Archer::id());

    // This will remove Archer from e
    world.component::<Archer>().destruct();
}

#[test]
fn component_traits_cleanup_traits_examples_ondeletetarget_delete_07() {
    let world = World::new();
    // Delete children when deleting parent
    world
        .component::<flecs::ChildOf>()
        .add_trait::<(flecs::OnDeleteTarget, flecs::Delete)>();

    let p = world.entity();
    let e = world.entity().add((flecs::ChildOf, p));

    // This will delete both p and e
    p.destruct();
}

#[test]
fn component_traits_cleanup_traits_cleanup_order_08() {
    let world = World::new();
    world
        .observer::<flecs::OnRemove, ()>()
        .with(Node)
        .each_entity(|e, _| {
    // This observer will be invoked when a Node is removed
    });

    let p = world.entity().add(Node::id());
    let c = world.entity().add(Node::id()).child_of(p);
}

#[test]
fn component_traits_dontfragment_trait_09() {
    let world = World::new();
    world.component::<Position>().add_trait::<flecs::DontFragment>();
}

#[test]
fn component_traits_exclusive_trait_10() {
    let world = World::new();
    let e = world.entity();
    let parent_a = world.entity();
    let parent_b = world.entity();
    e.child_of(parent_a);
    e.child_of(parent_b); // replaces (ChildOf, parent_a)
}

#[test]
fn component_traits_exclusive_trait_11() {
    let world = World::new();
    let married_to = world.entity().add_trait::<flecs::Exclusive>();
}

#[test]
fn component_traits_final_trait_12() {
    let world = World::new();
    let e = world.entity().add_trait::<flecs::Final>();

    /*
    let i = world.entity().is_a(e); // not allowed
    */
}

#[test]
fn component_traits_inheritable_trait_13() {
    let world = World::new();
    world.component::<Unit>().add_trait::<flecs::Inheritable>();

    let q = world.query::<()>()
      .with(Unit::id())
      .build();

    world.component::<Warrior>().is_a(Unit::id());

    q.each_entity(|e, _|  {
        // ...
    });
}

#[test]
fn component_traits_oneof_trait_14() {
    let world = World::new();
    // Enforce that target of relationship is child of Food
    let food = world.entity().add_trait::<flecs::OneOf>();
    let apples = world.entity().child_of(food);
    let fork = world.entity();

    // This is ok, Apples is a child of Food
    let a = world.entity().add((food, apples));

    /*
    // This is not ok, Fork is not a child of Food
    let b = world.entity().add((food, fork));
    */
}

#[test]
fn component_traits_oneof_trait_15() {
    let world = World::new();
    // Enforce that target of relationship is child of Food
    let food = world.entity();
    let eats = world.entity().add((flecs::OneOf::id(), food));
    let apples = world.entity().child_of(food);
    let fork = world.entity();

    // This is ok, Apples is a child of Food
    let a = world.entity().add((eats, apples));

    /*
    // This is not ok, Fork is not a child of Food
    let b = world.entity().add((eats, fork));
    */
}

#[test]
fn component_traits_oninstantiate_trait_override_16() {
    let world = World::new();
    // Register component with trait. Optional, since this is the default behavior.
    world
    .component::<Mass>()
    .add_trait::<(flecs::OnInstantiate, flecs::Override)>();

    let base = world.entity().set(Mass { value: 100.0 });
    let inst = world.entity().is_a(base); // Mass is copied to inst

    assert!(inst.owns(Mass::id()));
    assert!(base.cloned::<&Mass>() == inst.cloned::<&Mass>());
}

#[test]
fn component_traits_oninstantiate_trait_inherit_17() {
    let world = World::new();
    // Register component with trait
    world
    .component::<Mass>()
    .add_trait::<(flecs::OnInstantiate, flecs::Inherit)>();

    let base = world.entity().set(Mass { value: 100.0 });
    let inst = world.entity().is_a(base);

    assert!(inst.has(Mass::id()));
    assert!(!inst.owns(Mass::id()));
    assert!(base.cloned::<&Mass>() == inst.cloned::<&Mass>());
}

#[test]
fn component_traits_oninstantiate_trait_dontinherit_18() {
    let world = World::new();
    // Register component with trait
    world
    .component::<Mass>()
    .add_trait::<(flecs::OnInstantiate, flecs::DontInherit)>();

    let base = world.entity().set(Mass { value: 100.0 });
    let inst = world.entity().is_a(base);

    assert!(!inst.has(Mass::id()));
    assert!(!inst.owns(Mass::id()));
    assert!(inst.try_get::<&Mass>(|mass| {}).is_none());
}

#[test]
fn component_traits_orderedchildren_trait_19() {
    let world = World::new();
    let parent = world.entity().add_trait::<flecs::OrderedChildren>();

    let child_1 = world.entity().child_of(parent);
    let child_2 = world.entity().child_of(parent);
    let child_3 = world.entity().child_of(parent);

    // Adding/removing components usually changes the order in which children are
    // iterated, but with the OrderedChildren trait order is preserved.
    child_2.set(Position { x: 10.0, y: 20.0 });

    parent.each_child(|child| {
        // 1st result: child_1
        // 2nd result: child_2
        // 3rd result: child_3
    });
}

#[test]
fn component_traits_pairistag_trait_20() {
    let world = World::new();
    #[derive(Component)]
    struct Serializable; // Tag, contains no data

    let e = world
        .entity()
        .set(Position { x: 10.0, y: 20.9 })
        .add((Serializable::id(), Position::id())); // Because Serializable is a tag, the pair
    // has a value of type Position

    // Gets value from Position component
    e.get::<&Position>(|pos| {
        println!("Position: ({}, {})", pos.x, pos.y);
    });
    // Gets (unintended) value from (Serializable, Position) pair
    e.get::<&(Serializable, Position)>(|pos| {
        println!("Serializable Position: ({}, {})", pos.x, pos.y);
    });
}

#[test]
fn component_traits_pairistag_trait_21() {
    let world = World::new();
    // This is currently not supported in Rust due to safety concerns.
}

#[test]
fn component_traits_relationship_trait_22() {
    let world = World::new();
    #[derive(Component)]
    struct Likes;

    #[derive(Component)]
    struct Apples;

    world
        .component::<Likes>()
        .add_trait::<flecs::Relationship>();

    let e = world
        .entity()
        /*
        .add(Likes::id()) // Panic, 'Likes' is not used as relationship
        .add((Apples::id(), Likes::id())) // Panic, 'Likes' is not used as relationship, but as target
        */
        .add((Likes::id(), Apples::id())); // OK
}

#[test]
fn component_traits_relationship_trait_23() {
    let world = World::new();
    #[derive(Component)]
    struct Likes;

    #[derive(Component)]
    struct Loves;

    world
        .component::<Likes>()
        .add_trait::<flecs::Relationship>();

    // Even though Likes is marked as relationship and used as target here, this
    // won't panic as With is marked as trait.
    world
        .component::<Loves>()
        .add_trait::<(flecs::With, Likes)>();
}

#[test]
fn component_traits_singleton_trait_24() {
    let world = World::new();
    world.component::<TimeOfDay>().add_trait::<flecs::Singleton>();

    world.set(TimeOfDay(0.0));
}

#[test]
fn component_traits_singleton_trait_25() {
    let world = World::new();
    // Automatically matches TimeOfDay as singleton
    let q = world.new_query::<(&Position, &Velocity, &TimeOfDay)>();

    // Is the same as
    let q = world.query::<(&Position, &Velocity, &TimeOfDay)>()
      .term_at(2).set_src(TimeOfDay::id())
      .build();
}

#[test]
fn component_traits_sparse_trait_26() {
    let world = World::new();
    world.component::<Position>().add_trait::<flecs::Sparse>();
}

#[test]
fn component_traits_symmetric_trait_27() {
    let world = World::new();
    let married_to = world.entity().add_trait::<flecs::Symmetric>();
    let bob = world.entity();
    let alice = world.entity();
    bob.add((married_to, alice)); // Also adds (MarriedTo, Bob) to Alice
}

#[test]
fn component_traits_target_trait_28() {
    let world = World::new();
    #[derive(Component)]
    struct Likes;

    #[derive(Component)]
    struct Apples;

    world.component::<Apples>().add_trait::<flecs::Target>();

    let e = world
        .entity()
        /*
        .add(Apples::id()) // Panic, 'Apples' is not used as target
        .add((Apples::id(), Likes::id())) // Panic, 'Apples' is not used as target, but as relationship
        */
        .add((Likes::id(), Apples::id())); // OK
}

#[test]
fn component_traits_trait_trait_29() {
    let world = World::new();
    #[derive(Component)]
    struct Serializable;

    world
        .component::<Serializable>()
        .add_trait::<flecs::Trait>();
}

#[test]
fn component_traits_transitive_trait_30() {
    let world = World::new();
    let locatedin = world.entity();
    let manhattan = world.entity();
    let newyork = world.entity();
    let usa = world.entity();

    manhattan.add((locatedin, newyork));
    newyork.add((locatedin, usa));
}

#[test]
fn component_traits_transitive_trait_31() {
    let world = World::new();
    let locatedin = world.entity();
    locatedin.add_trait::<flecs::Transitive>();
}

#[test]
fn component_traits_with_trait_32() {
    let world = World::new();
    let responsibility = world.entity();
    let power = world.entity().add((flecs::With::id(), responsibility));

    // Create new entity that has both Power and Responsibility
    let e = world.entity().add(power);
}

#[test]
fn component_traits_with_trait_33() {
    let world = World::new();
    let likes = world.entity();
    let loves = world.entity().add_trait::<(flecs::With, Likes)>();
    let pears = world.entity();

    // Create new entity with both (Loves, Pears) and (Likes, Pears)
    let e = world.entity().add((loves, pears));
}