Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename CollisionLayers methods with_group, without_group etc. to add_group, remove_group #62

Merged
merged 2 commits into from
Jul 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions src/components/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ impl<L: PhysicsLayer> PhysicsLayer for &L {
///
/// The easiest way to build a [`CollisionLayers`] configuration is to use the [`CollisionLayers::new()`](#method.new) method
/// that takes in a list of groups and masks. Additional groups and masks can be added and removed by calling methods like
/// [`with_groups`](#method.with_groups), [`with_masks`](#method.with_masks), [`without_groups`](#method.without_groups) and
/// [`without_masks`](#method.without_masks).
/// [`add_groups`](#method.add_groups), [`add_masks`](#method.add_masks), [`remove_groups`](#method.remove_groups) and
/// [`remove_masks`](#method.remove_masks).
///
/// These methods require the layers to implement [`PhysicsLayer`]. The easiest way to define the physics layers is to
/// create an enum with `#[derive(PhysicsLayer)]`.
Expand Down Expand Up @@ -84,7 +84,7 @@ impl CollisionLayers {
groups: impl IntoIterator<Item = L>,
masks: impl IntoIterator<Item = L>,
) -> Self {
Self::none().with_groups(groups).with_masks(masks)
Self::none().add_groups(groups).add_masks(masks)
}

/// Contains all groups and masks.
Expand Down Expand Up @@ -128,13 +128,13 @@ impl CollisionLayers {
}

/// Adds the given layer into `groups`.
pub fn with_group(mut self, layer: impl PhysicsLayer) -> Self {
pub fn add_group(mut self, layer: impl PhysicsLayer) -> Self {
self.groups |= layer.to_bits();
self
}

/// Adds the given layers into `groups`.
pub fn with_groups(mut self, layers: impl IntoIterator<Item = impl PhysicsLayer>) -> Self {
pub fn add_groups(mut self, layers: impl IntoIterator<Item = impl PhysicsLayer>) -> Self {
for layer in layers.into_iter().map(|l| l.to_bits()) {
self.groups |= layer;
}
Expand All @@ -143,24 +143,33 @@ impl CollisionLayers {
}

/// Removes the given layer from `groups`.
pub fn without_group(mut self, layer: impl PhysicsLayer) -> Self {
pub fn remove_group(mut self, layer: impl PhysicsLayer) -> Self {
self.groups &= !layer.to_bits();
self
}

/// Removes the given layers from `groups`.
pub fn remove_groups(mut self, layers: impl IntoIterator<Item = impl PhysicsLayer>) -> Self {
for layer in layers.into_iter().map(|l| l.to_bits()) {
self.groups &= !layer;
}

self
}

/// Returns true if the given layer is contained in `masks`.
pub fn contains_mask(self, layer: impl PhysicsLayer) -> bool {
(self.masks & layer.to_bits()) != 0
}

/// Adds the given layer into `masks`.
pub fn with_mask(mut self, layer: impl PhysicsLayer) -> Self {
pub fn add_mask(mut self, layer: impl PhysicsLayer) -> Self {
self.masks |= layer.to_bits();
self
}

/// Adds the given layers in `masks`.
pub fn with_masks(mut self, layers: impl IntoIterator<Item = impl PhysicsLayer>) -> Self {
pub fn add_masks(mut self, layers: impl IntoIterator<Item = impl PhysicsLayer>) -> Self {
for layer in layers.into_iter().map(|l| l.to_bits()) {
self.masks |= layer;
}
Expand All @@ -169,11 +178,20 @@ impl CollisionLayers {
}

/// Removes the given layer from `masks`.
pub fn without_mask(mut self, layer: impl PhysicsLayer) -> Self {
pub fn remove_mask(mut self, layer: impl PhysicsLayer) -> Self {
self.masks &= !layer.to_bits();
self
}

/// Removes the given layers from `masks`.
pub fn remove_masks(mut self, layers: impl IntoIterator<Item = impl PhysicsLayer>) -> Self {
for layer in layers.into_iter().map(|l| l.to_bits()) {
self.masks &= !layer;
}

self
}

/// Returns the `groups` bitmask.
pub fn groups_bits(self) -> u32 {
self.groups
Expand Down