pub struct EntityBuilder<'w> { /* private fields */ }Expand description
A fluent builder for creating entities with components.
The EntityBuilder provides a convenient way to spawn entities and
attach multiple components in a single expression chain.
§Example
use goud_engine::sdk::EntityBuilder;
use goud_engine::sdk::components::{Transform2D, Sprite};
use goud_engine::ecs::World;
use goud_engine::core::math::Vec2;
use goud_engine::assets::AssetServer;
let mut world = World::new();
let mut assets = AssetServer::new();
// Create a fully configured entity
let entity = EntityBuilder::new(&mut world)
.with(Transform2D::from_position(Vec2::new(100.0, 200.0)))
.build();Implementations§
Source§impl<'w> EntityBuilder<'w>
impl<'w> EntityBuilder<'w>
Sourcepub fn new(world: &'w mut World) -> EntityBuilder<'w>
pub fn new(world: &'w mut World) -> EntityBuilder<'w>
Creates a new entity builder.
This immediately spawns an empty entity in the world.
Use the with() method to add components.
Sourcepub fn with<T>(self, component: T) -> EntityBuilder<'w>where
T: Component,
pub fn with<T>(self, component: T) -> EntityBuilder<'w>where
T: Component,
Adds a component to the entity.
This is the primary method for attaching components to the entity being built. Components can be chained.
§Example
use goud_engine::sdk::EntityBuilder;
use goud_engine::sdk::components::Transform2D;
use goud_engine::ecs::World;
use goud_engine::core::math::Vec2;
let mut world = World::new();
let entity = EntityBuilder::new(&mut world)
.with(Transform2D::from_position(Vec2::new(10.0, 20.0)))
.build();Sourcepub fn with_if<T>(self, condition: bool, component: T) -> EntityBuilder<'w>where
T: Component,
pub fn with_if<T>(self, condition: bool, component: T) -> EntityBuilder<'w>where
T: Component,
Conditionally adds a component to the entity.
The component is only added if condition is true.
Useful for optional components based on game state.
§Example
use goud_engine::sdk::EntityBuilder;
use goud_engine::sdk::components::Transform2D;
use goud_engine::ecs::World;
use goud_engine::core::math::Vec2;
let mut world = World::new();
let has_physics = true;
let entity = EntityBuilder::new(&mut world)
.with(Transform2D::default())
.with_if(has_physics, Transform2D::from_scale(Vec2::one()))
.build();Sourcepub fn with_if_else<T>(
self,
condition: bool,
if_true: impl FnOnce() -> T,
if_false: impl FnOnce() -> T,
) -> EntityBuilder<'w>where
T: Component,
pub fn with_if_else<T>(
self,
condition: bool,
if_true: impl FnOnce() -> T,
if_false: impl FnOnce() -> T,
) -> EntityBuilder<'w>where
T: Component,
Conditionally adds a component using a closure.
The closure is only called if condition is true, avoiding
unnecessary component construction.
Sourcepub fn build(self) -> Entity
pub fn build(self) -> Entity
Finalizes the builder and returns the created entity.
After calling build(), the builder is consumed and the entity
is ready for use.
Auto Trait Implementations§
impl<'w> Freeze for EntityBuilder<'w>
impl<'w> !RefUnwindSafe for EntityBuilder<'w>
impl<'w> !Send for EntityBuilder<'w>
impl<'w> !Sync for EntityBuilder<'w>
impl<'w> Unpin for EntityBuilder<'w>
impl<'w> UnsafeUnpin for EntityBuilder<'w>
impl<'w> !UnwindSafe for EntityBuilder<'w>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more