transform: TransformPlugin

This commit is contained in:
Carter Anderson 2020-07-16 16:32:39 -07:00
parent f546aad7f4
commit d9adea1b5e
16 changed files with 86 additions and 48 deletions

View File

@ -74,6 +74,10 @@ impl AppBuilder {
self.add_system_to_stage(stage::UPDATE, system) self.add_system_to_stage(stage::UPDATE, system)
} }
pub fn add_systems(&mut self, systems: Vec<Box<dyn System>>) -> &mut Self {
self.add_systems_to_stage(stage::UPDATE, systems)
}
pub fn init_system( pub fn init_system(
&mut self, &mut self,
build: impl FnMut(&mut Resources) -> Box<dyn System>, build: impl FnMut(&mut Resources) -> Box<dyn System>,
@ -101,6 +105,19 @@ impl AppBuilder {
self self
} }
pub fn add_startup_systems_to_stage(
&mut self,
stage_name: &'static str,
systems: Vec<Box<dyn System>>,
) -> &mut Self {
for system in systems {
self.app
.startup_schedule
.add_system_to_stage(stage_name, system);
}
self
}
pub fn add_startup_system(&mut self, system: Box<dyn System>) -> &mut Self { pub fn add_startup_system(&mut self, system: Box<dyn System>) -> &mut Self {
self.app self.app
.startup_schedule .startup_schedule
@ -108,6 +125,10 @@ impl AppBuilder {
self self
} }
pub fn add_startup_systems(&mut self, systems: Vec<Box<dyn System>>) -> &mut Self {
self.add_startup_systems_to_stage(startup_stage::STARTUP, systems)
}
pub fn init_startup_system( pub fn init_startup_system(
&mut self, &mut self,
build: impl FnMut(&mut Resources) -> Box<dyn System>, build: impl FnMut(&mut Resources) -> Box<dyn System>,
@ -144,6 +165,17 @@ impl AppBuilder {
self self
} }
pub fn add_systems_to_stage(
&mut self,
stage_name: &'static str,
systems: Vec<Box<dyn System>>,
) -> &mut Self {
for system in systems {
self.app.schedule.add_system_to_stage(stage_name, system);
}
self
}
pub fn add_event<T>(&mut self) -> &mut Self pub fn add_event<T>(&mut self) -> &mut Self
where where
T: Send + Sync + 'static, T: Send + Sync + 'static,

View File

@ -10,5 +10,4 @@ bevy_derive = { path = "../bevy_derive" }
bevy_ecs = { path = "../bevy_ecs" } bevy_ecs = { path = "../bevy_ecs" }
bevy_property = { path = "../bevy_property" } bevy_property = { path = "../bevy_property" }
bevy_type_registry = { path = "../bevy_type_registry" } bevy_type_registry = { path = "../bevy_type_registry" }
bevy_transform = { path = "../bevy_transform" }
glam = "0.8.7" glam = "0.8.7"

View File

@ -1,16 +1,10 @@
pub mod bytes; pub mod bytes;
pub mod float_ord; pub mod float_ord;
pub mod time; pub mod time;
pub mod transform; pub mod math;
use bevy_app::{stage, startup_stage, AppBuilder, AppPlugin}; use bevy_app::{stage, AppBuilder, AppPlugin};
use bevy_ecs::IntoQuerySystem; use bevy_ecs::IntoQuerySystem;
use bevy_transform::{
build_systems,
components::{
Children, LocalTransform, NonUniformScale, Rotation, Scale, Transform, Translation,
},
};
use bevy_type_registry::RegisterType; use bevy_type_registry::RegisterType;
use glam::{Mat3, Mat4, Quat, Vec2, Vec3}; use glam::{Mat3, Mat4, Quat, Vec2, Vec3};
use time::{time_system, timer_system, Time, Timer}; use time::{time_system, timer_system, Time, Timer};
@ -20,22 +14,7 @@ pub struct CorePlugin;
impl AppPlugin for CorePlugin { impl AppPlugin for CorePlugin {
fn build(&self, app: &mut AppBuilder) { fn build(&self, app: &mut AppBuilder) {
// we also add a copy of transform systems to startup to ensure we begin with correct transform/parent state
for transform_system in build_systems() {
app.add_startup_system_to_stage(startup_stage::POST_STARTUP, transform_system);
}
for transform_system in build_systems() {
app.add_system_to_stage(stage::POST_UPDATE, transform_system);
}
app.init_resource::<Time>() app.init_resource::<Time>()
.register_component::<Children>()
.register_component::<LocalTransform>()
.register_component::<Transform>()
.register_component::<Translation>()
.register_component::<Rotation>()
.register_component::<Scale>()
.register_component::<NonUniformScale>()
.register_component::<Timer>() .register_component::<Timer>()
.register_property_type::<Vec2>() .register_property_type::<Vec2>()
.register_property_type::<Vec3>() .register_property_type::<Vec3>()

View File

@ -1,5 +1,3 @@
mod face_toward; mod face_toward;
mod hierarchy;
pub use face_toward::*; pub use face_toward::*;
pub use hierarchy::*;

View File

@ -7,8 +7,10 @@ edition = "2018"
license = "MIT" license = "MIT"
[dependencies] [dependencies]
bevy_app = { path = "../bevy_app"}
bevy_ecs = { path = "../bevy_ecs"} bevy_ecs = { path = "../bevy_ecs"}
bevy_property = { path = "../bevy_property" } bevy_property = { path = "../bevy_property" }
bevy_type_registry = { path = "../bevy_type_registry" }
glam = "0.8.7" glam = "0.8.7"
log = "0.4" log = "0.4"
smallvec = { version = "1.4", features = ["serde"] } smallvec = { version = "1.4", features = ["serde"] }

View File

@ -1,5 +1,5 @@
use crate::components::Children;
use bevy_ecs::{Entity, Query}; use bevy_ecs::{Entity, Query};
use bevy_transform::prelude::Children;
pub fn run_on_hierarchy<T, S>( pub fn run_on_hierarchy<T, S>(
children_query: &Query<&Children>, children_query: &Query<&Children>,

View File

@ -109,7 +109,7 @@ pub fn hierarchy_maintenance_systems() -> Vec<Box<dyn System>> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::build_systems; use crate::transform_systems;
use bevy_ecs::{Resources, Schedule, World}; use bevy_ecs::{Resources, Schedule, World};
#[test] #[test]
@ -119,7 +119,7 @@ mod test {
let mut schedule = Schedule::default(); let mut schedule = Schedule::default();
schedule.add_stage("update"); schedule.add_stage("update");
for system in build_systems() { for system in transform_systems() {
schedule.add_system_to_stage("update", system); schedule.add_system_to_stage("update", system);
} }

View File

@ -0,0 +1,9 @@
mod child_builder;
mod hierarchy;
mod hierarchy_maintenance_system;
mod world_child_builder;
pub use child_builder::*;
pub use hierarchy::*;
pub use hierarchy_maintenance_system::*;
pub use world_child_builder::*;

View File

@ -1,27 +1,45 @@
pub use glam as math; pub use glam as math;
pub mod child_builder; pub mod hierarchy;
pub mod components; pub mod components;
pub mod hierarchy_maintenance_system;
pub mod local_transform_systems; pub mod local_transform_systems;
pub mod transform_propagate_system; pub mod transform_propagate_system;
pub mod transform_systems; pub mod transform_systems;
pub mod world_child_builder;
pub mod prelude { pub mod prelude {
pub use crate::{build_systems, child_builder::*, components::*, world_child_builder::*}; pub use crate::{components::*, hierarchy::*, TransformPlugin};
} }
use bevy_app::{AppBuilder, AppPlugin};
use bevy_ecs::{IntoQuerySystem, System}; use bevy_ecs::{IntoQuerySystem, System};
use bevy_type_registry::RegisterType;
use prelude::{Children, LocalTransform, NonUniformScale, Rotation, Scale, Transform, Translation};
// TODO: make this a plugin pub(crate) fn transform_systems() -> Vec<Box<dyn System>> {
pub fn build_systems() -> Vec<Box<dyn System>> { let mut systems = Vec::with_capacity(5);
let mut all_systems = Vec::with_capacity(5);
all_systems.append(&mut hierarchy_maintenance_system::hierarchy_maintenance_systems()); systems.append(&mut hierarchy::hierarchy_maintenance_systems());
all_systems.append(&mut local_transform_systems::local_transform_systems()); systems.append(&mut local_transform_systems::local_transform_systems());
all_systems.append(&mut transform_systems::transform_systems()); systems.append(&mut transform_systems::transform_systems());
all_systems.push(transform_propagate_system::transform_propagate_system.system()); systems.push(transform_propagate_system::transform_propagate_system.system());
all_systems systems
}
#[derive(Default)]
pub struct TransformPlugin;
impl AppPlugin for TransformPlugin {
fn build(&self, app: &mut AppBuilder) {
app.register_component::<Children>()
.register_component::<LocalTransform>()
.register_component::<Transform>()
.register_component::<Translation>()
.register_component::<Rotation>()
.register_component::<Scale>()
.register_component::<NonUniformScale>()
// add transform systems to startup so the first update is "correct"
.add_startup_systems(transform_systems())
.add_systems(transform_systems());
}
} }

View File

@ -70,7 +70,7 @@ fn propagate_recursive(
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::build_systems; use crate::transform_systems;
use bevy_ecs::{Resources, Schedule, World}; use bevy_ecs::{Resources, Schedule, World};
use glam::{Mat4, Vec3}; use glam::{Mat4, Vec3};
@ -81,7 +81,7 @@ mod test {
let mut schedule = Schedule::default(); let mut schedule = Schedule::default();
schedule.add_stage("update"); schedule.add_stage("update");
for system in build_systems() { for system in transform_systems() {
schedule.add_system_to_stage("update", system); schedule.add_system_to_stage("update", system);
} }

View File

@ -8,7 +8,6 @@ edition = "2018"
bevy_app = { path = "../bevy_app" } bevy_app = { path = "../bevy_app" }
bevy_asset = { path = "../bevy_asset" } bevy_asset = { path = "../bevy_asset" }
bevy_type_registry = { path = "../bevy_type_registry" } bevy_type_registry = { path = "../bevy_type_registry" }
bevy_core = { path = "../bevy_core" }
bevy_derive = { path = "../bevy_derive" } bevy_derive = { path = "../bevy_derive" }
bevy_ecs = { path = "../bevy_ecs" } bevy_ecs = { path = "../bevy_ecs" }
bevy_sprite = { path = "../bevy_sprite" } bevy_sprite = { path = "../bevy_sprite" }

View File

@ -1,7 +1,9 @@
use super::Node; use super::Node;
use bevy_core::transform::run_on_hierarchy;
use bevy_ecs::{Entity, Query, Res, Without}; use bevy_ecs::{Entity, Query, Res, Without};
use bevy_transform::prelude::{Children, Parent, Translation}; use bevy_transform::{
hierarchy,
prelude::{Children, Parent, Translation},
};
use bevy_window::Windows; use bevy_window::Windows;
use glam::Vec2; use glam::Vec2;
@ -39,7 +41,7 @@ pub fn ui_update_system(
size: window_size, size: window_size,
}); });
for entity in orphan_nodes { for entity in orphan_nodes {
previous_sibling_result = run_on_hierarchy( previous_sibling_result = hierarchy::run_on_hierarchy(
&children_query, &children_query,
&mut node_query, &mut node_query,
entity, entity,

View File

@ -7,7 +7,7 @@ pub use crate::{
audio::{AudioOutput, AudioSource}, audio::{AudioOutput, AudioSource},
core::{ core::{
time::{Time, Timer}, time::{Time, Timer},
transform::FaceToward, math::FaceToward,
}, },
diagnostic::DiagnosticsPlugin, diagnostic::DiagnosticsPlugin,
ecs::{ ecs::{