fix: changed the input type of the query functions from ComponentSet to Vec<TypeId>

This commit is contained in:
lisk77 2025-05-07 01:00:08 +02:00
parent 643c5caf0e
commit ff475f1775
4 changed files with 16 additions and 16 deletions

View file

@ -1,7 +1,6 @@
use std::any::{type_name, Any};
use std::any::{type_name, Any, TypeId};
use std::sync::Arc;
use comet_ecs::{Camera2D, Color, Component, Entity, Render2D, Scene, Text, Transform2D, Transform3D};
use winit::{
event::*,
event_loop::EventLoop,
@ -174,11 +173,11 @@ impl App {
self.scene.get_component_mut::<C>(entity_id)
}
pub fn get_entities_with(&self, components: ComponentSet) -> Vec<usize> {
pub fn get_entities_with(&self, components: Vec<TypeId>) -> Vec<usize> {
self.scene.get_entities_with(components)
}
pub fn delete_entities_with(&mut self, components: ComponentSet) {
pub fn delete_entities_with(&mut self, components: Vec<TypeId>) {
self.scene.delete_entities_with(components)
}

View file

@ -220,16 +220,17 @@ impl Scene {
}
/// Returns a list of entities that have the given components.
pub fn get_entities_with(&self, components: ComponentSet) -> Vec<usize> {
if self.archetypes.contains_archetype(&components) {
return self.archetypes.get_archetype(&components).unwrap().clone().iter().map(|x| *x as usize).collect();
pub fn get_entities_with(&self, components: Vec<TypeId>) -> Vec<usize> {
let component_set = ComponentSet::from_ids(components);
if self.archetypes.contains_archetype(&component_set) {
return self.archetypes.get_archetype(&component_set).unwrap().clone().iter().map(|x| *x as usize).collect();
}
Vec::new()
}
/// Deletes all entities that have the given components.
pub fn delete_entities_with(&mut self, components: ComponentSet) {
let entities = self.get_entities_with(components);
pub fn delete_entities_with(&mut self, components: Vec<TypeId>) {
let entities = self.get_entities_with(components);
for entity in entities {
self.delete_entity(entity);
}
@ -237,7 +238,7 @@ impl Scene {
/// Iterates over all entities that have the given components and calls the given function.
pub fn foreach<C: Component, K: Component>(&mut self, func: fn(&mut C,&mut K)) {
let entities = self.get_entities_with(ComponentSet::from_ids(vec![C::type_id(), K::type_id()]));
let entities = self.get_entities_with(vec![C::type_id(), K::type_id()]);
for entity in entities {
let c_ptr = self.get_component_mut::<C>(entity).unwrap() as *mut C;
let k_ptr = self.get_component_mut::<K>(entity).unwrap() as *mut K;

View file

@ -553,14 +553,14 @@ impl<'a> Renderer2D<'a> {
/// A function to automatically render all the entities of the `Scene` struct.
/// The entities must have the `Render2D` and `Transform2D` components to be rendered as well as set visible.
pub fn render_scene_2d(&mut self, scene: &Scene) {
let cameras = scene.get_entities_with(ComponentSet::from_ids(vec![Transform2D::type_id(), Camera2D::type_id()]));
let cameras = scene.get_entities_with(vec![Transform2D::type_id(), Camera2D::type_id()]);
if cameras.is_empty() {
return;
}
let entities = scene.get_entities_with(ComponentSet::from_ids(vec![Transform2D::type_id(), Render2D::type_id()]));
let texts = scene.get_entities_with(ComponentSet::from_ids(vec![Transform2D::type_id(), comet_ecs::Text::type_id()]));
let entities = scene.get_entities_with(vec![Transform2D::type_id(), Render2D::type_id()]);
let texts = scene.get_entities_with(vec![Transform2D::type_id(), comet_ecs::Text::type_id()]);
self.setup_camera(cameras, scene);