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
use std::fmt::Debug;
use std::ops::{Deref, DerefMut};

use cgmath::prelude::*;
use collision::{Aabb, Primitive};
use specs::{Component, Entity, VecStorage};

use collide::{CollisionShape, ContactEvent};
use collide::util::ContainerShapeWrapper;

use Real;

/// Retrieve the entity for the given object
pub trait GetEntity {
    /// Return the entity
    fn entity(&self) -> Entity;
}

impl<P, T> Component for CollisionShape<P, T>
where
    T: Send + Sync + 'static,
    P: Primitive + Send + Sync + 'static,
    P::Aabb: Send + Sync + 'static,
{
    type Storage = VecStorage<CollisionShape<P, T>>;
}

/// Contacts storage for use in ECS.
///
/// Will typically contain the contacts found in the last collision detection run.
///
/// # Type parameters:
///
/// - `P`: cgmath point type
#[derive(Debug)]
pub struct Contacts<P>
where
    P: EuclideanSpace,
    P::Diff: Debug,
{
    contacts: Vec<ContactEvent<Entity, P>>,
}

impl<P> Default for Contacts<P>
where
    P: EuclideanSpace,
    P::Diff: Debug,
{
    fn default() -> Self {
        Self {
            contacts: Vec::default(),
        }
    }
}

impl<P> Deref for Contacts<P>
where
    P: EuclideanSpace,
    P::Diff: Debug,
{
    type Target = Vec<ContactEvent<Entity, P>>;

    fn deref(&self) -> &Self::Target {
        &self.contacts
    }
}

impl<P> DerefMut for Contacts<P>
where
    P: EuclideanSpace,
    P::Diff: Debug,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.contacts
    }
}

impl<'a, P, T> From<(Entity, &'a CollisionShape<P, T>)> for ContainerShapeWrapper<Entity, P>
where
    P: Primitive,
    P::Aabb: Aabb<Scalar = Real>,
    <P::Point as EuclideanSpace>::Diff: Debug,
    T: Transform<P::Point>,
{
    fn from((entity, ref shape): (Entity, &CollisionShape<P, T>)) -> Self {
        Self::new(entity, shape.bound())
    }
}

impl<P> GetEntity for ContainerShapeWrapper<Entity, P>
where
    P: Primitive,
    P::Aabb: Aabb<Scalar = Real>,
    <P::Point as EuclideanSpace>::Diff: Debug,
{
    fn entity(&self) -> Entity {
        self.id
    }
}