Compare commits
No commits in common. "60ec62c55828a4ea273a10e5614e7cc589b005cf" and "01a74ab9a6f3fcaad143d2836737bcdd47906d22" have entirely different histories.
60ec62c558
...
01a74ab9a6
|
@ -1,6 +1,6 @@
|
|||
use std::{ptr::{NonNull, self}, alloc::{self, Layout, alloc, dealloc}, mem, collections::HashMap, cell::{RefCell, Ref, RefMut, BorrowError, BorrowMutError}, ops::DerefMut};
|
||||
|
||||
use crate::{bundle::Bundle, component_info::ComponentInfo, world::ArchetypeEntityId, DynTypeId, Entity, Tick};
|
||||
use crate::{world::ArchetypeEntityId, bundle::Bundle, component_info::ComponentInfo, DynTypeId, Tick, Entity};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ComponentColumn {
|
||||
|
@ -295,32 +295,21 @@ impl Archetype {
|
|||
self.capacity = new_cap;
|
||||
}
|
||||
|
||||
self.ensure_synced();
|
||||
debug_assert_eq!(self.entity_ids.len(), self.entities.len(), "Somehow the Archetype's entity storage got unsynced");
|
||||
let entity_index = ArchetypeEntityId(self.entity_ids.len() as u64);
|
||||
self.entity_ids.insert(entity, entity_index);
|
||||
self.entities.push(entity);
|
||||
|
||||
bundle.take(|data, type_id, info| {
|
||||
self.put_component_at(tick, data, type_id, info.layout().size(), entity_index.0 as _);
|
||||
bundle.take(|data, type_id, _size| {
|
||||
let col = self.get_column_mut(type_id).unwrap();
|
||||
unsafe { col.insert_entity(entity_index.0 as usize, data, *tick); }
|
||||
});
|
||||
|
||||
entity_index
|
||||
}
|
||||
|
||||
pub(crate) fn put_component_at(&mut self, tick: &Tick, ptr: NonNull<u8>, type_id: DynTypeId, size: usize, index: usize) {
|
||||
let _ = size;
|
||||
|
||||
let col = self.get_column_mut(type_id).unwrap();
|
||||
//unsafe { col.set_at(index, ptr, *tick) };
|
||||
unsafe { col.insert_entity(index, ptr, *tick); }
|
||||
}
|
||||
|
||||
/// Removes an entity from the Archetype and frees its components.
|
||||
///
|
||||
/// Inside the component columns, the entities are swap-removed. Meaning that the last
|
||||
/// entity in the column is moved in the position of the entity that was removed.
|
||||
/// If there was an entity that was swapped, this function returns the entity, and its
|
||||
/// new index in the archetype that was put in place of the removed entity.
|
||||
/// Removes an entity from the Archetype and frees its components. Returns the entity record
|
||||
/// that took its place in the component column.
|
||||
pub(crate) fn remove_entity(&mut self, entity: Entity, tick: &Tick) -> Option<(Entity, ArchetypeEntityId)> {
|
||||
let entity_index = self.entity_ids.remove(&entity)
|
||||
.expect("The entity is not in this Archetype!");
|
||||
|
@ -354,7 +343,7 @@ impl Archetype {
|
|||
|
||||
let removed = self.entities.swap_remove(entity_index.0 as _);
|
||||
assert_eq!(removed, entity);
|
||||
|
||||
|
||||
// now change the ArchetypeEntityId to be the index that the moved entity was moved into.
|
||||
removed_entity.map(|(e, _a)| (e, entity_index))
|
||||
}
|
||||
|
@ -400,11 +389,6 @@ impl Archetype {
|
|||
self.capacity = new_capacity;
|
||||
}
|
||||
|
||||
/// Attempts to find the column storing components of `type_id`
|
||||
pub fn get_column_at(&self, idx: usize) -> Option<&ComponentColumn> {
|
||||
self.columns.get(idx)
|
||||
}
|
||||
|
||||
/// Attempts to find the column storing components of `type_id`
|
||||
pub fn get_column<I: Into<DynTypeId>>(&self, type_id: I) -> Option<&ComponentColumn> {
|
||||
let type_id = type_id.into();
|
||||
|
@ -452,7 +436,7 @@ impl Archetype {
|
|||
/// The entity IS NOT removed from the old archetype. You must manually call [`Archetype::remove_entity`](crate::Archetype).
|
||||
/// It was done this way because I had some borrow check issues when writing [`World::insert`](crate::World)
|
||||
/// related to borrowing mutably from self more than once.
|
||||
/* pub fn move_into<B>(&mut self, entities: &mut Entities, tick: &Tick, into_arch: &mut Archetype, entity: Entity, new_components: B)
|
||||
/* pub fn move_into<B>(&self, into_arch: &mut Archetype, entity: Entity, new_components: B)
|
||||
where
|
||||
B: Bundle
|
||||
{
|
||||
|
@ -462,40 +446,25 @@ impl Archetype {
|
|||
|
||||
// move the existing components into the new archetype
|
||||
for col in self.columns.iter() {
|
||||
let into_col = into_arch.get_column_mut(col.info.type_id()).unwrap();
|
||||
let into_col = into_arch.get_column_mut(col.info.type_id).unwrap();
|
||||
|
||||
// copy from the old column into the new column, then remove it from the old one
|
||||
unsafe {
|
||||
let ptr = col.borrow_ptr();
|
||||
let ptr = NonNull::new_unchecked(ptr.as_ptr()
|
||||
.add(new_index.0 as usize * col.info.layout().size()));
|
||||
into_col.set_at(new_index.0 as _, ptr, *tick);
|
||||
//into_col.set_at(new_index.0 as _, ptr);
|
||||
.add(new_index.0 as usize * col.info.layout.size));
|
||||
into_col.set_at(new_index.0 as _, ptr);
|
||||
}
|
||||
}
|
||||
|
||||
// now move the new components into the new archetype
|
||||
new_components.take(|data, type_id, _size| {
|
||||
let col = into_arch.get_column_mut(type_id).unwrap();
|
||||
unsafe { col.set_at(new_index.0 as _, data, *tick); }
|
||||
unsafe { col.set_at(new_index.0 as _, data); }
|
||||
col.len += 1;
|
||||
});
|
||||
|
||||
if let Some((en, new_idx)) = self.remove_entity(entity, tick) {
|
||||
let moved_rec = Record {
|
||||
id: self.id,
|
||||
index: new_idx,
|
||||
};
|
||||
entities.insert_entity_record(en, moved_rec);
|
||||
}
|
||||
let new_rec = Record {
|
||||
id: into_arch.id,
|
||||
index: new_index,
|
||||
};
|
||||
entities.insert_entity_record(entity, new_rec);
|
||||
|
||||
into_arch.ensure_synced();
|
||||
self.ensure_synced();
|
||||
//self.remove_entity(entity);
|
||||
} */
|
||||
|
||||
/// Returns a borrow to the map used to find the column indices of the entity.
|
||||
|
@ -731,7 +700,7 @@ mod tests {
|
|||
#[test]
|
||||
fn dynamic_archetype() {
|
||||
let layout = Layout::new::<u32>();
|
||||
let info = ComponentInfo::new_unknown(Some("u32".to_string()), DynTypeId::Unknown(100), layout);
|
||||
let info = ComponentInfo::new_unknown(DynTypeId::Unknown(100), "u32", layout);
|
||||
let infos = vec![info.clone()];
|
||||
|
||||
let mut a = Archetype::from_bundle_info(super::ArchetypeId(0), infos);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use std::{ptr::NonNull, alloc::Layout};
|
||||
use std::{ptr::NonNull, mem::size_of, alloc::Layout};
|
||||
|
||||
use crate::{component::Component, component_info::ComponentInfo, DynTypeId};
|
||||
|
||||
|
@ -11,7 +11,7 @@ pub trait Bundle {
|
|||
|
||||
/// Take the bundle by calling the closure with pointers to each component, its type and size.
|
||||
/// The closure is expected to take ownership of the pointer.
|
||||
fn take(self, f: impl FnMut(NonNull<u8>, DynTypeId, ComponentInfo));
|
||||
fn take(self, f: impl FnMut(NonNull<u8>, DynTypeId, usize));
|
||||
|
||||
/// Returns a boolean indicating if this Bundle is dynamic. See [`DynamicBundle`]
|
||||
fn is_dynamic(&self) -> bool;
|
||||
|
@ -26,8 +26,8 @@ impl Bundle for () {
|
|||
vec![ComponentInfo::new::<()>()]
|
||||
}
|
||||
|
||||
fn take(self, mut f: impl FnMut(NonNull<u8>, DynTypeId, ComponentInfo)) {
|
||||
f(NonNull::from(&self).cast(), DynTypeId::of::<()>(), ComponentInfo::new::<()>());
|
||||
fn take(self, mut f: impl FnMut(NonNull<u8>, DynTypeId, usize)) {
|
||||
f(NonNull::from(&self).cast(), DynTypeId::of::<()>(), size_of::<()>());
|
||||
}
|
||||
|
||||
fn is_dynamic(&self) -> bool {
|
||||
|
@ -44,8 +44,8 @@ impl<C: Component> Bundle for C {
|
|||
vec![ComponentInfo::new::<C>()]
|
||||
}
|
||||
|
||||
fn take(self, mut f: impl FnMut(NonNull<u8>, DynTypeId, ComponentInfo)) {
|
||||
f(NonNull::from(&self).cast(), DynTypeId::of::<C>(), ComponentInfo::new::<C>());
|
||||
fn take(self, mut f: impl FnMut(NonNull<u8>, DynTypeId, usize)) {
|
||||
f(NonNull::from(&self).cast(), DynTypeId::of::<C>(), size_of::<C>());
|
||||
|
||||
// this must be done to avoid calling drop on heap memory that the component
|
||||
// may manage. So something like a Vec, or HashMap, etc.
|
||||
|
@ -70,12 +70,12 @@ macro_rules! impl_bundle_tuple {
|
|||
vec![$(ComponentInfo::new::<$name>()),+]
|
||||
}
|
||||
|
||||
fn take(self, mut f: impl FnMut(NonNull<u8>, DynTypeId, ComponentInfo)) {
|
||||
fn take(self, mut f: impl FnMut(NonNull<u8>, DynTypeId, usize)) {
|
||||
// these names wont follow rust convention, but its a macro so deal with it
|
||||
let ($($name,)+) = self;
|
||||
|
||||
$(
|
||||
f(NonNull::from(&$name).cast(), DynTypeId::of::<$name>(), ComponentInfo::new::<$name>());
|
||||
f(NonNull::from(&$name).cast(), DynTypeId::of::<$name>(), size_of::<$name>());
|
||||
// this must be done to avoid calling drop on heap memory that the component
|
||||
// may manage. So something like a Vec, or HashMap, etc.
|
||||
std::mem::forget($name);
|
||||
|
@ -136,6 +136,8 @@ impl DynamicBundle {
|
|||
where
|
||||
C: Component
|
||||
{
|
||||
let info = ComponentInfo::new::<C>();
|
||||
|
||||
// an owned pointer must be created from the provided component since comp would drop
|
||||
// out of scope and the data would become invalid
|
||||
let ptr = unsafe {
|
||||
|
@ -144,43 +146,17 @@ impl DynamicBundle {
|
|||
let layout = Layout::new::<C>();
|
||||
let alloc_ptr = NonNull::new_unchecked(std::alloc::alloc(layout)).cast::<C>();
|
||||
std::ptr::copy_nonoverlapping(data.as_ptr(), alloc_ptr.as_ptr(), 1);
|
||||
|
||||
// this must be done to avoid calling drop on heap memory that the component
|
||||
// may manage. So something like a Vec, or HashMap, etc.
|
||||
std::mem::forget(comp);
|
||||
|
||||
alloc_ptr.cast()
|
||||
};
|
||||
|
||||
let info = ComponentInfo::new::<C>();
|
||||
self.bundle.push((ptr, info));
|
||||
}
|
||||
|
||||
/// Push an unknown type to the end of the bundle.
|
||||
/// Push an unknown type to the bundle
|
||||
pub fn push_unknown(&mut self, data: NonNull<u8>, info: ComponentInfo) {
|
||||
self.bundle.push((data, info));
|
||||
}
|
||||
|
||||
/// Push a bundle to the end of this dynamic bundle.
|
||||
pub fn push_bundle<B>(&mut self, bundle: B)
|
||||
where
|
||||
B: Bundle
|
||||
{
|
||||
bundle.take(|ptr, _, info| {
|
||||
// unfortunately the components in the bundle must be copied since there is no guarantee that
|
||||
// `bundle` lasts for as long as the `DynamicBundle`. If the data wasn't copied, the pointers
|
||||
// could be invalid later.
|
||||
let p = unsafe {
|
||||
let alloc_ptr = NonNull::new_unchecked(std::alloc::alloc(info.layout()));
|
||||
std::ptr::copy_nonoverlapping(ptr.as_ptr(), alloc_ptr.as_ptr(), info.layout().size());
|
||||
|
||||
alloc_ptr
|
||||
};
|
||||
|
||||
self.push_unknown(p, info);
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Bundle for DynamicBundle {
|
||||
|
@ -192,9 +168,9 @@ impl Bundle for DynamicBundle {
|
|||
self.bundle.iter().map(|b| b.1).collect()
|
||||
}
|
||||
|
||||
fn take(self, mut f: impl FnMut(NonNull<u8>, DynTypeId, ComponentInfo)) {
|
||||
for (data, info) in self.bundle.into_iter() {
|
||||
f(data, info.type_id(), info);
|
||||
fn take(self, mut f: impl FnMut(NonNull<u8>, DynTypeId, usize)) {
|
||||
for (data, info) in self.bundle.iter() {
|
||||
f(*data, info.type_id(), info.layout().size());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -67,7 +67,7 @@ impl ComponentInfo {
|
|||
}
|
||||
|
||||
/// Create ComponentInfo from a type that is not known to rust
|
||||
pub fn new_unknown<D>(name: Option<String>, type_id: D, layout: Layout) -> Self
|
||||
pub fn new_unknown<D>(type_id: D, name: &str, layout: Layout) -> Self
|
||||
where
|
||||
D: Into<DynTypeId>,
|
||||
{
|
||||
|
|
|
@ -11,16 +11,6 @@ pub struct Entity {
|
|||
pub(crate) generation: u64,
|
||||
}
|
||||
|
||||
impl Entity {
|
||||
pub fn id(&self) -> EntityId {
|
||||
self.id
|
||||
}
|
||||
|
||||
pub fn generation(&self) -> u64 {
|
||||
self.generation
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Entities {
|
||||
pub(crate) arch_index: HashMap<EntityId, Record>,
|
||||
dead: VecDeque<Entity>,
|
||||
|
|
|
@ -111,7 +111,7 @@ mod tests {
|
|||
#[test]
|
||||
fn single_dynamic_view() {
|
||||
let comp_layout = Layout::new::<u32>();
|
||||
let comp_info = ComponentInfo::new_unknown(Some("u32".to_string()), DynTypeId::Unknown(100), comp_layout);
|
||||
let comp_info = ComponentInfo::new_unknown(DynTypeId::Unknown(100), "u32", comp_layout);
|
||||
|
||||
let mut dynamic_bundle = DynamicBundle::default();
|
||||
let comp = 50u32;
|
||||
|
|
|
@ -2,20 +2,21 @@ use crate::{archetype::Archetype, World, Entity};
|
|||
|
||||
use super::{Fetch, Query, AsQuery};
|
||||
|
||||
pub struct EntitiesFetch<'a> {
|
||||
archetype_entities: Option<&'a [Entity]>,
|
||||
pub struct EntitiesFetch {
|
||||
entities: Vec<Entity>,
|
||||
}
|
||||
|
||||
impl<'a> Fetch<'a> for EntitiesFetch<'a> {
|
||||
impl<'a> Fetch<'a> for EntitiesFetch {
|
||||
type Item = Entity;
|
||||
|
||||
unsafe fn get_item(&mut self, entity: crate::world::ArchetypeEntityId) -> Self::Item {
|
||||
self.archetype_entities.unwrap()[entity.0 as usize]
|
||||
let e = *self.entities.get_unchecked(entity.0 as usize);
|
||||
e
|
||||
}
|
||||
|
||||
fn dangling() -> Self {
|
||||
Self {
|
||||
archetype_entities: None,
|
||||
entities: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +27,7 @@ pub struct Entities;
|
|||
impl Query for Entities {
|
||||
type Item<'a> = Entity;
|
||||
|
||||
type Fetch<'a> = EntitiesFetch<'a>;
|
||||
type Fetch<'a> = EntitiesFetch;
|
||||
|
||||
fn new() -> Self {
|
||||
Entities
|
||||
|
@ -40,7 +41,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 {
|
||||
archetype_entities: Some(&archetype.entities),
|
||||
entities: archetype.entity_ids.keys().cloned().collect::<Vec<Entity>>(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,10 +2,6 @@ use std::marker::PhantomData;
|
|||
|
||||
use crate::{query::{AsQuery, Query}, Archetype, Component, DynTypeId, World};
|
||||
|
||||
/// A filter query that fetches when the entity has the component `C`.
|
||||
///
|
||||
/// This does not return a reference to the component, it returns `()` if the entity has
|
||||
/// the component. This query is great when its used with [`Or`](super::Or).
|
||||
#[derive(Default)]
|
||||
pub struct Has<C: Component> {
|
||||
_marker: PhantomData<C>
|
|
@ -1,8 +1,5 @@
|
|||
mod has;
|
||||
pub use has::*;
|
||||
pub mod has_component;
|
||||
pub use has_component::*;
|
||||
|
||||
mod or;
|
||||
pub use or::*;
|
||||
|
||||
mod not;
|
||||
pub use not::*;
|
||||
pub mod or;
|
||||
pub use or::*;
|
|
@ -1,44 +0,0 @@
|
|||
use crate::{query::{AsQuery, Query}, Archetype, World};
|
||||
|
||||
/// A filter query that fetches the inverse of `Q`.
|
||||
///
|
||||
/// This means that entities that `Q` fetches are skipped, and entities that
|
||||
/// `Q` does not fetch are not skipped.
|
||||
///
|
||||
/// ```rust,nobuild
|
||||
/// // Iterate over entities that has a transform, and are not the origin of a `ChildOf` relationship.
|
||||
/// for (en, pos, _) in world
|
||||
/// .view::<(Entities, &Transform, Not<Has<RelationOriginComponent<ChildOf>>>)>()
|
||||
/// .iter()
|
||||
/// {
|
||||
/// // ...
|
||||
/// }
|
||||
/// ```
|
||||
#[derive(Default, Copy, Clone)]
|
||||
pub struct Not<Q: Query> {
|
||||
query: Q,
|
||||
}
|
||||
|
||||
impl<Q: Query> Query for Not<Q> {
|
||||
type Item<'a> = ();
|
||||
|
||||
type Fetch<'a> = ();
|
||||
|
||||
fn new() -> Self {
|
||||
Not {
|
||||
query: Q::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn can_visit_archetype(&self, archetype: &Archetype) -> bool {
|
||||
!self.query.can_visit_archetype(archetype)
|
||||
}
|
||||
|
||||
unsafe fn fetch<'a>(&self, _world: &'a World, _: &'a Archetype, _: crate::Tick) -> Self::Fetch<'a> {
|
||||
()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Q: Query> AsQuery for Not<Q> {
|
||||
type Query = Self;
|
||||
}
|
|
@ -1,26 +1,5 @@
|
|||
use crate::{query::{AsQuery, Query}, Archetype, World};
|
||||
|
||||
/// A filter query returning when either `Q1` or `Q2` returns.
|
||||
///
|
||||
/// This checks if `Q1` can fetch before checking `Q2`.
|
||||
///
|
||||
/// ```rust,nobuild
|
||||
/// for (en, pos, _) in world
|
||||
/// .view::<(Entities, &Transform, Or<Has<Mesh>, Has<Scene>>)>()
|
||||
/// .iter()
|
||||
/// {
|
||||
/// // do some things with the position of the entities
|
||||
///
|
||||
/// // now handle do things with the Mesh or Scene that the entity could have
|
||||
/// if let Some(mesh) = world.view_one::<&Mesh>(en).get() {
|
||||
/// // do mesh things
|
||||
/// }
|
||||
///
|
||||
/// if let Some(scene) = world.view_one::<&Scene>(en).get() {
|
||||
/// // do scene things
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
#[derive(Default)]
|
||||
pub struct Or<Q1: AsQuery, Q2: AsQuery> {
|
||||
left: Q1::Query,
|
||||
|
|
|
@ -2,7 +2,7 @@ use std::{any::TypeId, collections::HashMap, ptr::NonNull};
|
|||
|
||||
use atomic_refcell::{AtomicRef, AtomicRefMut};
|
||||
|
||||
use crate::{archetype::{Archetype, ArchetypeId}, bundle::Bundle, query::{dynamic::DynamicView, AsQuery, Query, ViewIter, ViewOne, ViewState}, resource::ResourceData, ComponentInfo, DynTypeId, DynamicBundle, Entities, Entity, ResourceObject, Tick, TickTracker};
|
||||
use crate::{archetype::{Archetype, ArchetypeId}, bundle::Bundle, query::{dynamic::DynamicView, AsQuery, Query, ViewState, ViewIter, ViewOne}, resource::ResourceData, ComponentInfo, DynTypeId, Entities, Entity, ResourceObject, Tick, TickTracker};
|
||||
|
||||
/// The id of the entity for the Archetype.
|
||||
///
|
||||
|
@ -144,32 +144,37 @@ impl World {
|
|||
let current_arch = self.archetypes.get(&record.id).unwrap();
|
||||
let current_arch_len = current_arch.len();
|
||||
|
||||
// contains the type ids for the old component columns + the ids for the new components
|
||||
let mut combined_column_types: Vec<DynTypeId> = current_arch.columns.iter().map(|c| c.info.type_id()).collect();
|
||||
combined_column_types.extend(bundle.type_ids());
|
||||
let mut col_types: Vec<DynTypeId> = current_arch.columns.iter().map(|c| c.info.type_id()).collect();
|
||||
let orig_col = col_types.clone();
|
||||
col_types.extend(bundle.type_ids());
|
||||
|
||||
// contains the ComponentInfo for the old component columns + the info for the new components
|
||||
let mut combined_column_infos: Vec<ComponentInfo> = current_arch.columns.iter().map(|c| c.info).collect();
|
||||
combined_column_infos.extend(bundle.info());
|
||||
let mut col_infos: Vec<ComponentInfo> = current_arch.columns.iter().map(|c| c.info).collect();
|
||||
col_infos.extend(bundle.info());
|
||||
|
||||
// pointers only for the old columns
|
||||
let old_columns: Vec<(NonNull<u8>, ComponentInfo)> = current_arch.columns.iter()
|
||||
let col_ptrs: Vec<(NonNull<u8>, ComponentInfo)> = current_arch.columns.iter()
|
||||
.map(|c| unsafe { (NonNull::new_unchecked(c.borrow_ptr().as_ptr()), c.info) })
|
||||
.collect();
|
||||
|
||||
if let Some(arch) = self.archetypes.values_mut().find(|a| a.is_archetype_for(&combined_column_types)) {
|
||||
let mut dbun = DynamicBundle::new();
|
||||
// move old entity components into new archetype columns
|
||||
for (col_ptr, col_info) in old_columns.into_iter() {
|
||||
if let Some(arch) = self.archetypes.values_mut().find(|a| a.is_archetype_for(&col_types)) {
|
||||
let res_index = arch.reserve_one(entity);
|
||||
|
||||
for (col_type, (col_ptr, col_info)) in orig_col.into_iter().zip(col_ptrs.into_iter()) {
|
||||
unsafe {
|
||||
let ptr = NonNull::new_unchecked(col_ptr.as_ptr()
|
||||
.add(record.index.0 as usize * col_info.layout().size()));
|
||||
dbun.push_unknown(ptr, col_info);
|
||||
let col = arch.get_column_mut(col_type).unwrap();
|
||||
// set_at is used since the entity was reserved
|
||||
col.set_at(res_index.0 as _, ptr, tick);
|
||||
}
|
||||
}
|
||||
dbun.push_bundle(bundle);
|
||||
|
||||
let res_index = arch.add_entity(entity, dbun, &tick);
|
||||
bundle.take(|data, type_id, _size| {
|
||||
let col = arch.get_column_mut(type_id).unwrap();
|
||||
// set_at is used since the entity was reserved
|
||||
unsafe { col.set_at(res_index.0 as _, data, tick); }
|
||||
});
|
||||
|
||||
arch.entity_ids.insert(entity, res_index);
|
||||
arch.ensure_synced();
|
||||
|
||||
let new_record = Record {
|
||||
|
@ -182,24 +187,25 @@ impl World {
|
|||
// if this entity is the only entity for this archetype, add more columns to it
|
||||
let current_arch = self.archetypes.get_mut(&record.id).unwrap();
|
||||
current_arch.extend(&tick, vec![bundle]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
let new_arch_id = self.next_archetype_id.increment();
|
||||
let mut archetype = Archetype::from_bundle_info(new_arch_id, combined_column_infos);
|
||||
let mut archetype = Archetype::from_bundle_info(new_arch_id, col_infos);
|
||||
let entity_arch_id = archetype.add_entity(entity, bundle, &tick);
|
||||
|
||||
let mut dbun = DynamicBundle::new();
|
||||
for (column_ptr, column_info) in old_columns.into_iter() {
|
||||
// move the old components into the new archetype
|
||||
for (column_ptr, column_info) in col_ptrs.into_iter() {
|
||||
unsafe {
|
||||
// ptr of component for the entity
|
||||
let comp_ptr = NonNull::new_unchecked(column_ptr.as_ptr()
|
||||
.add(record.index.0 as usize * column_info.layout().size()));
|
||||
dbun.push_unknown(comp_ptr, column_info);
|
||||
|
||||
let col = archetype.get_column_mut(column_info.type_id()).unwrap();
|
||||
col.insert_entity(entity_arch_id.0 as _, comp_ptr, tick);
|
||||
}
|
||||
}
|
||||
dbun.push_bundle(bundle);
|
||||
|
||||
let entity_arch_id = archetype.add_entity(entity, dbun, &tick);
|
||||
|
||||
self.archetypes.insert(new_arch_id, archetype);
|
||||
|
||||
|
@ -213,103 +219,12 @@ impl World {
|
|||
}
|
||||
|
||||
let current_arch = self.archetypes.get_mut(&record.id).unwrap();
|
||||
if let Some((en, enar)) = current_arch.remove_entity(entity, &tick) {
|
||||
let rec = Record {
|
||||
id: current_arch.id(),
|
||||
index: enar
|
||||
};
|
||||
self.entities.insert_entity_record(en, rec);
|
||||
}
|
||||
|
||||
current_arch.ensure_synced();
|
||||
}
|
||||
|
||||
/// A method used for debugging implementation details of the ECS.
|
||||
///
|
||||
/// Here's an example of the output:
|
||||
/// ```
|
||||
/// Entities
|
||||
/// 1 in archetype 0 at 0
|
||||
/// 0 in archetype 1 at 0
|
||||
/// 2 in archetype 0 at 1
|
||||
/// 3 in archetype 2 at 0
|
||||
/// Arch 1 -- 1 entities
|
||||
/// Col 175564825027445222460146453544114453753
|
||||
/// 0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 128 63 0 0 128 63 0 0 128 63 0 0 128 63 78 86 0 0
|
||||
/// Col 162279302565774655543278578489329315472
|
||||
/// 0: 0 0 32 65 0 0 32 65 0 0 32 65 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 128 63 0 0 128 63 0 0 128 63 0 0 128 63 0 0 0 0
|
||||
/// Col 24291284537013640759061027938209843602
|
||||
/// 0: 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
||||
/// Arch 2 -- 1 entities
|
||||
/// Col 175564825027445222460146453544114453753
|
||||
/// 0: 0 0 0 0 0 0 0 0 0 0 0 0 237 127 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 128 63 0 0 128 63 0 0 128 63 0 0 128 63 78 86 0 0
|
||||
/// Col 162279302565774655543278578489329315472
|
||||
/// 0: 0 0 76 66 0 0 170 66 0 0 136 65 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 128 63 0 0 128 63 0 0 128 63 0 0 128 63 0 0 0 0
|
||||
/// Col 142862377085187052737282554588643015580
|
||||
/// 0: 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
||||
/// Arch 0 -- 2 entities
|
||||
/// Col 175564825027445222460146453544114453753
|
||||
/// 0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 128 63 0 0 128 63 0 0 128 63 0 0 128 63 0 0 0 0
|
||||
/// 1: 0 0 0 0 0 0 0 0 0 0 0 0 237 127 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 128 63 0 0 128 63 0 0 128 63 0 0 128 63 78 86 0 0
|
||||
/// Col 162279302565774655543278578489329315472
|
||||
/// 0: 0 0 112 65 0 0 112 65 0 0 112 65 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 128 63 0 0 128 63 0 0 128 63 0 0 128 63 0 0 0 0
|
||||
/// 1: 0 0 27 67 0 0 184 65 0 0 192 64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 128 63 0 0 128 63 0 0 128 63 0 0 128 63 0 0 0 0
|
||||
/// Col 142862377085187052737282554588643015580
|
||||
/// 0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
||||
/// 1: 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
||||
/// Col 24291284537013640759061027938209843602
|
||||
/// 0: 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
||||
/// 1: 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
||||
/// Arch 3 -- 0 entities
|
||||
/// Col 175564825027445222460146453544114453753
|
||||
/// Col 162279302565774655543278578489329315472
|
||||
/// ```
|
||||
///
|
||||
/// This output prints all Entity ids, the archetype they're in, and the index that they're
|
||||
/// in inside of the archetype. Additionally, the archetypes are printing, including their
|
||||
/// columns type ids, and the contents of the type ids. This output can be used to debug
|
||||
/// the contents of entities inside the archetypes.
|
||||
///
|
||||
/// Below is a template of the output:
|
||||
/// ```
|
||||
/// Entities
|
||||
/// %ENTITY_ID% in archetype %ARCHETYPE_ID% at %INDEX%
|
||||
/// Arch ID -- %ARCHETYPE_LEN% entities
|
||||
/// %FOR EACH COL%
|
||||
/// Col COLUMN_COMPONENT_TYPE_ID
|
||||
/// %FOR EACH ENTITY%
|
||||
/// %ENTITY_INDEX%: %COMPONENT_BYTES%
|
||||
/// ```
|
||||
/// If the template above doesn't help you in understanding the output, read the source code
|
||||
/// of the function. The source code is pretty simple.
|
||||
pub fn debug_print_world(&self) {
|
||||
println!("Entities");
|
||||
for (en, rec) in &self.entities.arch_index {
|
||||
println!(" {} in archetype {} at {}", en.0, rec.id.0, rec.index.0);
|
||||
}
|
||||
|
||||
for arch in self.archetypes.values() {
|
||||
println!("Arch {} -- {} entities", arch.id().0, arch.len());
|
||||
|
||||
for col in &arch.columns {
|
||||
// no clue if doing this is stable, but this is a debug function so :shrug:
|
||||
let tyid: u128 = unsafe { std::mem::transmute(col.info.type_id().as_rust()) };
|
||||
println!(" Col {}", tyid);
|
||||
|
||||
for en in 0..col.len {
|
||||
// get the ptr starting at the component
|
||||
let p = col.borrow_ptr();
|
||||
let p = unsafe { p.as_ptr().add(en * col.info.layout().size()) };
|
||||
|
||||
print!(" {}: ", en);
|
||||
// print each byte of the component
|
||||
for i in 0..col.info.layout().size() {
|
||||
let d = unsafe { *p.add(i) };
|
||||
print!("{} ", d);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
if current_arch.len() > 1 {
|
||||
current_arch.remove_entity(entity, &tick);
|
||||
} else if current_arch.len() == 1 {
|
||||
// The old archetype will only be removed if there was another archetype that would
|
||||
// work for the entity's components, and the old archetype only had a single entity.
|
||||
self.archetypes.remove(&record.id).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -449,10 +364,6 @@ impl World {
|
|||
self.resources.get(&TypeId::of::<T>())
|
||||
.map(|d| unsafe { NonNull::new_unchecked(d.data.as_ptr() as *mut T) })
|
||||
}
|
||||
|
||||
pub fn archetype_count(&self) -> usize {
|
||||
self.archetypes.len()
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Ensure that all non-send resources are only accessible on the main thread.
|
||||
|
|
|
@ -6,6 +6,5 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.81"
|
||||
lyra-ecs = { path = "../lyra-ecs", features = [ "math" ] }
|
||||
lyra-math = { path = "../lyra-math" }
|
||||
|
|
|
@ -1,11 +1,6 @@
|
|||
mod node;
|
||||
pub use node::*;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
mod world_transform;
|
||||
pub use world_transform::*;
|
||||
|
||||
use lyra_ecs::{query::{Entities, ViewOne}, relation::ChildOf, Bundle, Component, World};
|
||||
use lyra_math::Transform;
|
||||
use lyra_ecs::{query::Entities, relation::ChildOf, Bundle, Component, Entity, World};
|
||||
|
||||
// So we can use lyra_ecs::Component derive macro
|
||||
pub(crate) mod lyra_engine {
|
||||
|
@ -14,6 +9,10 @@ pub(crate) mod lyra_engine {
|
|||
}
|
||||
}
|
||||
|
||||
mod node;
|
||||
use lyra_math::{Transform, Vec3};
|
||||
pub use node::*;
|
||||
|
||||
/// A flag spawned on all scene node entities
|
||||
#[derive(Component)]
|
||||
pub struct SceneNodeFlag;
|
||||
|
@ -22,7 +21,7 @@ pub struct SceneNodeFlag;
|
|||
#[derive(Component)]
|
||||
pub struct SceneNodeRoot;
|
||||
|
||||
/// A Graph of nodes that represents the hierarchy of a Scene.
|
||||
/// A SceneGraph is a Graph of nodes that represents the hierarchy of a scene.
|
||||
///
|
||||
/// This SceneGraph is special in the sense that it is literally just an ECS world with methods
|
||||
/// implemented for it that make it easier to use for a SceneGraph.
|
||||
|
@ -42,7 +41,7 @@ impl SceneGraph {
|
|||
|
||||
/// Create a new SceneGraph inside an existing ECS World.
|
||||
pub fn from_world(mut world: World) -> Self {
|
||||
let root_en = world.spawn((WorldTransform::default(), Transform::default(), SceneNodeRoot));
|
||||
let root_en = world.spawn((Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)), SceneNodeRoot));
|
||||
let root = SceneNode::new(None, root_en);
|
||||
|
||||
Self {
|
||||
|
@ -51,21 +50,70 @@ impl SceneGraph {
|
|||
}
|
||||
}
|
||||
|
||||
/// Consume the SceneGraph, inserting its internal world into the provided world.
|
||||
///
|
||||
/// The `into` World will contain all nodes of the SceneGraph. You could iterate through the
|
||||
/// SceneGraph manually if you'd like. The root entity has a [`SceneNodeRoot`] flag component,
|
||||
/// and a Transform component. The children nodes have a [`SceneNodeFlag`] component and a
|
||||
/// Transform component. The transforms of the children will be local, you must find its world
|
||||
/// transform by traversing up the hierarchy of the scene manually with the `ChildOf` relation.
|
||||
///
|
||||
/// Returns: the Entity of the root node in the provided world.
|
||||
pub fn into_world(mut self, into: &mut World) -> Entity {
|
||||
// first insert the root entity into the World.
|
||||
let v = self.world.view_one::<&Transform>(self.root_node.entity());
|
||||
let pos = *v.get().unwrap();
|
||||
let new_root = into.spawn((pos, SceneNodeRoot));
|
||||
|
||||
// now process the children of the root node.
|
||||
Self::traverse_inserting_into(&mut self.world, self.root_node.entity(), new_root, into);
|
||||
new_root
|
||||
}
|
||||
|
||||
/// Recursively traverse the `from` world, starting at `node_en`, inserting the
|
||||
/// children entity into the world.
|
||||
///
|
||||
/// Parameters:
|
||||
/// * `scene_world` - The world that the SceneGraph exists in
|
||||
/// * `scene_en` - The entity of the parent entity inside of the `scene_world`.
|
||||
/// * `parent_en` - The entity of the parent entity inside of the `into` world.
|
||||
/// * `into` - The world to insert the SceneGraph world into.
|
||||
fn traverse_inserting_into(scene_world: &mut World, scene_en: Entity, parent_en: Entity, into: &mut World) {
|
||||
let v = scene_world.view::<(Entities, &Transform)>()
|
||||
.relates_to::<ChildOf>(scene_en);
|
||||
|
||||
// unfortunately, the borrow checker exists, and wasn't happy that `scene_world` was
|
||||
// immutably borrowed with the view (v), and being mutably borrowed in the
|
||||
// recursive call.
|
||||
let mut child_entities = VecDeque::new();
|
||||
|
||||
for ((child_scene_en, pos), _rel) in v.iter() {
|
||||
let into_en = into.spawn((*pos, SceneNodeFlag));
|
||||
into.add_relation(into_en, ChildOf, parent_en);
|
||||
|
||||
child_entities.push_back((child_scene_en, into_en));
|
||||
}
|
||||
|
||||
while let Some((child_scene_en, into_en)) = child_entities.pop_front() {
|
||||
Self::traverse_inserting_into(scene_world, child_scene_en, into_en, into);
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a node to the root node of the SceneGraph.
|
||||
///
|
||||
/// The spawned entity will have a `ChildOf` relation targeting the root node, the
|
||||
/// `SceneNodeFlag` component is also added to the entity.
|
||||
pub fn add_node<B: Bundle>(&mut self, bundle: B) -> SceneNode {
|
||||
pub fn add_node<B: Bundle>(&mut self, local_transform: Transform, bundle: B) -> SceneNode {
|
||||
let node = self.root_node.clone();
|
||||
self.add_node_under(&node, bundle)
|
||||
self.add_node_under(&node, local_transform, bundle)
|
||||
}
|
||||
|
||||
/// Add a node under a parent node.
|
||||
///
|
||||
/// The spawned entity will have a `ChildOf` relation targeting the provided parent node,
|
||||
/// the `SceneNodeFlag` component is also added to the entity.
|
||||
pub fn add_node_under<B: Bundle>(&mut self, parent: &SceneNode, bundle: B) -> SceneNode {
|
||||
world_add_child_node(&mut self.world, parent, bundle)
|
||||
pub fn add_node_under<B: Bundle>(&mut self, parent: &SceneNode, local_transform: Transform, bundle: B) -> SceneNode {
|
||||
world_add_child_node(&mut self.world, parent, local_transform, bundle)
|
||||
}
|
||||
|
||||
/// Insert a component bundle to a SceneNode.
|
||||
|
@ -85,32 +133,29 @@ impl SceneGraph {
|
|||
/// Traverses down the SceneGraph, calling `callback` with each SceneNode and its world transform.
|
||||
///
|
||||
/// The traversal does not include the root scene node.
|
||||
pub fn traverse_down<F, Q>(&self, mut callback: F)
|
||||
pub fn traverse_down<F>(&self, mut callback: F)
|
||||
where
|
||||
F: FnMut(&World, &SceneNode, ViewOne<Q::Query>),
|
||||
Q: lyra_ecs::query::AsQuery,
|
||||
F: FnMut(&World, &SceneNode, Transform),
|
||||
{
|
||||
self.traverse_down_from::<F, Q>(self.root_node.clone(), &mut callback);
|
||||
self.traverse_down_from(self.root_node.clone(), &mut callback);
|
||||
}
|
||||
|
||||
/// Recursively Traverses down the SceneGraph from a starting node, calling `callback` with each
|
||||
/// SceneNode and its world transform.
|
||||
fn traverse_down_from<F, Q>(&self, start: SceneNode, callback: &mut F)
|
||||
fn traverse_down_from<F>(&self, start: SceneNode, callback: &mut F)
|
||||
where
|
||||
F: FnMut(&World, &SceneNode, ViewOne<Q::Query>),
|
||||
Q: lyra_ecs::query::AsQuery,
|
||||
F: FnMut(&World, &SceneNode, Transform),
|
||||
{
|
||||
let v = self.world
|
||||
.view::<Entities>()
|
||||
.view::<(Entities, &Transform)>()
|
||||
.relates_to::<ChildOf>(start.entity());
|
||||
|
||||
for (e, _rel) in v.iter() {
|
||||
for ((e, _), _rel) in v.iter() {
|
||||
let node = SceneNode::new(Some(start.entity()), e);
|
||||
//let world_pos = node.world_transform(self);
|
||||
let v = self.world.view_one::<Q>(e);
|
||||
callback(&self.world, &node, v);
|
||||
let world_pos = node.world_transform(self);
|
||||
callback(&self.world, &node, world_pos);
|
||||
|
||||
self.traverse_down_from::<F, Q>(node, callback);
|
||||
self.traverse_down_from(node, callback);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -128,9 +173,9 @@ impl SceneGraph {
|
|||
///
|
||||
/// The spawned entity will have a `ChildOf` relation targeting the provided parent node,
|
||||
/// the `SceneNodeFlag` component is also added to the entity.
|
||||
pub(crate) fn world_add_child_node<B: Bundle>(world: &mut World, parent: &SceneNode, bundle: B) -> SceneNode {
|
||||
pub(crate) fn world_add_child_node<B: Bundle>(world: &mut World, parent: &SceneNode, local_transform: Transform, bundle: B) -> SceneNode {
|
||||
let e = world.spawn(bundle);
|
||||
world.insert(e, (SceneNodeFlag,));
|
||||
world.insert(e, (SceneNodeFlag, local_transform));
|
||||
world.add_relation(e, ChildOf, parent.entity());
|
||||
|
||||
SceneNode::new(Some(parent.entity()), e)
|
||||
|
@ -138,10 +183,10 @@ pub(crate) fn world_add_child_node<B: Bundle>(world: &mut World, parent: &SceneN
|
|||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use lyra_ecs::{query::{filter::{Has, Not}, Entities}, relation::{ChildOf, RelationOriginComponent}, Component};
|
||||
use lyra_ecs::Component;
|
||||
use lyra_math::{Transform, Vec3};
|
||||
|
||||
use crate::{lyra_engine, WorldTransform, SceneGraph};
|
||||
use crate::{lyra_engine, SceneGraph};
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct FakeMesh;
|
||||
|
@ -150,7 +195,7 @@ pub mod tests {
|
|||
fn single_node_hierarchy() {
|
||||
let mut scene = SceneGraph::new();
|
||||
|
||||
let a = scene.add_node((Transform::from_translation(Vec3::new(10.0, 10.0, 10.0)), FakeMesh));
|
||||
let a = scene.add_node(Transform::from_translation(Vec3::new(10.0, 10.0, 10.0)), FakeMesh);
|
||||
assert!(a.parent(&scene).unwrap() == scene.root_node);
|
||||
}
|
||||
|
||||
|
@ -158,10 +203,10 @@ pub mod tests {
|
|||
fn double_node_hierarchy() {
|
||||
let mut scene = SceneGraph::new();
|
||||
|
||||
let a = scene.add_node((Transform::from_translation(Vec3::new(10.0, 10.0, 10.0)), FakeMesh));
|
||||
let a = scene.add_node(Transform::from_translation(Vec3::new(10.0, 10.0, 10.0)), FakeMesh);
|
||||
assert!(a.parent(&scene).unwrap() == scene.root_node);
|
||||
|
||||
let b = a.add_node(&mut scene, (Transform::from_translation(Vec3::new(50.0, 50.0, 50.0)), FakeMesh));
|
||||
let b = a.add_node(&mut scene, Transform::from_translation(Vec3::new(50.0, 50.0, 50.0)), FakeMesh);
|
||||
assert!(b.parent(&scene).unwrap() == a);
|
||||
}
|
||||
|
||||
|
@ -171,25 +216,68 @@ pub mod tests {
|
|||
|
||||
let mut scene = SceneGraph::new();
|
||||
|
||||
let a = scene.add_node((WorldTransform::default(), Transform::from_translation(v2s[0]), FakeMesh));
|
||||
let a = scene.add_node(Transform::from_translation(v2s[0]), FakeMesh);
|
||||
assert!(a.parent(&scene).unwrap() == scene.root_node);
|
||||
let b = a.add_node(&mut scene, (WorldTransform::default(), Transform::from_translation(v2s[1]), FakeMesh));
|
||||
let b = a.add_node(&mut scene, Transform::from_translation(v2s[1]), FakeMesh);
|
||||
assert!(b.parent(&scene).unwrap() == a);
|
||||
|
||||
let view = scene.world.view::<(Entities, &mut WorldTransform, &Transform, Not<Has<RelationOriginComponent<ChildOf>>>)>();
|
||||
crate::system_update_world_transforms(&scene.world, view).unwrap();
|
||||
|
||||
let mut idx = 0;
|
||||
scene.traverse_down::<_, &WorldTransform>(|_, _, v| {
|
||||
let pos = v.get().unwrap();
|
||||
scene.traverse_down(|_, _, pos| {
|
||||
if idx == 0 {
|
||||
assert_eq!(**pos, Transform::from_translation(v2s[idx]));
|
||||
assert_eq!(pos, Transform::from_translation(v2s[idx]));
|
||||
} else if idx == 1 {
|
||||
let t = v2s.iter().sum();
|
||||
assert_eq!(**pos, Transform::from_translation(t));
|
||||
assert_eq!(pos, Transform::from_translation(t));
|
||||
}
|
||||
|
||||
idx += 1;
|
||||
});
|
||||
}
|
||||
|
||||
/* #[test]
|
||||
fn inserting_and_from_world() {
|
||||
let v2s = vec![Vec3::new(10.0, 10.0, 10.0), Vec3::new(50.0, 50.0, 50.0)];
|
||||
|
||||
let mut scene = SceneGraph::new();
|
||||
|
||||
let a = scene.add_node(Transform::from_translation(v2s[0]), FakeMesh);
|
||||
assert!(a.parent(&scene).unwrap() == scene.root_node);
|
||||
let b = a.add_node(&mut scene, Transform::from_translation(v2s[1]), FakeMesh);
|
||||
assert!(b.parent(&scene).unwrap() == a);
|
||||
|
||||
let mut other_world = World::new();
|
||||
let root = scene.into_world(&mut other_world);
|
||||
|
||||
// check all of the entities inside of the World
|
||||
let (root_pos, _) = other_world.view_one::<(&Transform, &SceneNodeRoot)>(root).get().unwrap();
|
||||
assert_eq!(*root_pos, Transform::from_xyz(0.0, 0.0, 0.0));
|
||||
|
||||
let ((child_en, child_pos), _) = other_world.view::<(Entities, &Transform)>()
|
||||
.relates_to::<ChildOf>(root).iter().next().unwrap();
|
||||
assert_eq!(*child_pos, Transform::from_translation(v2s[0]));
|
||||
|
||||
let ((_, childchild_pos), _) = other_world.view::<(Entities, &Transform)>()
|
||||
.relates_to::<ChildOf>(child_en).iter().next().unwrap();
|
||||
assert_eq!(*childchild_pos, Transform::from_translation(v2s[1]));
|
||||
|
||||
drop(root_pos);
|
||||
drop(child_pos);
|
||||
drop(childchild_pos);
|
||||
|
||||
// Now get the SceneGraph inside the World and use the nice utility tools to traverse it.
|
||||
let scene = SceneGraph::from_world(&mut other_world, root)
|
||||
.expect("Failed to get SceneGraph from World!");
|
||||
|
||||
let mut idx = 0;
|
||||
scene.traverse_down(|_e, pos| {
|
||||
if idx == 0 {
|
||||
assert_eq!(pos, Transform::from_translation(v2s[idx]));
|
||||
} else if idx == 1 {
|
||||
let t = v2s.iter().sum();
|
||||
assert_eq!(pos, Transform::from_translation(t));
|
||||
}
|
||||
|
||||
idx += 1;
|
||||
});
|
||||
} */
|
||||
}
|
|
@ -1,10 +1,11 @@
|
|||
use lyra_ecs::{query::Entities, relation::{ChildOf, RelationOriginComponent}, Bundle, Entity};
|
||||
use lyra_math::Transform;
|
||||
|
||||
use crate::SceneGraph;
|
||||
|
||||
/// A node inside of the Scene
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct SceneNode {
|
||||
//world: WorldLock,
|
||||
parent: Option<Entity>,
|
||||
entity: Entity
|
||||
}
|
||||
|
@ -51,33 +52,50 @@ impl SceneNode {
|
|||
///
|
||||
/// The spawned entity backing the scene node will have a `ChildOf` relation targeting
|
||||
/// the provided parent node, the `SceneNodeFlag` component is also added to the entity.
|
||||
pub fn add_node<B: Bundle>(&self, graph: &mut SceneGraph, bundle: B) -> SceneNode {
|
||||
graph.add_node_under(self, bundle)
|
||||
pub fn add_node<B: Bundle>(&self, graph: &mut SceneGraph, local_transform: Transform, bundle: B) -> SceneNode {
|
||||
graph.add_node_under(self, local_transform, bundle)
|
||||
}
|
||||
|
||||
pub fn local_transform(&self, graph: &SceneGraph) -> Transform {
|
||||
let v = graph.world.view_one::<&Transform>(self.entity);
|
||||
|
||||
let t = v.get().expect("Somehow the SceneNode is missing its Transform!");
|
||||
t.clone()
|
||||
}
|
||||
|
||||
/// Retrieves the world transform of the Node.
|
||||
///
|
||||
/// This traverses up the SceneGraph from this node.
|
||||
pub fn world_transform(&self, graph: &SceneGraph) -> Transform {
|
||||
match self.parent(graph) {
|
||||
Some(parent) => {
|
||||
let pt = parent.world_transform(graph);
|
||||
pt + self.local_transform(graph)
|
||||
},
|
||||
None => {
|
||||
self.local_transform(graph)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use lyra_ecs::{query::{filter::{Has, Not}, Entities}, relation::{ChildOf, RelationOriginComponent}};
|
||||
use lyra_math::{Transform, Vec3};
|
||||
|
||||
use crate::{tests::FakeMesh, WorldTransform, SceneGraph};
|
||||
use crate::{tests::FakeMesh, SceneGraph};
|
||||
|
||||
#[test]
|
||||
fn node_world_transform() {
|
||||
let mut scene = SceneGraph::new();
|
||||
|
||||
let a = scene.add_node((WorldTransform::default(), Transform::from_translation(Vec3::new(10.0, 10.0, 10.0)), FakeMesh));
|
||||
let a = scene.add_node(Transform::from_translation(Vec3::new(10.0, 10.0, 10.0)), FakeMesh);
|
||||
assert!(a.parent(&scene).unwrap() == scene.root_node);
|
||||
|
||||
let b = a.add_node(&mut scene, (WorldTransform::default(), Transform::from_translation(Vec3::new(50.0, 50.0, 50.0)), FakeMesh));
|
||||
let b = a.add_node(&mut scene, Transform::from_translation(Vec3::new(50.0, 50.0, 50.0)), FakeMesh);
|
||||
assert!(b.parent(&scene).unwrap() == a);
|
||||
|
||||
// update global transforms
|
||||
let view = scene.world.view::<(Entities, &mut WorldTransform, &Transform, Not<Has<RelationOriginComponent<ChildOf>>>)>();
|
||||
crate::system_update_world_transforms(&scene.world, view).unwrap();
|
||||
|
||||
let tran = scene.world.view_one::<&WorldTransform>(b.entity).get().unwrap();
|
||||
assert_eq!(**tran, Transform::from_translation(Vec3::new(60.0, 60.0, 60.0)));
|
||||
let wrld_tran = b.world_transform(&scene);
|
||||
assert_eq!(wrld_tran, Transform::from_translation(Vec3::new(60.0, 60.0, 60.0)));
|
||||
}
|
||||
}
|
|
@ -1,171 +0,0 @@
|
|||
use std::ops::Deref;
|
||||
|
||||
use lyra_ecs::{query::{filter::{Has, Not}, Entities, View}, relation::{ChildOf, RelationOriginComponent}, Component, Entity, World};
|
||||
use lyra_math::Transform;
|
||||
use crate::lyra_engine;
|
||||
|
||||
/// The world transform of an entity.
|
||||
///
|
||||
/// A Transform represents the relative position of the entity to its parent entity, while
|
||||
/// a world transform is the position relative to the World. When wanting to move an entity,
|
||||
/// you should use its [`Transform`]. You cannot mutate [`WorldTransform`] as its managed completey
|
||||
/// by the [`system_update_world_transforms`] system. For the WorldTransform to work properly, you
|
||||
/// must have both a [`Transform`] and [`WorldTransform`] on the entities in the scene.
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Default, Component)]
|
||||
pub struct WorldTransform(pub(crate) Transform);
|
||||
|
||||
impl Deref for WorldTransform {
|
||||
type Target = Transform;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// A system that updates the [`WorldTransform`]'s for entities and their children.
|
||||
///
|
||||
/// For entities without parents, this will update world transform to match local transform.
|
||||
/// For any children entities, their [`WorldTransform`]s will be updated to reflect the changes
|
||||
/// of its parent entity.
|
||||
pub fn system_update_world_transforms(world: &World, view: View<(Entities, &mut WorldTransform, &Transform, Not<Has<RelationOriginComponent<ChildOf>>>)>) -> anyhow::Result<()> {
|
||||
for (en, mut world_tran, tran, _) in view.into_iter() {
|
||||
world_tran.0 = *tran;
|
||||
recurse_update_trans(world, &world_tran, en);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn recurse_update_trans(world: &World, parent_transform: &WorldTransform, entity: Entity) {
|
||||
// store entities and their world transform to process outside of the view.
|
||||
// it must be done after to avoid attempts of multiple mutable borrows to the archetype column
|
||||
// with WorldTransform.
|
||||
let mut next_entities = vec![];
|
||||
|
||||
for ((en, mut world_tran, tran), _) in
|
||||
world.view::<(Entities, &mut WorldTransform, &Transform)>()
|
||||
.relates_to::<ChildOf>(entity)
|
||||
.into_iter()
|
||||
{
|
||||
world_tran.0 = parent_transform.0 + *tran;
|
||||
next_entities.push((en, world_tran.0.clone()));
|
||||
}
|
||||
|
||||
for (en, pos) in next_entities.into_iter() {
|
||||
recurse_update_trans(world, &WorldTransform(pos), en);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use lyra_ecs::{query::{filter::{Has, Not}, Entities}, relation::{ChildOf, RelationOriginComponent}, World};
|
||||
use lyra_math::Transform;
|
||||
|
||||
use crate::{system_update_world_transforms, WorldTransform};
|
||||
|
||||
#[test]
|
||||
fn test_system() {
|
||||
let mut world = World::new();
|
||||
|
||||
let parent = world.spawn((WorldTransform::default(), Transform::from_xyz(10.0, 10.0, 10.0)));
|
||||
let child = world.spawn((WorldTransform::default(), Transform::from_xyz(15.0, 15.0, 15.0)));
|
||||
world.add_relation(child, ChildOf, parent);
|
||||
|
||||
let view = world.view::<(Entities, &mut WorldTransform, &Transform, Not<Has<RelationOriginComponent<ChildOf>>>)>();
|
||||
system_update_world_transforms(&world, view).unwrap();
|
||||
|
||||
let g = world.view_one::<&WorldTransform>(child).get().unwrap();
|
||||
assert_eq!(**g, Transform::from_xyz(25.0, 25.0, 25.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_system_many_entities() {
|
||||
let mut world = World::new();
|
||||
|
||||
let parent = world.spawn((WorldTransform::default(), Transform::from_xyz(10.0, 10.0, 10.0)));
|
||||
|
||||
let mut children = vec![];
|
||||
let mut base_offset = 15.0;
|
||||
for _ in 0..10 {
|
||||
let en = world.spawn((WorldTransform::default(), Transform::from_xyz(base_offset, base_offset, base_offset)));
|
||||
world.add_relation(en, ChildOf, parent);
|
||||
base_offset += 10.0;
|
||||
children.push(en);
|
||||
}
|
||||
|
||||
let second_child = world.spawn((WorldTransform::default(), Transform::from_xyz(5.0, 3.0, 8.0)));
|
||||
world.add_relation(second_child, ChildOf, parent);
|
||||
|
||||
let view = world.view::<(Entities, &mut WorldTransform, &Transform, Not<Has<RelationOriginComponent<ChildOf>>>)>();
|
||||
system_update_world_transforms(&world, view).unwrap();
|
||||
|
||||
let mut base_offset = 25.0;
|
||||
for child in children.into_iter() {
|
||||
let g = world.view_one::<&WorldTransform>(child).get().unwrap();
|
||||
println!("Child {:?} at {:?}", child, g.translation);
|
||||
assert_eq!(**g, Transform::from_xyz(base_offset, base_offset, base_offset));
|
||||
|
||||
base_offset += 10.0;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_system_many_children() {
|
||||
let mut world = World::new();
|
||||
|
||||
let parent = world.spawn((WorldTransform::default(), Transform::from_xyz(10.0, 10.0, 10.0)));
|
||||
let first_child = world.spawn((WorldTransform::default(), Transform::from_xyz(15.0, 15.0, 15.0)));
|
||||
world.add_relation(first_child, ChildOf, parent);
|
||||
|
||||
let sec_chi = world.spawn((WorldTransform::default(), Transform::from_xyz(155.0, 23.0, 6.0)));
|
||||
world.add_relation(sec_chi, ChildOf, first_child);
|
||||
|
||||
let thir_chi = world.spawn((WorldTransform::default(), Transform::from_xyz(51.0, 85.0, 17.0)));
|
||||
world.add_relation(thir_chi, ChildOf, sec_chi);
|
||||
|
||||
let four_child = world.spawn((WorldTransform::default(), Transform::from_xyz(24.0, 61.0, 65.0)));
|
||||
world.add_relation(four_child, ChildOf, thir_chi);
|
||||
|
||||
let five_child = world.spawn((WorldTransform::default(), Transform::from_xyz(356.0, 54.0, 786.0)));
|
||||
world.add_relation(five_child, ChildOf, four_child);
|
||||
|
||||
let view = world.view::<(Entities, &mut WorldTransform, &Transform, Not<Has<RelationOriginComponent<ChildOf>>>)>();
|
||||
system_update_world_transforms(&world, view).unwrap();
|
||||
|
||||
let g = world.view_one::<&WorldTransform>(five_child).get().unwrap();
|
||||
assert_eq!(**g, Transform::from_xyz(611.0, 248.0, 899.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_system_branched_children() {
|
||||
let mut world = World::new();
|
||||
|
||||
let parent = world.spawn((WorldTransform::default(), Transform::from_xyz(10.0, 10.0, 10.0)));
|
||||
let first_child = world.spawn((WorldTransform::default(), Transform::from_xyz(15.0, 15.0, 15.0)));
|
||||
world.add_relation(first_child, ChildOf, parent);
|
||||
|
||||
let sec_chi = world.spawn((WorldTransform::default(), Transform::from_xyz(155.0, 23.0, 6.0)));
|
||||
world.add_relation(sec_chi, ChildOf, first_child);
|
||||
|
||||
let thir_chi = world.spawn((WorldTransform::default(), Transform::from_xyz(51.0, 85.0, 17.0)));
|
||||
world.add_relation(thir_chi, ChildOf, first_child);
|
||||
|
||||
let four_child = world.spawn((WorldTransform::default(), Transform::from_xyz(24.0, 61.0, 65.0)));
|
||||
world.add_relation(four_child, ChildOf, thir_chi);
|
||||
|
||||
let five_child = world.spawn((WorldTransform::default(), Transform::from_xyz(356.0, 54.0, 786.0)));
|
||||
world.add_relation(five_child, ChildOf, sec_chi);
|
||||
|
||||
let view = world.view::<(Entities, &mut WorldTransform, &Transform, Not<Has<RelationOriginComponent<ChildOf>>>)>();
|
||||
system_update_world_transforms(&world, view).unwrap();
|
||||
|
||||
let g = world.view_one::<&WorldTransform>(five_child).get().unwrap();
|
||||
assert_eq!(**g, Transform::from_xyz(536.0, 102.0, 817.0));
|
||||
|
||||
let g = world.view_one::<&WorldTransform>(thir_chi).get().unwrap();
|
||||
assert_eq!(**g, Transform::from_xyz(76.0, 110.0, 42.0));
|
||||
|
||||
let g = world.view_one::<&WorldTransform>(four_child).get().unwrap();
|
||||
assert_eq!(**g, Transform::from_xyz(100.0, 171.0, 107.0));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue