bevy/crates/bevy_transform/src/lib.rs

127 lines
4.7 KiB
Rust
Raw Normal View History

#![warn(missing_docs)]
#![doc = include_str!("../README.md")]
/// The basic components of the transform crate
pub mod components;
/// Establishing and updating the transform hierarchy
pub mod hierarchy;
/// Propagating transform changes down the transform hierarchy
pub mod transform_propagate_system;
#[doc(hidden)]
pub mod prelude {
#[doc(hidden)]
pub use crate::{components::*, hierarchy::*, TransformBundle, TransformPlugin};
2020-07-10 04:18:35 +00:00
}
use bevy_app::prelude::*;
use bevy_ecs::{
bundle::Bundle,
schedule::{ParallelSystemDescriptorCoercion, SystemLabel},
};
use prelude::{parent_update_system, Children, GlobalTransform, Parent, PreviousParent, Transform};
2020-07-16 23:32:39 +00:00
/// A [`Bundle`] of the [`Transform`] and [`GlobalTransform`]
/// [`Component`](bevy_ecs::component::Component)s, which describe the position of an entity.
///
/// * To place or move an entity, you should set its [`Transform`].
/// * To get the global position of an entity, you should get its [`GlobalTransform`].
/// * For transform hierarchies to work correctly, you must have both a [`Transform`] and a [`GlobalTransform`].
/// * You may use the [`TransformBundle`] to guarantee this.
///
/// ## [`Transform`] and [`GlobalTransform`]
///
/// [`Transform`] is the position of an entity relative to its parent position, or the reference
/// frame if it doesn't have a [`Parent`](Parent).
///
/// [`GlobalTransform`] is the position of an entity relative to the reference frame.
///
/// [`GlobalTransform`] is updated from [`Transform`] in the system
/// [`transform_propagate_system`](crate::transform_propagate_system::transform_propagate_system).
///
/// This system runs in stage [`CoreStage::PostUpdate`](crate::CoreStage::PostUpdate). If you
/// update the[`Transform`] of an entity in this stage or after, you will notice a 1 frame lag
/// before the [`GlobalTransform`] is updated.
#[derive(Bundle, Clone, Copy, Debug, Default)]
pub struct TransformBundle {
/// The transform of the entity.
pub local: Transform,
/// The global transform of the entity.
pub global: GlobalTransform,
}
impl TransformBundle {
/// Creates a new [`TransformBundle`] from a [`Transform`].
///
/// This initializes [`GlobalTransform`] as identity, to be updated later by the
/// [`CoreStage::PostUpdate`](crate::CoreStage::PostUpdate) stage.
#[inline]
pub const fn from_transform(transform: Transform) -> Self {
TransformBundle {
local: transform,
// Note: `..Default::default()` cannot be used here, because it isn't const
..Self::identity()
}
}
/// Creates a new identity [`TransformBundle`], with no translation, rotation, and a scale of 1
/// on all axes.
#[inline]
pub const fn identity() -> Self {
TransformBundle {
local: Transform::identity(),
global: GlobalTransform::identity(),
}
}
}
impl From<Transform> for TransformBundle {
#[inline]
fn from(transform: Transform) -> Self {
Self::from_transform(transform)
}
}
/// The base plugin for handling [`Transform`] components
2020-07-16 23:32:39 +00:00
#[derive(Default)]
pub struct TransformPlugin;
/// Label enum for the types of systems relating to transform
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
pub enum TransformSystem {
/// Propagates changes in transform to childrens' [`GlobalTransform`]
TransformPropagate,
/// Updates [`Parent`] when changes in the hierarchy occur
ParentUpdate,
}
2020-08-08 03:22:17 +00:00
impl Plugin for TransformPlugin {
fn build(&self, app: &mut App) {
2020-11-28 00:39:59 +00:00
app.register_type::<Children>()
.register_type::<Parent>()
.register_type::<PreviousParent>()
.register_type::<Transform>()
.register_type::<GlobalTransform>()
2020-07-16 23:32:39 +00:00
// add transform systems to startup so the first update is "correct"
.add_startup_system_to_stage(
StartupStage::PostStartup,
parent_update_system.label(TransformSystem::ParentUpdate),
)
.add_startup_system_to_stage(
StartupStage::PostStartup,
transform_propagate_system::transform_propagate_system
.label(TransformSystem::TransformPropagate)
.after(TransformSystem::ParentUpdate),
)
.add_system_to_stage(
CoreStage::PostUpdate,
parent_update_system.label(TransformSystem::ParentUpdate),
)
.add_system_to_stage(
CoreStage::PostUpdate,
transform_propagate_system::transform_propagate_system
.label(TransformSystem::TransformPropagate)
.after(TransformSystem::ParentUpdate),
);
2020-07-16 23:32:39 +00:00
}
}