ecs: fix issue with Entities query returning the incorrect entity ids

This commit is contained in:
SeanOMik 2024-04-10 22:27:23 -04:00
parent 4a0d003181
commit 4162150c5f
Signed by: SeanOMik
GPG Key ID: FEC9E2FC15235964
1 changed files with 7 additions and 8 deletions

View File

@ -2,21 +2,20 @@ use crate::{archetype::Archetype, World, Entity};
use super::{Fetch, Query, AsQuery};
pub struct EntitiesFetch {
entities: Vec<Entity>,
pub struct EntitiesFetch<'a> {
archetype_entities: Option<&'a [Entity]>,
}
impl<'a> Fetch<'a> for EntitiesFetch {
impl<'a> Fetch<'a> for EntitiesFetch<'a> {
type Item = Entity;
unsafe fn get_item(&mut self, entity: crate::world::ArchetypeEntityId) -> Self::Item {
let e = *self.entities.get_unchecked(entity.0 as usize);
e
self.archetype_entities.unwrap()[entity.0 as usize]
}
fn dangling() -> Self {
Self {
entities: vec![],
archetype_entities: None,
}
}
}
@ -27,7 +26,7 @@ pub struct Entities;
impl Query for Entities {
type Item<'a> = Entity;
type Fetch<'a> = EntitiesFetch;
type Fetch<'a> = EntitiesFetch<'a>;
fn new() -> Self {
Entities
@ -41,7 +40,7 @@ impl Query for Entities {
unsafe fn fetch<'a>(&self, _world: &'a World, archetype: &'a Archetype, tick: crate::Tick) -> Self::Fetch<'a> {
let _ = tick; // ignore unused warnings
EntitiesFetch {
entities: archetype.entity_ids.keys().cloned().collect::<Vec<Entity>>(),
archetype_entities: Some(&archetype.entities),
}
}
}