Skip to content

Commit

Permalink
Make methods const
Browse files Browse the repository at this point in the history
  • Loading branch information
Jondolf committed Sep 7, 2024
1 parent 43f3286 commit edb26f8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 31 deletions.
16 changes: 11 additions & 5 deletions src/spatial_query/query_filter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use bevy::{prelude::*, utils::EntityHashSet};
use bevy::{
prelude::*,
utils::{EntityHash, EntityHashSet},
};

use crate::prelude::*;

Expand Down Expand Up @@ -41,14 +44,17 @@ pub struct SpatialQueryFilter {

impl Default for SpatialQueryFilter {
fn default() -> Self {
Self {
mask: LayerMask::ALL,
excluded_entities: default(),
}
Self::DEFAULT
}
}

impl SpatialQueryFilter {
/// The default [`SpatialQueryFilter`] configuration that includes all collision layers and has no excluded entities.
pub const DEFAULT: Self = Self {
mask: LayerMask::ALL,
excluded_entities: EntityHashSet::with_hasher(EntityHash),
};

/// Creates a new [`SpatialQueryFilter`] with the given [`LayerMask`] determining
/// which [collision layers](CollisionLayers) will be included in the [spatial query](crate::spatial_query).
pub fn from_mask(mask: impl Into<LayerMask>) -> Self {
Expand Down
24 changes: 16 additions & 8 deletions src/spatial_query/ray_caster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,36 +395,44 @@ impl Default for RayCastConfig {
}

impl RayCastConfig {
/// The default [`RayCastConfig`] configuration.
pub const DEFAULT: Self = Self {
max_distance: Scalar::MAX,
solid: true,
filter: SpatialQueryFilter::DEFAULT,
};

/// Creates a new [`RayCastConfig`] with a given maximum distance the ray can travel.
#[inline]
pub fn from_max_distance(max_distance: Scalar) -> Self {
pub const fn from_max_distance(max_distance: Scalar) -> Self {
Self {
max_distance,
..default()
solid: true,
filter: SpatialQueryFilter::DEFAULT,
}
}

/// Creates a new [`RayCastConfig`] with a given [`SpatialQueryFilter`].
#[inline]
pub fn from_filter(filter: SpatialQueryFilter) -> Self {
pub const fn from_filter(filter: SpatialQueryFilter) -> Self {
Self {
max_distance: Scalar::MAX,
solid: true,
filter,
..default()
}
}

/// Sets the maximum distance the ray can travel.
#[inline]
pub fn with_max_distance(mut self, max_distance: Scalar) -> Self {
pub const fn with_max_distance(mut self, max_distance: Scalar) -> Self {
self.max_distance = max_distance;
self
}

/// Sets the [`SpatialQueryFilter`] for the ray cast.
#[inline]
pub fn with_filter(mut self, filter: SpatialQueryFilter) -> Self {
self.filter = filter;
self
pub const fn with_filter(&self, filter: SpatialQueryFilter) -> Self {
Self { filter, ..*self }
}
}

Expand Down
47 changes: 29 additions & 18 deletions src/spatial_query/shape_caster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,64 +463,75 @@ pub struct ShapeCastConfig {

impl Default for ShapeCastConfig {
fn default() -> Self {
Self {
max_distance: Scalar::MAX,
target_distance: 0.0,
compute_impact_on_penetration: true,
ignore_origin_penetration: false,
filter: SpatialQueryFilter::default(),
}
Self::DEFAULT
}
}

impl ShapeCastConfig {
/// The default [`ShapeCastConfig`] configuration.
pub const DEFAULT: Self = Self {
max_distance: Scalar::MAX,
target_distance: 0.0,
compute_impact_on_penetration: true,
ignore_origin_penetration: false,
filter: SpatialQueryFilter::DEFAULT,
};

/// Creates a new [`ShapeCastConfig`] with a given maximum distance the shape can travel.
#[inline]
pub fn from_max_distance(max_distance: Scalar) -> Self {
pub const fn from_max_distance(max_distance: Scalar) -> Self {
Self {
max_distance,
..default()
target_distance: 0.0,
compute_impact_on_penetration: true,
ignore_origin_penetration: false,
filter: SpatialQueryFilter::DEFAULT,
}
}

/// Creates a new [`ShapeCastConfig`] with a given separation distance at which
/// the shapes will be considered as impacting.
#[inline]
pub fn from_target_distance(target_distance: Scalar) -> Self {
pub const fn from_target_distance(target_distance: Scalar) -> Self {
Self {
max_distance: Scalar::MAX,
target_distance,
..default()
compute_impact_on_penetration: true,
ignore_origin_penetration: false,
filter: SpatialQueryFilter::DEFAULT,
}
}

/// Creates a new [`ShapeCastConfig`] with a given [`SpatialQueryFilter`].
#[inline]
pub fn from_filter(filter: SpatialQueryFilter) -> Self {
pub const fn from_filter(filter: SpatialQueryFilter) -> Self {
Self {
max_distance: Scalar::MAX,
target_distance: 0.0,
compute_impact_on_penetration: true,
ignore_origin_penetration: false,
filter,
..default()
}
}

/// Sets the maximum distance the shape can travel.
#[inline]
pub fn with_max_distance(mut self, max_distance: Scalar) -> Self {
pub const fn with_max_distance(mut self, max_distance: Scalar) -> Self {
self.max_distance = max_distance;
self
}

/// Sets the separation distance at which the shapes will be considered as impacting.
#[inline]
pub fn with_target_distance(mut self, target_distance: Scalar) -> Self {
pub const fn with_target_distance(mut self, target_distance: Scalar) -> Self {
self.target_distance = target_distance;
self
}

/// Sets the [`SpatialQueryFilter`] for the shape cast.
#[inline]
pub fn with_filter(mut self, filter: SpatialQueryFilter) -> Self {
self.filter = filter;
self
pub const fn with_filter(&self, filter: SpatialQueryFilter) -> Self {
Self { filter, ..*self }
}
}

Expand Down

0 comments on commit edb26f8

Please sign in to comment.