cargo fmt

main
Elijah C. Voigt 2 years ago
parent 2d60508471
commit dc981aafea

@ -23,24 +23,23 @@ fn main() {
.add_systems(Update, apply_skybox) .add_systems(Update, apply_skybox)
.add_systems(Update, set_scene) .add_systems(Update, set_scene)
.add_systems(Update, rotate) .add_systems(Update, rotate)
.add_systems(Update, toggle_material.run_if( .add_systems(
|keys: Res<Input<KeyCode>>| -> bool { Update,
keys.just_pressed(KeyCode::Space) toggle_material
}) .run_if(|keys: Res<Input<KeyCode>>| -> bool { keys.just_pressed(KeyCode::Space) }),
)
.add_systems(
Update,
init_materials
.run_if(|events: Query<Entity, Added<Transform>>| -> bool { !events.is_empty() }),
) )
.add_systems(Update, init_materials.run_if(|events: Query<Entity, Added<Transform>>| -> bool {
!events.is_empty()
}))
.run(); .run();
} }
#[derive(Component)] #[derive(Component)]
struct Root; struct Root;
fn setup( fn setup(mut commands: Commands, assets: Res<AssetServer>) {
mut commands: Commands,
assets: Res<AssetServer>,
) {
commands.spawn((SceneBundle { ..default() }, Root)); commands.spawn((SceneBundle { ..default() }, Root));
commands.spawn(PointLightBundle { commands.spawn(PointLightBundle {
@ -52,13 +51,10 @@ fn setup(
..default() ..default()
}); });
commands.spawn(( commands.spawn((Camera3dBundle {
Camera3dBundle { transform: Transform::from_xyz(-1.0, 1.5, 4.0).looking_at(Vec3::ZERO, Vec3::Y),
transform: Transform::from_xyz(-1.0, 1.5, 4.0).looking_at(Vec3::ZERO, Vec3::Y), ..default()
..default() },));
},
)
);
} }
fn apply_skybox( fn apply_skybox(
@ -118,7 +114,6 @@ impl MaterialExtension for MatExt {
} }
} }
#[derive(Debug, Component)] #[derive(Debug, Component)]
struct Backup<T: Component>(T); struct Backup<T: Component>(T);
@ -143,9 +138,7 @@ fn init_materials(
let ext_handle = materials.add(ExtendedMaterial { base, extension }); let ext_handle = materials.add(ExtendedMaterial { base, extension });
commands commands.entity(entity).insert(Backup(ext_handle.clone()));
.entity(entity)
.insert(Backup(ext_handle.clone()));
}); });
} }
@ -158,9 +151,7 @@ fn toggle_material(
query.iter().for_each(|entity| { query.iter().for_each(|entity| {
// Entity has standard material, and had extended material // Entity has standard material, and had extended material
// Swap these materials // Swap these materials
if let Ok( if let Ok((std_handle, Some(Backup(ext_handle)))) = std_mat.get(entity) {
(std_handle, Some(Backup(ext_handle)))
) = std_mat.get(entity) {
info!("Swapping standard material for extended material"); info!("Swapping standard material for extended material");
commands commands
.entity(entity) .entity(entity)
@ -169,13 +160,11 @@ fn toggle_material(
.remove::<Backup<Handle<MyMat>>>() .remove::<Backup<Handle<MyMat>>>()
.remove::<Handle<StandardMaterial>>(); .remove::<Handle<StandardMaterial>>();
return return;
} }
// In this branch we have the extended material assigned to the object // In this branch we have the extended material assigned to the object
if let Ok( if let Ok((ext_handle, Some(Backup(std_handle)))) = ext_mat.get(entity) {
(ext_handle, Some(Backup(std_handle)))
) = ext_mat.get(entity) {
// Entity has standard material, and had extended material // Swap these materials // Entity has standard material, and had extended material // Swap these materials
info!("Swapping extended material for standard material"); info!("Swapping extended material for standard material");
@ -186,7 +175,7 @@ fn toggle_material(
.remove::<Backup<Handle<StandardMaterial>>>() .remove::<Backup<Handle<StandardMaterial>>>()
.remove::<Handle<MyMat>>(); .remove::<Handle<MyMat>>();
return return;
} }
panic!("What is happening?") panic!("What is happening?")

@ -7,14 +7,12 @@ impl Plugin for CreditsPlugin {
app.add_systems(Startup, init_credits_ui) app.add_systems(Startup, init_credits_ui)
.add_systems( .add_systems(
Update, Update,
( (menu::exit_to_menu.run_if(in_state(GameState::Credits)),),
menu::exit_to_menu.run_if(in_state(GameState::Credits)), )
), .add_systems(
OnEnter(GameState::Credits),
(update_credits, activate::<Credits>.after(update_credits)),
) )
.add_systems(OnEnter(GameState::Credits), (
update_credits,
activate::<Credits>.after(update_credits),
))
.add_systems(OnExit(GameState::Credits), deactivate::<Credits>); .add_systems(OnExit(GameState::Credits), deactivate::<Credits>);
} }
} }
@ -64,18 +62,17 @@ fn update_credits(
.get(tweaks_file.handle.clone()) .get(tweaks_file.handle.clone())
.expect("Load tweakfile"); .expect("Load tweakfile");
query query.iter_mut().for_each(|mut text| {
.iter_mut() let credits_text = tweak.get::<String>("credits_text").unwrap();
.for_each(|mut text| { text.sections = {
let credits_text = tweak.get::<String>("credits_text").unwrap(); credits_text
text.sections = { .split('\n')
credits_text.split('\n').map(|l| { .map(|l| TextSection {
TextSection { value: format!("{}\n", l),
value: format!("{}\n", l), style: TextStyle { ..default() },
style: TextStyle { ..default() }, })
} .collect()
}).collect() };
}; info!("Text sections: {:?}", text.sections);
info!("Text sections: {:?}", text.sections); });
});
} }

@ -4,18 +4,24 @@ use crate::{
tweak::Tweaks, tweak::Tweaks,
}; };
use bevy::{ use bevy::{
animation::RepeatAnimation, core_pipeline::{ animation::RepeatAnimation,
core_pipeline::{
experimental::taa::{TemporalAntiAliasPlugin, TemporalAntiAliasSettings}, experimental::taa::{TemporalAntiAliasPlugin, TemporalAntiAliasSettings},
prepass::MotionVectorPrepass, prepass::MotionVectorPrepass,
tonemapping::{DebandDither, Tonemapping}, tonemapping::{DebandDither, Tonemapping},
Skybox, Skybox,
}, input::mouse::{MouseButtonInput, MouseMotion, MouseScrollUnit, MouseWheel}, pbr::{ },
ExtendedMaterial, MaterialExtension, input::mouse::{MouseButtonInput, MouseMotion, MouseScrollUnit, MouseWheel},
ScreenSpaceAmbientOcclusionBundle, ScreenSpaceAmbientOcclusionSettings, pbr::{
}, render::{ ExtendedMaterial, MaterialExtension, ScreenSpaceAmbientOcclusionBundle,
ScreenSpaceAmbientOcclusionSettings,
},
render::{
render_resource::{AsBindGroup, ShaderRef, TextureViewDescriptor, TextureViewDimension}, render_resource::{AsBindGroup, ShaderRef, TextureViewDescriptor, TextureViewDimension},
view::ColorGrading, view::ColorGrading,
}, utils::HashMap, window::PrimaryWindow },
utils::HashMap,
window::PrimaryWindow,
}; };
use tweaks::*; use tweaks::*;
@ -72,7 +78,9 @@ impl Plugin for Display3dPlugin {
), ),
setup_capture_piece.run_if(any_component_changed::<Handle<StandardMaterial>>), setup_capture_piece.run_if(any_component_changed::<Handle<StandardMaterial>>),
capture_piece.run_if(any_with_component::<game::Captured>()), capture_piece.run_if(any_with_component::<game::Captured>()),
skip_animation.run_if(|keys: Res<Input<KeyCode>>| -> bool { keys.just_pressed(KeyCode::Return) }), skip_animation.run_if(|keys: Res<Input<KeyCode>>| -> bool {
keys.just_pressed(KeyCode::Return)
}),
), ),
) )
.add_systems( .add_systems(
@ -572,7 +580,7 @@ fn set_piece_texture(
if let Some(gltf) = gltfs.get(assets_handle) { if let Some(gltf) = gltfs.get(assets_handle) {
// Why can't we just models.iter_many_mut(...).for_each(...)?? // Why can't we just models.iter_many_mut(...).for_each(...)??
let mut stuff = models.iter_many_mut(children.iter_descendants(entity)); let mut stuff = models.iter_many_mut(children.iter_descendants(entity));
while let Some((n, mut m)) = stuff.fetch_next() { while let Some((n, mut m)) = stuff.fetch_next() {
debug!("Setting piece texture for {:?}", n); debug!("Setting piece texture for {:?}", n);
match (*piece, side, n.as_str()) { match (*piece, side, n.as_str()) {
(Piece::Queen, Side::A, "Queen.0") => { (Piece::Queen, Side::A, "Queen.0") => {
@ -1288,7 +1296,13 @@ struct Backup<T: Component>(T);
/// Sets up all pieces to have an associated "dissolve" material ready for capture /// Sets up all pieces to have an associated "dissolve" material ready for capture
fn setup_capture_piece( fn setup_capture_piece(
// All entities with materials are candidates for this procedure // All entities with materials are candidates for this procedure
events: Query<(Entity, &Handle<StandardMaterial>), (Added<Handle<StandardMaterial>>, Changed<Handle<StandardMaterial>>)>, events: Query<
(Entity, &Handle<StandardMaterial>),
(
Added<Handle<StandardMaterial>>,
Changed<Handle<StandardMaterial>>,
),
>,
// Only process newly created pieces (we do not delete pieces at runtime) // Only process newly created pieces (we do not delete pieces at runtime)
query: Query<Entity, (With<Piece>, Added<Children>)>, query: Query<Entity, (With<Piece>, Added<Children>)>,
// Children of pieces are the actual meshes that need materials // Children of pieces are the actual meshes that need materials
@ -1305,9 +1319,7 @@ fn setup_capture_piece(
events events
.iter() .iter()
// Only process if this is a child of a piece // Only process if this is a child of a piece
.filter(|(child, _)| { .filter(|(child, _)| query.iter_many(parents.iter_ancestors(*child)).count() > 0)
query.iter_many(parents.iter_ancestors(*child)).count() > 0
})
// Handle this entity (mesh) // Handle this entity (mesh)
.for_each(|(child, std_handle)| { .for_each(|(child, std_handle)| {
let dis_handle = match cache.get(std_handle) { let dis_handle = match cache.get(std_handle) {
@ -1322,7 +1334,7 @@ fn setup_capture_piece(
.clone(); .clone();
dissolve_materials.add(ExtendedMaterial { base, extension }) dissolve_materials.add(ExtendedMaterial { base, extension })
}, }
Some(dis_handle) => dis_handle.clone(), Some(dis_handle) => dis_handle.clone(),
}; };
@ -1330,9 +1342,7 @@ fn setup_capture_piece(
cache.insert(std_handle.clone(), dis_handle.clone()); cache.insert(std_handle.clone(), dis_handle.clone());
// Add the dissolve handle as a Backup(T) // Add the dissolve handle as a Backup(T)
commands commands.entity(child).insert(Backup(dis_handle.clone()));
.entity(child)
.insert(Backup(dis_handle.clone()));
}); });
} }
@ -1351,8 +1361,16 @@ fn capture_piece(
mut kids: Local<Vec<Entity>>, mut kids: Local<Vec<Entity>>,
mut prog: Local<f32>, mut prog: Local<f32>,
mut dissolve_materials: ResMut<Assets<DissolveMaterial>>, mut dissolve_materials: ResMut<Assets<DissolveMaterial>>,
object_standard_materials: Query<(Entity, &Handle<StandardMaterial>, &Backup<Handle<DissolveMaterial>>)>, object_standard_materials: Query<(
object_dissolve_materials: Query<(Entity, &Handle<DissolveMaterial>, &Backup<Handle<StandardMaterial>>)>, Entity,
&Handle<StandardMaterial>,
&Backup<Handle<DissolveMaterial>>,
)>,
object_dissolve_materials: Query<(
Entity,
&Handle<DissolveMaterial>,
&Backup<Handle<StandardMaterial>>,
)>,
children: Query<&Children>, children: Query<&Children>,
mut commands: Commands, mut commands: Commands,
time: Res<Time>, time: Res<Time>,
@ -1381,7 +1399,8 @@ fn capture_piece(
// Return child entity to be processed later in flow // Return child entity to be processed later in flow
child child
}).collect(); })
.collect();
// Set the next state to start fading out // Set the next state to start fading out
game::CaptureFlow::FadeOut(entity) game::CaptureFlow::FadeOut(entity)
@ -1408,9 +1427,8 @@ fn capture_piece(
// This takes effect after updating all children // This takes effect after updating all children
} }
object_dissolve_materials object_dissolve_materials.iter_many(kids.iter()).for_each(
.iter_many(kids.iter()) |(_child, dis_handle, _)| {
.for_each(|(_child, dis_handle, _)| {
let dissolve_material = dissolve_materials let dissolve_material = dissolve_materials
.get_mut(dis_handle) .get_mut(dis_handle)
.expect("Get the dissolve material"); .expect("Get the dissolve material");
@ -1422,7 +1440,7 @@ fn capture_piece(
"Play fade out animation {:?} {:?}", "Play fade out animation {:?} {:?}",
delta, dissolve_material.extension.percentage delta, dissolve_material.extension.percentage
); );
} },
); );
} }
} }
@ -1435,8 +1453,14 @@ fn capture_piece(
*v = Visibility::Hidden; *v = Visibility::Hidden;
// HACK: This is dirty. Why side, but score.captures(!side)? // HACK: This is dirty. Why side, but score.captures(!side)?
info!("Capture translation: ({:?}, {:?}) {:?}", side, score.captures(!*side).saturating_sub(1), t.translation); info!(
t.translation = capture_translation(side, score.captures(!*side).saturating_sub(1)); "Capture translation: ({:?}, {:?}) {:?}",
side,
score.captures(!*side).saturating_sub(1),
t.translation
);
t.translation =
capture_translation(side, score.captures(!*side).saturating_sub(1));
*state = s.next(); *state = s.next();
} }
@ -1465,30 +1489,32 @@ fn capture_piece(
// Play fade-in animation // Play fade-in animation
{ {
object_dissolve_materials.iter_many(kids.iter()).for_each(|(child, dis_handle, Backup(std_handle))| { object_dissolve_materials.iter_many(kids.iter()).for_each(
let dissolve_material = dissolve_materials |(child, dis_handle, Backup(std_handle))| {
.get_mut(dis_handle) let dissolve_material = dissolve_materials
.expect("Get the dissolve material"); .get_mut(dis_handle)
.expect("Get the dissolve material");
// Change the material's value to create animation
dissolve_material.extension.percentage = *prog; // Change the material's value to create animation
dissolve_material.extension.percentage = *prog;
debug!(
"Play fade in animation {:?} {:?}", debug!(
delta, dissolve_material.extension.percentage "Play fade in animation {:?} {:?}",
); delta, dissolve_material.extension.percentage
);
// If we are done with the animation cleanup
if dissolve_material.extension.percentage >= 1.0 { // If we are done with the animation cleanup
// Re-add the original material if dissolve_material.extension.percentage >= 1.0 {
commands // Re-add the original material
.entity(child) commands
.insert(std_handle.clone()) .entity(child)
.insert(Backup(dis_handle.clone())) .insert(std_handle.clone())
.remove::<Handle<DissolveMaterial>>() .insert(Backup(dis_handle.clone()))
.remove::<Backup<Handle<StandardMaterial>>>(); .remove::<Handle<DissolveMaterial>>()
} .remove::<Backup<Handle<StandardMaterial>>>();
}); }
},
);
} }
} }
} }

@ -118,7 +118,7 @@ pub(crate) struct Score {
a: usize, a: usize,
b: usize, b: usize,
captures_a: usize, captures_a: usize,
captures_b: usize captures_b: usize,
} }
impl Score { impl Score {
@ -132,7 +132,7 @@ impl Score {
Side::A => { Side::A => {
self.captures_a += 1; self.captures_a += 1;
self.a += i; self.a += i;
}, }
Side::B => { Side::B => {
self.captures_b += 1; self.captures_b += 1;
self.b += i; self.b += i;

Loading…
Cancel
Save