[][src]Struct tiny_ecs::Entities

pub struct Entities { /* fields omitted */ }

This is the root of the ECS implementation

Methods

impl Entities[src]

pub fn new(entity_count: Option<usize>, part_count: Option<usize>) -> Entities[src]

Create a new root entity container.

  • entity_count will initialize the entity map and new component maps to this size. This is a good thing to do to prevent unnecessary (re)allocations if you know the total active entity count. Entity removals will free up slots which are then reused instead of expanding the map.
  • part_count is as above, for total part types/kinds, there is a maximum of either 32 or 64 individual parts you can add of which the index starts at 0 to n-1

pub fn new_entity(&mut self) -> &mut Self[src]

Find the first EMPTY ID number to use

As entities are only created by actually inserting new components in to the Entity structure, this should be called to find the first available entity slot. Slot states are determined by the mask it holds.

pub fn with_part<T: 'static>(&mut self, part: T) -> &mut Self[src]

Adding a part requires a valid slot along with the initialised data to use with the entity. Effectively creates the entity if the slot is currently empty.

A bitmask is created internally for each data type added (only one per type).

Example

struct Test1 {}

let entity_1 = entities.new_entity().with_part(Test1 {}).return_entity_id();
assert_eq!(entity_1, 0);

pub fn return_entity_id(&self) -> usize[src]

pub fn get_entity_mask(&self, id: usize) -> Option<&u64>[src]

Returns the mask of the requested entity enabling you to manually check composition using bitmasks.

pub fn get_type_mask<T: 'static>(&self) -> Option<&u64>[src]

Returns the mask associated with the requested type.

pub fn entity_contains<T: 'static>(&self, id: usize) -> bool[src]

pub fn rm_part<T: 'static>(&mut self, id: usize) -> Result<(), ECSError>[src]

Remove an entities part. If no components are left after part removal then the entity is considered deleted and an EMPTY (0) mask is inserted in its place.

Removal requires the ID of the entity and the components type signature.

Example

#[derive(Debug, PartialEq)]
struct Test1 {}

assert!(entities.rm_part::<Test1>(entity_1).is_ok());
assert_eq!(*entities.get_entity_mask(entity_1).unwrap(), EMPTY);

pub fn borrow<T: 'static>(&self) -> Result<MapRef<T>, ECSError>[src]

Get a plain reference to the selected entity part map. Borrow rules are checked at runtime.

You may have multiple immutable references to the requested ComponentMap type but no mutable references if the same typed ComponentMap is currently referenced.

  • Option is whether or not there is a part of <T> for that entity.
  • Borrowing (Ref) is checked at runtime.

Example

let components = entities
    .borrow::<Test1>()
    .unwrap();
let part = components.get_part_ref(entity_1).unwrap();

pub unsafe fn borrow_unchecked<T: 'static>(
    &self
) -> Result<&ComponentMap<T>, ECSError>
[src]

Allows you to borrow without runtime check overhead

Unsafe: Borrows are not checked at compile-time or runtime. An unchecked borrow on a mutably borrowed ComponentMap is UB.

pub fn borrow_mut<T: 'static>(&self) -> Result<MapRefMut<T>, ECSError>[src]

Get a mutable reference to the selected entity part map. Borrow rules are checked at runtime.

You may have only one mutable reference to the requested ComponentMap type and no immutable references. You can however, have multiple mutable references to different types of ComponentMap

  • Result covers if the map was able to be borrowed mutably or not.
  • Borrowing is checked at runtime.

Example

// Because we later need a ref to the same `Type` of map, the mut ref
// will need to be scoped. If the later ref was of a different type,
// eg: Vector2, then it wouldn't need scoping.
{
    let mut components = entities
        .borrow_mut::<Test1>()
        .unwrap();
    for id in 0..5 {
        if let Ok(part) = components.get_part_mut(id) {
            part.x = 42;
        }
    }
}

// Now get a ref to the modified part
let components = entities.borrow::<Test1>().unwrap();
let part = components.get_part_ref(entity_1).unwrap();
assert_eq!(part.x, 42);

pub unsafe fn borrow_mut_unchecked<T: 'static>(
    &self
) -> Result<&mut ComponentMap<T>, ECSError>
[src]

Allows you to borrow mutably without runtime check overhead

Unsafe: Borrows are not checked at compile-time or runtime. An unchecked mutable on an immutably borrowed ComponentMap is UB.

Trait Implementations

impl Default for Entities[src]

fn default() -> Self[src]

Create a new Entities struct with no pre-allocated memory for maps

impl Debug for Entities[src]

Auto Trait Implementations

impl Unpin for Entities

impl !Sync for Entities

impl !Send for Entities

impl !RefUnwindSafe for Entities

impl !UnwindSafe for Entities

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]