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-03-15 01:54:05 +00:00
|
|
|
mod systems;
|
|
|
|
pub use crate::systems::transform_propagate_system;
|
2019-12-02 09:31:07 +00:00
|
|
|
|
2022-11-02 19:35:06 +00:00
|
|
|
#[allow(missing_docs)]
|
2019-12-02 09:31:07 +00:00
|
|
|
pub mod prelude {
|
2022-11-02 19:35:06 +00:00
|
|
|
#[doc(no_inline)]
|
2022-03-15 01:54:05 +00:00
|
|
|
pub use crate::{components::*, TransformBundle, TransformPlugin};
|
2020-07-10 04:18:35 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 21:20:37 +00:00
|
|
|
use bevy_app::prelude::*;
|
2022-03-15 01:54:05 +00:00
|
|
|
use bevy_ecs::prelude::*;
|
2022-09-19 16:12:11 +00:00
|
|
|
use bevy_hierarchy::ValidParentCheckPlugin;
|
2022-03-15 01:54:05 +00:00
|
|
|
use prelude::{GlobalTransform, 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`].
|
2022-07-18 23:27:30 +00:00
|
|
|
/// * To get the global transform of an entity, you should get its [`GlobalTransform`].
|
2022-02-06 01:07:55 +00:00
|
|
|
/// * 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
|
2022-03-15 01:54:05 +00:00
|
|
|
/// frame if it doesn't have a parent.
|
2022-02-06 01:07:55 +00:00
|
|
|
///
|
|
|
|
/// [`GlobalTransform`] is the position of an entity relative to the reference frame.
|
|
|
|
///
|
|
|
|
/// [`GlobalTransform`] is updated from [`Transform`] in the system
|
2022-03-15 01:54:05 +00:00
|
|
|
/// [`transform_propagate_system`].
|
2022-02-06 01:07:55 +00:00
|
|
|
///
|
|
|
|
/// This system runs in stage [`CoreStage::PostUpdate`](crate::CoreStage::PostUpdate). If you
|
2022-07-21 20:46:54 +00:00
|
|
|
/// update the [`Transform`] of an entity in this stage or after, you will notice a 1 frame lag
|
2022-02-06 01:07:55 +00:00
|
|
|
/// 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 {
|
2022-08-30 22:10:24 +00:00
|
|
|
/// An identity [`TransformBundle`] with no translation, rotation, and a scale of 1 on all axes.
|
|
|
|
pub const IDENTITY: Self = TransformBundle {
|
|
|
|
local: Transform::IDENTITY,
|
|
|
|
global: GlobalTransform::IDENTITY,
|
|
|
|
};
|
|
|
|
|
2022-02-06 01:07:55 +00:00
|
|
|
/// 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,
|
2022-08-30 22:10:24 +00:00
|
|
|
..Self::IDENTITY
|
2022-02-06 01:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Transform> for TransformBundle {
|
|
|
|
#[inline]
|
|
|
|
fn from(transform: Transform) -> Self {
|
|
|
|
Self::from_transform(transform)
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 01:54:05 +00:00
|
|
|
/// Label enum for the systems relating to transform propagation
|
2021-02-18 21:20:37 +00:00
|
|
|
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
|
|
|
|
pub enum TransformSystem {
|
2022-07-21 20:46:54 +00:00
|
|
|
/// Propagates changes in transform to children's [`GlobalTransform`](crate::components::GlobalTransform)
|
2021-02-18 21:20:37 +00:00
|
|
|
TransformPropagate,
|
|
|
|
}
|
|
|
|
|
2022-03-15 01:54:05 +00:00
|
|
|
/// The base plugin for handling [`Transform`] components
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct TransformPlugin;
|
|
|
|
|
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) {
|
2022-03-15 01:54:05 +00:00
|
|
|
app.register_type::<Transform>()
|
2020-11-28 00:39:59 +00:00
|
|
|
.register_type::<GlobalTransform>()
|
2022-09-19 16:12:11 +00:00
|
|
|
.add_plugin(ValidParentCheckPlugin::<GlobalTransform>::default())
|
2022-07-10 20:29:06 +00:00
|
|
|
// add transform systems to startup so the first update is "correct"
|
2021-02-17 02:00:12 +00:00
|
|
|
.add_startup_system_to_stage(
|
2021-02-18 21:20:37 +00:00
|
|
|
StartupStage::PostStartup,
|
2022-07-10 20:29:06 +00:00
|
|
|
systems::transform_propagate_system.label(TransformSystem::TransformPropagate),
|
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,
|
2022-07-10 20:29:06 +00:00
|
|
|
systems::transform_propagate_system.label(TransformSystem::TransformPropagate),
|
2020-11-17 02:18:00 +00:00
|
|
|
);
|
2020-07-16 23:32:39 +00:00
|
|
|
}
|
2019-12-02 09:31:07 +00:00
|
|
|
}
|