mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 04:33:37 +00:00
fix parenting example, add missing transform components, add parenting to world builder
This commit is contained in:
parent
6ef1c099ff
commit
f6dd6a5ca9
3 changed files with 67 additions and 73 deletions
|
@ -10,12 +10,13 @@ fn main() {
|
|||
.run();
|
||||
}
|
||||
|
||||
// rotates the parent, which will result in the child also rotating
|
||||
fn build_rotator_system() -> Box<dyn Schedulable> {
|
||||
SystemBuilder::new("Rotator")
|
||||
.read_resource::<Time>()
|
||||
.with_query(<(Write<Rotator>, Write<Rotation>)>::query())
|
||||
.build(move |_, world, time, light_query| {
|
||||
for (_, mut rotation) in light_query.iter_mut(world) {
|
||||
.build(move |_, world, time, rotator_query| {
|
||||
for (_rotator, mut rotation) in rotator_query.iter_mut(world) {
|
||||
rotation.0 = rotation.0 * Quat::from_rotation_x(3.0 * time.delta_seconds);
|
||||
}
|
||||
})
|
||||
|
@ -23,86 +24,51 @@ fn build_rotator_system() -> Box<dyn Schedulable> {
|
|||
|
||||
fn setup(world: &mut World, resources: &mut Resources) {
|
||||
let cube = Mesh::load(MeshType::Cube);
|
||||
let plane = Mesh::load(MeshType::Plane { size: 10.0 });
|
||||
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
|
||||
let cube_handle = mesh_storage.add(cube);
|
||||
|
||||
let (cube_handle, plane_handle) = {
|
||||
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
|
||||
(mesh_storage.add(cube), mesh_storage.add(plane))
|
||||
};
|
||||
|
||||
// plane
|
||||
world.insert(
|
||||
(),
|
||||
vec![(
|
||||
plane_handle,
|
||||
StandardMaterial {
|
||||
albedo: math::vec4(0.1, 0.2, 0.1, 1.0).into(),
|
||||
world
|
||||
.build()
|
||||
// parent cube
|
||||
.add_archetype(MeshEntity {
|
||||
mesh: cube_handle,
|
||||
material: StandardMaterial {
|
||||
albedo: math::vec4(0.5, 0.4, 0.3, 1.0).into(),
|
||||
},
|
||||
LocalToWorld::identity(),
|
||||
Translation::new(0.0, 0.0, -5.0),
|
||||
)],
|
||||
);
|
||||
|
||||
// cube
|
||||
let parent_cube = *world
|
||||
.insert(
|
||||
(),
|
||||
vec![(
|
||||
cube_handle,
|
||||
StandardMaterial {
|
||||
albedo: math::vec4(0.5, 0.3, 0.3, 1.0).into(),
|
||||
},
|
||||
LocalToWorld::identity(),
|
||||
Translation::new(0.0, 0.0, 1.0),
|
||||
Rotation::from_euler_angles(0.0, 0.0, 0.0),
|
||||
Rotator,
|
||||
)],
|
||||
)
|
||||
.first()
|
||||
.unwrap();
|
||||
|
||||
// cube
|
||||
world.insert(
|
||||
(),
|
||||
vec![(
|
||||
cube_handle,
|
||||
StandardMaterial {
|
||||
albedo: math::vec4(0.5, 0.3, 0.3, 1.0).into(),
|
||||
translation: Translation::new(0.0, 0.0, 1.0),
|
||||
..Default::default()
|
||||
})
|
||||
.add(Rotator)
|
||||
// cube
|
||||
.add_archetype(MeshEntity {
|
||||
mesh: cube_handle,
|
||||
material: StandardMaterial {
|
||||
albedo: math::vec4(0.5, 0.4, 0.3, 1.0).into(),
|
||||
},
|
||||
LocalToWorld::identity(),
|
||||
Translation::new(0.0, 0.0, 3.0),
|
||||
Parent(parent_cube),
|
||||
LocalToParent::identity(),
|
||||
)],
|
||||
);
|
||||
|
||||
// light
|
||||
world.insert(
|
||||
(),
|
||||
vec![(
|
||||
Light::default(),
|
||||
LocalToWorld::identity(),
|
||||
Translation::new(4.0, -4.0, 5.0),
|
||||
Rotation::from_euler_angles(0.0, 0.0, 0.0),
|
||||
)],
|
||||
);
|
||||
|
||||
// camera
|
||||
world.insert(
|
||||
(),
|
||||
vec![(
|
||||
Camera::new(CameraType::Projection {
|
||||
translation: Translation::new(0.0, 0.0, 3.0),
|
||||
..Default::default()
|
||||
})
|
||||
.set_last_entity_as_parent()
|
||||
// light
|
||||
.add_archetype(LightEntity {
|
||||
translation: Translation::new(4.0, -4.0, 5.0),
|
||||
rotation: Rotation::from_euler_angles(0.0, 0.0, 0.0),
|
||||
..Default::default()
|
||||
})
|
||||
// camera
|
||||
.add_archetype(CameraEntity {
|
||||
camera: Camera::new(CameraType::Projection {
|
||||
fov: std::f32::consts::PI / 4.0,
|
||||
near: 1.0,
|
||||
far: 1000.0,
|
||||
aspect_ratio: 1.0,
|
||||
}),
|
||||
ActiveCamera,
|
||||
LocalToWorld(Mat4::look_at_rh(
|
||||
Vec3::new(3.0, -15.0, 8.0),
|
||||
active_camera: ActiveCamera,
|
||||
local_to_world: LocalToWorld(Mat4::look_at_rh(
|
||||
Vec3::new(5.0, 10.0, 10.0),
|
||||
Vec3::new(0.0, 0.0, 0.0),
|
||||
Vec3::new(0.0, 0.0, 1.0),
|
||||
)),
|
||||
)],
|
||||
);
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
|
|
@ -13,6 +13,8 @@ pub struct MeshEntity {
|
|||
pub renderable: Renderable,
|
||||
pub local_to_world: LocalToWorld,
|
||||
pub translation: Translation,
|
||||
pub rotation: Rotation,
|
||||
pub scale: Scale,
|
||||
}
|
||||
|
||||
#[derive(EntityArchetype, Default)]
|
||||
|
@ -22,6 +24,8 @@ pub struct MeshMaterialEntity<T: Default + Send + Sync + 'static> {
|
|||
pub renderable: Renderable,
|
||||
pub local_to_world: LocalToWorld,
|
||||
pub translation: Translation,
|
||||
pub rotation: Rotation,
|
||||
pub scale: Scale,
|
||||
}
|
||||
|
||||
#[derive(EntityArchetype, Default)]
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use crate::ecs::EntityArchetype;
|
||||
use bevy_transform::components::{LocalToParent, Parent};
|
||||
use legion::{
|
||||
filter::{ChunksetFilterData, Filter},
|
||||
prelude::*,
|
||||
|
@ -14,6 +15,7 @@ impl WorldBuilderSource for World {
|
|||
WorldBuilder {
|
||||
world: self,
|
||||
current_entity: None,
|
||||
last_entity: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,11 +23,16 @@ impl WorldBuilderSource for World {
|
|||
pub struct WorldBuilder<'a> {
|
||||
world: &'a mut World,
|
||||
current_entity: Option<Entity>,
|
||||
last_entity: Option<Entity>,
|
||||
}
|
||||
|
||||
impl<'a> WorldBuilder<'a> {
|
||||
pub fn build_entity(mut self) -> Self {
|
||||
let entity = *self.world.insert((), vec![()]).first().unwrap();
|
||||
if let Some(last_entity) = self.current_entity.take() {
|
||||
self.last_entity = Some(last_entity);
|
||||
}
|
||||
|
||||
self.current_entity = Some(entity);
|
||||
self
|
||||
}
|
||||
|
@ -62,7 +69,24 @@ impl<'a> WorldBuilder<'a> {
|
|||
}
|
||||
|
||||
pub fn add_archetype(mut self, entity_archetype: impl EntityArchetype) -> Self {
|
||||
if let Some(last_entity) = self.current_entity.take() {
|
||||
self.last_entity = Some(last_entity);
|
||||
}
|
||||
|
||||
self.current_entity = Some(entity_archetype.insert(self.world));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_last_entity_as_parent(self) -> Self {
|
||||
let current_entity = self.current_entity.unwrap();
|
||||
let _ = self.world.add_component(
|
||||
current_entity,
|
||||
Parent(self.last_entity.unwrap()),
|
||||
);
|
||||
let _ = self
|
||||
.world
|
||||
.add_component(current_entity, LocalToParent::identity());
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue