-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a SpatialBundle with visibility and transform components (#5344)
# Objective - Help user when they need to add both a `TransformBundle` and a `VisibilityBundle` ## Solution - Add a `SpatialBundle` adding all components
- Loading branch information
Showing
8 changed files
with
92 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use bevy_ecs::prelude::Bundle; | ||
use bevy_transform::prelude::{GlobalTransform, Transform}; | ||
|
||
use crate::view::{ComputedVisibility, Visibility}; | ||
|
||
/// A [`Bundle`] with the following [`Component`](bevy_ecs::component::Component)s: | ||
/// * [`Visibility`] and [`ComputedVisibility`], which describe the visibility of an entity | ||
/// * [`Transform`] and [`GlobalTransform`], which describe the position of an entity | ||
/// | ||
/// * To show or hide an entity, you should set its [`Visibility`]. | ||
/// * To get the computed visibility of an entity, you should get its [`ComputedVisibility`]. | ||
/// * To place or move an entity, you should set its [`Transform`]. | ||
/// * To get the global transform of an entity, you should get its [`GlobalTransform`]. | ||
/// * For hierarchies to work correctly, you must have all four components. | ||
/// * You may use the [`SpatialBundle`] to guarantee this. | ||
#[derive(Bundle, Debug, Default)] | ||
pub struct SpatialBundle { | ||
/// The visibility of the entity. | ||
pub visibility: Visibility, | ||
/// The computed visibility of the entity. | ||
pub computed: ComputedVisibility, | ||
/// The transform of the entity. | ||
pub transform: Transform, | ||
/// The global transform of the entity. | ||
pub global_transform: GlobalTransform, | ||
} | ||
|
||
impl SpatialBundle { | ||
/// Creates a new [`SpatialBundle`] from a [`Transform`]. | ||
/// | ||
/// This initializes [`GlobalTransform`] as identity, and visibility as visible | ||
#[inline] | ||
pub const fn from_transform(transform: Transform) -> Self { | ||
SpatialBundle { | ||
transform, | ||
// Note: `..Default::default()` cannot be used here, because it isn't const | ||
..Self::visible_identity() | ||
} | ||
} | ||
|
||
/// Creates a new identity [`SpatialBundle`], with no translation, rotation, and a scale of 1 | ||
/// on all axes. | ||
#[inline] | ||
pub const fn visible_identity() -> Self { | ||
SpatialBundle { | ||
transform: Transform::identity(), | ||
global_transform: GlobalTransform::identity(), | ||
visibility: Visibility::visible(), | ||
computed: ComputedVisibility::not_visible(), | ||
} | ||
} | ||
} | ||
|
||
impl From<Transform> for SpatialBundle { | ||
#[inline] | ||
fn from(transform: Transform) -> Self { | ||
Self::from_transform(transform) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters