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
use bevy::ecs::entity::Entity;

use crate::CollisionLayers;

/// An event fired when the collision state between two entities changed
///
/// # Example
///
/// ```
/// # use bevy::prelude::*;
/// # use heron_core::*;
/// fn detect_collisions(mut events: EventReader<CollisionEvent>) {
///     for event in events.iter() {
///         match event {
///             CollisionEvent::Started(data1, data2) => {
///                 println!("Entity {:?} and {:?} started to collide", data1.rigid_body_entity(), data2.rigid_body_entity())
///             }
///             CollisionEvent::Stopped(data1, data2) => {
///                 println!("Entity {:?} and {:?} stopped to collide", data1.rigid_body_entity(), data2.rigid_body_entity())
///             }
///         }
///     }
/// }
/// ```
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum CollisionEvent {
    /// The two entities started to collide
    Started(CollisionData, CollisionData),

    /// The two entities no longer collide
    Stopped(CollisionData, CollisionData),
}

/// Collision data concerning one of the two entity that collided
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct CollisionData {
    rigid_body_entity: Entity,
    collision_shape_entity: Entity,
    collision_layers: CollisionLayers,
}

impl From<CollisionEvent> for (CollisionData, CollisionData) {
    fn from(event: CollisionEvent) -> Self {
        event.data()
    }
}

impl CollisionEvent {
    /// Returns true if the event represent the "start" of a collision
    #[must_use]
    pub fn is_started(&self) -> bool {
        matches!(self, CollisionEvent::Started(_, _))
    }

    /// Returns true if the event represent the "end" of a collision
    #[must_use]
    pub fn is_stopped(&self) -> bool {
        matches!(self, CollisionEvent::Stopped(_, _))
    }

    /// Returns the data for the two entities that collided
    #[must_use]
    pub fn data(self) -> (CollisionData, CollisionData) {
        match self {
            CollisionEvent::Started(d1, d2) | CollisionEvent::Stopped(d1, d2) => (d1, d2),
        }
    }

    /// Returns the entities containing the [`CollisionShape`](crate::RigidBody) involved in the collision
    #[must_use]
    pub fn collision_shape_entities(&self) -> (Entity, Entity) {
        match self {
            CollisionEvent::Started(d1, d2) | CollisionEvent::Stopped(d1, d2) => {
                (d1.collision_shape_entity, d2.collision_shape_entity)
            }
        }
    }

    /// Returns the entities containing the [`RigidBody`](crate::RigidBody) involved in the collision
    #[must_use]
    pub fn rigid_body_entities(&self) -> (Entity, Entity) {
        match self {
            CollisionEvent::Started(d1, d2) | CollisionEvent::Stopped(d1, d2) => {
                (d1.rigid_body_entity, d2.rigid_body_entity)
            }
        }
    }

    /// Returns the two [`CollisionLayers`] involved in the collision
    #[must_use]
    pub fn collision_layers(&self) -> (CollisionLayers, CollisionLayers) {
        match self {
            CollisionEvent::Started(d1, d2) | CollisionEvent::Stopped(d1, d2) => {
                (d1.collision_layers, d2.collision_layers)
            }
        }
    }
}

impl CollisionData {
    #[must_use]
    #[allow(missing_docs)]
    pub fn new(
        rigid_body_entity: Entity,
        collision_shape_entity: Entity,
        collision_layers: CollisionLayers,
    ) -> Self {
        Self {
            rigid_body_entity,
            collision_shape_entity,
            collision_layers,
        }
    }

    /// Returns the entity containing the [`RigidBody`](crate::RigidBody)
    #[must_use]
    pub fn rigid_body_entity(&self) -> Entity {
        self.rigid_body_entity
    }

    /// Returns the entity containing the [`CollisionShape`](crate::CollisionShape)
    #[must_use]
    pub fn collision_shape_entity(&self) -> Entity {
        self.collision_shape_entity
    }

    /// Returns the [`CollisionLayers`] of the collision shape entity
    #[must_use]
    pub fn collision_layers(&self) -> CollisionLayers {
        self.collision_layers
    }
}