From 3391657470cacba6d64eadc93893db415ada4a6d Mon Sep 17 00:00:00 2001 From: Selyatin Ismet Date: Tue, 23 Nov 2021 19:27:13 +0200 Subject: [PATCH] Added Loading screen for multiplayer and fixed some more scaling issues on Multiplayer Screen --- Cargo.toml | 4 +-- src/main.rs | 5 +-- src/screens.rs | 91 +++++++++++++++++++++++++++++++++++++++++++++++--- src/types.rs | 1 + 4 files changed, 93 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 028c888..fd7cd68 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "tip" -version = "0.1.0" +name = "tip-game" +version = "0.4.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/main.rs b/src/main.rs index 3a3facf..b46f8a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,6 +47,7 @@ fn main_loop(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { Screen::SinglePlayer => screens::single_player(stdout, state)?, Screen::Join => screens::join(stdout, state)?, Screen::MultiPlayer => screens::multi_player(stdout, state)?, + Screen::Loading => screens::loading(stdout, state)? }; if let Some(err) = &state.err { @@ -117,7 +118,7 @@ fn main_loop(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { player.input.clear(); - state.screen = Screen::MultiPlayer; + state.screen = Screen::Loading; } _ => (), }, @@ -185,7 +186,7 @@ fn main_loop(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { .iter_mut() .for_each(|word| word.y = rng.u16(0..state.rows - 1)); - state.screen = Screen::MultiPlayer; + state.screen = Screen::Loading; } Event::Key(KeyEvent { code: KeyCode::F(3), diff --git a/src/screens.rs b/src/screens.rs index 4853a67..1f9fbe5 100644 --- a/src/screens.rs +++ b/src/screens.rs @@ -1,4 +1,4 @@ -use super::types::{Action, Player, State}; +use super::types::{Action, Player, Screen, State}; use crossterm::{ cursor::MoveTo, queue, @@ -143,7 +143,7 @@ pub fn multi_player(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { let players_len = state.players.len(); - let space_per_player = (rows / players_len as f32) as u16 - 2; + let space_per_player = ((rows - 2.0) / players_len as f32) as u16; let line: String = iter::repeat('-').take(columns.into()).collect(); @@ -162,13 +162,16 @@ pub fn multi_player(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { _ => Color::White, }; + let print_you = if player.current_player { " (You)" } else { "" }; + queue!( stdout, MoveTo(0, y_end), PrintStyledContent(style(&line).with(color)), MoveTo(5, y_end), PrintStyledContent("Player ".with(color)), - PrintStyledContent(style(i + 1).with(color)) + PrintStyledContent(style(i + 1).with(color)), + PrintStyledContent(style(print_you).with(color)) )?; let mut add_x: u16 = 4; @@ -178,8 +181,10 @@ pub fn multi_player(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { .enumerate() { let mut correct_chars = 0; + + let (y_start, y_end) = (y_start + 1, y_end - 1); - let word_y = ((word.y as f32 / rows) * (y_end - y_start) as f32) as u16 + y_start + 1; + let word_y = ((word.y as f32 / rows) * (y_end - y_start) as f32) as u16 + y_start; for (n, c) in word.value.chars().enumerate() { let mut color = Color::White; @@ -220,6 +225,84 @@ pub fn multi_player(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { Ok(()) } +pub fn loading(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { + let socket = state.socket.as_ref().unwrap(); + + for action in socket.actions().drain(..) { + match action { + Action::Join(position) => { + let mut player = Player::default(); + player.sort_position = position; + state.players.push(player); + state.players.sort_by(|player_a, player_b| { + player_a.sort_position.cmp(&player_b.sort_position) + }); + for (i, player) in state.players.iter().enumerate() { + if player.current_player { + state.current_player = i; + break; + } + } + } + Action::Left(position) => { + state.players.remove(position); + state.players.sort_by(|player_a, player_b| { + player_a.sort_position.cmp(&player_b.sort_position) + }); + for (i, player) in state.players.iter().enumerate() { + if player.current_player { + state.current_player = i; + break; + } + } + } + Action::Forward => { + state.screen = Screen::MultiPlayer; + return Ok(()); + } + _ => (), + }; + } + + let (columns, rows) = ( + (state.columns as f32 * 0.35) as u16, + (state.rows as f32 * 0.4) as u16, + ); + + queue!( + stdout, + MoveTo(columns, rows), + PrintStyledContent( + "Waiting 10 seconds for other players to join." + .green() + .bold() + ), + MoveTo(columns, rows + 2) + )?; + + for (i, player) in state.players.iter().enumerate() { + let color = match i { + 0 => Color::Blue, + 1 => Color::Red, + 2 => Color::Green, + 3 => Color::Yellow, + _ => Color::White, + }; + + let print_you = if player.current_player { " (You)" } else { "" }; + + queue!( + stdout, + PrintStyledContent("Player ".with(color).bold()), + PrintStyledContent(style(i + 1).with(color).bold()), + PrintStyledContent(style(print_you).with(color).bold()), + Print(' ') + )?; + } + + Ok(()) +} + pub fn join(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { print_help(stdout, &state)?; diff --git a/src/types.rs b/src/types.rs index 9a5f346..7a2bc4d 100644 --- a/src/types.rs +++ b/src/types.rs @@ -31,6 +31,7 @@ pub enum Screen { SinglePlayer, MultiPlayer, Join, + Loading } #[derive(Clone)]