2022-01-14 18:47:48 +00:00
|
|
|
#![warn(missing_docs)]
|
2021-12-18 22:59:55 +00:00
|
|
|
#![doc = include_str!("../README.md")]
|
|
|
|
|
2022-01-14 18:47:48 +00:00
|
|
|
/// The basic components of the transform crate
|
2019-12-02 09:31:07 +00:00
|
|
|
pub mod components;
|
2022-01-14 18:47:48 +00:00
|
|
|
/// Establishing and updating the transform hierarchy
|
2020-07-16 23:51:45 +00:00
|
|
|
pub mod hierarchy;
|
2022-01-14 18:47:48 +00:00
|
|
|
/// Propagating transform changes down the transform hierarchy
|
2020-06-07 20:39:50 +00:00
|
|
|
pub mod transform_propagate_system;
|
2019-12-02 09:31:07 +00:00
|
|
|
|
2022-01-14 18:47:48 +00:00
|
|
|
#[doc(hidden)]
|
2019-12-02 09:31:07 +00:00
|
|
|
pub mod prelude {
|
2021-04-27 18:29:33 +00:00
|
|
|
#[doc(hidden)]
|
2022-02-06 01:07:55 +00:00
|
|
|
pub use crate::{components::*, hierarchy::*, TransformBundle, TransformPlugin};
|
2020-07-10 04:18:35 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 21:20:37 +00:00
|
|
|
use bevy_app::prelude::*;
|
2022-02-06 01:07:55 +00:00
|
|
|
use bevy_ecs::{
|
|
|
|
bundle::Bundle,
|
|
|
|
schedule::{ParallelSystemDescriptorCoercion, SystemLabel},
|
|
|
|
};
|
2020-11-22 20:05:58 +00:00
|
|
|
use prelude::{parent_update_system, Children, GlobalTransform, Parent, PreviousParent, Transform};
|
2020-07-16 23:32:39 +00:00
|
|
|
|
2022-02-06 01:07:55 +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)
|
|
|
|
}
|
|
|
|
}
|
2022-01-14 18:47:48 +00:00
|
|
|
/// The base plugin for handling [`Transform`] components
|
2020-07-16 23:32:39 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct TransformPlugin;
|
|
|
|
|
2022-01-14 18:47:48 +00:00
|
|
|
/// Label enum for the types of systems relating to transform
|
2021-02-18 21:20:37 +00:00
|
|
|
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
|
|
|
|
pub enum TransformSystem {
|
2022-01-14 18:47:48 +00:00
|
|
|
/// Propagates changes in transform to childrens' [`GlobalTransform`]
|
2021-02-18 21:20:37 +00:00
|
|
|
TransformPropagate,
|
2022-01-14 18:47:48 +00:00
|
|
|
/// Updates [`Parent`] when changes in the hierarchy occur
|
2021-02-18 21:20:37 +00:00
|
|
|
ParentUpdate,
|
|
|
|
}
|
|
|
|
|
2020-08-08 03:22:17 +00:00
|
|
|
impl Plugin for TransformPlugin {
|
2021-07-27 20:21:06 +00:00
|
|
|
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"
|
2020-11-29 22:07:47 +00:00
|
|
|
.add_startup_system_to_stage(
|
2021-02-18 21:20:37 +00:00
|
|
|
StartupStage::PostStartup,
|
2021-07-27 23:42:36 +00:00
|
|
|
parent_update_system.label(TransformSystem::ParentUpdate),
|
2021-02-17 02:00:12 +00:00
|
|
|
)
|
|
|
|
.add_startup_system_to_stage(
|
2021-02-18 21:20:37 +00:00
|
|
|
StartupStage::PostStartup,
|
2021-02-17 02:00:12 +00:00
|
|
|
transform_propagate_system::transform_propagate_system
|
2021-02-18 21:20:37 +00:00
|
|
|
.label(TransformSystem::TransformPropagate)
|
|
|
|
.after(TransformSystem::ParentUpdate),
|
2021-02-17 02:00:12 +00:00
|
|
|
)
|
|
|
|
.add_system_to_stage(
|
2021-02-18 21:20:37 +00:00
|
|
|
CoreStage::PostUpdate,
|
2021-07-27 23:42:36 +00:00
|
|
|
parent_update_system.label(TransformSystem::ParentUpdate),
|
2020-11-29 22:07:47 +00:00
|
|
|
)
|
2020-11-17 02:18:00 +00:00
|
|
|
.add_system_to_stage(
|
2021-02-18 21:20:37 +00:00
|
|
|
CoreStage::PostUpdate,
|
2021-02-17 02:00:12 +00:00
|
|
|
transform_propagate_system::transform_propagate_system
|
2021-02-18 21:20:37 +00:00
|
|
|
.label(TransformSystem::TransformPropagate)
|
|
|
|
.after(TransformSystem::ParentUpdate),
|
2020-11-17 02:18:00 +00:00
|
|
|
);
|
2020-07-16 23:32:39 +00:00
|
|
|
}
|
2019-12-02 09:31:07 +00:00
|
|
|
}
|