Saving my place before making a big change

Going to try simplifying the piece updating logic into one method
main
Elijah C. Voigt 2 years ago
parent 3d613864bd
commit c9cf364c13

@ -143,7 +143,5 @@ fn toggle_volume(mut vol: ResMut<AudioVolume>) {
} }
fn control_volume(vol: Res<AudioVolume>, query: Query<&AudioSource>) { fn control_volume(vol: Res<AudioVolume>, query: Query<&AudioSource>) {
query query.iter().for_each(|aud_src| aud_src.set_volume(vol.0));
.iter()
.for_each(|aud_src| aud_src.set_volume(vol.0));
} }

@ -24,34 +24,30 @@ impl Plugin for Display3dPlugin {
.run_if(in_state(GameState::Loading)) .run_if(in_state(GameState::Loading))
.run_if(on_event::<AssetEvent<Tweaks>>()), .run_if(on_event::<AssetEvent<Tweaks>>()),
hydrate_camera.run_if(any_component_added::<Camera3d>()), hydrate_camera.run_if(any_component_added::<Camera3d>()),
set_piece_model.run_if(any_component_added::<Piece>()), update_tweaks
set_piece_model.run_if(any_component_changed::<Piece>()), .run_if(on_event::<AssetEvent<Tweaks>>())
set_board_model.run_if(any_component_added::<game::BoardComponent>()), .run_if(resource_exists::<tweak::GameTweaks>),
set_board_model.run_if(any_component_added::<TilesComponent>()), switch_sides
set_valid_move_model.run_if(any_component_added::<game::ValidMove>()), .run_if(in_state(GameState::Play))
set_tile_hitbox.run_if(any_component_added::<game::Tile>()), .run_if(state_changed::<game::TurnState>),
set_piece_position.run_if(any_component_changed::<BoardIndex>()), set_piece_model
.run_if(any_component_added::<Piece>().or_else(any_component_changed::<Piece>())),
set_piece_texture set_piece_texture
.run_if(any_component_changed::<Side>()) .run_if(any_component_changed::<Side>().or_else(any_component_added::<Side>()))
.run_if(resource_exists::<tweak::GameTweaks>), .run_if(resource_exists::<tweak::GameTweaks>),
set_piece_position.run_if(any_component_changed::<BoardIndex>()),
set_board_model.run_if(
any_component_added::<game::BoardComponent>()
.or_else(any_component_added::<TilesComponent>())
),
set_valid_move_model.run_if(any_component_added::<game::ValidMove>()),
set_tile_hitbox.run_if(any_component_added::<game::Tile>()),
select select
.run_if(in_state(GameState::Play)) .run_if(in_state(GameState::Play))
.run_if(in_state(DisplayState::Display3d)) .run_if(in_state(DisplayState::Display3d))
.run_if(on_event::<MouseButtonInput>()), .run_if(on_event::<MouseButtonInput>()),
pick_up.run_if(any_component_added::<game::Selected>()), pick_up.run_if(any_component_added::<game::Selected>()),
put_down.run_if(any_component_removed::<game::Selected>()), put_down.run_if(any_component_removed::<game::Selected>()),
switch_sides
.run_if(in_state(GameState::Play))
.run_if(state_changed::<game::TurnState>),
update_tweaks
.run_if(on_event::<AssetEvent<Tweaks>>())
.run_if(resource_exists::<tweak::GameTweaks>),
scale_lighting.run_if(
any_component_added::<DirectionalLight>()
.or_else(any_component_added::<SpotLight>())
.or_else(any_component_added::<PointLight>())
.or_else(on_event::<AssetEvent<Tweaks>>()),
),
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 skip_animation
@ -560,12 +556,11 @@ fn mouse_zoom(
/// PERF: We are saving what to work on in a Vector which is bad. /// PERF: We are saving what to work on in a Vector which is bad.
/// CAVEAT: We are only exeucting this when a piece changes or state is changed. /// CAVEAT: We are only exeucting this when a piece changes or state is changed.
fn set_piece_texture( fn set_piece_texture(
events: Query< query: Query<
(Entity, &Piece, &Side), (Entity, &Piece, &Side),
( (
With<game::Piece>, With<game::Piece>,
With<Display3d>, With<Display3d>,
Or<(Changed<Side>, Added<Side>)>,
), ),
>, >,
gltfs: Res<Assets<Gltf>>, gltfs: Res<Assets<Gltf>>,
@ -574,7 +569,7 @@ fn set_piece_texture(
tweaks: Res<Assets<Tweaks>>, tweaks: Res<Assets<Tweaks>>,
tweaks_file: Res<tweak::GameTweaks>, tweaks_file: Res<tweak::GameTweaks>,
) { ) {
events.iter().for_each(|(entity, piece, side)| { query.iter().for_each(|(entity, piece, side)| {
debug!("Checking piece texture for {:?}", entity); debug!("Checking piece texture for {:?}", entity);
let tweak = tweaks.get(tweaks_file.handle.clone()).unwrap(); let tweak = tweaks.get(tweaks_file.handle.clone()).unwrap();
let assets_handle = tweak let assets_handle = tweak
@ -757,14 +752,17 @@ fn select(
.filter(|ev| ev.state == ButtonState::Pressed) .filter(|ev| ev.state == ButtonState::Pressed)
.for_each(|_| { .for_each(|_| {
windows.iter().for_each(|window| { windows.iter().for_each(|window| {
if let Some(pos) = window.cursor_position() { cameras.iter().for_each(|(camera, gt)| { if let Some(pos) = window.cursor_position() {
if let Some(ray) = camera.viewport_to_world(gt, pos) { query cameras.iter().for_each(|(camera, gt)| {
if let Some(ray) = camera.viewport_to_world(gt, pos) {
query
.iter() .iter()
.filter_map(|(entity, handle, gt)| { .filter_map(|(entity, handle, gt)| {
meshes.get(handle).map(|mesh| (entity, mesh, gt)) meshes.get(handle).map(|mesh| (entity, mesh, gt))
}) })
.for_each(|(entity, mesh, gt)| { .for_each(|(entity, mesh, gt)| {
if let Some(_hit) = hit::intersects3d(&ray, mesh, gt) { selectable if let Some(_hit) = hit::intersects3d(&ray, mesh, gt) {
selectable
.iter() .iter()
.find_map(|(e, &board_index, &side)| { .find_map(|(e, &board_index, &side)| {
// Check the side of the selection if no piece is selected // Check the side of the selection if no piece is selected
@ -789,11 +787,13 @@ fn select(
.iter() .iter()
.for_each(|&board_index| { .for_each(|&board_index| {
info!("Board index selected: {:?}", board_index); info!("Board index selected: {:?}", board_index);
selections selections.send(game::Selection(board_index));
.send(game::Selection(board_index)); });
}); } }
}); } });
}); } }
});
}
}); });
}); });
} }
@ -1042,64 +1042,6 @@ fn switch_sides(
}); });
} }
fn scale_lighting(
mut directional: Query<(
Entity,
&mut DirectionalLight,
Option<&Original<DirectionalLight>>,
)>,
mut spot: Query<(Entity, &mut SpotLight, Option<&Original<SpotLight>>)>,
mut point: Query<(Entity, &mut PointLight, Option<&Original<PointLight>>)>,
mut commands: Commands,
tweaks: Res<Assets<Tweaks>>,
tweaks_file: Res<tweak::GameTweaks>,
) {
let tweak = tweaks
.get(tweaks_file.handle.clone())
.expect("Load tweakfile");
let directional_tweak = tweak
.get::<f32>("display3d_lights_scaling_directional")
.expect("Directional lighting scalar");
directional
.iter_mut()
.for_each(|(entity, mut val, original)| {
debug!("Scaling directional light {:?}", entity);
if let Some(Original(v)) = original {
val.illuminance = v.illuminance * directional_tweak;
} else {
commands.entity(entity).insert(Original(val.clone()));
val.illuminance *= directional_tweak;
}
});
let spot_tweak = tweak
.get::<f32>("display3d_lights_scaling_spot")
.expect("Spot lighting scalar");
spot.iter_mut().for_each(|(entity, mut val, original)| {
debug!("Scaling spot light {:?}", entity);
if let Some(Original(v)) = original {
val.intensity = v.intensity * spot_tweak;
} else {
commands.entity(entity).insert(Original(*val));
val.intensity *= spot_tweak;
}
});
let point_tweak = tweak
.get::<f32>("display3d_lights_scaling_point")
.expect("Point lighting scalar");
point.iter_mut().for_each(|(entity, mut val, original)| {
debug!("Scaling point light {:?}", entity);
if let Some(Original(v)) = original {
val.intensity = v.intensity * point_tweak;
} else {
commands.entity(entity).insert(Original(*val));
val.intensity *= point_tweak;
}
});
}
pub(super) mod tweaks { pub(super) mod tweaks {
use super::*; use super::*;

@ -27,7 +27,7 @@ impl Plugin for GamePlugin {
hide_valid_moves.run_if(any_component_removed::<Selected>()), hide_valid_moves.run_if(any_component_removed::<Selected>()),
manage_score.run_if(any_component_added::<Captured>()), manage_score.run_if(any_component_added::<Captured>()),
check_endgame.run_if(resource_changed::<Board>), check_endgame.run_if(resource_changed::<Board>),
reset_game.run_if(just_pressed(KeyCode::KeyR)), reset_game.run_if(in_state(GameState::Restart)),
), ),
) )
.add_systems(OnEnter(GameState::Endgame), set_endgame.after(manage_score)) .add_systems(OnEnter(GameState::Endgame), set_endgame.after(manage_score))
@ -321,9 +321,10 @@ impl Board {
.iter() .iter()
.enumerate() .enumerate()
.flat_map(|(y, nested)| { .flat_map(|(y, nested)| {
nested.iter().enumerate().filter_map(move |(x, p)| { nested
p.as_ref().map(|val| (BoardIndex { x, y }, *val)) .iter()
}) .enumerate()
.filter_map(move |(x, p)| p.as_ref().map(|val| (BoardIndex { x, y }, *val)))
}) })
.collect() .collect()
} }
@ -660,8 +661,8 @@ fn set_endgame(
.with_children(|parent| { .with_children(|parent| {
parent parent
.spawn(( .spawn((
menu::ButtonAction(GameState::Restart), ButtonAction(GameState::Restart),
menu::ButtonAction(menu::MenuState::Off), ButtonAction(MenuState::Off),
ButtonBundle { ButtonBundle {
style: Style { style: Style {
padding: UiRect::all(Val::Px(5.0)), padding: UiRect::all(Val::Px(5.0)),
@ -699,8 +700,8 @@ fn set_endgame(
// Quit button // Quit button
parent parent
.spawn(( .spawn((
menu::ButtonAction(menu::MenuState::Off), ButtonAction(MenuState::Off),
menu::ButtonAction(GameState::Quit), ButtonAction(GameState::Quit),
ButtonBundle { ButtonBundle {
style: Style { style: Style {
padding: UiRect::all(Val::Px(5.0)), padding: UiRect::all(Val::Px(5.0)),
@ -936,10 +937,7 @@ fn reset_game(
.iter() .iter()
.zip(board.pieces().iter()) .zip(board.pieces().iter())
.for_each(|(e, (i, p))| { .for_each(|(e, (i, p))| {
commands commands.entity(e).insert((*i, *p)).remove::<Captured>();
.entity(e)
.insert((*i, *p))
.remove::<Captured>();
}); });
} }
@ -948,6 +946,6 @@ fn handle_quit(mut app_exit_events: EventWriter<AppExit>) {
app_exit_events.send(AppExit); app_exit_events.send(AppExit);
} }
fn handle_restart() { fn handle_restart(mut game_state: ResMut<NextState<GameState>>) {
todo!("Restart is not implemented yet!"); game_state.set(GameState::Play);
} }

@ -118,6 +118,45 @@ fn init_play_menu(
}); });
}); });
// Restart button
parent
.spawn((
ButtonAction(GameState::Restart),
ButtonAction(MenuState::Off),
ButtonBundle {
style: Style {
padding: UiRect::all(Val::Px(5.0)),
margin: UiRect::all(Val::Px(5.0)),
..default()
},
image: UiImage {
texture: button_handle.clone(),
..default()
},
..default()
},
))
.with_children(|parent| {
parent.spawn(TextBundle {
text: Text {
sections: vec![TextSection {
value: "R e s t a r t".into(),
style: TextStyle {
color: Color::WHITE,
font_size: 16.0,
font: font_handle.clone(),
},
}],
..default()
},
style: Style {
margin: UiRect::all(Val::Px(20.0)),
..default()
},
..default()
});
});
// Tutorial button // Tutorial button
parent parent
.spawn(( .spawn((

@ -1,4 +1,5 @@
pub(crate) use crate::{audio::AudioEvent, game::*, tutorial::*, tweak::*, *}; pub(crate) use crate::{audio::AudioEvent, game::*, menu::*, tutorial::*, tweak::*, *};
pub(crate) use bevy::{ pub(crate) use bevy::{
animation::RepeatAnimation, animation::RepeatAnimation,
app::AppExit, app::AppExit,
@ -13,20 +14,12 @@ pub(crate) use bevy::{
}, },
diagnostic::*, diagnostic::*,
gltf::Gltf, gltf::Gltf,
input::{ input::{keyboard::KeyboardInput, mouse::*, ButtonState},
keyboard::KeyboardInput,
mouse::*,
ButtonState,
},
pbr::*, pbr::*,
prelude::*, prelude::*,
reflect::TypePath, reflect::TypePath,
render::{ render::{mesh::*, render_resource::*, view::*},
mesh::*, utils::{hashbrown::hash_map::Iter, BoxedFuture, HashMap, HashSet},
render_resource::*,
view::*,
},
utils::{hashbrown::hash_map::Iter, HashMap, HashSet, BoxedFuture},
window::PrimaryWindow, window::PrimaryWindow,
winit::WinitWindows, winit::WinitWindows,
}; };

Loading…
Cancel
Save