World

Struct World 

Source
pub struct World { /* private fields */ }
Expand description

The game world containing all entities and their components.

§Example

use jugar_core::{World, Entity, Position, Velocity};

let mut world = World::new();
let entity = world.spawn();

world.add_component(entity, Position::new(0.0, 0.0));
world.add_component(entity, Velocity::new(1.0, 0.0));

// Query and update
if let Some(pos) = world.get_component_mut::<Position>(entity) {
    pos.x += 10.0;
}

Implementations§

Source§

impl World

Source

pub fn new() -> Self

Creates a new empty world

Source

pub fn spawn(&mut self) -> Entity

Spawns a new entity and returns its handle

Source

pub fn despawn(&mut self, entity: Entity) -> Result<()>

Despawns an entity and removes all its components

§Errors

Returns CoreError::EntityNotFound if the entity doesn’t exist.

Source

pub fn add_component<T: Any + Send + Sync>( &mut self, entity: Entity, component: T, )

Adds a component to an entity

If the entity already has this component type, it is replaced.

Source

pub fn get_component<T: Any>(&self, entity: Entity) -> Option<&T>

Gets a reference to a component on an entity

Source

pub fn get_component_mut<T: Any>(&mut self, entity: Entity) -> Option<&mut T>

Gets a mutable reference to a component on an entity

Source

pub fn has_component<T: Any>(&self, entity: Entity) -> bool

Checks if an entity has a specific component

Source

pub fn remove_component<T: Any>(&mut self, entity: Entity) -> bool

Removes a component from an entity

Returns true if the component was removed, false if it didn’t exist.

Source

pub fn entity_count(&self) -> usize

Returns the number of entities in the world

Source

pub fn entities(&self) -> impl Iterator<Item = Entity> + '_

Returns an iterator over all entities

Source

pub fn contains(&self, entity: Entity) -> bool

Checks if an entity exists in the world

Source

pub fn component_type_count(&self) -> usize

Returns the number of registered component types

This is used by the probar introspection module.

Source

pub fn entity_component_count_internal(&self, entity: Entity) -> usize

Returns the number of components attached to an entity

This is used by the probar introspection module.

Trait Implementations§

Source§

impl Debug for World

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for World

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for World

§

impl !RefUnwindSafe for World

§

impl Send for World

§

impl Sync for World

§

impl Unpin for World

§

impl !UnwindSafe for World

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<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, 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.