edict 0.6.1

Experimental entity-component-system library
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! [`Relation`] is a concept that is similar to [`Component`].
//! The main difference is that they are not components, but rather relations.
//!
//! [`Component`] is data that can be attached to individual entity
//! and [`Relation`] is data that connects two entities together.
//!
//! [`Component`]: ../component/trait.Component.html

use alloc::{vec, vec::Vec};
use core::{marker::PhantomData, mem::ManuallyDrop};

use crate::{
    action::LocalActionEncoder,
    component::{Component, ComponentBorrow},
    entity::EntityId,
};

pub use edict_proc::Relation;

pub use self::{
    child_of::ChildOf,
    query::{
        FetchFilterRelatedBy, FetchRelated, FetchRelatesExclusiveRead, FetchRelatesExclusiveWrite,
        FetchRelatesRead, FetchRelatesToRead, FetchRelatesToWrite, FetchRelatesWrite,
        FilterFetchRelatesTo, FilterRelated, FilterRelatedBy, FilterRelates, FilterRelatesTo,
        Related, Relates, RelatesExclusive, RelatesReadIter, RelatesTo, RelatesWriteIter,
    },
};

mod child_of;
mod query;

/// Trait that must be implemented for types to be
/// used as relation components.
///
/// Relation components are special in a way that they are bound to
/// a pair of entities, not just one.
/// One entity is called "origin" and the other is called "target".
///
/// Relation components are used to connect two entities together.
/// For example [`ChildOf`] relation component is used to connect
/// child entity ("origin") to parent entity ("target").
///
/// Relation components are dropped when either of the "origin" or "target"
/// is dropped. Appropriate hook method is called when this happens.
/// `on_drop` is called when relation is dropped from "origin" entity.
/// `on_target_drop` is called when "target" entity is dropped.
pub trait Relation: Copy + 'static {
    /// If `true` then relation can be added only once to an entity.
    /// If another exclusive relation is added to the same entity,
    /// then the old one is removed.
    /// `on_replace` is called when this happens.
    ///
    /// Non-exclusive relations is replaced only if re-added
    /// with same target.
    const EXCLUSIVE: bool = false;

    /// If `true` then when relation is added to an entity
    /// it is also added to the target in reverse direction.
    const SYMMETRIC: bool = false;

    /// If `true` then origin entity in relation is "owned" by the target.
    /// This means that when last target is dropped, entity is despawned.
    const OWNED: bool = false;

    /// Returns name of the relation type.
    ///
    /// Can be overriden to provide custom name.
    #[inline(always)]
    #[must_use]
    fn name() -> &'static str {
        core::any::type_name::<Self>()
    }

    /// Method that is called when relation is dropped on origin entity.
    /// Does nothing by default.
    #[inline(always)]
    fn on_drop(&mut self, origin: EntityId, target: EntityId, encoder: LocalActionEncoder) {
        let _ = origin;
        let _ = target;
        let _ = encoder;
    }

    /// Method that is called when relation is re-inserted.
    /// For non-exclusive relations this happens when relation is re-inserted with the same
    /// origin-target entity pair.
    /// For exclusive relations this happens when relation is re-inserted with
    /// origin that has relation of this type with any target.
    ///
    /// If returns `true`, `on_drop` will be called.
    ///
    /// Does nothing by default and returns `true`, causing `on_drop` to be called.
    #[inline(always)]
    fn on_replace(
        &mut self,
        value: &Self,
        origin: EntityId,
        target: EntityId,
        new_target: EntityId,
        encoder: LocalActionEncoder,
    ) -> bool {
        let _ = value;
        let _ = origin;
        let _ = target;
        let _ = new_target;
        let _ = encoder;

        true
    }

    /// Method that is called when target entity of the relation is dropped.
    ///
    /// Does nothing by default.
    #[inline(always)]
    fn on_target_drop(origin: EntityId, target: EntityId, encoder: LocalActionEncoder) {
        let _ = origin;
        let _ = target;
        let _ = encoder;
    }
}

/// Sub-trait for exclusive relations.
/// It should be implemented for relations that specify `EXCLUSIVE = true`,
/// to enable use of `RelatesExclusive` query.
/// Implementing it for relation with `EXCLUSIVE = false` will cause
/// compilation error or runtime panic.
///
/// `Relation` derive macro implements this trait automatically.
pub trait ExclusiveRelation: Relation {
    #[doc(hidden)]
    const ASSERT_EXCLUSIVE: () = assert!(Self::EXCLUSIVE);
}

pub(crate) struct RelationTarget<R> {
    pub target: EntityId,
    pub relation: R,
}

pub(crate) union OriginComponent<R: Relation> {
    exclusive: ManuallyDrop<RelationTarget<R>>,
    non_exclusive: ManuallyDrop<Vec<RelationTarget<R>>>,
}

impl<R> Drop for OriginComponent<R>
where
    R: Relation,
{
    fn drop(&mut self) {
        match R::EXCLUSIVE {
            false => unsafe { ManuallyDrop::drop(&mut self.non_exclusive) },
            true => unsafe { ManuallyDrop::drop(&mut self.exclusive) },
        }
    }
}

impl<R> OriginComponent<R>
where
    R: Relation,
{
    /// Called when new relation is added to an entity.
    #[must_use]
    pub fn new_relation(target: EntityId, relation: R) -> Self {
        match R::EXCLUSIVE {
            false => OriginComponent {
                non_exclusive: ManuallyDrop::new(vec![RelationTarget { target, relation }]),
            },
            true => OriginComponent {
                exclusive: ManuallyDrop::new(RelationTarget { target, relation }),
            },
        }
    }

    /// Called when new relation is added to an entity that already has relation of this type.
    pub fn add_relation(
        &mut self,
        origin: EntityId,
        target: EntityId,
        relation: R,
        encoder: LocalActionEncoder,
    ) {
        match R::EXCLUSIVE {
            false => {
                let relations = unsafe { &mut *self.non_exclusive };
                for r in relations.iter_mut() {
                    if r.target == target {
                        Self::set_one(&mut r.relation, relation, origin, target, target, encoder);
                        return;
                    }
                }
                relations.push(RelationTarget { target, relation });
            }
            true => {
                let r = unsafe { &mut *self.exclusive };
                Self::set_one(&mut r.relation, relation, origin, r.target, target, encoder);
                r.target = target;
            }
        }
    }

    /// Called when relation is removed from an entity.
    /// This won't trigger any hooks.
    pub fn remove_relation(
        &mut self,
        origin: EntityId,
        target: EntityId,
        mut encoder: LocalActionEncoder,
    ) -> Option<R> {
        match R::EXCLUSIVE {
            false => {
                let relations = unsafe { &mut *self.non_exclusive };
                for idx in 0..relations.len() {
                    if relations[idx].target == target {
                        let r = relations.swap_remove(idx);
                        if relations.is_empty() {
                            encoder.drop::<Self>(origin);
                        }
                        return Some(r.relation);
                    }
                }
                None
            }
            true => {
                let r = unsafe { &mut *self.exclusive };
                if r.target == target {
                    encoder.drop::<Self>(origin);
                    return Some(r.relation);
                }
                None
            }
        }
    }

    /// Called by target relation component when it is dropped or replaced.
    fn on_target_drop(origin: EntityId, target: EntityId, mut encoder: LocalActionEncoder) {
        if R::EXCLUSIVE {
            if R::OWNED {
                encoder.despawn(origin);
            } else {
                encoder.drop::<Self>(origin);
            }
        } else {
            encoder.closure(move |world| {
                let Ok(mut origin) = world.entity(origin) else {
                    return;
                };

                let Some(comp) = origin.get_mut::<&mut Self>() else {
                    return;
                };

                let origins = unsafe { &mut *comp.non_exclusive };

                for idx in 0..origins.len() {
                    if origins[idx].target == target {
                        origins.swap_remove(idx);
                        break;
                    }
                }

                if origins.is_empty() {
                    if R::OWNED {
                        origin.despawn();
                    } else {
                        origin.drop::<Self>();
                    }
                }
            });
        }
    }

    #[must_use]
    pub fn relations(&self) -> &[RelationTarget<R>] {
        match R::EXCLUSIVE {
            false => unsafe { &*self.non_exclusive },
            true => core::slice::from_ref(unsafe { &*self.exclusive }),
        }
    }

    #[must_use]
    pub fn relations_mut(&mut self) -> &mut [RelationTarget<R>] {
        match R::EXCLUSIVE {
            false => unsafe { &mut *self.non_exclusive },
            true => core::slice::from_mut(unsafe { &mut *self.exclusive }),
        }
    }

    fn drop_one(
        relation: &mut R,
        origin: EntityId,
        target: EntityId,
        mut encoder: LocalActionEncoder,
    ) {
        relation.on_drop(origin, target, encoder.reborrow());
        if R::SYMMETRIC {
            if target != origin {
                Self::on_target_drop(target, origin, encoder);
            }
        } else {
            TargetComponent::<R>::on_origin_drop(origin, target, encoder)
        }
    }

    fn set_one(
        relation: &mut R,
        new_relation: R,
        origin: EntityId,
        target: EntityId,
        new_target: EntityId,
        mut encoder: LocalActionEncoder,
    ) {
        let on_replace = relation.on_replace(
            &new_relation,
            origin,
            target,
            new_target,
            encoder.reborrow(),
        );
        if on_replace {
            relation.on_drop(origin, target, encoder.reborrow());
        }
        if new_target != target {
            if R::SYMMETRIC {
                if target != origin {
                    Self::on_target_drop(target, origin, encoder);
                }
            } else {
                TargetComponent::<R>::on_origin_drop(origin, target, encoder)
            }
        }
        *relation = new_relation;
    }
}

impl<R> Component for OriginComponent<R>
where
    R: Relation,
{
    #[inline(always)]
    fn on_drop(&mut self, origin: EntityId, mut encoder: LocalActionEncoder) {
        for r in self.relations_mut() {
            Self::drop_one(&mut r.relation, origin, r.target, encoder.reborrow());
        }
    }

    #[inline(always)]
    fn on_replace(
        &mut self,
        _value: &Self,
        _origin: EntityId,
        _encoder: LocalActionEncoder,
    ) -> bool {
        unimplemented!("This method is not intended to be called");
    }

    #[inline(always)]
    #[must_use]
    fn borrows() -> Vec<ComponentBorrow> {
        Vec::new()
    }
}

/// Component that is added to target entity of the non-symmetric relation.
pub(crate) struct TargetComponent<R> {
    origins: Vec<EntityId>,
    relation: PhantomData<fn() -> R>,
}

impl<R> TargetComponent<R>
where
    R: Relation,
{
    #[must_use]
    pub(crate) fn new(origin: EntityId) -> Self {
        debug_assert!(!R::SYMMETRIC);

        TargetComponent {
            origins: vec![origin],
            relation: PhantomData,
        }
    }

    pub(crate) fn add(&mut self, origin: EntityId) {
        debug_assert!(!self.origins.contains(&origin));
        self.origins.push(origin);
    }

    /// Called when relation is removed from an entity.
    /// This won't trigger any hooks.
    pub fn remove_relation(
        &mut self,
        origin: EntityId,
        target: EntityId,
        mut encoder: LocalActionEncoder,
    ) {
        for idx in 0..self.origins.len() {
            if self.origins[idx] == origin {
                self.origins.swap_remove(idx);
                if self.origins.is_empty() {
                    encoder.drop::<Self>(target);
                }
            }
        }
    }

    /// Called when relation is removed from origin entity.
    /// Or origin entity is dropped.
    fn on_origin_drop(origin: EntityId, target: EntityId, mut encoder: LocalActionEncoder) {
        encoder.closure(move |world| {
            let Ok(mut target) = world.entity(target) else {
                return;
            };
            let Some(comp) = target.get_mut::<&mut Self>() else {
                return;
            };

            for idx in 0..comp.origins.len() {
                if comp.origins[idx] == origin {
                    comp.origins.swap_remove(idx);
                    break;
                }
            }

            if comp.origins.is_empty() {
                target.drop::<Self>();
            }
        })
    }
}

impl<R> Component for TargetComponent<R>
where
    R: Relation,
{
    #[inline(always)]
    fn on_drop(&mut self, target: EntityId, mut encoder: LocalActionEncoder) {
        for &origin in &self.origins {
            R::on_target_drop(origin, target, encoder.reborrow());
            OriginComponent::<R>::on_target_drop(origin, target, encoder.reborrow());
        }
    }

    #[inline(always)]
    fn on_replace(
        &mut self,
        _value: &Self,
        _entity: EntityId,
        _encoder: LocalActionEncoder,
    ) -> bool {
        unimplemented!("This method is not intended to be called");
    }

    #[inline(always)]
    #[must_use]
    fn borrows() -> Vec<ComponentBorrow> {
        Vec::new()
    }
}