cargo fmt... also maybe fix that shared materials thing

main
Elijah C. Voigt 2 years ago
parent 2d476b89da
commit 3275582176

@ -222,7 +222,10 @@ fn initialize(mut commands: Commands, board: Res<game::Board>, assets: Res<Asset
*index, *index,
SceneBundle { ..default() }, SceneBundle { ..default() },
game::Selectable, game::Selectable,
Dissolvable { start: 1.0, duration: 3.0 }, // Marks pieces as dissolving Dissolvable {
start: 1.0,
duration: 3.0,
}, // Marks pieces as dissolving
)); ));
}); });
@ -233,7 +236,10 @@ fn initialize(mut commands: Commands, board: Res<game::Board>, assets: Res<Asset
GameState::Title, GameState::Title,
SceneBundle { ..default() }, SceneBundle { ..default() },
TitleText, TitleText,
Dissolvable { start: 0.0, duration: 3.0 }, // Marks title text as dissolving Dissolvable {
start: 0.0,
duration: 3.0,
}, // Marks title text as dissolving
)); ));
}); });
}); });
@ -1192,8 +1198,6 @@ fn setup_dissolve_materials(
mut dissolve_materials: ResMut<Assets<DissolveMaterial>>, mut dissolve_materials: ResMut<Assets<DissolveMaterial>>,
// Used to insert Handle<DissolveMaterial>; // Used to insert Handle<DissolveMaterial>;
mut commands: Commands, mut commands: Commands,
// Cache dissolve textures that have already been created
mut cache: Local<HashMap<Handle<StandardMaterial>, Handle<DissolveMaterial>>>,
) { ) {
events events
.iter() .iter()
@ -1202,31 +1206,24 @@ fn setup_dissolve_materials(
// Handle this entity (mesh) // Handle this entity (mesh)
.for_each(|(child, std_handle)| { .for_each(|(child, std_handle)| {
info!("Setting up dissolve material for {:?}", child); info!("Setting up dissolve material for {:?}", child);
let dis_handle = match cache.get(std_handle) {
// We have not seen this material, so create a new dissolve material
None => {
// Extension we will add to existing gltf-sourced materials
let extension = DissolveExtension { percentage: 1.0 };
// Base material we will extend for the duration of the dissolve effect
let mut base: StandardMaterial = standard_materials
.get(std_handle)
.expect("Resolve material data")
.clone();
base.alpha_mode = AlphaMode::Mask(0.5);
base.base_color = Color::NONE.with_a(0.0);
info!("Base material {:#?}", base);
dissolve_materials.add(ExtendedMaterial { base, extension })
}
Some(dis_handle) => dis_handle.clone(),
};
// Insert this handle into the cache (may be redundant) // Extension we will add to existing gltf-sourced materials
cache.insert(std_handle.clone(), dis_handle.clone()); let extension = DissolveExtension { percentage: 1.0 };
// Base material we will extend for the duration of the dissolve effect
let mut base: StandardMaterial = standard_materials
.get(std_handle)
.expect("Resolve material data")
.clone();
base.alpha_mode = AlphaMode::Mask(0.5);
base.base_color = Color::NONE.with_a(0.0);
info!("Base material {:#?}", base);
let dis_handle = dissolve_materials.add(ExtendedMaterial { base, extension });
// Add the dissolve handle as a Backup(T) // Add the dissolve handle as a Backup(T)
commands.entity(child) commands
.entity(child)
.insert(dis_handle.clone()) .insert(dis_handle.clone())
.remove::<Handle<StandardMaterial>>(); .remove::<Handle<StandardMaterial>>();
}); });
@ -1363,52 +1360,54 @@ fn dissolve_animation(
children: Query<&Children>, children: Query<&Children>,
// Used to create Handle<DissolveMaterial> // Used to create Handle<DissolveMaterial>
mut dissolve_materials: ResMut<Assets<DissolveMaterial>>, mut dissolve_materials: ResMut<Assets<DissolveMaterial>>,
object_materials: Query<( object_materials: Query<(Entity, &Handle<DissolveMaterial>)>,
Entity,
&Handle<DissolveMaterial>,
)>,
mut commands: Commands, mut commands: Commands,
time: Res<Time>, time: Res<Time>,
) { ) {
query.iter_mut().for_each(|(entity, dissolvable, mut dissolving)| { query
debug!("Entity {:?} has Dissolving {:?}", entity, dissolving); .iter_mut()
.for_each(|(entity, dissolvable, mut dissolving)| {
debug!("Entity {:?} has Dissolving {:?}", entity, dissolving);
let percentage = match *dissolving { let percentage = match *dissolving {
Dissolving::In(mut sec) => { Dissolving::In(mut sec) => {
// Check if seconds is below 0.0 // Check if seconds is below 0.0
sec = (sec - time.delta_seconds()).max(0.0); sec = (sec - time.delta_seconds()).max(0.0);
*dissolving = Dissolving::In(sec); *dissolving = Dissolving::In(sec);
// Calculate the target percentage value // Calculate the target percentage value
1.0 - (sec / dissolvable.duration) 1.0 - (sec / dissolvable.duration)
} }
Dissolving::Out(mut sec) => { Dissolving::Out(mut sec) => {
// Check if seconds is below 0.0 // Check if seconds is below 0.0
sec = (sec - time.delta_seconds()).max(0.0); sec = (sec - time.delta_seconds()).max(0.0);
*dissolving = Dissolving::Out(sec); *dissolving = Dissolving::Out(sec);
// Calculate the target percentage value // Calculate the target percentage value
sec / dissolvable.duration sec / dissolvable.duration
} }
}; };
object_materials.iter_many(children.iter_descendants(entity)).for_each( object_materials
|(_child, handle)| { .iter_many(children.iter_descendants(entity))
let dissolve_material = dissolve_materials .for_each(|(_child, handle)| {
.get_mut(handle) let dissolve_material = dissolve_materials
.expect("Get the dissolve material"); .get_mut(handle)
.expect("Get the dissolve material");
// Change the material's value to create animation // Change the material's value to create animation
dissolve_material.extension.percentage = percentage; dissolve_material.extension.percentage = percentage;
}, });
);
// If animation is done, remove dissolving component // If animation is done, remove dissolving component
if percentage <= 0.0 || percentage >= 1.0 { if percentage <= 0.0 || percentage >= 1.0 {
info!("Removing dissolving from {:?} with percentage {:?}", entity, percentage); info!(
commands.entity(entity).remove::<Dissolving>(); "Removing dissolving from {:?} with percentage {:?}",
} entity, percentage
}); );
commands.entity(entity).remove::<Dissolving>();
}
});
} }
Loading…
Cancel
Save