Skip to content

Commit

Permalink
Merge pull request amethyst#2612 from jlowry/prefabs-with-modified-de…
Browse files Browse the repository at this point in the history
…pendencies

Fixing and refactoring prefabs
  • Loading branch information
suspistew authored Jan 21, 2021
2 parents 71bbbb1 + 6a60a03 commit 87c546a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
52 changes: 22 additions & 30 deletions amethyst_assets/src/prefab/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,48 +28,41 @@ pub fn prefab_spawning_tick(world: &mut World, resources: &mut Resources) {
&legion_prefab::CookedPrefab,
u32,
HashMap<Entity, Entity, EntityHasher>,
Handle<Prefab>,
)> = Vec::new();

let mut entity_query = <(Entity,)>::query();

<(Entity, &Handle<Prefab>, TryWrite<PrefabInstance>)>::query().for_each_mut(
world,
|(entity, handle, instance)| {
if let Some(prefab) = prefab_storage.get(handle) {
if let Some(cooked_prefab) = prefab.cooked.as_ref() {
if let Some(instance) = instance {
if instance.version < prefab.version {
log::debug!("Updating existing prefab.");
if let Some((root_entity,)) =
entity_query.iter(&cooked_prefab.world).next()
{
instance.entity_map.insert(*root_entity, *entity);
};

prefabs.push((
*entity,
cooked_prefab,
prefab.version,
instance.entity_map.clone(),
handle.clone(),
));
}
} else {
log::debug!("Spawning new prefab.");
let mut map = HashMap::<Entity, Entity, EntityHasher>::default();
if let Some(Prefab {
cooked: Some(cooked_prefab),
version: prefab_version,
..
}) = prefab_storage.get(handle)
{
let instance_version = instance
.as_ref()
.map(|instance| instance.version)
.unwrap_or(0);
if instance_version < *prefab_version {
let mut entity_map = instance
.as_ref()
.map(|instance| instance.entity_map.clone())
.unwrap_or_default();
if entity_map.is_empty() {
if let Some((root_entity,)) = entity_query.iter(&cooked_prefab.world).next()
{
map.insert(*root_entity, *entity);
};
prefabs.push((*entity, cooked_prefab, prefab.version, map, handle.clone()));
entity_map.insert(*root_entity, *entity);
}
}
prefabs.push((*entity, cooked_prefab, *prefab_version, entity_map));
}
}
},
);

for (entity, prefab, version, prev_entity_map, handle) in prefabs.into_iter() {
for (entity, prefab, version, prev_entity_map) in prefabs.into_iter() {
let entity_map = world.clone_from(
&prefab.world,
&query::any(),
Expand All @@ -82,8 +75,8 @@ pub fn prefab_spawning_tick(world: &mut World, resources: &mut Resources) {
log::debug!("new entity_map: {:?}", entity_map);
log::debug!("old entity map: {:?}", prev_entity_map);

for value in prev_entities.difference(&live_entities) {
if world.remove(*value) {
for value in prev_entities.difference(&live_entities).copied() {
if world.remove(value) {
log::debug!("Removed entity {:?}", value)
}
}
Expand All @@ -95,7 +88,6 @@ pub fn prefab_spawning_tick(world: &mut World, resources: &mut Resources) {
version,
entity_map,
});
entry.add_component(handle);
} else {
log::error!("Could not update entity");
}
Expand Down
2 changes: 2 additions & 0 deletions amethyst_assets/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ where
.write_resource::<ProcessingQueue<A::Data>>()
.write_resource::<AssetStorage<A>>()
.build(|_, _, (queue, storage), _| {
// drain the changed queue
while let Some(_) = queue.changed.pop() {}
queue.process(storage, ProcessableAsset::process);
storage.process_custom_drop(|_| {});
}),
Expand Down
15 changes: 9 additions & 6 deletions amethyst_assets/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashMap;

use atelier_assets::loader::{handle::AssetHandle, storage::IndirectionTable, LoadHandle};
use crossbeam_queue::SegQueue;
use fnv::FnvHashMap;

struct AssetState<A> {
version: u32,
Expand All @@ -14,8 +13,8 @@ struct AssetState<A> {
///
/// * `A`: Asset Rust type.
pub struct AssetStorage<A> {
assets: HashMap<LoadHandle, AssetState<A>>,
uncommitted: HashMap<LoadHandle, AssetState<A>>,
assets: FnvHashMap<LoadHandle, AssetState<A>>,
uncommitted: FnvHashMap<LoadHandle, AssetState<A>>,
to_drop: SegQueue<A>,
indirection_table: IndirectionTable,
}
Expand All @@ -33,8 +32,12 @@ impl<A> AssetStorage<A> {

/// Added to make api compatible with previous storage
pub fn unload_all(&mut self) {
// FIXME do the unload correctly
self.assets.clear();
for (_, data) in self.uncommitted.drain() {
self.to_drop.push(data.asset);
}
for (_, data) in self.assets.drain() {
self.to_drop.push(data.asset);
}
}

pub(crate) fn update_asset(&mut self, handle: LoadHandle, asset: A, version: u32) {
Expand Down

0 comments on commit 87c546a

Please sign in to comment.