Skip to content

Commit

Permalink
feat: Add matchmaking screen loading animation
Browse files Browse the repository at this point in the history
  • Loading branch information
PraxTube committed Oct 31, 2023
1 parent ed0ea23 commit d3ff454
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/player/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::world::Rematch;
use crate::RollbackState;

// Movement
pub const MIN_SPEED: f32 = 000.0 / 60.0;
pub const MIN_SPEED: f32 = 200.0 / 60.0;
pub const DELTA_SPEED: f32 = 75.0 / 60.0 / 100.0;
pub const DELTA_STEERING: f32 = 3.5 / 60.0;
// Collision
Expand Down
62 changes: 54 additions & 8 deletions src/ui/matchmaking_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ use crate::{GameAssets, GameState};

#[derive(Component)]
struct MatchmakingScreen;
#[derive(Component)]
struct MatchmakingText;

fn spawn_title_text(commands: &mut Commands, font: Handle<Font>) -> Entity {
let text_style = TextStyle {
font,
font_size: 75.0,
color: Color::WHITE,
};
let text_bundle = TextBundle::from_sections([TextSection::new(
"WAITING FOR 1 OTHER PLAYER...".to_string(),
text_style,
)])
.with_text_alignment(TextAlignment::Center);
commands.spawn(text_bundle).id()
let text_bundle =
TextBundle::from_sections([TextSection::new("LOADING -".to_string(), text_style)]);
commands.spawn((MatchmakingText, text_bundle)).id()
}

fn spawn_quit_text(commands: &mut Commands, font: Handle<Font>) -> Entity {
Expand Down Expand Up @@ -70,11 +69,58 @@ fn despawn_matchmaking_screen(
}
}

fn animate_matchmaking_screen(
mut query: Query<&mut Text, With<MatchmakingText>>,
mut ticks: Local<f32>,
mut forward: Local<bool>,
time: Res<Time>,
) {
let mut text = if let Ok(t) = query.get_single_mut() {
t
} else {
error!("there are multiple or none matchmaking texts, there should be exactly one");
return;
};

*ticks += time.delta_seconds();
if *ticks < 0.2 {
return;
}
*ticks = 0.0;

let content = &text.sections[0].value;
let new_text = if content.ends_with('-') {
*forward = !*forward;
if *forward {
"LOADING /"
} else {
"LOADING \\"
}
} else if content.ends_with('/') {
"LOADING -"
} else if content.ends_with('\\') {
"LOADING |"
} else {
*forward = !*forward;
if *forward {
"LOADING /"
} else {
"LOADING \\"
}
};

text.sections[0].value = new_text.to_string();
}

pub struct MatchmakingUiPlugin;

impl Plugin for MatchmakingUiPlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnEnter(GameState::Matchmaking), spawn_matchmaking_screen)
.add_systems(OnExit(GameState::Matchmaking), despawn_matchmaking_screen);
app.add_systems(
Update,
animate_matchmaking_screen.run_if(in_state(GameState::Matchmaking)),
)
.add_systems(OnEnter(GameState::Matchmaking), spawn_matchmaking_screen)
.add_systems(OnExit(GameState::Matchmaking), despawn_matchmaking_screen);
}
}

0 comments on commit d3ff454

Please sign in to comment.