Skip to main content

EntityBuilder

Struct EntityBuilder 

Source
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>

Source

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.

Source

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();
Source

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();
Source

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.

Source

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.

Source

pub fn entity(&self) -> Entity

Returns a reference to the entity being built.

Useful for accessing the entity ID before finalizing.

Source

pub fn world_mut(&mut self) -> &mut World

Provides mutable access to the world during building.

Use this for advanced scenarios where you need to perform world operations while building an entity.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,