From 92c44320eedf512850676f7e2d27814a7df864a9 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Thu, 25 Jun 2020 11:21:56 -0700 Subject: [PATCH] ecs: rename EntityArchetype to ComponentSet --- .../{entity_archetype.rs => component_set.rs} | 4 +- crates/bevy_app/src/lib.rs | 6 +-- .../bevy_core/src/transform/world_builder.rs | 49 ++++++------------- .../{entity_archetype.rs => component_set.rs} | 4 +- crates/bevy_derive/src/lib.rs | 8 +-- crates/bevy_pbr/src/entity.rs | 12 ++--- crates/bevy_render/src/entity.rs | 26 +++++----- crates/bevy_sprite/src/entity.rs | 14 +++--- crates/bevy_ui/src/entity.rs | 18 +++---- examples/2d/sprite.rs | 4 +- examples/2d/sprite_sheet.rs | 6 +-- examples/2d/texture_atlas.rs | 6 +-- examples/3d/3d_scene.rs | 8 +-- examples/3d/load_model.rs | 6 +-- examples/3d/parenting.rs | 12 ++--- examples/3d/spawner.rs | 10 ++-- examples/3d/texture.rs | 8 +-- examples/3d/z_sort_debug.rs | 12 ++--- .../example_plugin/src/lib.rs | 6 +-- examples/asset/asset_loading.rs | 10 ++-- examples/asset/hot_asset_reloading.rs | 6 +-- examples/scene/load_scene.rs | 10 ++-- examples/shader/shader_custom_material.rs | 4 +- examples/shader/shader_defs.rs | 6 +-- examples/ui/font_atlas_debug.rs | 8 +-- examples/ui/text.rs | 4 +- examples/ui/ui.rs | 28 +++++------ examples/ui/ui_bench.rs | 4 +- src/prelude.rs | 4 +- 29 files changed, 141 insertions(+), 162 deletions(-) rename crates/bevy_app/src/{entity_archetype.rs => component_set.rs} (82%) rename crates/bevy_derive/src/{entity_archetype.rs => component_set.rs} (90%) diff --git a/crates/bevy_app/src/entity_archetype.rs b/crates/bevy_app/src/component_set.rs similarity index 82% rename from crates/bevy_app/src/entity_archetype.rs rename to crates/bevy_app/src/component_set.rs index 15a8e34756..466d1a3457 100644 --- a/crates/bevy_app/src/entity_archetype.rs +++ b/crates/bevy_app/src/component_set.rs @@ -1,11 +1,11 @@ use legion::prelude::*; // builder macro that makes defaults easy? Object3dBuilder { Option } impl Builder for Object3dBuilder { } -pub trait EntityArchetype { +pub trait ComponentSet { fn insert(self, world: &mut World) -> Entity; fn insert_command_buffer(self, command_buffer: &mut CommandBuffer) -> Entity; - // this would make composing entities from multiple archetypes possible + // this would make composing entities from multiple sets possible // add_components appears to be missing from World. it will be less efficient without that // fn add_components(self, world: &mut World); diff --git a/crates/bevy_app/src/lib.rs b/crates/bevy_app/src/lib.rs index dae36d2310..cf9847ac1d 100644 --- a/crates/bevy_app/src/lib.rs +++ b/crates/bevy_app/src/lib.rs @@ -1,6 +1,6 @@ mod app; mod app_builder; -mod entity_archetype; +mod component_set; mod event; mod plugin; mod resources; @@ -12,8 +12,8 @@ mod system; pub use app::*; pub use app_builder::*; -pub use bevy_derive::{DynamicAppPlugin, EntityArchetype, FromResources}; -pub use entity_archetype::*; +pub use bevy_derive::{DynamicAppPlugin, ComponentSet, FromResources}; +pub use component_set::*; pub use event::*; pub use plugin::*; pub use resources::*; diff --git a/crates/bevy_core/src/transform/world_builder.rs b/crates/bevy_core/src/transform/world_builder.rs index 1e97f8e871..3af92b4e24 100644 --- a/crates/bevy_core/src/transform/world_builder.rs +++ b/crates/bevy_core/src/transform/world_builder.rs @@ -1,4 +1,4 @@ -use bevy_app::EntityArchetype; +use bevy_app::ComponentSet; use bevy_transform::components::{LocalTransform, Parent}; use legion::{ filter::{ChunksetFilterData, Filter}, @@ -27,7 +27,7 @@ pub struct WorldBuilder<'a> { } impl<'a> WorldBuilder<'a> { - pub fn build_entity(&mut self) -> &mut Self { + pub fn entity(&mut self) -> &mut Self { let entity = *self.world.insert((), vec![()]).first().unwrap(); self.current_entity = Some(entity); self.add_parent_to_current_entity(); @@ -35,7 +35,7 @@ impl<'a> WorldBuilder<'a> { } /// note: this is slow and does a full entity copy - pub fn add(&mut self, component: T) -> &mut Self + pub fn with(&mut self, component: T) -> &mut Self where T: legion::storage::Component, { @@ -45,33 +45,22 @@ impl<'a> WorldBuilder<'a> { self } - pub fn tag(&mut self, tag: T) -> &mut Self + pub fn entities(&mut self, components: C) -> &mut Self where - T: legion::storage::Tag, - { - let _ = self - .world - .add_tag(*self.current_entity.as_ref().unwrap(), tag); - self - } - - pub fn add_entities(&mut self, tags: T, components: C) -> &mut Self - where - T: TagSet + TagLayout + for<'b> Filter>, C: IntoComponentSource, { - self.world.insert(tags, components); + self.world.insert((), components); self } - pub fn add_entity(&mut self, entity_archetype: impl EntityArchetype) -> &mut Self { - let current_entity = entity_archetype.insert(self.world); + pub fn entity_with(&mut self, component_set: impl ComponentSet) -> &mut Self { + let current_entity = component_set.insert(self.world); self.current_entity = Some(current_entity); self.add_parent_to_current_entity(); self } - pub fn add_children(&mut self, mut build_children: impl FnMut(&mut Self) -> &mut Self) -> &mut Self { + pub fn with_children(&mut self, mut build_children: impl FnMut(&mut Self) -> &mut Self) -> &mut Self { self.parent_entity = self.current_entity; self.current_entity = None; @@ -116,7 +105,7 @@ pub struct CommandBufferBuilder<'a> { } impl<'a> CommandBufferBuilder<'a> { - pub fn build_entity(&mut self) -> &mut Self { + pub fn entity(&mut self) -> &mut Self { let entity = *self.command_buffer.insert((), vec![()]).first().unwrap(); self.current_entity = Some(entity); self.add_parent_to_current_entity(); @@ -124,7 +113,7 @@ impl<'a> CommandBufferBuilder<'a> { } // note: this is slow and does a full entity copy - pub fn add(&mut self, component: T) -> &mut Self + pub fn with(&mut self, component: T) -> &mut Self where T: legion::storage::Component, { @@ -134,17 +123,7 @@ impl<'a> CommandBufferBuilder<'a> { self } - pub fn tag(&mut self, tag: T) -> &mut Self - where - T: legion::storage::Tag, - { - let _ = self - .command_buffer - .add_tag(*self.current_entity.as_ref().unwrap(), tag); - self - } - - pub fn add_entities(&mut self, tags: T, components: C) -> &mut Self + pub fn entities(&mut self, tags: T, components: C) -> &mut Self where T: TagSet + TagLayout + for<'b> Filter> + 'static, C: IntoComponentSource + 'static, @@ -153,14 +132,14 @@ impl<'a> CommandBufferBuilder<'a> { self } - pub fn add_entity(&mut self, entity_archetype: impl EntityArchetype) -> &mut Self { - let current_entity = entity_archetype.insert_command_buffer(self.command_buffer); + pub fn entity_with(&mut self, component_set: impl ComponentSet) -> &mut Self { + let current_entity = component_set.insert_command_buffer(self.command_buffer); self.current_entity = Some(current_entity); self.add_parent_to_current_entity(); self } - pub fn add_children(&mut self, mut build_children: impl FnMut(&mut Self) -> &mut Self) -> &mut Self { + pub fn with_children(&mut self, mut build_children: impl FnMut(&mut Self) -> &mut Self) -> &mut Self { self.parent_entity = self.current_entity; self.current_entity = None; diff --git a/crates/bevy_derive/src/entity_archetype.rs b/crates/bevy_derive/src/component_set.rs similarity index 90% rename from crates/bevy_derive/src/entity_archetype.rs rename to crates/bevy_derive/src/component_set.rs index a114459fdb..6ea60ebfd8 100644 --- a/crates/bevy_derive/src/entity_archetype.rs +++ b/crates/bevy_derive/src/component_set.rs @@ -3,7 +3,7 @@ use proc_macro::TokenStream; use quote::quote; use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Fields, Ident}; -pub fn derive_entity_archetype(input: TokenStream) -> TokenStream { +pub fn derive_component_set(input: TokenStream) -> TokenStream { let ast = parse_macro_input!(input as DeriveInput); let fields = match &ast.data { Data::Struct(DataStruct { @@ -28,7 +28,7 @@ pub fn derive_entity_archetype(input: TokenStream) -> TokenStream { let struct_name = &ast.ident; TokenStream::from(quote! { - impl #impl_generics #bevy_app_path::EntityArchetype for #struct_name#ty_generics { + impl #impl_generics #bevy_app_path::ComponentSet for #struct_name#ty_generics { fn insert(self, world: &mut #legion_path::prelude::World) -> #legion_path::prelude::Entity { *world.insert((), vec![( diff --git a/crates/bevy_derive/src/lib.rs b/crates/bevy_derive/src/lib.rs index b8f860bc69..36f39f9f8e 100644 --- a/crates/bevy_derive/src/lib.rs +++ b/crates/bevy_derive/src/lib.rs @@ -4,7 +4,7 @@ mod app_plugin; mod as_vertex_buffer_descriptor; mod attributes; mod bytes; -mod entity_archetype; +mod component_set; mod modules; mod render_resource; mod render_resources; @@ -43,9 +43,9 @@ pub fn derive_as_vertex_buffer_descriptor(input: TokenStream) -> TokenStream { as_vertex_buffer_descriptor::derive_as_vertex_buffer_descriptor(input) } -#[proc_macro_derive(EntityArchetype, attributes(tag, module))] -pub fn derive_entity_archetype(input: TokenStream) -> TokenStream { - entity_archetype::derive_entity_archetype(input) +#[proc_macro_derive(ComponentSet, attributes(tag, module))] +pub fn derive_component_set(input: TokenStream) -> TokenStream { + component_set::derive_component_set(input) } #[proc_macro_derive(DynamicAppPlugin)] diff --git a/crates/bevy_pbr/src/entity.rs b/crates/bevy_pbr/src/entity.rs index 6f2c8fedb8..dc0d70f677 100644 --- a/crates/bevy_pbr/src/entity.rs +++ b/crates/bevy_pbr/src/entity.rs @@ -1,6 +1,6 @@ use crate::{light::Light, material::StandardMaterial, pipelines::FORWARD_PIPELINE_HANDLE}; use bevy_asset::Handle; -use bevy_derive::EntityArchetype; +use bevy_derive::ComponentSet; use bevy_render::{ draw::Draw, mesh::Mesh, @@ -8,8 +8,8 @@ use bevy_render::{ }; use bevy_transform::prelude::{Rotation, Scale, Transform, Translation}; -#[derive(EntityArchetype)] -pub struct MeshEntity { +#[derive(ComponentSet)] +pub struct MeshComponents { pub mesh: Handle, pub material: Handle, pub draw: Draw, @@ -20,7 +20,7 @@ pub struct MeshEntity { pub scale: Scale, } -impl Default for MeshEntity { +impl Default for MeshComponents { fn default() -> Self { Self { render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized( @@ -52,8 +52,8 @@ impl Default for MeshEntity { } } -#[derive(EntityArchetype, Default)] -pub struct LightEntity { +#[derive(ComponentSet, Default)] +pub struct LightComponents { pub light: Light, pub transform: Transform, pub translation: Translation, diff --git a/crates/bevy_render/src/entity.rs b/crates/bevy_render/src/entity.rs index f5ce3bd23a..59de3ce8b7 100644 --- a/crates/bevy_render/src/entity.rs +++ b/crates/bevy_render/src/entity.rs @@ -3,11 +3,11 @@ use crate::{ OrthographicProjection, PerspectiveProjection, WindowOrigin, VisibleEntities, }; use bevy_asset::Handle; -use bevy_derive::EntityArchetype; +use bevy_derive::ComponentSet; use bevy_transform::components::{Rotation, Scale, Transform, Translation}; -#[derive(EntityArchetype, Default)] -pub struct MeshMaterialEntity { +#[derive(ComponentSet, Default)] +pub struct MeshMaterialComponents { pub mesh: Handle, pub material: Handle, pub draw: Draw, @@ -18,8 +18,8 @@ pub struct MeshMaterialEntity { pub scale: Scale, } -#[derive(EntityArchetype)] -pub struct PerspectiveCameraEntity { +#[derive(ComponentSet)] +pub struct PerspectiveCameraComponents { pub camera: Camera, pub perspective_projection: PerspectiveProjection, pub visible_entities: VisibleEntities, @@ -29,9 +29,9 @@ pub struct PerspectiveCameraEntity { pub scale: Scale, } -impl Default for PerspectiveCameraEntity { +impl Default for PerspectiveCameraComponents { fn default() -> Self { - PerspectiveCameraEntity { + PerspectiveCameraComponents { camera: Camera { name: Some(base_render_graph::camera::CAMERA.to_string()), ..Default::default() @@ -46,8 +46,8 @@ impl Default for PerspectiveCameraEntity { } } -#[derive(EntityArchetype)] -pub struct OrthographicCameraEntity { +#[derive(ComponentSet)] +pub struct OrthographicCameraComponents { pub camera: Camera, pub orthographic_projection: OrthographicProjection, pub visible_entities: VisibleEntities, @@ -57,9 +57,9 @@ pub struct OrthographicCameraEntity { pub scale: Scale, } -impl OrthographicCameraEntity { +impl OrthographicCameraComponents { pub fn ui() -> Self { - OrthographicCameraEntity { + OrthographicCameraComponents { camera: Camera { name: Some("UiCamera".to_string()), ..Default::default() @@ -77,9 +77,9 @@ impl OrthographicCameraEntity { } } -impl Default for OrthographicCameraEntity { +impl Default for OrthographicCameraComponents { fn default() -> Self { - OrthographicCameraEntity { + OrthographicCameraComponents { camera: Camera { name: Some(base_render_graph::camera::CAMERA2D.to_string()), ..Default::default() diff --git a/crates/bevy_sprite/src/entity.rs b/crates/bevy_sprite/src/entity.rs index b021fae8aa..563d7099a3 100644 --- a/crates/bevy_sprite/src/entity.rs +++ b/crates/bevy_sprite/src/entity.rs @@ -2,7 +2,7 @@ use crate::{ render::SPRITE_PIPELINE_HANDLE, sprite::Sprite, ColorMaterial, TextureAtlas, TextureAtlasSprite, QUAD_HANDLE, SPRITE_SHEET_PIPELINE_HANDLE, }; -use bevy_app::EntityArchetype; +use bevy_app::ComponentSet; use bevy_asset::Handle; use bevy_render::{ draw::Draw, @@ -11,8 +11,8 @@ use bevy_render::{ }; use bevy_transform::prelude::{Rotation, Scale, Transform, Translation}; -#[derive(EntityArchetype)] -pub struct SpriteEntity { +#[derive(ComponentSet)] +pub struct SpriteComponents { pub sprite: Sprite, pub mesh: Handle, // TODO: maybe abstract this out pub material: Handle, @@ -24,7 +24,7 @@ pub struct SpriteEntity { pub scale: Scale, } -impl Default for SpriteEntity { +impl Default for SpriteComponents { fn default() -> Self { Self { mesh: QUAD_HANDLE, @@ -60,8 +60,8 @@ impl Default for SpriteEntity { } } -#[derive(EntityArchetype)] -pub struct SpriteSheetEntity { +#[derive(ComponentSet)] +pub struct SpriteSheetComponents { pub sprite: TextureAtlasSprite, pub texture_atlas: Handle, pub draw: Draw, @@ -73,7 +73,7 @@ pub struct SpriteSheetEntity { pub scale: Scale, } -impl Default for SpriteSheetEntity { +impl Default for SpriteSheetComponents { fn default() -> Self { Self { render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized( diff --git a/crates/bevy_ui/src/entity.rs b/crates/bevy_ui/src/entity.rs index 384fb4003f..b02fe8ca6f 100644 --- a/crates/bevy_ui/src/entity.rs +++ b/crates/bevy_ui/src/entity.rs @@ -1,13 +1,13 @@ use super::Node; use crate::{render::UI_PIPELINE_HANDLE, widget::Label}; use bevy_asset::Handle; -use bevy_derive::EntityArchetype; +use bevy_derive::ComponentSet; use bevy_render::{draw::Draw, mesh::Mesh, pipeline::{PipelineSpecialization, RenderPipelines, DynamicBinding, RenderPipeline}}; use bevy_sprite::{ColorMaterial, QUAD_HANDLE}; use bevy_transform::prelude::{Translation, Transform, Rotation, Scale}; -#[derive(EntityArchetype)] -pub struct UiEntity { +#[derive(ComponentSet)] +pub struct UiComponents { pub node: Node, pub mesh: Handle, // TODO: maybe abstract this out pub material: Handle, @@ -19,9 +19,9 @@ pub struct UiEntity { pub scale: Scale, } -impl Default for UiEntity { +impl Default for UiComponents { fn default() -> Self { - UiEntity { + UiComponents { mesh: QUAD_HANDLE, render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized( UI_PIPELINE_HANDLE, @@ -52,8 +52,8 @@ impl Default for UiEntity { } } -#[derive(EntityArchetype)] -pub struct LabelEntity { +#[derive(ComponentSet)] +pub struct LabelComponents { pub node: Node, pub draw: Draw, pub label: Label, @@ -63,9 +63,9 @@ pub struct LabelEntity { pub scale: Scale, } -impl Default for LabelEntity { +impl Default for LabelComponents { fn default() -> Self { - LabelEntity { + LabelComponents { label: Label::default(), node: Default::default(), draw: Draw { diff --git a/examples/2d/sprite.rs b/examples/2d/sprite.rs index 7ec836092e..7842c09c8e 100644 --- a/examples/2d/sprite.rs +++ b/examples/2d/sprite.rs @@ -15,8 +15,8 @@ fn setup( let texture_handle = asset_server.load("assets/branding/icon.png").unwrap(); command_buffer .build() - .add_entity(OrthographicCameraEntity::default()) - .add_entity(SpriteEntity { + .entity_with(OrthographicCameraComponents::default()) + .entity_with(SpriteComponents { material: materials.add(texture_handle.into()), ..Default::default() }); diff --git a/examples/2d/sprite_sheet.rs b/examples/2d/sprite_sheet.rs index 9d42103f89..17526f733a 100644 --- a/examples/2d/sprite_sheet.rs +++ b/examples/2d/sprite_sheet.rs @@ -38,11 +38,11 @@ fn setup( let texture_atlas_handle = texture_atlases.add(texture_atlas); command_buffer .build() - .add_entity(OrthographicCameraEntity::default()) - .add_entity(SpriteSheetEntity { + .entity_with(OrthographicCameraComponents::default()) + .entity_with(SpriteSheetComponents { texture_atlas: texture_atlas_handle, scale: Scale(6.0), ..Default::default() }) - .add(Timer::from_seconds(0.1)); + .with(Timer::from_seconds(0.1)); } diff --git a/examples/2d/texture_atlas.rs b/examples/2d/texture_atlas.rs index 5163dfd0ff..e585521713 100644 --- a/examples/2d/texture_atlas.rs +++ b/examples/2d/texture_atlas.rs @@ -27,7 +27,7 @@ fn setup( .unwrap(); command_buffer .build() - .add_entity(OrthographicCameraEntity::default()); + .entity_with(OrthographicCameraComponents::default()); } #[derive(Default)] @@ -68,7 +68,7 @@ fn load_atlas( command_buffer .build() // draw a sprite from the atlas - .add_entity(SpriteSheetEntity { + .entity_with(SpriteSheetComponents { scale: Scale(4.0), translation: Translation(Vec3::new(150.0, 0.0, 0.0)), sprite: TextureAtlasSprite::new(vendor_index as u32), @@ -76,7 +76,7 @@ fn load_atlas( ..Default::default() }) // draw the atlas itself - .add_entity(SpriteEntity { + .entity_with(SpriteComponents { material: materials.add(texture_atlas_texture.into()), translation: Vec3::new(-300.0, 0., 0.0).into(), ..Default::default() diff --git a/examples/3d/3d_scene.rs b/examples/3d/3d_scene.rs index 5e25fbb173..eb66de1de2 100644 --- a/examples/3d/3d_scene.rs +++ b/examples/3d/3d_scene.rs @@ -31,25 +31,25 @@ fn setup( command_buffer .build() // plane - .add_entity(MeshEntity { + .entity_with(MeshComponents { mesh: plane_handle, material: plane_material_handle, ..Default::default() }) // cube - .add_entity(MeshEntity { + .entity_with(MeshComponents { mesh: cube_handle, material: cube_material_handle, translation: Translation::new(0.0, 1.0, 0.0), ..Default::default() }) // light - .add_entity(LightEntity { + .entity_with(LightComponents { translation: Translation::new(4.0, 5.0, -4.0), ..Default::default() }) // camera - .add_entity(PerspectiveCameraEntity { + .entity_with(PerspectiveCameraComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(3.0, 5.0, 8.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/3d/load_model.rs b/examples/3d/load_model.rs index c48e313858..73fe03435f 100644 --- a/examples/3d/load_model.rs +++ b/examples/3d/load_model.rs @@ -27,18 +27,18 @@ fn setup( command_buffer .build() // mesh - .add_entity(MeshEntity { + .entity_with(MeshComponents { mesh: mesh_handle, material: material_handle, ..Default::default() }) // light - .add_entity(LightEntity { + .entity_with(LightComponents { translation: Translation::new(4.0, 5.0, 4.0), ..Default::default() }) // camera - .add_entity(PerspectiveCameraEntity { + .entity_with(PerspectiveCameraComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(-2.0, 2.0, 6.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/3d/parenting.rs b/examples/3d/parenting.rs index e421f5b6eb..15604d7545 100644 --- a/examples/3d/parenting.rs +++ b/examples/3d/parenting.rs @@ -30,16 +30,16 @@ fn setup( command_buffer .build() // parent cube - .add_entity(MeshEntity { + .entity_with(MeshComponents { mesh: cube_handle, material: cube_material_handle, translation: Translation::new(0.0, 0.0, 1.0), ..Default::default() }) - .add(Rotator) - .add_children(|builder| { + .with(Rotator) + .with_children(|builder| { // child cube - builder.add_entity(MeshEntity { + builder.entity_with(MeshComponents { mesh: cube_handle, material: cube_material_handle, translation: Translation::new(0.0, 0.0, 3.0), @@ -47,12 +47,12 @@ fn setup( }) }) // light - .add_entity(LightEntity { + .entity_with(LightComponents { translation: Translation::new(4.0, 5.0, -4.0), ..Default::default() }) // camera - .add_entity(PerspectiveCameraEntity { + .entity_with(PerspectiveCameraComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(5.0, 10.0, 10.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/3d/spawner.rs b/examples/3d/spawner.rs index 0b83a4ca57..bbc5f96b90 100644 --- a/examples/3d/spawner.rs +++ b/examples/3d/spawner.rs @@ -44,25 +44,25 @@ fn setup( let mut builder = command_buffer.build(); builder // plane - .add_entity(MeshEntity { + .entity_with(MeshComponents { mesh: plane_handle, material: plane_material_handle, ..Default::default() }) // cube - .add_entity(MeshEntity { + .entity_with(MeshComponents { mesh: cube_handle, material: cube_material_handle, translation: Translation::new(0.0, 0.0, 1.0), ..Default::default() }) // light - .add_entity(LightEntity { + .entity_with(LightComponents { translation: Translation::new(4.0, -4.0, 5.0), ..Default::default() }) // camera - .add_entity(PerspectiveCameraEntity { + .entity_with(PerspectiveCameraComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(3.0, 5.0, -8.0), Vec3::new(0.0, 0.0, 0.0), @@ -81,7 +81,7 @@ fn setup( ), ..Default::default() }); - builder.add_entity(MeshEntity { + builder.entity_with(MeshComponents { mesh: cube_handle, material: spawned_material_handle, translation: Translation::new( diff --git a/examples/3d/texture.rs b/examples/3d/texture.rs index 8095ec6393..d437c19ef2 100644 --- a/examples/3d/texture.rs +++ b/examples/3d/texture.rs @@ -56,7 +56,7 @@ fn setup( command_buffer .build() // textured quad - normal - .add_entity(MeshEntity { + .entity_with(MeshComponents { mesh: quad_handle, material: material_handle, translation: Translation::new(0.0, 0.0, -1.5), @@ -68,7 +68,7 @@ fn setup( ..Default::default() }) // textured quad - modulated - .add_entity(MeshEntity { + .entity_with(MeshComponents { mesh: quad_handle, material: red_material_handle, translation: Translation::new(0.0, 0.0, 0.0), @@ -80,7 +80,7 @@ fn setup( ..Default::default() }) // textured quad - modulated - .add_entity(MeshEntity { + .entity_with(MeshComponents { mesh: quad_handle, material: blue_material_handle, translation: Translation::new(0.0, 0.0, 1.5), @@ -92,7 +92,7 @@ fn setup( ..Default::default() }) // camera - .add_entity(PerspectiveCameraEntity { + .entity_with(PerspectiveCameraComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(3.0, 5.0, -8.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/3d/z_sort_debug.rs b/examples/3d/z_sort_debug.rs index e1aea82bd1..b6fc2e70e2 100644 --- a/examples/3d/z_sort_debug.rs +++ b/examples/3d/z_sort_debug.rs @@ -45,7 +45,7 @@ fn setup( command_buffer .build() // parent cube - .add_entity(MeshEntity { + .entity_with(MeshComponents { mesh: cube_handle, material: materials.add(StandardMaterial { shaded: false, @@ -54,11 +54,11 @@ fn setup( translation: Translation::new(0.0, 0.0, 1.0), ..Default::default() }) - .add(Rotator) - .add_children(|builder| { + .with(Rotator) + .with_children(|builder| { // child cubes builder - .add_entity(MeshEntity { + .entity_with(MeshComponents { mesh: cube_handle, material: materials.add(StandardMaterial { shaded: false, @@ -67,7 +67,7 @@ fn setup( translation: Translation::new(0.0, 3.0, 0.0), ..Default::default() }) - .add_entity(MeshEntity { + .entity_with(MeshComponents { mesh: cube_handle, material: materials.add(StandardMaterial { shaded: false, @@ -78,7 +78,7 @@ fn setup( }) }) // camera - .add_entity(PerspectiveCameraEntity { + .entity_with(PerspectiveCameraComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(5.0, 10.0, 10.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/app/dynamic_plugin_loading/example_plugin/src/lib.rs b/examples/app/dynamic_plugin_loading/example_plugin/src/lib.rs index 40aed7b331..4d989b76d8 100644 --- a/examples/app/dynamic_plugin_loading/example_plugin/src/lib.rs +++ b/examples/app/dynamic_plugin_loading/example_plugin/src/lib.rs @@ -23,18 +23,18 @@ fn setup( command_buffer .build() // cube - .add_entity(MeshEntity { + .entity_with(MeshComponents { mesh: cube_handle, material: cube_material_handle, ..Default::default() }) // light - .add_entity(LightEntity { + .entity_with(LightComponents { translation: Translation::new(4.0, 5.0, 4.0), ..Default::default() }) // camera - .add_entity(PerspectiveCameraEntity { + .entity_with(PerspectiveCameraComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(3.0, 5.0, 8.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/asset/asset_loading.rs b/examples/asset/asset_loading.rs index 8785e9118a..9b80af0930 100644 --- a/examples/asset/asset_loading.rs +++ b/examples/asset/asset_loading.rs @@ -45,33 +45,33 @@ fn setup( command_buffer .build() // monkey - .add_entity(MeshEntity { + .entity_with(MeshComponents { mesh: monkey_handle, material: material_handle, translation: Translation::new(-3.0, 0.0, 0.0), ..Default::default() }) // cube - .add_entity(MeshEntity { + .entity_with(MeshComponents { mesh: cube_handle, material: material_handle, translation: Translation::new(0.0, 0.0, 0.0), ..Default::default() }) // sphere - .add_entity(MeshEntity { + .entity_with(MeshComponents { mesh: sphere_handle, material: material_handle, translation: Translation::new(3.0, 0.0, 0.0), ..Default::default() }) // light - .add_entity(LightEntity { + .entity_with(LightComponents { translation: Translation::new(4.0, 5.0, 4.0), ..Default::default() }) // camera - .add_entity(PerspectiveCameraEntity { + .entity_with(PerspectiveCameraComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(0.0, 3.0, 10.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/asset/hot_asset_reloading.rs b/examples/asset/hot_asset_reloading.rs index d4ff9b8d4f..079087bedd 100644 --- a/examples/asset/hot_asset_reloading.rs +++ b/examples/asset/hot_asset_reloading.rs @@ -35,18 +35,18 @@ fn setup( command_buffer .build() // mesh - .add_entity(MeshEntity { + .entity_with(MeshComponents { mesh: mesh_handle, material: material_handle, ..Default::default() }) // light - .add_entity(LightEntity { + .entity_with(LightComponents { translation: Translation::new(4.0, 5.0, 4.0), ..Default::default() }) // camera - .add_entity(PerspectiveCameraEntity { + .entity_with(PerspectiveCameraComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(2.0, 2.0, 6.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/scene/load_scene.rs b/examples/scene/load_scene.rs index 7c0684856c..209bbb1fd1 100644 --- a/examples/scene/load_scene.rs +++ b/examples/scene/load_scene.rs @@ -107,14 +107,14 @@ fn save_scene_system(_world: &mut World, resources: &mut Resources) { let mut world = World::new(); world .build() - .build_entity() - .add(ComponentA { x: 1.0, y: 2.0 }) - .add(ComponentB { + .entity() + .with(ComponentA { x: 1.0, y: 2.0 }) + .with(ComponentB { value: "hello".to_string(), ..ComponentB::from_resources(resources) }) - .build_entity() - .add(ComponentA { x: 3.0, y: 4.0 }); + .entity() + .with(ComponentA { x: 3.0, y: 4.0 }); // The component registry resource contains information about all registered components. This is used to construct scenes. let type_registry = resources.get::().unwrap(); diff --git a/examples/shader/shader_custom_material.rs b/examples/shader/shader_custom_material.rs index e6f1879c02..70547d48e0 100644 --- a/examples/shader/shader_custom_material.rs +++ b/examples/shader/shader_custom_material.rs @@ -76,7 +76,7 @@ fn setup( command_buffer .build() // cube - .add_entity(MeshMaterialEntity:: { + .entity_with(MeshMaterialComponents:: { mesh: cube_handle, render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized( pipeline_handle, @@ -102,7 +102,7 @@ fn setup( ..Default::default() }) // camera - .add_entity(PerspectiveCameraEntity { + .entity_with(PerspectiveCameraComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(3.0, 5.0, -8.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/shader/shader_defs.rs b/examples/shader/shader_defs.rs index e79f6331a0..09574539b3 100644 --- a/examples/shader/shader_defs.rs +++ b/examples/shader/shader_defs.rs @@ -93,7 +93,7 @@ fn setup( command_buffer .build() // cube - .add_entity(MeshMaterialEntity:: { + .entity_with(MeshMaterialComponents:: { mesh: cube_handle, render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized( pipeline_handle, @@ -119,7 +119,7 @@ fn setup( ..Default::default() }) // cube - .add_entity(MeshMaterialEntity:: { + .entity_with(MeshMaterialComponents:: { mesh: cube_handle, render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized( pipeline_handle, @@ -145,7 +145,7 @@ fn setup( ..Default::default() }) // camera - .add_entity(PerspectiveCameraEntity { + .entity_with(PerspectiveCameraComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(3.0, 5.0, -8.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/ui/font_atlas_debug.rs b/examples/ui/font_atlas_debug.rs index 22afae0034..93e3b885c4 100644 --- a/examples/ui/font_atlas_debug.rs +++ b/examples/ui/font_atlas_debug.rs @@ -40,7 +40,7 @@ fn atlas_render_system( for (_size, font_atlas) in set.iter() { state.added = true; let texture_atlas = texture_atlases.get(&font_atlas.texture_atlas).unwrap(); - command_buffer.build().add_entity(SpriteEntity { + command_buffer.build().entity_with(SpriteComponents { material: materials.add(texture_atlas.texture.into()), translation: Vec3::new(-300.0, 0., 0.0).into(), ..Default::default() @@ -68,10 +68,10 @@ fn setup( command_buffer .build() // 2d camera - .add_entity(OrthographicCameraEntity::default()) - .add_entity(OrthographicCameraEntity::ui()) + .entity_with(OrthographicCameraComponents::default()) + .entity_with(OrthographicCameraComponents::ui()) // texture - .add_entity(LabelEntity { + .entity_with(LabelComponents { node: Node::new( Anchors::TOP_LEFT, Margins::new(0.0, 250.0, 0.0, 60.0), diff --git a/examples/ui/text.rs b/examples/ui/text.rs index 77879da56d..9335879e30 100644 --- a/examples/ui/text.rs +++ b/examples/ui/text.rs @@ -25,9 +25,9 @@ fn setup(command_buffer: &mut CommandBuffer, asset_server: Res) { command_buffer .build() // 2d camera - .add_entity(OrthographicCameraEntity::ui()) + .entity_with(OrthographicCameraComponents::ui()) // texture - .add_entity(LabelEntity { + .entity_with(LabelComponents { node: Node::new( Anchors::TOP_LEFT, Margins::new(0.0, 250.0, 0.0, 60.0), diff --git a/examples/ui/ui.rs b/examples/ui/ui.rs index 5a49319cfc..fa88b2ba7e 100644 --- a/examples/ui/ui.rs +++ b/examples/ui/ui.rs @@ -56,9 +56,9 @@ fn setup( // ..Default::default() // }) // ui camera - .add_entity(OrthographicCameraEntity::ui()) + .entity_with(OrthographicCameraComponents::ui()) // left vertical fill - .add_entity(UiEntity { + .entity_with(UiComponents { node: Node::new( Anchors::LEFT_FULL, Margins::new(10.0, 200.0, 10.0, 10.0), @@ -66,8 +66,8 @@ fn setup( material: materials.add(Color::rgb(0.02, 0.02, 0.02).into()), ..Default::default() }) - .add_children(|builder| { - builder.add_entity(LabelEntity { + .with_children(|builder| { + builder.entity_with(LabelComponents { node: Node::new( Anchors::TOP_LEFT, Margins::new(10.0, 200.0, 40.0, 10.0), @@ -84,7 +84,7 @@ fn setup( }) }) // right vertical fill - .add_entity(UiEntity { + .entity_with(UiComponents { node: Node::new( Anchors::RIGHT_FULL, Margins::new(10.0, 100.0, 100.0, 100.0), @@ -93,7 +93,7 @@ fn setup( ..Default::default() }) // render order test: reddest in the back, whitest in the front - .add_entity(UiEntity { + .entity_with(UiComponents { node: Node::positioned( math::vec2(75.0, 60.0), Anchors::CENTER, @@ -102,7 +102,7 @@ fn setup( material: materials.add(Color::rgb(1.0, 0.0, 0.0).into()), ..Default::default() }) - .add_entity(UiEntity { + .entity_with(UiComponents { node: Node::positioned( math::vec2(50.0, 35.0), Anchors::CENTER, @@ -111,7 +111,7 @@ fn setup( material: materials.add(Color::rgb(1.0, 0.3, 0.3).into()), ..Default::default() }) - .add_entity(UiEntity { + .entity_with(UiComponents { node: Node::positioned( math::vec2(100.0, 85.0), Anchors::CENTER, @@ -120,7 +120,7 @@ fn setup( material: materials.add(Color::rgb(1.0, 0.5, 0.5).into()), ..Default::default() }) - .add_entity(UiEntity { + .entity_with(UiComponents { node: Node::positioned( math::vec2(150.0, 135.0), Anchors::CENTER, @@ -130,7 +130,7 @@ fn setup( ..Default::default() }) // parenting - .add_entity(UiEntity { + .entity_with(UiComponents { node: Node::positioned( math::vec2(210.0, 0.0), Anchors::BOTTOM_LEFT, @@ -139,8 +139,8 @@ fn setup( material: materials.add(Color::rgb(0.1, 0.1, 1.0).into()), ..Default::default() }) - .add_children(|builder| { - builder.add_entity(UiEntity { + .with_children(|builder| { + builder.entity_with(UiComponents { node: Node::new( Anchors::FULL, Margins::new(20.0, 20.0, 20.0, 20.0), @@ -150,7 +150,7 @@ fn setup( }) }) // alpha test - .add_entity(UiEntity { + .entity_with(UiComponents { node: Node::positioned( math::vec2(200.0, 185.0), Anchors::CENTER, @@ -160,7 +160,7 @@ fn setup( ..Default::default() }) // texture - .add_entity(UiEntity { + .entity_with(UiComponents { node: Node::new( Anchors::CENTER_TOP, Margins::new(-250.0, 250.0, 510.0 * aspect, 10.0), diff --git a/examples/ui/ui_bench.rs b/examples/ui/ui_bench.rs index 320c43efae..7ff83d2141 100644 --- a/examples/ui/ui_bench.rs +++ b/examples/ui/ui_bench.rs @@ -23,14 +23,14 @@ fn placement_system( fn setup(command_buffer: &mut CommandBuffer, mut materials: ResMut>) { let mut builder = command_buffer.build(); - builder.add_entity(OrthographicCameraEntity::ui()); + builder.entity_with(OrthographicCameraComponents::ui()); let mut prev = Vec2::default(); let count = 1000; for i in 0..count { // 2d camera let cur = Vec2::new(1.0, 1.0) + prev; - builder.add_entity(UiEntity { + builder.entity_with(UiComponents { node: Node { position: Vec2::new(75.0, 75.0) + cur, anchors: Anchors::new(0.5, 0.5, 0.5, 0.5), diff --git a/src/prelude.rs b/src/prelude.rs index 476751858e..1c9ffac319 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,7 +1,7 @@ pub use crate::{ app::{ schedule_runner::ScheduleRunnerPlugin, stage, App, AppBuilder, AppPlugin, DynamicAppPlugin, - EntityArchetype, EventReader, Events, FromResources, System, + ComponentSet, EventReader, Events, FromResources, System, }, asset::{AddAsset, AssetEvent, AssetServer, Assets, Handle}, core::{ @@ -32,7 +32,7 @@ pub use crate::{ }, scene::{Scene, SceneSpawner}, sprite::{ - entity::{SpriteEntity, SpriteSheetEntity}, + entity::{SpriteComponents, SpriteSheetComponents}, ColorMaterial, Sprite, TextureAtlas, TextureAtlasSprite, }, text::{Font, TextStyle},