ecs: rename EntityArchetype to ComponentSet

This commit is contained in:
Carter Anderson 2020-06-25 11:21:56 -07:00
parent f17cf82a87
commit 92c44320ee
29 changed files with 141 additions and 162 deletions

View File

@ -1,11 +1,11 @@
use legion::prelude::*; use legion::prelude::*;
// builder macro that makes defaults easy? Object3dBuilder { Option<Material> } impl Builder for Object3dBuilder { } // builder macro that makes defaults easy? Object3dBuilder { Option<Material> } impl Builder for Object3dBuilder { }
pub trait EntityArchetype { pub trait ComponentSet {
fn insert(self, world: &mut World) -> Entity; fn insert(self, world: &mut World) -> Entity;
fn insert_command_buffer(self, command_buffer: &mut CommandBuffer) -> 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 // add_components appears to be missing from World. it will be less efficient without that
// fn add_components(self, world: &mut World); // fn add_components(self, world: &mut World);

View File

@ -1,6 +1,6 @@
mod app; mod app;
mod app_builder; mod app_builder;
mod entity_archetype; mod component_set;
mod event; mod event;
mod plugin; mod plugin;
mod resources; mod resources;
@ -12,8 +12,8 @@ mod system;
pub use app::*; pub use app::*;
pub use app_builder::*; pub use app_builder::*;
pub use bevy_derive::{DynamicAppPlugin, EntityArchetype, FromResources}; pub use bevy_derive::{DynamicAppPlugin, ComponentSet, FromResources};
pub use entity_archetype::*; pub use component_set::*;
pub use event::*; pub use event::*;
pub use plugin::*; pub use plugin::*;
pub use resources::*; pub use resources::*;

View File

@ -1,4 +1,4 @@
use bevy_app::EntityArchetype; use bevy_app::ComponentSet;
use bevy_transform::components::{LocalTransform, Parent}; use bevy_transform::components::{LocalTransform, Parent};
use legion::{ use legion::{
filter::{ChunksetFilterData, Filter}, filter::{ChunksetFilterData, Filter},
@ -27,7 +27,7 @@ pub struct WorldBuilder<'a> {
} }
impl<'a> 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(); let entity = *self.world.insert((), vec![()]).first().unwrap();
self.current_entity = Some(entity); self.current_entity = Some(entity);
self.add_parent_to_current_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 /// note: this is slow and does a full entity copy
pub fn add<T>(&mut self, component: T) -> &mut Self pub fn with<T>(&mut self, component: T) -> &mut Self
where where
T: legion::storage::Component, T: legion::storage::Component,
{ {
@ -45,33 +45,22 @@ impl<'a> WorldBuilder<'a> {
self self
} }
pub fn tag<T>(&mut self, tag: T) -> &mut Self pub fn entities<C>(&mut self, components: C) -> &mut Self
where where
T: legion::storage::Tag,
{
let _ = self
.world
.add_tag(*self.current_entity.as_ref().unwrap(), tag);
self
}
pub fn add_entities<T, C>(&mut self, tags: T, components: C) -> &mut Self
where
T: TagSet + TagLayout + for<'b> Filter<ChunksetFilterData<'b>>,
C: IntoComponentSource, C: IntoComponentSource,
{ {
self.world.insert(tags, components); self.world.insert((), components);
self self
} }
pub fn add_entity(&mut self, entity_archetype: impl EntityArchetype) -> &mut Self { pub fn entity_with(&mut self, component_set: impl ComponentSet) -> &mut Self {
let current_entity = entity_archetype.insert(self.world); let current_entity = component_set.insert(self.world);
self.current_entity = Some(current_entity); self.current_entity = Some(current_entity);
self.add_parent_to_current_entity(); self.add_parent_to_current_entity();
self 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.parent_entity = self.current_entity;
self.current_entity = None; self.current_entity = None;
@ -116,7 +105,7 @@ pub struct CommandBufferBuilder<'a> {
} }
impl<'a> 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(); let entity = *self.command_buffer.insert((), vec![()]).first().unwrap();
self.current_entity = Some(entity); self.current_entity = Some(entity);
self.add_parent_to_current_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 // note: this is slow and does a full entity copy
pub fn add<T>(&mut self, component: T) -> &mut Self pub fn with<T>(&mut self, component: T) -> &mut Self
where where
T: legion::storage::Component, T: legion::storage::Component,
{ {
@ -134,17 +123,7 @@ impl<'a> CommandBufferBuilder<'a> {
self self
} }
pub fn tag<T>(&mut self, tag: T) -> &mut Self pub fn entities<T, C>(&mut self, tags: T, components: C) -> &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<T, C>(&mut self, tags: T, components: C) -> &mut Self
where where
T: TagSet + TagLayout + for<'b> Filter<ChunksetFilterData<'b>> + 'static, T: TagSet + TagLayout + for<'b> Filter<ChunksetFilterData<'b>> + 'static,
C: IntoComponentSource + 'static, C: IntoComponentSource + 'static,
@ -153,14 +132,14 @@ impl<'a> CommandBufferBuilder<'a> {
self self
} }
pub fn add_entity(&mut self, entity_archetype: impl EntityArchetype) -> &mut Self { pub fn entity_with(&mut self, component_set: impl ComponentSet) -> &mut Self {
let current_entity = entity_archetype.insert_command_buffer(self.command_buffer); let current_entity = component_set.insert_command_buffer(self.command_buffer);
self.current_entity = Some(current_entity); self.current_entity = Some(current_entity);
self.add_parent_to_current_entity(); self.add_parent_to_current_entity();
self 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.parent_entity = self.current_entity;
self.current_entity = None; self.current_entity = None;

View File

@ -3,7 +3,7 @@ use proc_macro::TokenStream;
use quote::quote; use quote::quote;
use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Fields, Ident}; 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 ast = parse_macro_input!(input as DeriveInput);
let fields = match &ast.data { let fields = match &ast.data {
Data::Struct(DataStruct { Data::Struct(DataStruct {
@ -28,7 +28,7 @@ pub fn derive_entity_archetype(input: TokenStream) -> TokenStream {
let struct_name = &ast.ident; let struct_name = &ast.ident;
TokenStream::from(quote! { 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 { fn insert(self, world: &mut #legion_path::prelude::World) -> #legion_path::prelude::Entity {
*world.insert((), *world.insert((),
vec![( vec![(

View File

@ -4,7 +4,7 @@ mod app_plugin;
mod as_vertex_buffer_descriptor; mod as_vertex_buffer_descriptor;
mod attributes; mod attributes;
mod bytes; mod bytes;
mod entity_archetype; mod component_set;
mod modules; mod modules;
mod render_resource; mod render_resource;
mod render_resources; 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) as_vertex_buffer_descriptor::derive_as_vertex_buffer_descriptor(input)
} }
#[proc_macro_derive(EntityArchetype, attributes(tag, module))] #[proc_macro_derive(ComponentSet, attributes(tag, module))]
pub fn derive_entity_archetype(input: TokenStream) -> TokenStream { pub fn derive_component_set(input: TokenStream) -> TokenStream {
entity_archetype::derive_entity_archetype(input) component_set::derive_component_set(input)
} }
#[proc_macro_derive(DynamicAppPlugin)] #[proc_macro_derive(DynamicAppPlugin)]

View File

@ -1,6 +1,6 @@
use crate::{light::Light, material::StandardMaterial, pipelines::FORWARD_PIPELINE_HANDLE}; use crate::{light::Light, material::StandardMaterial, pipelines::FORWARD_PIPELINE_HANDLE};
use bevy_asset::Handle; use bevy_asset::Handle;
use bevy_derive::EntityArchetype; use bevy_derive::ComponentSet;
use bevy_render::{ use bevy_render::{
draw::Draw, draw::Draw,
mesh::Mesh, mesh::Mesh,
@ -8,8 +8,8 @@ use bevy_render::{
}; };
use bevy_transform::prelude::{Rotation, Scale, Transform, Translation}; use bevy_transform::prelude::{Rotation, Scale, Transform, Translation};
#[derive(EntityArchetype)] #[derive(ComponentSet)]
pub struct MeshEntity { pub struct MeshComponents {
pub mesh: Handle<Mesh>, pub mesh: Handle<Mesh>,
pub material: Handle<StandardMaterial>, pub material: Handle<StandardMaterial>,
pub draw: Draw, pub draw: Draw,
@ -20,7 +20,7 @@ pub struct MeshEntity {
pub scale: Scale, pub scale: Scale,
} }
impl Default for MeshEntity { impl Default for MeshComponents {
fn default() -> Self { fn default() -> Self {
Self { Self {
render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized( render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized(
@ -52,8 +52,8 @@ impl Default for MeshEntity {
} }
} }
#[derive(EntityArchetype, Default)] #[derive(ComponentSet, Default)]
pub struct LightEntity { pub struct LightComponents {
pub light: Light, pub light: Light,
pub transform: Transform, pub transform: Transform,
pub translation: Translation, pub translation: Translation,

View File

@ -3,11 +3,11 @@ use crate::{
OrthographicProjection, PerspectiveProjection, WindowOrigin, VisibleEntities, OrthographicProjection, PerspectiveProjection, WindowOrigin, VisibleEntities,
}; };
use bevy_asset::Handle; use bevy_asset::Handle;
use bevy_derive::EntityArchetype; use bevy_derive::ComponentSet;
use bevy_transform::components::{Rotation, Scale, Transform, Translation}; use bevy_transform::components::{Rotation, Scale, Transform, Translation};
#[derive(EntityArchetype, Default)] #[derive(ComponentSet, Default)]
pub struct MeshMaterialEntity<T: Default + Send + Sync + 'static> { pub struct MeshMaterialComponents<T: Default + Send + Sync + 'static> {
pub mesh: Handle<Mesh>, pub mesh: Handle<Mesh>,
pub material: Handle<T>, pub material: Handle<T>,
pub draw: Draw, pub draw: Draw,
@ -18,8 +18,8 @@ pub struct MeshMaterialEntity<T: Default + Send + Sync + 'static> {
pub scale: Scale, pub scale: Scale,
} }
#[derive(EntityArchetype)] #[derive(ComponentSet)]
pub struct PerspectiveCameraEntity { pub struct PerspectiveCameraComponents {
pub camera: Camera, pub camera: Camera,
pub perspective_projection: PerspectiveProjection, pub perspective_projection: PerspectiveProjection,
pub visible_entities: VisibleEntities, pub visible_entities: VisibleEntities,
@ -29,9 +29,9 @@ pub struct PerspectiveCameraEntity {
pub scale: Scale, pub scale: Scale,
} }
impl Default for PerspectiveCameraEntity { impl Default for PerspectiveCameraComponents {
fn default() -> Self { fn default() -> Self {
PerspectiveCameraEntity { PerspectiveCameraComponents {
camera: Camera { camera: Camera {
name: Some(base_render_graph::camera::CAMERA.to_string()), name: Some(base_render_graph::camera::CAMERA.to_string()),
..Default::default() ..Default::default()
@ -46,8 +46,8 @@ impl Default for PerspectiveCameraEntity {
} }
} }
#[derive(EntityArchetype)] #[derive(ComponentSet)]
pub struct OrthographicCameraEntity { pub struct OrthographicCameraComponents {
pub camera: Camera, pub camera: Camera,
pub orthographic_projection: OrthographicProjection, pub orthographic_projection: OrthographicProjection,
pub visible_entities: VisibleEntities, pub visible_entities: VisibleEntities,
@ -57,9 +57,9 @@ pub struct OrthographicCameraEntity {
pub scale: Scale, pub scale: Scale,
} }
impl OrthographicCameraEntity { impl OrthographicCameraComponents {
pub fn ui() -> Self { pub fn ui() -> Self {
OrthographicCameraEntity { OrthographicCameraComponents {
camera: Camera { camera: Camera {
name: Some("UiCamera".to_string()), name: Some("UiCamera".to_string()),
..Default::default() ..Default::default()
@ -77,9 +77,9 @@ impl OrthographicCameraEntity {
} }
} }
impl Default for OrthographicCameraEntity { impl Default for OrthographicCameraComponents {
fn default() -> Self { fn default() -> Self {
OrthographicCameraEntity { OrthographicCameraComponents {
camera: Camera { camera: Camera {
name: Some(base_render_graph::camera::CAMERA2D.to_string()), name: Some(base_render_graph::camera::CAMERA2D.to_string()),
..Default::default() ..Default::default()

View File

@ -2,7 +2,7 @@ use crate::{
render::SPRITE_PIPELINE_HANDLE, sprite::Sprite, ColorMaterial, TextureAtlas, render::SPRITE_PIPELINE_HANDLE, sprite::Sprite, ColorMaterial, TextureAtlas,
TextureAtlasSprite, QUAD_HANDLE, SPRITE_SHEET_PIPELINE_HANDLE, TextureAtlasSprite, QUAD_HANDLE, SPRITE_SHEET_PIPELINE_HANDLE,
}; };
use bevy_app::EntityArchetype; use bevy_app::ComponentSet;
use bevy_asset::Handle; use bevy_asset::Handle;
use bevy_render::{ use bevy_render::{
draw::Draw, draw::Draw,
@ -11,8 +11,8 @@ use bevy_render::{
}; };
use bevy_transform::prelude::{Rotation, Scale, Transform, Translation}; use bevy_transform::prelude::{Rotation, Scale, Transform, Translation};
#[derive(EntityArchetype)] #[derive(ComponentSet)]
pub struct SpriteEntity { pub struct SpriteComponents {
pub sprite: Sprite, pub sprite: Sprite,
pub mesh: Handle<Mesh>, // TODO: maybe abstract this out pub mesh: Handle<Mesh>, // TODO: maybe abstract this out
pub material: Handle<ColorMaterial>, pub material: Handle<ColorMaterial>,
@ -24,7 +24,7 @@ pub struct SpriteEntity {
pub scale: Scale, pub scale: Scale,
} }
impl Default for SpriteEntity { impl Default for SpriteComponents {
fn default() -> Self { fn default() -> Self {
Self { Self {
mesh: QUAD_HANDLE, mesh: QUAD_HANDLE,
@ -60,8 +60,8 @@ impl Default for SpriteEntity {
} }
} }
#[derive(EntityArchetype)] #[derive(ComponentSet)]
pub struct SpriteSheetEntity { pub struct SpriteSheetComponents {
pub sprite: TextureAtlasSprite, pub sprite: TextureAtlasSprite,
pub texture_atlas: Handle<TextureAtlas>, pub texture_atlas: Handle<TextureAtlas>,
pub draw: Draw, pub draw: Draw,
@ -73,7 +73,7 @@ pub struct SpriteSheetEntity {
pub scale: Scale, pub scale: Scale,
} }
impl Default for SpriteSheetEntity { impl Default for SpriteSheetComponents {
fn default() -> Self { fn default() -> Self {
Self { Self {
render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized( render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized(

View File

@ -1,13 +1,13 @@
use super::Node; use super::Node;
use crate::{render::UI_PIPELINE_HANDLE, widget::Label}; use crate::{render::UI_PIPELINE_HANDLE, widget::Label};
use bevy_asset::Handle; 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_render::{draw::Draw, mesh::Mesh, pipeline::{PipelineSpecialization, RenderPipelines, DynamicBinding, RenderPipeline}};
use bevy_sprite::{ColorMaterial, QUAD_HANDLE}; use bevy_sprite::{ColorMaterial, QUAD_HANDLE};
use bevy_transform::prelude::{Translation, Transform, Rotation, Scale}; use bevy_transform::prelude::{Translation, Transform, Rotation, Scale};
#[derive(EntityArchetype)] #[derive(ComponentSet)]
pub struct UiEntity { pub struct UiComponents {
pub node: Node, pub node: Node,
pub mesh: Handle<Mesh>, // TODO: maybe abstract this out pub mesh: Handle<Mesh>, // TODO: maybe abstract this out
pub material: Handle<ColorMaterial>, pub material: Handle<ColorMaterial>,
@ -19,9 +19,9 @@ pub struct UiEntity {
pub scale: Scale, pub scale: Scale,
} }
impl Default for UiEntity { impl Default for UiComponents {
fn default() -> Self { fn default() -> Self {
UiEntity { UiComponents {
mesh: QUAD_HANDLE, mesh: QUAD_HANDLE,
render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized( render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized(
UI_PIPELINE_HANDLE, UI_PIPELINE_HANDLE,
@ -52,8 +52,8 @@ impl Default for UiEntity {
} }
} }
#[derive(EntityArchetype)] #[derive(ComponentSet)]
pub struct LabelEntity { pub struct LabelComponents {
pub node: Node, pub node: Node,
pub draw: Draw, pub draw: Draw,
pub label: Label, pub label: Label,
@ -63,9 +63,9 @@ pub struct LabelEntity {
pub scale: Scale, pub scale: Scale,
} }
impl Default for LabelEntity { impl Default for LabelComponents {
fn default() -> Self { fn default() -> Self {
LabelEntity { LabelComponents {
label: Label::default(), label: Label::default(),
node: Default::default(), node: Default::default(),
draw: Draw { draw: Draw {

View File

@ -15,8 +15,8 @@ fn setup(
let texture_handle = asset_server.load("assets/branding/icon.png").unwrap(); let texture_handle = asset_server.load("assets/branding/icon.png").unwrap();
command_buffer command_buffer
.build() .build()
.add_entity(OrthographicCameraEntity::default()) .entity_with(OrthographicCameraComponents::default())
.add_entity(SpriteEntity { .entity_with(SpriteComponents {
material: materials.add(texture_handle.into()), material: materials.add(texture_handle.into()),
..Default::default() ..Default::default()
}); });

View File

@ -38,11 +38,11 @@ fn setup(
let texture_atlas_handle = texture_atlases.add(texture_atlas); let texture_atlas_handle = texture_atlases.add(texture_atlas);
command_buffer command_buffer
.build() .build()
.add_entity(OrthographicCameraEntity::default()) .entity_with(OrthographicCameraComponents::default())
.add_entity(SpriteSheetEntity { .entity_with(SpriteSheetComponents {
texture_atlas: texture_atlas_handle, texture_atlas: texture_atlas_handle,
scale: Scale(6.0), scale: Scale(6.0),
..Default::default() ..Default::default()
}) })
.add(Timer::from_seconds(0.1)); .with(Timer::from_seconds(0.1));
} }

View File

@ -27,7 +27,7 @@ fn setup(
.unwrap(); .unwrap();
command_buffer command_buffer
.build() .build()
.add_entity(OrthographicCameraEntity::default()); .entity_with(OrthographicCameraComponents::default());
} }
#[derive(Default)] #[derive(Default)]
@ -68,7 +68,7 @@ fn load_atlas(
command_buffer command_buffer
.build() .build()
// draw a sprite from the atlas // draw a sprite from the atlas
.add_entity(SpriteSheetEntity { .entity_with(SpriteSheetComponents {
scale: Scale(4.0), scale: Scale(4.0),
translation: Translation(Vec3::new(150.0, 0.0, 0.0)), translation: Translation(Vec3::new(150.0, 0.0, 0.0)),
sprite: TextureAtlasSprite::new(vendor_index as u32), sprite: TextureAtlasSprite::new(vendor_index as u32),
@ -76,7 +76,7 @@ fn load_atlas(
..Default::default() ..Default::default()
}) })
// draw the atlas itself // draw the atlas itself
.add_entity(SpriteEntity { .entity_with(SpriteComponents {
material: materials.add(texture_atlas_texture.into()), material: materials.add(texture_atlas_texture.into()),
translation: Vec3::new(-300.0, 0., 0.0).into(), translation: Vec3::new(-300.0, 0., 0.0).into(),
..Default::default() ..Default::default()

View File

@ -31,25 +31,25 @@ fn setup(
command_buffer command_buffer
.build() .build()
// plane // plane
.add_entity(MeshEntity { .entity_with(MeshComponents {
mesh: plane_handle, mesh: plane_handle,
material: plane_material_handle, material: plane_material_handle,
..Default::default() ..Default::default()
}) })
// cube // cube
.add_entity(MeshEntity { .entity_with(MeshComponents {
mesh: cube_handle, mesh: cube_handle,
material: cube_material_handle, material: cube_material_handle,
translation: Translation::new(0.0, 1.0, 0.0), translation: Translation::new(0.0, 1.0, 0.0),
..Default::default() ..Default::default()
}) })
// light // light
.add_entity(LightEntity { .entity_with(LightComponents {
translation: Translation::new(4.0, 5.0, -4.0), translation: Translation::new(4.0, 5.0, -4.0),
..Default::default() ..Default::default()
}) })
// camera // camera
.add_entity(PerspectiveCameraEntity { .entity_with(PerspectiveCameraComponents {
transform: Transform::new_sync_disabled(Mat4::face_toward( transform: Transform::new_sync_disabled(Mat4::face_toward(
Vec3::new(3.0, 5.0, 8.0), Vec3::new(3.0, 5.0, 8.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),

View File

@ -27,18 +27,18 @@ fn setup(
command_buffer command_buffer
.build() .build()
// mesh // mesh
.add_entity(MeshEntity { .entity_with(MeshComponents {
mesh: mesh_handle, mesh: mesh_handle,
material: material_handle, material: material_handle,
..Default::default() ..Default::default()
}) })
// light // light
.add_entity(LightEntity { .entity_with(LightComponents {
translation: Translation::new(4.0, 5.0, 4.0), translation: Translation::new(4.0, 5.0, 4.0),
..Default::default() ..Default::default()
}) })
// camera // camera
.add_entity(PerspectiveCameraEntity { .entity_with(PerspectiveCameraComponents {
transform: Transform::new_sync_disabled(Mat4::face_toward( transform: Transform::new_sync_disabled(Mat4::face_toward(
Vec3::new(-2.0, 2.0, 6.0), Vec3::new(-2.0, 2.0, 6.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),

View File

@ -30,16 +30,16 @@ fn setup(
command_buffer command_buffer
.build() .build()
// parent cube // parent cube
.add_entity(MeshEntity { .entity_with(MeshComponents {
mesh: cube_handle, mesh: cube_handle,
material: cube_material_handle, material: cube_material_handle,
translation: Translation::new(0.0, 0.0, 1.0), translation: Translation::new(0.0, 0.0, 1.0),
..Default::default() ..Default::default()
}) })
.add(Rotator) .with(Rotator)
.add_children(|builder| { .with_children(|builder| {
// child cube // child cube
builder.add_entity(MeshEntity { builder.entity_with(MeshComponents {
mesh: cube_handle, mesh: cube_handle,
material: cube_material_handle, material: cube_material_handle,
translation: Translation::new(0.0, 0.0, 3.0), translation: Translation::new(0.0, 0.0, 3.0),
@ -47,12 +47,12 @@ fn setup(
}) })
}) })
// light // light
.add_entity(LightEntity { .entity_with(LightComponents {
translation: Translation::new(4.0, 5.0, -4.0), translation: Translation::new(4.0, 5.0, -4.0),
..Default::default() ..Default::default()
}) })
// camera // camera
.add_entity(PerspectiveCameraEntity { .entity_with(PerspectiveCameraComponents {
transform: Transform::new_sync_disabled(Mat4::face_toward( transform: Transform::new_sync_disabled(Mat4::face_toward(
Vec3::new(5.0, 10.0, 10.0), Vec3::new(5.0, 10.0, 10.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),

View File

@ -44,25 +44,25 @@ fn setup(
let mut builder = command_buffer.build(); let mut builder = command_buffer.build();
builder builder
// plane // plane
.add_entity(MeshEntity { .entity_with(MeshComponents {
mesh: plane_handle, mesh: plane_handle,
material: plane_material_handle, material: plane_material_handle,
..Default::default() ..Default::default()
}) })
// cube // cube
.add_entity(MeshEntity { .entity_with(MeshComponents {
mesh: cube_handle, mesh: cube_handle,
material: cube_material_handle, material: cube_material_handle,
translation: Translation::new(0.0, 0.0, 1.0), translation: Translation::new(0.0, 0.0, 1.0),
..Default::default() ..Default::default()
}) })
// light // light
.add_entity(LightEntity { .entity_with(LightComponents {
translation: Translation::new(4.0, -4.0, 5.0), translation: Translation::new(4.0, -4.0, 5.0),
..Default::default() ..Default::default()
}) })
// camera // camera
.add_entity(PerspectiveCameraEntity { .entity_with(PerspectiveCameraComponents {
transform: Transform::new_sync_disabled(Mat4::face_toward( transform: Transform::new_sync_disabled(Mat4::face_toward(
Vec3::new(3.0, 5.0, -8.0), Vec3::new(3.0, 5.0, -8.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),
@ -81,7 +81,7 @@ fn setup(
), ),
..Default::default() ..Default::default()
}); });
builder.add_entity(MeshEntity { builder.entity_with(MeshComponents {
mesh: cube_handle, mesh: cube_handle,
material: spawned_material_handle, material: spawned_material_handle,
translation: Translation::new( translation: Translation::new(

View File

@ -56,7 +56,7 @@ fn setup(
command_buffer command_buffer
.build() .build()
// textured quad - normal // textured quad - normal
.add_entity(MeshEntity { .entity_with(MeshComponents {
mesh: quad_handle, mesh: quad_handle,
material: material_handle, material: material_handle,
translation: Translation::new(0.0, 0.0, -1.5), translation: Translation::new(0.0, 0.0, -1.5),
@ -68,7 +68,7 @@ fn setup(
..Default::default() ..Default::default()
}) })
// textured quad - modulated // textured quad - modulated
.add_entity(MeshEntity { .entity_with(MeshComponents {
mesh: quad_handle, mesh: quad_handle,
material: red_material_handle, material: red_material_handle,
translation: Translation::new(0.0, 0.0, 0.0), translation: Translation::new(0.0, 0.0, 0.0),
@ -80,7 +80,7 @@ fn setup(
..Default::default() ..Default::default()
}) })
// textured quad - modulated // textured quad - modulated
.add_entity(MeshEntity { .entity_with(MeshComponents {
mesh: quad_handle, mesh: quad_handle,
material: blue_material_handle, material: blue_material_handle,
translation: Translation::new(0.0, 0.0, 1.5), translation: Translation::new(0.0, 0.0, 1.5),
@ -92,7 +92,7 @@ fn setup(
..Default::default() ..Default::default()
}) })
// camera // camera
.add_entity(PerspectiveCameraEntity { .entity_with(PerspectiveCameraComponents {
transform: Transform::new_sync_disabled(Mat4::face_toward( transform: Transform::new_sync_disabled(Mat4::face_toward(
Vec3::new(3.0, 5.0, -8.0), Vec3::new(3.0, 5.0, -8.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),

View File

@ -45,7 +45,7 @@ fn setup(
command_buffer command_buffer
.build() .build()
// parent cube // parent cube
.add_entity(MeshEntity { .entity_with(MeshComponents {
mesh: cube_handle, mesh: cube_handle,
material: materials.add(StandardMaterial { material: materials.add(StandardMaterial {
shaded: false, shaded: false,
@ -54,11 +54,11 @@ fn setup(
translation: Translation::new(0.0, 0.0, 1.0), translation: Translation::new(0.0, 0.0, 1.0),
..Default::default() ..Default::default()
}) })
.add(Rotator) .with(Rotator)
.add_children(|builder| { .with_children(|builder| {
// child cubes // child cubes
builder builder
.add_entity(MeshEntity { .entity_with(MeshComponents {
mesh: cube_handle, mesh: cube_handle,
material: materials.add(StandardMaterial { material: materials.add(StandardMaterial {
shaded: false, shaded: false,
@ -67,7 +67,7 @@ fn setup(
translation: Translation::new(0.0, 3.0, 0.0), translation: Translation::new(0.0, 3.0, 0.0),
..Default::default() ..Default::default()
}) })
.add_entity(MeshEntity { .entity_with(MeshComponents {
mesh: cube_handle, mesh: cube_handle,
material: materials.add(StandardMaterial { material: materials.add(StandardMaterial {
shaded: false, shaded: false,
@ -78,7 +78,7 @@ fn setup(
}) })
}) })
// camera // camera
.add_entity(PerspectiveCameraEntity { .entity_with(PerspectiveCameraComponents {
transform: Transform::new_sync_disabled(Mat4::face_toward( transform: Transform::new_sync_disabled(Mat4::face_toward(
Vec3::new(5.0, 10.0, 10.0), Vec3::new(5.0, 10.0, 10.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),

View File

@ -23,18 +23,18 @@ fn setup(
command_buffer command_buffer
.build() .build()
// cube // cube
.add_entity(MeshEntity { .entity_with(MeshComponents {
mesh: cube_handle, mesh: cube_handle,
material: cube_material_handle, material: cube_material_handle,
..Default::default() ..Default::default()
}) })
// light // light
.add_entity(LightEntity { .entity_with(LightComponents {
translation: Translation::new(4.0, 5.0, 4.0), translation: Translation::new(4.0, 5.0, 4.0),
..Default::default() ..Default::default()
}) })
// camera // camera
.add_entity(PerspectiveCameraEntity { .entity_with(PerspectiveCameraComponents {
transform: Transform::new_sync_disabled(Mat4::face_toward( transform: Transform::new_sync_disabled(Mat4::face_toward(
Vec3::new(3.0, 5.0, 8.0), Vec3::new(3.0, 5.0, 8.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),

View File

@ -45,33 +45,33 @@ fn setup(
command_buffer command_buffer
.build() .build()
// monkey // monkey
.add_entity(MeshEntity { .entity_with(MeshComponents {
mesh: monkey_handle, mesh: monkey_handle,
material: material_handle, material: material_handle,
translation: Translation::new(-3.0, 0.0, 0.0), translation: Translation::new(-3.0, 0.0, 0.0),
..Default::default() ..Default::default()
}) })
// cube // cube
.add_entity(MeshEntity { .entity_with(MeshComponents {
mesh: cube_handle, mesh: cube_handle,
material: material_handle, material: material_handle,
translation: Translation::new(0.0, 0.0, 0.0), translation: Translation::new(0.0, 0.0, 0.0),
..Default::default() ..Default::default()
}) })
// sphere // sphere
.add_entity(MeshEntity { .entity_with(MeshComponents {
mesh: sphere_handle, mesh: sphere_handle,
material: material_handle, material: material_handle,
translation: Translation::new(3.0, 0.0, 0.0), translation: Translation::new(3.0, 0.0, 0.0),
..Default::default() ..Default::default()
}) })
// light // light
.add_entity(LightEntity { .entity_with(LightComponents {
translation: Translation::new(4.0, 5.0, 4.0), translation: Translation::new(4.0, 5.0, 4.0),
..Default::default() ..Default::default()
}) })
// camera // camera
.add_entity(PerspectiveCameraEntity { .entity_with(PerspectiveCameraComponents {
transform: Transform::new_sync_disabled(Mat4::face_toward( transform: Transform::new_sync_disabled(Mat4::face_toward(
Vec3::new(0.0, 3.0, 10.0), Vec3::new(0.0, 3.0, 10.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),

View File

@ -35,18 +35,18 @@ fn setup(
command_buffer command_buffer
.build() .build()
// mesh // mesh
.add_entity(MeshEntity { .entity_with(MeshComponents {
mesh: mesh_handle, mesh: mesh_handle,
material: material_handle, material: material_handle,
..Default::default() ..Default::default()
}) })
// light // light
.add_entity(LightEntity { .entity_with(LightComponents {
translation: Translation::new(4.0, 5.0, 4.0), translation: Translation::new(4.0, 5.0, 4.0),
..Default::default() ..Default::default()
}) })
// camera // camera
.add_entity(PerspectiveCameraEntity { .entity_with(PerspectiveCameraComponents {
transform: Transform::new_sync_disabled(Mat4::face_toward( transform: Transform::new_sync_disabled(Mat4::face_toward(
Vec3::new(2.0, 2.0, 6.0), Vec3::new(2.0, 2.0, 6.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),

View File

@ -107,14 +107,14 @@ fn save_scene_system(_world: &mut World, resources: &mut Resources) {
let mut world = World::new(); let mut world = World::new();
world world
.build() .build()
.build_entity() .entity()
.add(ComponentA { x: 1.0, y: 2.0 }) .with(ComponentA { x: 1.0, y: 2.0 })
.add(ComponentB { .with(ComponentB {
value: "hello".to_string(), value: "hello".to_string(),
..ComponentB::from_resources(resources) ..ComponentB::from_resources(resources)
}) })
.build_entity() .entity()
.add(ComponentA { x: 3.0, y: 4.0 }); .with(ComponentA { x: 3.0, y: 4.0 });
// The component registry resource contains information about all registered components. This is used to construct scenes. // The component registry resource contains information about all registered components. This is used to construct scenes.
let type_registry = resources.get::<TypeRegistry>().unwrap(); let type_registry = resources.get::<TypeRegistry>().unwrap();

View File

@ -76,7 +76,7 @@ fn setup(
command_buffer command_buffer
.build() .build()
// cube // cube
.add_entity(MeshMaterialEntity::<MyMaterial> { .entity_with(MeshMaterialComponents::<MyMaterial> {
mesh: cube_handle, mesh: cube_handle,
render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized( render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized(
pipeline_handle, pipeline_handle,
@ -102,7 +102,7 @@ fn setup(
..Default::default() ..Default::default()
}) })
// camera // camera
.add_entity(PerspectiveCameraEntity { .entity_with(PerspectiveCameraComponents {
transform: Transform::new_sync_disabled(Mat4::face_toward( transform: Transform::new_sync_disabled(Mat4::face_toward(
Vec3::new(3.0, 5.0, -8.0), Vec3::new(3.0, 5.0, -8.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),

View File

@ -93,7 +93,7 @@ fn setup(
command_buffer command_buffer
.build() .build()
// cube // cube
.add_entity(MeshMaterialEntity::<MyMaterial> { .entity_with(MeshMaterialComponents::<MyMaterial> {
mesh: cube_handle, mesh: cube_handle,
render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized( render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized(
pipeline_handle, pipeline_handle,
@ -119,7 +119,7 @@ fn setup(
..Default::default() ..Default::default()
}) })
// cube // cube
.add_entity(MeshMaterialEntity::<MyMaterial> { .entity_with(MeshMaterialComponents::<MyMaterial> {
mesh: cube_handle, mesh: cube_handle,
render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized( render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized(
pipeline_handle, pipeline_handle,
@ -145,7 +145,7 @@ fn setup(
..Default::default() ..Default::default()
}) })
// camera // camera
.add_entity(PerspectiveCameraEntity { .entity_with(PerspectiveCameraComponents {
transform: Transform::new_sync_disabled(Mat4::face_toward( transform: Transform::new_sync_disabled(Mat4::face_toward(
Vec3::new(3.0, 5.0, -8.0), Vec3::new(3.0, 5.0, -8.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),

View File

@ -40,7 +40,7 @@ fn atlas_render_system(
for (_size, font_atlas) in set.iter() { for (_size, font_atlas) in set.iter() {
state.added = true; state.added = true;
let texture_atlas = texture_atlases.get(&font_atlas.texture_atlas).unwrap(); 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()), material: materials.add(texture_atlas.texture.into()),
translation: Vec3::new(-300.0, 0., 0.0).into(), translation: Vec3::new(-300.0, 0., 0.0).into(),
..Default::default() ..Default::default()
@ -68,10 +68,10 @@ fn setup(
command_buffer command_buffer
.build() .build()
// 2d camera // 2d camera
.add_entity(OrthographicCameraEntity::default()) .entity_with(OrthographicCameraComponents::default())
.add_entity(OrthographicCameraEntity::ui()) .entity_with(OrthographicCameraComponents::ui())
// texture // texture
.add_entity(LabelEntity { .entity_with(LabelComponents {
node: Node::new( node: Node::new(
Anchors::TOP_LEFT, Anchors::TOP_LEFT,
Margins::new(0.0, 250.0, 0.0, 60.0), Margins::new(0.0, 250.0, 0.0, 60.0),

View File

@ -25,9 +25,9 @@ fn setup(command_buffer: &mut CommandBuffer, asset_server: Res<AssetServer>) {
command_buffer command_buffer
.build() .build()
// 2d camera // 2d camera
.add_entity(OrthographicCameraEntity::ui()) .entity_with(OrthographicCameraComponents::ui())
// texture // texture
.add_entity(LabelEntity { .entity_with(LabelComponents {
node: Node::new( node: Node::new(
Anchors::TOP_LEFT, Anchors::TOP_LEFT,
Margins::new(0.0, 250.0, 0.0, 60.0), Margins::new(0.0, 250.0, 0.0, 60.0),

View File

@ -56,9 +56,9 @@ fn setup(
// ..Default::default() // ..Default::default()
// }) // })
// ui camera // ui camera
.add_entity(OrthographicCameraEntity::ui()) .entity_with(OrthographicCameraComponents::ui())
// left vertical fill // left vertical fill
.add_entity(UiEntity { .entity_with(UiComponents {
node: Node::new( node: Node::new(
Anchors::LEFT_FULL, Anchors::LEFT_FULL,
Margins::new(10.0, 200.0, 10.0, 10.0), 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()), material: materials.add(Color::rgb(0.02, 0.02, 0.02).into()),
..Default::default() ..Default::default()
}) })
.add_children(|builder| { .with_children(|builder| {
builder.add_entity(LabelEntity { builder.entity_with(LabelComponents {
node: Node::new( node: Node::new(
Anchors::TOP_LEFT, Anchors::TOP_LEFT,
Margins::new(10.0, 200.0, 40.0, 10.0), Margins::new(10.0, 200.0, 40.0, 10.0),
@ -84,7 +84,7 @@ fn setup(
}) })
}) })
// right vertical fill // right vertical fill
.add_entity(UiEntity { .entity_with(UiComponents {
node: Node::new( node: Node::new(
Anchors::RIGHT_FULL, Anchors::RIGHT_FULL,
Margins::new(10.0, 100.0, 100.0, 100.0), Margins::new(10.0, 100.0, 100.0, 100.0),
@ -93,7 +93,7 @@ fn setup(
..Default::default() ..Default::default()
}) })
// render order test: reddest in the back, whitest in the front // render order test: reddest in the back, whitest in the front
.add_entity(UiEntity { .entity_with(UiComponents {
node: Node::positioned( node: Node::positioned(
math::vec2(75.0, 60.0), math::vec2(75.0, 60.0),
Anchors::CENTER, Anchors::CENTER,
@ -102,7 +102,7 @@ fn setup(
material: materials.add(Color::rgb(1.0, 0.0, 0.0).into()), material: materials.add(Color::rgb(1.0, 0.0, 0.0).into()),
..Default::default() ..Default::default()
}) })
.add_entity(UiEntity { .entity_with(UiComponents {
node: Node::positioned( node: Node::positioned(
math::vec2(50.0, 35.0), math::vec2(50.0, 35.0),
Anchors::CENTER, Anchors::CENTER,
@ -111,7 +111,7 @@ fn setup(
material: materials.add(Color::rgb(1.0, 0.3, 0.3).into()), material: materials.add(Color::rgb(1.0, 0.3, 0.3).into()),
..Default::default() ..Default::default()
}) })
.add_entity(UiEntity { .entity_with(UiComponents {
node: Node::positioned( node: Node::positioned(
math::vec2(100.0, 85.0), math::vec2(100.0, 85.0),
Anchors::CENTER, Anchors::CENTER,
@ -120,7 +120,7 @@ fn setup(
material: materials.add(Color::rgb(1.0, 0.5, 0.5).into()), material: materials.add(Color::rgb(1.0, 0.5, 0.5).into()),
..Default::default() ..Default::default()
}) })
.add_entity(UiEntity { .entity_with(UiComponents {
node: Node::positioned( node: Node::positioned(
math::vec2(150.0, 135.0), math::vec2(150.0, 135.0),
Anchors::CENTER, Anchors::CENTER,
@ -130,7 +130,7 @@ fn setup(
..Default::default() ..Default::default()
}) })
// parenting // parenting
.add_entity(UiEntity { .entity_with(UiComponents {
node: Node::positioned( node: Node::positioned(
math::vec2(210.0, 0.0), math::vec2(210.0, 0.0),
Anchors::BOTTOM_LEFT, Anchors::BOTTOM_LEFT,
@ -139,8 +139,8 @@ fn setup(
material: materials.add(Color::rgb(0.1, 0.1, 1.0).into()), material: materials.add(Color::rgb(0.1, 0.1, 1.0).into()),
..Default::default() ..Default::default()
}) })
.add_children(|builder| { .with_children(|builder| {
builder.add_entity(UiEntity { builder.entity_with(UiComponents {
node: Node::new( node: Node::new(
Anchors::FULL, Anchors::FULL,
Margins::new(20.0, 20.0, 20.0, 20.0), Margins::new(20.0, 20.0, 20.0, 20.0),
@ -150,7 +150,7 @@ fn setup(
}) })
}) })
// alpha test // alpha test
.add_entity(UiEntity { .entity_with(UiComponents {
node: Node::positioned( node: Node::positioned(
math::vec2(200.0, 185.0), math::vec2(200.0, 185.0),
Anchors::CENTER, Anchors::CENTER,
@ -160,7 +160,7 @@ fn setup(
..Default::default() ..Default::default()
}) })
// texture // texture
.add_entity(UiEntity { .entity_with(UiComponents {
node: Node::new( node: Node::new(
Anchors::CENTER_TOP, Anchors::CENTER_TOP,
Margins::new(-250.0, 250.0, 510.0 * aspect, 10.0), Margins::new(-250.0, 250.0, 510.0 * aspect, 10.0),

View File

@ -23,14 +23,14 @@ fn placement_system(
fn setup(command_buffer: &mut CommandBuffer, mut materials: ResMut<Assets<ColorMaterial>>) { fn setup(command_buffer: &mut CommandBuffer, mut materials: ResMut<Assets<ColorMaterial>>) {
let mut builder = command_buffer.build(); let mut builder = command_buffer.build();
builder.add_entity(OrthographicCameraEntity::ui()); builder.entity_with(OrthographicCameraComponents::ui());
let mut prev = Vec2::default(); let mut prev = Vec2::default();
let count = 1000; let count = 1000;
for i in 0..count { for i in 0..count {
// 2d camera // 2d camera
let cur = Vec2::new(1.0, 1.0) + prev; let cur = Vec2::new(1.0, 1.0) + prev;
builder.add_entity(UiEntity { builder.entity_with(UiComponents {
node: Node { node: Node {
position: Vec2::new(75.0, 75.0) + cur, position: Vec2::new(75.0, 75.0) + cur,
anchors: Anchors::new(0.5, 0.5, 0.5, 0.5), anchors: Anchors::new(0.5, 0.5, 0.5, 0.5),

View File

@ -1,7 +1,7 @@
pub use crate::{ pub use crate::{
app::{ app::{
schedule_runner::ScheduleRunnerPlugin, stage, App, AppBuilder, AppPlugin, DynamicAppPlugin, schedule_runner::ScheduleRunnerPlugin, stage, App, AppBuilder, AppPlugin, DynamicAppPlugin,
EntityArchetype, EventReader, Events, FromResources, System, ComponentSet, EventReader, Events, FromResources, System,
}, },
asset::{AddAsset, AssetEvent, AssetServer, Assets, Handle}, asset::{AddAsset, AssetEvent, AssetServer, Assets, Handle},
core::{ core::{
@ -32,7 +32,7 @@ pub use crate::{
}, },
scene::{Scene, SceneSpawner}, scene::{Scene, SceneSpawner},
sprite::{ sprite::{
entity::{SpriteEntity, SpriteSheetEntity}, entity::{SpriteComponents, SpriteSheetComponents},
ColorMaterial, Sprite, TextureAtlas, TextureAtlasSprite, ColorMaterial, Sprite, TextureAtlas, TextureAtlasSprite,
}, },
text::{Font, TextStyle}, text::{Font, TextStyle},