port entity functions
This commit is contained in:
parent
cb982d007c
commit
7e5880737c
@ -42,7 +42,7 @@ macro_rules! impl_from_T_for_Error {
|
|||||||
|
|
||||||
impl_from_T_for_Error!(String, WindowBuildError, IntegerOrSdlError);
|
impl_from_T_for_Error!(String, WindowBuildError, IntegerOrSdlError);
|
||||||
|
|
||||||
pub struct Entity(u64);
|
pub struct Entity(u64, Vec<Box<dyn Component>>);
|
||||||
|
|
||||||
pub trait Component {
|
pub trait Component {
|
||||||
fn inner_type_id(&self) -> TypeId;
|
fn inner_type_id(&self) -> TypeId;
|
||||||
@ -61,12 +61,50 @@ where
|
|||||||
canvas: &'context mut Canvas<Window>,
|
canvas: &'context mut Canvas<Window>,
|
||||||
texture_creator: *const TextureCreator<WindowContext>,
|
texture_creator: *const TextureCreator<WindowContext>,
|
||||||
entities: &'context mut Vec<Entity>,
|
entities: &'context mut Vec<Entity>,
|
||||||
components: &'context mut Vec<(u64, Box<dyn Component>)>,
|
|
||||||
systems: &'context mut Vec<Box<dyn System>>,
|
systems: &'context mut Vec<Box<dyn System>>,
|
||||||
textures: &'context mut HashMap<std::path::PathBuf, Texture<'game>>,
|
textures: &'context mut HashMap<std::path::PathBuf, Texture<'game>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'context, 'game> Context<'context, 'game> {
|
impl<'context, 'game> Context<'context, 'game> {
|
||||||
|
pub fn entities_with_component<T: 'static>(&self) -> Vec<u64> {
|
||||||
|
let entity_type_id = TypeId::of::<T>();
|
||||||
|
self.entities
|
||||||
|
.iter()
|
||||||
|
.filter_map(|Entity(id, components)| {
|
||||||
|
let contains_component = components
|
||||||
|
.iter()
|
||||||
|
.any(|entity| (*entity).inner_type_id() == entity_type_id);
|
||||||
|
if contains_component {
|
||||||
|
Some(*id)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn entity_component<T: 'static>(&mut self, entity_id: u64) -> &mut T {
|
||||||
|
let entity_type_id = TypeId::of::<T>();
|
||||||
|
let Entity(_id, components) = self
|
||||||
|
.entities
|
||||||
|
.iter_mut()
|
||||||
|
.find(|Entity(id, _)| *id == entity_id)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let component = components
|
||||||
|
.iter_mut()
|
||||||
|
.find_map(|entity| {
|
||||||
|
let is_id = (*entity).inner_type_id() == entity_type_id;
|
||||||
|
if is_id {
|
||||||
|
Some(entity.as_any().downcast_mut::<T>().unwrap())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
component
|
||||||
|
}
|
||||||
|
|
||||||
pub fn load_sprite<P>(&mut self, path: P) -> Result<Sprite, Error>
|
pub fn load_sprite<P>(&mut self, path: P) -> Result<Sprite, Error>
|
||||||
where
|
where
|
||||||
P: AsRef<std::path::Path>,
|
P: AsRef<std::path::Path>,
|
||||||
@ -95,9 +133,7 @@ impl<'context, 'game> Context<'context, 'game> {
|
|||||||
pub fn spawn(&mut self, components: Vec<Box<dyn Component>>) {
|
pub fn spawn(&mut self, components: Vec<Box<dyn Component>>) {
|
||||||
let id = *self.id_counter;
|
let id = *self.id_counter;
|
||||||
*self.id_counter += 1;
|
*self.id_counter += 1;
|
||||||
self.entities.push(Entity(id));
|
self.entities.push(Entity(id, components));
|
||||||
self.components
|
|
||||||
.extend(components.into_iter().map(|component| (id, component)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_system(&mut self, system: Box<dyn System>) {
|
pub fn add_system(&mut self, system: Box<dyn System>) {
|
||||||
@ -187,7 +223,6 @@ impl<'game> Game<'game> {
|
|||||||
canvas: &mut self.canvas,
|
canvas: &mut self.canvas,
|
||||||
texture_creator: &self.texture_creator,
|
texture_creator: &self.texture_creator,
|
||||||
entities: &mut self.entities,
|
entities: &mut self.entities,
|
||||||
components: &mut self.components,
|
|
||||||
systems: &mut self.systems,
|
systems: &mut self.systems,
|
||||||
textures: &mut self.textures,
|
textures: &mut self.textures,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user