[−][src]Struct tiny_ecs::Entities
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]
Start the creation of a new entity
If no parts are added to this call with with()
then the entity will
not be created.
pub fn with<T: 'static>(&mut self, part: T) -> Result<&mut Self, ECSError>
[src]
Chained with new_entity()
to add components
with()
can be chained multiple times to add many components.
Example
struct Component1 {} struct Component2 {} let entity_1 = entities .new_entity() .with(Component1 {})? .with(Component2 {})? .finalise()?; assert_eq!(entity_1, 0);
pub fn finalise(&mut self) -> Result<usize, ECSError>
[src]
Optional final call in creating an entity - returns ID
If you don't finalise an entity you can keep adding
components to it later. This isn't recommended though
as it's easy to lose track of which entity you are working
on. The add_component()
method allows you to add extra
components to an existing entity if you know the ID.
pub fn entity_exists(&self, id: usize) -> bool
[src]
Check if an entity ID is valid (alive and has components)
If false
then there are no components attached to this ID and
the entity is None
.
pub fn entity_contains<T: 'static>(&self, id: usize) -> bool
[src]
pub fn rm_component<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
Removal requires the ID of the entity and the components type signature.
Example
#[derive(Debug, PartialEq)] struct Test1 {} assert!(entities.rm_component::<Test1>(entity_1).is_ok()); assert!(!entities.entity_contains::<Test1>(entity_1));
pub fn rm_entity(&mut self, id: usize)
[src]
Remove an entity and all its components
Example
#[derive(Debug, PartialEq)] struct Test1 {} struct Test2 {} struct Test3 {} let entity_1 = entities.new_entity() .with(Test1 {})? .with(Test2 {})? .with(Test3 {})?.finalise()?; assert!(entities.entity_contains::<Test1>(entity_1)); assert!(entities.entity_contains::<Test2>(entity_1)); assert!(entities.entity_contains::<Test3>(entity_1)); entities.rm_entity(entity_1); assert!(!entities.entity_contains::<Test1>(entity_1)); assert!(!entities.entity_contains::<Test2>(entity_1)); assert!(!entities.entity_contains::<Test3>(entity_1)); assert_eq!(entities.entity_exists(entity_1), false);
pub fn add_component<T: 'static>(
&mut self,
id: usize,
component: T
) -> Result<(), ECSError>
[src]
&mut self,
id: usize,
component: T
) -> Result<(), ECSError>
Add a component to the existing Entity ID
Example
struct Test1 {} struct Test2 {} struct Test3 {} let mut entities = Entities::new(Some(3), Some(3)); let entity_1 = entities .new_entity() .with(Test1 {})? .with(Test2 {})? .finalise()?; entities.add_component(entity_1, Test3 {}); assert!(entities.entity_contains::<Test1>(entity_1));
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 VecMap
type but no mutable references if the same typed VecMap
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(entity_1).unwrap();
pub unsafe fn borrow_unchecked<T: 'static>(
&self
) -> Result<&VecMap<T>, ECSError>
[src]
&self
) -> Result<&VecMap<T>, ECSError>
Allows you to borrow without runtime check overhead or to borrow when the standard borrow rules prevent you from doing so and you enforce safety
Unsafe: Borrows are not checked at compile-time or runtime. An unchecked borrow
on a mutably borrowed VecMap
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 VecMap
type and no immutable references. You can however, have multiple
mutable references to different types of VecMap
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>()?; for id in 0..5 { if let Some(part) = components.get_mut(id) { part.x = 42; } } } // Now get a ref to the modified part let components = entities.borrow::<Test1>()?; let part = components.get(entity_1).unwrap(); assert_eq!(part.x, 42);
pub unsafe fn borrow_mut_unchecked<T: 'static>(
&self
) -> Result<&mut VecMap<T>, ECSError>
[src]
&self
) -> Result<&mut VecMap<T>, ECSError>
Allows you to borrow mutably without runtime check overhead or to borrow when the standard borrow rules prevent you from doing so and you enforce safety
Unsafe: Borrows are not checked at compile-time or runtime. An unchecked mutable
on an immutably borrowed VecMap
is UB.
Trait Implementations
Auto Trait Implementations
impl !Send for Entities
impl Unpin for Entities
impl !Sync for Entities
impl !UnwindSafe for Entities
impl !RefUnwindSafe for Entities
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,