Skip to content

Commit

Permalink
Added Loading screen for multiplayer and fixed some more scaling issu…
Browse files Browse the repository at this point in the history
…es on Multiplayer Screen
  • Loading branch information
Selyatin committed Nov 23, 2021
1 parent 3792d06 commit 3391657
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
_ => (),
},
Expand Down Expand Up @@ -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),
Expand Down
91 changes: 87 additions & 4 deletions src/screens.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::types::{Action, Player, State};
use super::types::{Action, Player, Screen, State};
use crossterm::{
cursor::MoveTo,
queue,
Expand Down Expand Up @@ -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();

Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)?;

Expand Down
1 change: 1 addition & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub enum Screen {
SinglePlayer,
MultiPlayer,
Join,
Loading
}

#[derive(Clone)]
Expand Down

0 comments on commit 3391657

Please sign in to comment.