Trait RegisterComponent

Source
pub trait RegisterComponent {
    // Required methods
    fn add_column(&mut self, tinfo: TypeInfo) -> Option<usize>;
    fn remove_column(&mut self, ci: usize) -> Option<TypeInfo>;
    fn get_column_index(&self, ty: &TypeId) -> Option<usize>;
    fn get_column_info(&self, ci: usize) -> Option<&TypeInfo>;
    fn num_columns(&self) -> usize;

    // Provided method
    fn contains_column(&self, ty: &TypeId) -> bool { ... }
}
Expand description

A trait for adding or removing component types from an entity container.

See ContainEntity for more information.

Required Methods§

Source

fn add_column(&mut self, tinfo: TypeInfo) -> Option<usize>

Adds a component column to the entity container then returns column index.

But the entity container failed to add new entity for some reason, returns None. You can get TypeInfo from any static types using tinfo macro.

Column index is guaranteed to be increased one by one from zero whenever you call this method, which means you can get column index from order you added components.

§Examples
use my_ecs::prelude::*;
use std::{hash::RandomState, any::TypeId};

#[derive(Component)]
struct Ca;

let mut cont: SparseSet<RandomState> = SparseSet::new();

cont.add_column(tinfo!(Ca));
assert!(cont.contains_column(&TypeId::of::<Ca>()));

// Duplicated component columns are not allowed.
let ret = cont.add_column(tinfo!(Ca));
assert!(ret.is_none());
Source

fn remove_column(&mut self, ci: usize) -> Option<TypeInfo>

Removes the component column from the entity container.

If removal is successful, returns TypeInfo of the removed component column. But if the entity container doesn’t have component column for the given column index, returns None.

§Examples
use my_ecs::prelude::*;
use std::{hash::RandomState, any::TypeId};

#[derive(Component)]
struct Ca;

let mut cont: SparseSet<RandomState> = SparseSet::new();

let col_idx = cont.add_column(tinfo!(Ca)).unwrap();
assert!(cont.contains_column(&TypeId::of::<Ca>()));

let tinfo = cont.remove_column(col_idx);
assert_eq!(tinfo, Some(tinfo!(Ca)));
assert!(!cont.contains_column(&TypeId::of::<Ca>()));
Source

fn get_column_index(&self, ty: &TypeId) -> Option<usize>

Retrieves column index for the given component type.

If there is not the component column in the entity container, returns None

§Examples
use my_ecs::prelude::*;
use std::{hash::RandomState, any::TypeId};

#[derive(Component)]
struct Ca;
#[derive(Component)]
struct Cb;

let mut cont: SparseSet<RandomState> = SparseSet::new();

let col_idx = cont.add_column(tinfo!(Ca)).unwrap();
assert_eq!(cont.get_column_index(&TypeId::of::<Ca>()).unwrap(), col_idx);

assert!(cont.get_column_index(&TypeId::of::<Cb>()).is_none());
Source

fn get_column_info(&self, ci: usize) -> Option<&TypeInfo>

Retrieves TypeInfo of the component for the given column index.

If there is not the component column in the entity container, returns None

§Examples
use my_ecs::prelude::*;
use std::hash::RandomState;

#[derive(Component)]
struct Ca;

let mut cont: SparseSet<RandomState> = SparseSet::new();

let col_idx = cont.add_column(tinfo!(Ca)).unwrap();
let tinfo = cont.get_column_info(col_idx);
assert_eq!(tinfo, Some(&tinfo!(Ca)));
Source

fn num_columns(&self) -> usize

Retrieves number of component columns in the entity container.

§Examples
use my_ecs::prelude::*;
use std::hash::RandomState;

#[derive(Component)]
struct Ca;
#[derive(Component)]
struct Cb;

let mut cont: SparseSet<RandomState> = SparseSet::new();
assert_eq!(cont.num_columns(), 0);

cont.add_column(tinfo!(Ca));
assert_eq!(cont.num_columns(), 1);
cont.add_column(tinfo!(Cb));
assert_eq!(cont.num_columns(), 2);

Provided Methods§

Source

fn contains_column(&self, ty: &TypeId) -> bool

Returns true if the entity container contains given component type.

§Examples
use my_ecs::prelude::*;
use std::{hash::RandomState, any::TypeId};

#[derive(Component)]
struct Ca;

let mut cont: SparseSet<RandomState> = SparseSet::new();
assert!(!cont.contains_column(&TypeId::of::<Ca>()));

cont.add_column(tinfo!(Ca));
assert!(cont.contains_column(&TypeId::of::<Ca>()));

Implementors§

Source§

impl<S> RegisterComponent for ChunkSparseSet<S>
where S: BuildHasher + Default + Clone + 'static,

Source§

impl<S> RegisterComponent for SparseSet<S>
where S: BuildHasher + Default + Clone + 'static,