2024-10-19 21:45:59 +00:00
|
|
|
use std::{any::TypeId, collections::HashMap, ops::Deref, ptr::NonNull};
|
2024-03-31 02:12:00 +00:00
|
|
|
|
|
|
|
use atomic_refcell::{AtomicRef, AtomicRefMut};
|
2023-05-25 04:11:16 +00:00
|
|
|
|
2024-09-28 01:03:27 +00:00
|
|
|
use crate::{archetype::{Archetype, ArchetypeId}, bundle::Bundle, query::{dynamic::DynamicView, AsFilter, AsQuery, Query, Res, ResMut, ViewIter, ViewOne, ViewState}, resource::ResourceData, ComponentInfo, DynTypeId, DynamicBundle, Entities, Entity, ResourceObject, Tick, TickTracker, TrackedResource};
|
2023-05-25 04:11:16 +00:00
|
|
|
|
|
|
|
/// The id of the entity for the Archetype.
|
2024-03-03 01:20:38 +00:00
|
|
|
///
|
|
|
|
/// The Archetype uses this as the index in the component columns
|
2023-05-25 04:11:16 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct ArchetypeEntityId(pub u64);
|
|
|
|
|
2024-10-19 21:45:59 +00:00
|
|
|
impl Deref for ArchetypeEntityId {
|
|
|
|
type Target = u64;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-14 05:25:09 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
2023-11-25 23:43:11 +00:00
|
|
|
pub struct Record {
|
2023-12-14 05:25:09 +00:00
|
|
|
pub id: ArchetypeId,
|
|
|
|
pub index: ArchetypeEntityId,
|
2023-05-25 04:11:16 +00:00
|
|
|
}
|
|
|
|
|
2024-04-27 04:21:26 +00:00
|
|
|
#[derive(Clone)]
|
2023-05-25 04:11:16 +00:00
|
|
|
pub struct World {
|
2024-10-21 01:20:43 +00:00
|
|
|
pub archetypes: HashMap<ArchetypeId, Archetype>,
|
2023-05-25 04:11:16 +00:00
|
|
|
next_archetype_id: ArchetypeId,
|
2023-11-29 04:25:47 +00:00
|
|
|
resources: HashMap<TypeId, ResourceData>,
|
2023-12-26 19:12:53 +00:00
|
|
|
tracker: TickTracker,
|
2024-10-21 01:20:43 +00:00
|
|
|
pub entities: Entities,
|
2023-05-25 04:11:16 +00:00
|
|
|
}
|
|
|
|
|
2023-12-22 17:22:10 +00:00
|
|
|
impl Default for World {
|
|
|
|
fn default() -> Self {
|
2023-05-25 04:11:16 +00:00
|
|
|
Self {
|
|
|
|
archetypes: HashMap::new(),
|
|
|
|
next_archetype_id: ArchetypeId(0),
|
2023-11-29 04:25:47 +00:00
|
|
|
resources: HashMap::new(),
|
2023-12-26 19:12:53 +00:00
|
|
|
tracker: TickTracker::new(),
|
2024-02-23 21:34:21 +00:00
|
|
|
entities: Entities::default(),
|
2023-05-25 04:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-22 17:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl World {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
2023-05-25 04:11:16 +00:00
|
|
|
|
2024-02-23 21:34:21 +00:00
|
|
|
/// Reserves an entity in the world
|
|
|
|
pub fn reserve_entity(&mut self) -> Entity {
|
|
|
|
self.entities.reserve()
|
2023-05-25 04:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn spawn<B>(&mut self, bundle: B) -> Entity
|
2024-02-23 21:34:21 +00:00
|
|
|
where
|
|
|
|
B: Bundle
|
|
|
|
{
|
|
|
|
let new_entity = self.reserve_entity();
|
|
|
|
self.spawn_into(new_entity, bundle);
|
|
|
|
new_entity
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Spawn the components into a reserved entity. Only do this with entities that
|
2024-03-03 02:20:19 +00:00
|
|
|
/// were reserved with [`World::reserve_entity`].
|
2024-02-23 21:34:21 +00:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
/// Do not use this method with an entity that is currently alive, it WILL cause undefined behavior.
|
|
|
|
pub fn spawn_into<B>(&mut self, entity: Entity, bundle: B)
|
2023-05-25 04:11:16 +00:00
|
|
|
where
|
|
|
|
B: Bundle
|
|
|
|
{
|
2024-09-28 01:03:27 +00:00
|
|
|
let tick = self.current_tick();
|
2023-11-26 05:56:30 +00:00
|
|
|
let bundle_types = bundle.type_ids();
|
2023-05-25 04:11:16 +00:00
|
|
|
|
|
|
|
// try to find an archetype
|
|
|
|
let archetype = self.archetypes
|
|
|
|
.values_mut()
|
2023-12-14 05:25:09 +00:00
|
|
|
.find(|a| a.is_archetype_for(&bundle_types));
|
2023-05-25 04:11:16 +00:00
|
|
|
|
|
|
|
if let Some(archetype) = archetype {
|
2024-02-23 21:34:21 +00:00
|
|
|
// make at just one check to ensure you're not spawning twice
|
2024-03-03 02:20:19 +00:00
|
|
|
debug_assert!(!archetype.entity_ids.contains_key(&entity),
|
2024-02-23 21:34:21 +00:00
|
|
|
"You attempted to spawn components into an entity that already exists!");
|
|
|
|
|
|
|
|
let arche_idx = archetype.add_entity(entity, bundle, &tick);
|
2023-05-25 04:11:16 +00:00
|
|
|
|
|
|
|
// Create entity record and store it
|
|
|
|
let record = Record {
|
2024-03-03 02:20:19 +00:00
|
|
|
id: archetype.id(),
|
2023-05-25 04:11:16 +00:00
|
|
|
index: arche_idx,
|
|
|
|
};
|
|
|
|
|
2024-02-23 21:34:21 +00:00
|
|
|
self.entities.insert_entity_record(entity, record);
|
2023-05-25 04:11:16 +00:00
|
|
|
}
|
|
|
|
// create a new archetype if one isn't found
|
|
|
|
else {
|
|
|
|
// create archetype
|
|
|
|
let new_arch_id = self.next_archetype_id.increment();
|
2023-11-25 23:43:11 +00:00
|
|
|
let mut archetype = Archetype::from_bundle_info(new_arch_id, bundle.info());
|
2024-02-23 21:34:21 +00:00
|
|
|
let entity_arch_id = archetype.add_entity(entity, bundle, &tick);
|
2023-05-25 04:11:16 +00:00
|
|
|
|
|
|
|
// store archetype
|
|
|
|
self.archetypes.insert(new_arch_id, archetype);
|
|
|
|
|
|
|
|
// Create entity record and store it
|
|
|
|
let record = Record {
|
|
|
|
id: new_arch_id,
|
|
|
|
// this is the first entity in the archetype
|
2023-11-25 23:43:11 +00:00
|
|
|
index: entity_arch_id,
|
2023-05-25 04:11:16 +00:00
|
|
|
};
|
|
|
|
|
2024-02-23 21:34:21 +00:00
|
|
|
self.entities.insert_entity_record(entity, record);
|
2023-05-25 04:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-27 02:05:35 +00:00
|
|
|
/// Despawn an entity from the World
|
|
|
|
pub fn despawn(&mut self, entity: Entity) {
|
2024-09-28 01:03:27 +00:00
|
|
|
let tick = self.current_tick();
|
2024-02-23 21:34:21 +00:00
|
|
|
if let Some(record) = self.entities.arch_index.get_mut(&entity.id) {
|
2023-11-27 02:05:35 +00:00
|
|
|
let arch = self.archetypes.get_mut(&record.id).unwrap();
|
|
|
|
|
2023-12-26 19:12:53 +00:00
|
|
|
if let Some((moved, new_index)) = arch.remove_entity(entity, &tick) {
|
2023-11-27 02:05:35 +00:00
|
|
|
// replace the archetype index of the moved index with its new index.
|
2024-02-23 21:34:21 +00:00
|
|
|
self.entities.arch_index.get_mut(&moved.id).unwrap().index = new_index;
|
2023-11-27 02:05:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-22 04:20:42 +00:00
|
|
|
/// Insert a component bundle into an existing entity.
|
|
|
|
///
|
|
|
|
/// If the components are already existing on the entity, they will be updated, else the
|
|
|
|
/// entity will be moved to a different Archetype that can store the entity. That may
|
|
|
|
/// involve creating a new Archetype.
|
2023-12-14 05:25:09 +00:00
|
|
|
pub fn insert<B>(&mut self, entity: Entity, bundle: B)
|
|
|
|
where
|
|
|
|
B: Bundle
|
|
|
|
{
|
2024-09-28 01:03:27 +00:00
|
|
|
let tick = self.current_tick();
|
2024-10-03 00:54:54 +00:00
|
|
|
let record = self.entities.entity_record(entity);
|
|
|
|
|
|
|
|
if record.is_none() {
|
|
|
|
//let mut combined_column_infos: Vec<ComponentInfo> = bundle.info().columns.iter().map(|c| c.info).collect();
|
|
|
|
let new_arch_id = self.next_archetype_id.increment();
|
|
|
|
let mut archetype = Archetype::from_bundle_info(new_arch_id, bundle.info());
|
|
|
|
|
|
|
|
let mut dbun = DynamicBundle::new();
|
|
|
|
dbun.push_bundle(bundle);
|
|
|
|
|
|
|
|
let entity_arch_id = archetype.add_entity(entity, dbun, &tick);
|
|
|
|
|
|
|
|
self.archetypes.insert(new_arch_id, archetype);
|
|
|
|
|
|
|
|
// Create entity record and store it
|
|
|
|
let record = Record {
|
|
|
|
id: new_arch_id,
|
|
|
|
index: entity_arch_id,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.entities.insert_entity_record(entity, record);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let record = record.unwrap();
|
|
|
|
|
2023-12-14 05:25:09 +00:00
|
|
|
let current_arch = self.archetypes.get(&record.id).unwrap();
|
2024-03-03 21:19:59 +00:00
|
|
|
let current_arch_len = current_arch.len();
|
|
|
|
|
2024-04-22 04:20:42 +00:00
|
|
|
let mut contains_all = true;
|
|
|
|
for id in bundle.type_ids() {
|
|
|
|
contains_all = contains_all && current_arch.get_column(id).is_some();
|
|
|
|
}
|
|
|
|
|
|
|
|
if contains_all {
|
|
|
|
let current_arch = self.archetypes.get_mut(&record.id).unwrap();
|
|
|
|
let entry_idx = *current_arch.entity_indexes()
|
|
|
|
.get(&entity).unwrap();
|
|
|
|
|
|
|
|
bundle.take(|ptr, id, _info| {
|
|
|
|
let col = current_arch.get_column_mut(id).unwrap();
|
|
|
|
unsafe { col.set_at(entry_idx.0 as _, ptr, tick) };
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-11 02:26:49 +00:00
|
|
|
// 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());
|
2023-12-14 05:25:09 +00:00
|
|
|
|
2024-04-11 02:26:49 +00:00
|
|
|
// 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());
|
2023-12-14 05:25:09 +00:00
|
|
|
|
2024-04-11 02:26:49 +00:00
|
|
|
// pointers only for the old columns
|
|
|
|
let old_columns: Vec<(NonNull<u8>, ComponentInfo)> = current_arch.columns.iter()
|
2024-03-03 21:19:59 +00:00
|
|
|
.map(|c| unsafe { (NonNull::new_unchecked(c.borrow_ptr().as_ptr()), c.info) })
|
|
|
|
.collect();
|
2023-12-26 19:12:53 +00:00
|
|
|
|
2024-10-03 00:54:54 +00:00
|
|
|
// try to find an archetype that this entity and its new components can fit into
|
2024-04-11 02:26:49 +00:00
|
|
|
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() {
|
2023-12-14 05:25:09 +00:00
|
|
|
unsafe {
|
|
|
|
let ptr = NonNull::new_unchecked(col_ptr.as_ptr()
|
2024-03-03 21:19:59 +00:00
|
|
|
.add(record.index.0 as usize * col_info.layout().size()));
|
2024-04-11 02:26:49 +00:00
|
|
|
dbun.push_unknown(ptr, col_info);
|
2023-12-14 05:25:09 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-11 02:26:49 +00:00
|
|
|
dbun.push_bundle(bundle);
|
2023-12-14 05:25:09 +00:00
|
|
|
|
2024-04-11 02:26:49 +00:00
|
|
|
let res_index = arch.add_entity(entity, dbun, &tick);
|
2024-03-03 21:19:59 +00:00
|
|
|
arch.ensure_synced();
|
2023-12-14 05:25:09 +00:00
|
|
|
|
|
|
|
let new_record = Record {
|
2024-03-03 02:20:19 +00:00
|
|
|
id: arch.id(),
|
2023-12-14 05:25:09 +00:00
|
|
|
index: res_index,
|
|
|
|
};
|
2024-02-23 21:34:21 +00:00
|
|
|
self.entities.insert_entity_record(entity, new_record);
|
2023-12-14 05:25:09 +00:00
|
|
|
} else {
|
2024-03-03 21:19:59 +00:00
|
|
|
if current_arch_len == 1 {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2023-12-14 05:25:09 +00:00
|
|
|
let new_arch_id = self.next_archetype_id.increment();
|
2024-04-11 02:26:49 +00:00
|
|
|
let mut archetype = Archetype::from_bundle_info(new_arch_id, combined_column_infos);
|
2024-03-03 21:19:59 +00:00
|
|
|
|
2024-04-11 02:26:49 +00:00
|
|
|
let mut dbun = DynamicBundle::new();
|
|
|
|
for (column_ptr, column_info) in old_columns.into_iter() {
|
2024-03-03 21:19:59 +00:00
|
|
|
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()));
|
2024-04-11 02:26:49 +00:00
|
|
|
dbun.push_unknown(comp_ptr, column_info);
|
2024-03-03 21:19:59 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-11 02:26:49 +00:00
|
|
|
dbun.push_bundle(bundle);
|
|
|
|
|
|
|
|
let entity_arch_id = archetype.add_entity(entity, dbun, &tick);
|
2023-12-14 05:25:09 +00:00
|
|
|
|
|
|
|
self.archetypes.insert(new_arch_id, archetype);
|
|
|
|
|
|
|
|
// Create entity record and store it
|
|
|
|
let record = Record {
|
|
|
|
id: new_arch_id,
|
|
|
|
index: entity_arch_id,
|
|
|
|
};
|
|
|
|
|
2024-02-23 21:34:21 +00:00
|
|
|
self.entities.insert_entity_record(entity, record);
|
2023-12-14 05:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let current_arch = self.archetypes.get_mut(&record.id).unwrap();
|
2024-04-11 02:26:49 +00:00
|
|
|
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:
|
2024-04-22 04:20:42 +00:00
|
|
|
/// ```nobuild
|
2024-04-11 02:26:49 +00:00
|
|
|
/// 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:
|
2024-04-22 04:20:42 +00:00
|
|
|
/// ```nobuild
|
2024-04-11 02:26:49 +00:00
|
|
|
/// 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!();
|
|
|
|
}
|
|
|
|
}
|
2024-03-03 21:19:59 +00:00
|
|
|
}
|
2023-12-14 05:25:09 +00:00
|
|
|
}
|
|
|
|
|
2024-01-13 16:52:20 +00:00
|
|
|
pub fn entity_archetype(&self, entity: Entity) -> Option<&Archetype> {
|
2024-02-23 21:34:21 +00:00
|
|
|
self.entities.entity_record(entity)
|
2024-01-13 16:52:20 +00:00
|
|
|
.and_then(|record| self.archetypes.get(&record.id))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn entity_archetype_mut(&mut self, entity: Entity) -> Option<&mut Archetype> {
|
2024-02-23 21:34:21 +00:00
|
|
|
self.entities.entity_record(entity)
|
2024-01-13 16:52:20 +00:00
|
|
|
.and_then(|record| self.archetypes.get_mut(&record.id))
|
|
|
|
}
|
|
|
|
|
2023-12-14 05:25:09 +00:00
|
|
|
/// View into the world for a set of entities that satisfy the queries.
|
2024-03-03 01:20:38 +00:00
|
|
|
pub fn view<Q: AsQuery>(&self) -> ViewState<Q::Query, ()> {
|
|
|
|
self.filtered_view::<Q, ()>()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// View into the world for a set of entities that satisfy the query and the filter.
|
2024-09-21 18:06:21 +00:00
|
|
|
pub fn filtered_view<Q: AsQuery, F: AsFilter>(&self) -> ViewState<Q::Query, F::Filter> {
|
2024-03-03 01:20:38 +00:00
|
|
|
let archetypes = self.archetypes.values().collect();
|
2024-09-21 18:06:21 +00:00
|
|
|
ViewState::<Q::Query, F::Filter>::new(self, Q::Query::new(), F::Filter::new(), archetypes)
|
2024-03-03 01:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// View into the world for a set of entities that satisfy the queries.
|
|
|
|
pub fn view_iter<Q: AsQuery>(&self) -> ViewIter<Q::Query, ()> {
|
2023-11-26 00:58:14 +00:00
|
|
|
let archetypes = self.archetypes.values().collect();
|
2024-03-03 01:20:38 +00:00
|
|
|
let v = ViewState::new(self, Q::Query::new(), (), archetypes);
|
2023-11-26 00:58:14 +00:00
|
|
|
v.into_iter()
|
2023-05-26 02:59:27 +00:00
|
|
|
}
|
2023-11-29 04:25:47 +00:00
|
|
|
|
2024-03-08 05:19:23 +00:00
|
|
|
/// View into the world for a set of entities that satisfy the queries.
|
2024-09-21 18:06:21 +00:00
|
|
|
pub fn filtered_view_iter<Q: AsQuery, F: AsFilter>(&self) -> ViewIter<Q::Query, F::Filter> {
|
2024-03-08 05:19:23 +00:00
|
|
|
let archetypes = self.archetypes.values().collect();
|
2024-09-21 18:06:21 +00:00
|
|
|
let v = ViewState::new(self, Q::Query::new(), F::Filter::new(), archetypes);
|
2024-03-08 05:19:23 +00:00
|
|
|
v.into_iter()
|
|
|
|
}
|
|
|
|
|
2023-12-09 23:55:57 +00:00
|
|
|
pub fn dynamic_view(&self) -> DynamicView {
|
|
|
|
DynamicView::new(self)
|
|
|
|
}
|
|
|
|
|
2024-03-03 01:20:38 +00:00
|
|
|
pub fn view_one<T: AsQuery>(&self, entity: Entity) -> ViewOne<T::Query> {
|
2023-12-14 05:25:09 +00:00
|
|
|
ViewOne::new(self, entity.id, T::Query::new())
|
|
|
|
}
|
|
|
|
|
2024-07-19 20:07:03 +00:00
|
|
|
/// Add a resource to the world.
|
2024-03-31 02:12:00 +00:00
|
|
|
pub fn add_resource<T: ResourceObject>(&mut self, data: T) {
|
2024-09-28 01:03:27 +00:00
|
|
|
self.resources.insert(TypeId::of::<T>(), ResourceData::new(data, self.current_tick()));
|
2023-11-29 04:25:47 +00:00
|
|
|
}
|
|
|
|
|
2024-07-19 20:07:03 +00:00
|
|
|
/// Add the default value of a resource.
|
|
|
|
///
|
|
|
|
/// > Note: This will replace existing values.
|
2024-03-31 02:12:00 +00:00
|
|
|
pub fn add_resource_default<T: ResourceObject + Default>(&mut self) {
|
2024-09-28 01:03:27 +00:00
|
|
|
self.resources.insert(TypeId::of::<T>(), ResourceData::new(T::default(), self.current_tick()));
|
2024-07-19 20:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Add the default value of a resource if it does not already exist.
|
|
|
|
///
|
2024-09-28 01:03:27 +00:00
|
|
|
/// Returns a boolean indicating if the resource was added.
|
2024-07-19 20:07:03 +00:00
|
|
|
pub fn add_resource_default_if_absent<T: ResourceObject + Default>(&mut self) -> bool {
|
|
|
|
let id = TypeId::of::<T>();
|
|
|
|
if !self.resources.contains_key(&id) {
|
2024-09-28 01:03:27 +00:00
|
|
|
self.resources.insert(id, ResourceData::new(T::default(), self.current_tick()));
|
2024-07-19 20:07:03 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2024-01-05 04:25:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-22 17:22:10 +00:00
|
|
|
/// Get a resource from the world, or insert it into the world with the provided
|
|
|
|
/// `fn` and return it.
|
2024-09-28 01:03:27 +00:00
|
|
|
pub fn get_resource_or_else<T: ResourceObject, F>(&mut self, f: F) -> ResMut<T>
|
2023-12-22 17:22:10 +00:00
|
|
|
where
|
|
|
|
F: Fn() -> T + 'static
|
|
|
|
{
|
2024-09-28 01:03:27 +00:00
|
|
|
let tick = self.current_tick();
|
2024-07-19 20:07:03 +00:00
|
|
|
let res = self.resources.entry(TypeId::of::<T>())
|
|
|
|
.or_insert_with(|| ResourceData::new(f(), tick));
|
2024-09-28 01:03:27 +00:00
|
|
|
|
|
|
|
ResMut {
|
|
|
|
inner: res.data.borrow_mut(),
|
|
|
|
world_tick: tick,
|
|
|
|
_marker: std::marker::PhantomData::<T>,
|
|
|
|
}
|
2023-12-22 17:22:10 +00:00
|
|
|
}
|
|
|
|
|
2024-09-28 01:03:27 +00:00
|
|
|
/// Get a resource from the world, or insert its default value.
|
|
|
|
pub fn get_resource_or_default<T: ResourceObject + Default>(&mut self) -> ResMut<T>
|
2024-02-23 21:34:21 +00:00
|
|
|
{
|
2024-09-28 01:03:27 +00:00
|
|
|
let tick = self.current_tick();
|
2024-07-19 20:07:03 +00:00
|
|
|
let res = self.resources.entry(TypeId::of::<T>())
|
|
|
|
.or_insert_with(|| ResourceData::new(T::default(), tick));
|
2024-09-28 01:03:27 +00:00
|
|
|
|
|
|
|
ResMut {
|
|
|
|
inner: res.data.borrow_mut(),
|
|
|
|
world_tick: tick,
|
|
|
|
_marker: std::marker::PhantomData::<T>,
|
|
|
|
}
|
2024-02-23 21:34:21 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 04:25:47 +00:00
|
|
|
/// Gets a resource from the World.
|
2024-09-28 01:03:27 +00:00
|
|
|
pub fn get_resource<T: ResourceObject>(&self) -> Option<Res<T>> {
|
|
|
|
self.get_tracked_resource::<T>().map(|r| Res {
|
|
|
|
inner: r,
|
|
|
|
world_tick: self.current_tick(),
|
|
|
|
_marker: std::marker::PhantomData::<T>,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-10-21 01:20:43 +00:00
|
|
|
/// Get the tick of a resource.
|
|
|
|
///
|
|
|
|
/// This tick represents the last time the resource was mutated.
|
|
|
|
pub fn get_resource_tick<T: ResourceObject>(&self) -> Option<Tick> {
|
|
|
|
self.get_tracked_resource::<T>().map(|r| r.tick)
|
|
|
|
}
|
|
|
|
|
2024-09-28 01:03:27 +00:00
|
|
|
/// Gets a reference to a change tracked resource.
|
|
|
|
///
|
|
|
|
/// You will have to manually downcast the inner resource. Most people don't need this, see
|
|
|
|
/// [`World::get_resource`].
|
|
|
|
pub fn get_tracked_resource<T: ResourceObject>(&self) -> Option<AtomicRef<TrackedResource<dyn ResourceObject>>> {
|
|
|
|
self.resources.get(&TypeId::of::<T>())
|
|
|
|
.map(|r| r.data.borrow())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a mutable borrow to a change tracked resource.
|
2023-11-29 04:25:47 +00:00
|
|
|
///
|
2024-09-28 01:03:27 +00:00
|
|
|
/// You will have to manually downcast the inner resource. Most people don't need this, see
|
|
|
|
/// [`World::get_resource_mut`].
|
|
|
|
pub fn get_tracked_resource_mut<T: ResourceObject>(&self) -> Option<AtomicRefMut<TrackedResource<dyn ResourceObject>>> {
|
2024-02-25 22:06:53 +00:00
|
|
|
self.resources.get(&TypeId::of::<T>())
|
2024-09-28 01:03:27 +00:00
|
|
|
.map(|r| r.data.borrow_mut())
|
2023-11-29 04:25:47 +00:00
|
|
|
}
|
|
|
|
|
2024-07-19 20:07:03 +00:00
|
|
|
/// Returns a boolean indicating if the resource changed.
|
|
|
|
///
|
|
|
|
/// This will return false if the resource doesn't exist.
|
|
|
|
pub fn has_resource_changed<T: ResourceObject>(&self) -> bool {
|
|
|
|
let tick = self.current_tick();
|
|
|
|
self.resources.get(&TypeId::of::<T>())
|
|
|
|
.map(|r| r.changed(tick))
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the [`Tick`] that the resource was last modified at.
|
|
|
|
pub fn resource_tick<T: ResourceObject>(&self) -> Option<Tick> {
|
|
|
|
self.resources.get(&TypeId::of::<T>())
|
2024-09-28 01:03:27 +00:00
|
|
|
.map(|r| r.data.borrow().tick)
|
2024-07-19 20:07:03 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 21:34:21 +00:00
|
|
|
/// Returns boolean indicating if the World contains a resource of type `T`.
|
2024-03-31 02:12:00 +00:00
|
|
|
pub fn has_resource<T: ResourceObject>(&self) -> bool {
|
2024-02-23 21:34:21 +00:00
|
|
|
self.resources.contains_key(&TypeId::of::<T>())
|
|
|
|
}
|
|
|
|
|
2023-11-29 04:25:47 +00:00
|
|
|
/// Gets a mutable borrow of a resource from the World.
|
2024-09-28 01:03:27 +00:00
|
|
|
pub fn get_resource_mut<T: ResourceObject>(&self) -> Option<ResMut<T>> {
|
|
|
|
self.get_tracked_resource_mut::<T>().map(|r| ResMut {
|
|
|
|
inner: r,
|
|
|
|
world_tick: self.current_tick(),
|
|
|
|
_marker: std::marker::PhantomData::<T>,
|
|
|
|
})
|
2023-11-29 04:25:47 +00:00
|
|
|
}
|
2023-12-26 19:12:53 +00:00
|
|
|
|
2024-07-19 20:07:03 +00:00
|
|
|
/// Get the corresponding [`ResourceData`].
|
2024-09-28 01:03:27 +00:00
|
|
|
pub fn get_resource_data<T: ResourceObject>(&self) -> Option<ResourceData> {
|
2024-06-30 02:23:49 +00:00
|
|
|
self.resources.get(&TypeId::of::<T>())
|
|
|
|
.map(|r| r.clone())
|
|
|
|
}
|
|
|
|
|
2024-09-28 01:03:27 +00:00
|
|
|
/// Increments the world current tick for tracking changes to components and resources.
|
2023-12-26 19:12:53 +00:00
|
|
|
///
|
2024-09-28 01:03:27 +00:00
|
|
|
/// # Note:
|
|
|
|
/// For change tracking to work correctly, this must be ran each loop before you run world
|
|
|
|
/// systems.
|
2023-12-26 19:12:53 +00:00
|
|
|
pub fn tick(&self) -> Tick {
|
|
|
|
self.tracker.tick()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the current tick that the world is at.
|
|
|
|
///
|
2024-09-28 01:03:27 +00:00
|
|
|
/// See [`World::tick`].
|
2023-12-26 19:12:53 +00:00
|
|
|
pub fn current_tick(&self) -> Tick {
|
|
|
|
self.tracker.current()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tick_tracker(&self) -> &TickTracker {
|
|
|
|
&self.tracker
|
|
|
|
}
|
2024-01-20 05:54:36 +00:00
|
|
|
|
|
|
|
/// Attempts to find a resource in the world and returns a NonNull pointer to it
|
2024-09-28 01:03:27 +00:00
|
|
|
pub unsafe fn get_resource_ptr<T: ResourceObject>(&self) -> Option<NonNull<T>> {
|
2024-01-20 05:54:36 +00:00
|
|
|
self.resources.get(&TypeId::of::<T>())
|
2024-09-29 18:35:24 +00:00
|
|
|
.map(|d| unsafe {
|
|
|
|
let data = d.data.borrow();
|
|
|
|
let ptr = NonNull::from(&data.res);
|
|
|
|
NonNull::new_unchecked(ptr.as_ptr() as *mut T)
|
|
|
|
})
|
2024-01-20 05:54:36 +00:00
|
|
|
}
|
2024-04-11 02:26:49 +00:00
|
|
|
|
|
|
|
pub fn archetype_count(&self) -> usize {
|
|
|
|
self.archetypes.len()
|
|
|
|
}
|
2023-11-25 23:43:11 +00:00
|
|
|
}
|
|
|
|
|
2024-03-31 02:12:00 +00:00
|
|
|
// TODO: Ensure that all non-send resources are only accessible on the main thread.
|
2024-03-31 04:32:31 +00:00
|
|
|
unsafe impl Send for World {}
|
|
|
|
unsafe impl Sync for World {}
|
2024-03-31 02:12:00 +00:00
|
|
|
|
2023-11-25 23:43:11 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-03-03 21:19:59 +00:00
|
|
|
use crate::{query::TickOf, tests::{Vec2, Vec3}, Entity};
|
2023-11-25 23:43:11 +00:00
|
|
|
|
2023-11-26 00:58:14 +00:00
|
|
|
use super::World;
|
2023-11-25 23:43:11 +00:00
|
|
|
|
2023-11-30 04:21:27 +00:00
|
|
|
struct SimpleCounter(i32);
|
|
|
|
|
2023-11-25 23:43:11 +00:00
|
|
|
#[test]
|
|
|
|
fn spawning_entity() {
|
|
|
|
let mut world = World::new();
|
|
|
|
let _e = world.spawn((Vec2 {
|
|
|
|
x: 10.0,
|
|
|
|
y: 15.0,
|
|
|
|
}, ));
|
|
|
|
}
|
2023-11-26 00:58:14 +00:00
|
|
|
|
|
|
|
#[test]
|
2023-11-26 05:56:30 +00:00
|
|
|
fn world_view_entities() {
|
2023-11-26 00:58:14 +00:00
|
|
|
let mut world = World::new();
|
|
|
|
world.spawn((Vec2 {
|
|
|
|
x: 10.0,
|
|
|
|
y: 15.0,
|
|
|
|
}, ));
|
|
|
|
world.spawn((Vec2 {
|
|
|
|
x: 152.0,
|
|
|
|
y: 3585.0,
|
|
|
|
}, ));
|
|
|
|
world.spawn((Vec2 {
|
|
|
|
x: 235.0,
|
|
|
|
y: 734.0,
|
|
|
|
}, ));
|
|
|
|
|
|
|
|
let mut count = 0;
|
2023-12-09 23:55:57 +00:00
|
|
|
for pos in world.view_iter::<&Vec2>() {
|
2023-11-26 00:58:14 +00:00
|
|
|
println!("Found entity at {:?}", pos);
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
assert!(count == 3);
|
|
|
|
}
|
2023-11-27 02:05:35 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn despawn_entity() {
|
|
|
|
let mut world = World::new();
|
|
|
|
world.spawn((Vec2::rand(),));
|
|
|
|
let middle_en = world.spawn((Vec2::rand(),));
|
|
|
|
let last_en = world.spawn((Vec2::rand(),));
|
|
|
|
|
|
|
|
world.despawn(middle_en);
|
|
|
|
|
2024-02-23 21:34:21 +00:00
|
|
|
let record = world.entities.entity_record(last_en).unwrap();
|
2023-11-27 02:05:35 +00:00
|
|
|
assert_eq!(record.index.0, 1);
|
|
|
|
}
|
2023-11-29 04:25:47 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_resource() {
|
|
|
|
let mut world = World::new();
|
|
|
|
{
|
|
|
|
let counter = SimpleCounter(0);
|
|
|
|
world.add_resource(counter);
|
|
|
|
}
|
|
|
|
|
2024-09-28 01:03:27 +00:00
|
|
|
let counter = world.get_resource::<SimpleCounter>()
|
|
|
|
.expect("Counter resource is missing");
|
2023-11-30 04:21:27 +00:00
|
|
|
assert_eq!(counter.0, 0);
|
2023-11-29 04:25:47 +00:00
|
|
|
drop(counter);
|
|
|
|
|
2024-09-28 01:03:27 +00:00
|
|
|
let mut counter = world.get_resource_mut::<SimpleCounter>()
|
|
|
|
.expect("Counter resource is missing");
|
2023-11-29 04:25:47 +00:00
|
|
|
counter.0 += 4582;
|
|
|
|
drop(counter);
|
|
|
|
|
2024-09-28 01:03:27 +00:00
|
|
|
assert!(world.get_resource::<u32>().is_none());
|
2023-11-30 04:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resource_multi_borrow() {
|
|
|
|
let mut world = World::new();
|
2024-03-31 02:12:00 +00:00
|
|
|
let counter = SimpleCounter(4582);
|
|
|
|
world.add_resource(counter);
|
2023-11-30 04:21:27 +00:00
|
|
|
|
|
|
|
// test multiple borrows at the same time
|
2024-09-28 01:03:27 +00:00
|
|
|
let counter = world.get_resource::<SimpleCounter>()
|
|
|
|
.expect("Counter resource is missing");
|
2023-11-30 04:21:27 +00:00
|
|
|
assert_eq!(counter.0, 4582);
|
2024-09-28 01:03:27 +00:00
|
|
|
let counter2 = world.get_resource::<SimpleCounter>()
|
|
|
|
.expect("Counter resource is missing");
|
2024-03-31 02:12:00 +00:00
|
|
|
assert_eq!(counter.0, 4582);
|
2023-11-30 04:21:27 +00:00
|
|
|
assert_eq!(counter2.0, 4582);
|
|
|
|
}
|
2023-11-29 04:25:47 +00:00
|
|
|
|
2023-11-30 04:21:27 +00:00
|
|
|
#[test]
|
|
|
|
fn resource_one_mutable_borrow() {
|
|
|
|
let mut world = World::new();
|
|
|
|
{
|
|
|
|
let counter = SimpleCounter(4582);
|
|
|
|
world.add_resource(counter);
|
|
|
|
}
|
|
|
|
|
2024-03-31 02:12:00 +00:00
|
|
|
// test that its only possible to get a single mutable borrow
|
2024-09-28 01:03:27 +00:00
|
|
|
let counter = world.get_resource_mut::<SimpleCounter>()
|
|
|
|
.expect("Counter resource is missing");
|
2023-11-30 04:21:27 +00:00
|
|
|
assert_eq!(counter.0, 4582);
|
2024-09-28 01:03:27 +00:00
|
|
|
assert!(world.get_resource_mut::<SimpleCounter>().is_none());
|
2023-11-30 04:21:27 +00:00
|
|
|
assert_eq!(counter.0, 4582);
|
2023-11-29 04:25:47 +00:00
|
|
|
}
|
2023-12-14 05:25:09 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_into_existing_archetype() {
|
|
|
|
let mut world = World::new();
|
|
|
|
let e = world.spawn((Vec2::rand(),));
|
|
|
|
world.spawn((Vec2::rand(),Vec3::rand()));
|
|
|
|
|
|
|
|
world.insert(e, (Vec3::rand(),));
|
|
|
|
|
|
|
|
assert!(world.view_one::<&Vec3>(e).get().is_some())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_into_new_archetype() {
|
|
|
|
let mut world = World::new();
|
|
|
|
let e = world.spawn((Vec2::rand(),));
|
|
|
|
|
|
|
|
world.insert(e, (Vec3::rand(),));
|
|
|
|
|
|
|
|
assert!(world.view_one::<&Vec3>(e).get().is_some())
|
|
|
|
}
|
|
|
|
|
2024-03-03 21:19:59 +00:00
|
|
|
#[test]
|
|
|
|
fn insert_multiple_times() {
|
|
|
|
let v2s = &[Vec2::rand(), Vec2::rand(), Vec2::rand()];
|
|
|
|
let v3s = &[Vec3::rand(), Vec3::rand(), Vec3::rand()];
|
|
|
|
|
|
|
|
let mut world = World::new();
|
|
|
|
let e1 = world.spawn(v2s[0]);
|
|
|
|
let e2 = world.spawn(v2s[1]);
|
|
|
|
let e3 = world.spawn(v2s[2]);
|
|
|
|
println!("Spawned entities");
|
|
|
|
|
|
|
|
let ev2 = world.view_one::<&Vec2>(e2).get()
|
|
|
|
.expect("Failed to find Vec2 and Vec3 on inserted entity!");
|
|
|
|
assert_eq!(*ev2, v2s[1]);
|
|
|
|
drop(ev2);
|
|
|
|
|
|
|
|
let insert_and_assert = |world: &mut World, e: Entity, v2: Vec2, v3: Vec3| {
|
|
|
|
println!("inserting entity");
|
|
|
|
world.insert(e, (v3,));
|
|
|
|
println!("inserted entity");
|
|
|
|
|
|
|
|
let (ev2, ev3) = world.view_one::<(&Vec2, &Vec3)>(e).get()
|
|
|
|
.expect("Failed to find Vec2 and Vec3 on inserted entity!");
|
|
|
|
assert_eq!(*ev2, v2);
|
|
|
|
assert_eq!(*ev3, v3);
|
|
|
|
};
|
|
|
|
|
|
|
|
insert_and_assert(&mut world, e2, v2s[1], v3s[1]);
|
|
|
|
println!("Entity 2 is good");
|
|
|
|
insert_and_assert(&mut world, e3, v2s[2], v3s[2]);
|
|
|
|
println!("Entity 3 is good");
|
|
|
|
assert_eq!(world.archetypes.len(), 2);
|
|
|
|
println!("No extra archetypes were created");
|
|
|
|
|
|
|
|
insert_and_assert(&mut world, e1, v2s[0], v3s[0]);
|
|
|
|
println!("Entity 1 is good");
|
|
|
|
}
|
|
|
|
|
2023-12-14 05:25:09 +00:00
|
|
|
#[test]
|
|
|
|
fn view_one() {
|
|
|
|
let v = Vec2::rand();
|
|
|
|
|
|
|
|
let mut world = World::new();
|
|
|
|
let e = world.spawn((v,));
|
|
|
|
|
|
|
|
let view = world.view_one::<&Vec2>(e);
|
|
|
|
assert_eq!(*view.get().unwrap(), v);
|
|
|
|
}
|
2023-12-26 19:12:53 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn view_change_tracking() {
|
|
|
|
let mut world = World::new();
|
|
|
|
|
|
|
|
println!("spawning");
|
|
|
|
world.spawn((Vec2::new(10.0, 10.0),));
|
|
|
|
world.spawn((Vec2::new(5.0, 5.0),));
|
|
|
|
println!("spawned");
|
|
|
|
|
|
|
|
for mut v in world.view_iter::<&mut Vec2>() {
|
|
|
|
v.y += 50.0;
|
|
|
|
println!("Moved v to {:?}", v);
|
|
|
|
}
|
|
|
|
|
|
|
|
let world_tick = world.current_tick();
|
|
|
|
println!("The world tick is {}", *world_tick);
|
|
|
|
for (v, tick) in world.view_iter::<(&Vec2, TickOf<Vec2>)>() {
|
|
|
|
println!("Is at {:?}, it was changed at {}", v, *tick);
|
|
|
|
assert!(v.y > 50.0);
|
|
|
|
assert!(tick >= world_tick);
|
|
|
|
}
|
|
|
|
}
|
2024-04-22 04:20:42 +00:00
|
|
|
|
|
|
|
/// Tests replacing components using World::insert
|
|
|
|
#[test]
|
|
|
|
fn entity_insert_replace() {
|
|
|
|
let mut world = World::new();
|
|
|
|
let first = world.spawn((Vec2::new(10.0, 10.0),));
|
|
|
|
let second = world.spawn((Vec2::new(5.0, 5.0),));
|
|
|
|
|
|
|
|
world.insert(first, Vec2::new(50.0, 50.0));
|
|
|
|
|
|
|
|
let pos = world.view_one::<&mut Vec2>(first).get().unwrap();
|
|
|
|
assert_eq!(*pos, Vec2::new(50.0, 50.0));
|
|
|
|
drop(pos);
|
|
|
|
|
|
|
|
let pos = world.view_one::<&mut Vec2>(second).get().unwrap();
|
|
|
|
assert_eq!(*pos, Vec2::new(5.0, 5.0));
|
|
|
|
}
|
2024-07-19 20:07:03 +00:00
|
|
|
|
|
|
|
/// Tests resource change checks
|
|
|
|
#[test]
|
|
|
|
fn resource_changed() {
|
|
|
|
let mut world = World::new();
|
|
|
|
world.add_resource(SimpleCounter(50));
|
|
|
|
|
|
|
|
assert!(world.has_resource_changed::<SimpleCounter>());
|
|
|
|
|
|
|
|
world.spawn(Vec2::new(50.0, 50.0));
|
|
|
|
|
|
|
|
assert!(!world.has_resource_changed::<SimpleCounter>());
|
|
|
|
|
2024-09-28 01:03:27 +00:00
|
|
|
let mut counter = world.get_resource_mut::<SimpleCounter>()
|
|
|
|
.expect("Counter resource is missing");
|
2024-07-19 20:07:03 +00:00
|
|
|
counter.0 += 100;
|
|
|
|
|
|
|
|
assert!(world.has_resource_changed::<SimpleCounter>());
|
|
|
|
}
|
2023-05-25 04:11:16 +00:00
|
|
|
}
|