Skip to content

Commit

Permalink
refactor: clean up the code
Browse files Browse the repository at this point in the history
  • Loading branch information
eerii committed Jul 17, 2024
1 parent 4af65c4 commit f0b7f85
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 83 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ release = [ # Only in release (build with --release --no-default-features --feat
"bevy_embedded_assets",
"common",
"tts",
"touch",
]
common = ["input", "menu", "persist", "ui"]

Expand All @@ -36,7 +35,6 @@ navigation = ["bevy-alt-ui-navigation-lite"]
persist = ["bevy-persistent"]
pixel_perfect = []
resizable = []
touch = ["input"]
trace = ["release", "bevy/trace_tracy"]
ui = ["sickle_ui"]

Expand Down
37 changes: 10 additions & 27 deletions examples/jump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ impl Plugin for SampleGamePlugin {
)
.run_if(in_state(GameState::Play)),
)
.add_systems(OnEnter(GameState::End), restart_game)
.register_type::<Player>();
.add_systems(OnEnter(GameState::End), restart_game);
}
}

Expand All @@ -78,7 +77,7 @@ struct PlatformInfo {
// Components
// ··········

#[derive(Reflect, Component, Default)]
#[derive(Component, Default)]
struct Player {
velocity: Vec2,
remainder: Vec2,
Expand Down Expand Up @@ -168,13 +167,9 @@ fn update_player(
mut player: Query<(&mut Player, &mut Transform)>,
platforms: Query<(&Sprite, &Transform), (With<Platform>, Without<Player>)>,
) {
let Ok((mut player, mut trans)) = player.get_single_mut() else {
return;
};
let Ok((mut player, mut trans)) = player.get_single_mut() else { return };

let Ok(input) = input.get_single() else {
return;
};
let Ok(input) = input.get_single() else { return };

let mut pos = trans.translation.xy();
pos += player.remainder;
Expand Down Expand Up @@ -270,9 +265,7 @@ fn update_camera(
mut cam: Query<(&mut Transform, &mut CameraFollow)>,
player: Query<&Player>,
) {
let Ok(player) = player.get_single() else {
return;
};
let Ok(player) = player.get_single() else { return };

for (mut trans, mut follow) in cam.iter_mut() {
let vel = (CAMERA_VEL * follow.target_pos / LEVEL_SIZE.y).powf(0.8);
Expand All @@ -289,12 +282,8 @@ fn update_camera(
}

fn update_counter(mut counter: Query<(&mut Counter, &mut Text)>, player: Query<&Player>) {
let Ok((mut counter, mut text)) = counter.get_single_mut() else {
return;
};
let Ok(player) = player.get_single() else {
return;
};
let Ok((mut counter, mut text)) = counter.get_single_mut() else { return };
let Ok(player) = player.get_single() else { return };

counter.0 = (player.max_height as u32 / SPACE_BETWEEN_PLATFORMS).saturating_sub(1);
text.sections[0].value = counter.0.to_string();
Expand All @@ -306,9 +295,7 @@ fn spawn_platforms(
mut info: ResMut<PlatformInfo>,
player: Query<&Player>,
) {
let Ok(player) = player.get_single() else {
return;
};
let Ok(player) = player.get_single() else { return };

while info.last_platform * SPACE_BETWEEN_PLATFORMS
< (player.max_height + LEVEL_SIZE.y * 0.5) as u32
Expand Down Expand Up @@ -340,9 +327,7 @@ fn check_game_over(
player: Query<&Transform, With<Player>>,
cam: Query<&CameraFollow, With<GameCamera>>,
) {
let Ok(player) = player.get_single() else {
return;
};
let Ok(player) = player.get_single() else { return };
let Ok(cam) = cam.get_single() else { return };

if player.translation.y < cam.target_pos - LEVEL_SIZE.y * 0.5 {
Expand All @@ -358,9 +343,7 @@ fn restart_game(
mut follow: Query<&mut CameraFollow>,
platforms: Query<Entity, (With<Platform>, Without<Floor>)>,
) {
let Ok((mut player, mut trans)) = player.get_single_mut() else {
return;
};
let Ok((mut player, mut trans)) = player.get_single_mut() else { return };

player.max_height = 0.;
trans.translation.y = -32.;
Expand Down
27 changes: 14 additions & 13 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,20 @@ impl LoadingData {
fn current(&mut self, asset_server: &AssetServer) -> (usize, usize) {
// Find assets that have already been loaded and remove them from the list
self.assets.retain(|asset| {
if let Some(state) = asset_server.get_load_states(asset) {
if let bevy::asset::RecursiveDependencyLoadState::Loaded = state.2 {
self.loaded += 1;
debug!(
"\"{:?}\" loaded! ({}/{})",
asset.path(),
self.loaded,
self.total
);
return false;
}
}
true
let Some(state) = asset_server.get_load_states(asset) else { return true };

let bevy::asset::RecursiveDependencyLoadState::Loaded = state.2 else {
return true;
};

self.loaded += 1;
debug!(
"\"{:?}\" loaded! ({}/{})",
asset.path(),
self.loaded,
self.total
);
false
});

(self.loaded, self.total)
Expand Down
4 changes: 1 addition & 3 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ struct UiRootContainer;

/// Create a new input manager if there are no others
fn init(mut cmd: Commands, camera: Query<Entity, With<FinalCamera>>) {
let Ok(camera) = camera.get_single() else {
return;
};
let Ok(camera) = camera.get_single() else { return };

// Ui Root
cmd.ui_builder(UiRoot).container(
Expand Down
6 changes: 2 additions & 4 deletions src/ui/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Plugin for MenuPlugin {

/// Menu state
/// Useful for navigating submenus
#[derive(SubStates, Debug, Default, Clone, Eq, PartialEq, Hash, Reflect)]
#[derive(SubStates, Debug, Default, Clone, Eq, PartialEq, Hash)]
#[source(GameState = GameState::Menu)]
pub(super) enum MenuState {
/// Main menu screen, allows to play or exit the game and access further
Expand Down Expand Up @@ -118,9 +118,7 @@ fn handle_buttons(
request: NavRequest::Action,
} => {
// If the action matches one of our buttons
let Ok(buttons) = buttons.get(*from.first()) else {
continue;
};
let Ok(buttons) = buttons.get(*from.first()) else { continue };

// Do something based on the button type
match buttons {
Expand Down
4 changes: 1 addition & 3 deletions src/ui/menu/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ pub(super) fn open(
assets: Res<CoreAssets>,
options: Res<Persistent<GameOptions>>,
) {
let Ok(root) = root.get_single() else {
return;
};
let Ok(root) = root.get_single() else { return };

cmd.ui_builder(root)
.column(|column| {
Expand Down
8 changes: 2 additions & 6 deletions src/ui/menu/mappings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ pub(super) fn open(
assets: Res<CoreAssets>,
options: Res<Persistent<GameOptions>>,
) {
let Ok(root) = root.get_single() else {
return;
};
let Ok(root) = root.get_single() else { return };

cmd.ui_builder(root)
.column(|column| {
Expand All @@ -49,9 +47,7 @@ pub(super) fn open(

column.title("Mappings".into(), assets.font.clone());

let Ok(input_map) = input_map.get_single() else {
return;
};
let Ok(input_map) = input_map.get_single() else { return };

for (action, maps) in input_map
.iter()
Expand Down
17 changes: 4 additions & 13 deletions src/ui/navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,14 @@ fn update_focus(
};

if fill.contains(entity) {
let Ok(mut color) = background.get_mut(entity) else {
continue;
};
let Ok(mut color) = background.get_mut(entity) else { continue };
*color = match focus.state() {
FocusState::Focused => BUTTON_COLOR,
_ => Srgba::NONE.into(),
}
.into();
} else {
let Ok(mut color) = border.get_mut(entity) else {
continue;
};
let Ok(mut color) = border.get_mut(entity) else { continue };
*color = match focus.state() {
FocusState::Focused => BUTTON_COLOR.lighter(0.3),
FocusState::Blocked => BUTTON_COLOR.darker(0.3),
Expand All @@ -164,9 +160,7 @@ fn handle_input(
focusables: Query<(Entity, &Node, &GlobalTransform), With<Focusable>>,
mut nav_request_writer: EventWriter<NavRequest>,
) {
let Ok(input) = input.get_single() else {
return;
};
let Ok(input) = input.get_single() else { return };

// Either go back a level or go back to the game
if input.just_pressed(&UiAction::Back) {
Expand Down Expand Up @@ -225,14 +219,11 @@ fn on_mouse_move(
focusables: Query<(Entity, &Node, &GlobalTransform), With<Focusable>>,
mut nav_request_writer: EventWriter<NavRequest>,
) {
let Ok(window) = window.get_single() else {
return;
};
let Ok(window) = window.get_single() else { return };
if let Some(mouse) = window
.cursor_position()
.map(|cursor| Aabb2d::new(cursor, Vec2::ZERO))
{
warn!("{:?}", mouse);
for (entity, node, trans) in focusables.iter() {
let focused = focused.get_single().ok().unwrap_or(Entity::PLACEHOLDER);
if entity == focused {
Expand Down
17 changes: 5 additions & 12 deletions src/ui/tts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ pub struct SpeechTag(pub String);

/// Create a text to speech system and save it as a resource
fn init(mut cmd: Commands) {
let Ok(mut tts) = Tts::default() else {
return;
};
let Ok(mut tts) = Tts::default() else { return };

info!(
"{} screen reader is available on this platform.",
Expand All @@ -74,9 +72,8 @@ fn init(mut cmd: Commands) {
if !voice {
break 'v;
}
let Ok(voices) = tts.voices() else {
break 'v;
};
let Ok(voices) = tts.voices() else { break 'v };

if let Some(voice) = voices
.iter()
.find(|&v| v.language().primary_language() == "en")
Expand Down Expand Up @@ -107,9 +104,7 @@ fn navigation_speech(
speech: Option<ResMut<Speech>>,
mut nav_event_reader: EventReader<NavEvent>,
) {
let Some(mut speech) = speech else {
return;
};
let Some(mut speech) = speech else { return };
if !options.text_to_speech {
return;
}
Expand Down Expand Up @@ -155,9 +150,7 @@ fn speak_focusable(
speech: &mut Speech,
interrupt: bool,
) -> bool {
let Ok((entity, _, children)) = query.get(current) else {
return false;
};
let Ok((entity, _, children)) = query.get(current) else { return false };

let mut interrupt = interrupt;
if let Ok(tag) = speech_tag.get(entity) {
Expand Down

0 comments on commit f0b7f85

Please sign in to comment.