Skip to main content

azalea_physics/collision_impl/
entity_collisions.rs

1use azalea_core::aabb::Aabb;
2use azalea_entity::{
3    Physics,
4    metadata::{AbstractBoat, Shulker},
5};
6use azalea_physics_collision::shape::VoxelShape;
7use azalea_world::World;
8use bevy_ecs::{
9    component::Component,
10    entity::Entity,
11    query::{Changed, Or, With},
12    system::{Commands, Query},
13};
14use tracing::error;
15
16/// This query matches on entities that we can collide with (boats and
17/// shulkers).
18///
19/// If you want to use this in a more complex query, use
20/// [`CollidableEntityFilter`] as a filter instead.
21pub type CollidableEntityQuery<'world, 'state> = Query<'world, 'state, (), CollidableEntityFilter>;
22/// This filter matches on entities that we can collide with (boats and
23/// shulkers).
24///
25/// Use [`CollidableEntityQuery`] if you want an empty query that matches with
26/// this.
27pub type CollidableEntityFilter = Or<(With<AbstractBoat>, With<Shulker>)>;
28
29/// A component that mirrors the Physics::bounding_box of every entity, but is
30/// updated before client-side physics is done.
31#[derive(Component)]
32pub struct LastBoundingBox(pub Aabb);
33
34pub type AabbQuery<'world, 'state, 'a> = Query<'world, 'state, &'a LastBoundingBox>;
35
36/// Update the [`LastBoundingBox`] for every entity.
37pub fn update_last_bounding_box(
38    mut commands: Commands,
39    mut query: Query<(Entity, Option<&mut LastBoundingBox>, &Physics), Changed<Physics>>,
40) {
41    for (entity, mut last_bounding_box, physics) in &mut query {
42        if let Some(last_bounding_box) = last_bounding_box.as_mut() {
43            last_bounding_box.0 = physics.bounding_box;
44        } else {
45            commands
46                .entity(entity)
47                .insert(LastBoundingBox(physics.bounding_box));
48        }
49    }
50}
51
52pub fn get_entity_collisions(
53    world: &World,
54    aabb: &Aabb,
55    source_entity: Option<Entity>,
56    aabb_query: &AabbQuery,
57    collidable_entity_query: &CollidableEntityQuery,
58) -> Vec<VoxelShape> {
59    if aabb.size() < 1.0E-7 {
60        return vec![];
61    }
62
63    let collision_predicate = |entity| collidable_entity_query.get(entity).is_ok();
64
65    let collidable_entities = get_entities(
66        world,
67        source_entity,
68        &aabb.inflate_all(1.0E-7),
69        &collision_predicate,
70        aabb_query,
71    );
72
73    collidable_entities
74        .into_iter()
75        .map(|(_entity, aabb)| VoxelShape::from(aabb))
76        .collect()
77}
78
79/// Return all entities that are colliding with the given bounding box and match
80/// the given predicate.
81///
82/// `source_entity` is the entity that the bounding box belongs to, and won't be
83/// one of the returned entities.
84pub fn get_entities(
85    world: &World,
86    source_entity: Option<Entity>,
87    aabb: &Aabb,
88    predicate: &dyn Fn(Entity) -> bool,
89    aabb_query: &AabbQuery,
90) -> Vec<(Entity, Aabb)> {
91    let mut matches = Vec::new();
92
93    super::world_collisions::for_entities_in_chunks_colliding_with(
94        world,
95        aabb,
96        |_chunk_pos, entities_in_chunk| {
97            // now check if the entity itself collides
98            for &candidate in entities_in_chunk {
99                if Some(candidate) != source_entity && predicate(candidate) {
100                    let Ok(candidate_aabb) = aabb_query.get(candidate) else {
101                        error!(
102                            "Entity {candidate} (found from for_entities_in_chunks_colliding_with) is missing required components."
103                        );
104                        continue;
105                    };
106                    let candidate_aabb = &candidate_aabb.0;
107
108                    if aabb.intersects_aabb(candidate_aabb) {
109                        matches.push((candidate, candidate_aabb.to_owned()));
110                    }
111                }
112            }
113        },
114    );
115
116    matches
117}