diff --git a/rust/src/cli_parser.rs b/rust/src/cli_parser.rs index 1bbcd13..d261588 100644 --- a/rust/src/cli_parser.rs +++ b/rust/src/cli_parser.rs @@ -110,6 +110,7 @@ impl CliParser { map.bind().name.to_string().to_lowercase() == map_name.to_lowercase() }); if let Some((map_idx, _)) = map { + game.bind_mut().forced_map_selection = true; game.bind_mut().change_map_selection(map_idx as u8, true); } } diff --git a/rust/src/game/mod.rs b/rust/src/game/mod.rs index 9453a0f..e13f168 100644 --- a/rust/src/game/mod.rs +++ b/rust/src/game/mod.rs @@ -167,6 +167,7 @@ impl GameManager { #[derive(GodotClass)] #[class(base=Node, init)] pub struct Game { + pub forced_map_selection: bool, pub selected_map_idx: u8, pub selected_map: Option>, pub maps: Array>, diff --git a/rust/src/ui/lobby.rs b/rust/src/ui/lobby.rs index 786e388..320d498 100644 --- a/rust/src/ui/lobby.rs +++ b/rust/src/ui/lobby.rs @@ -10,6 +10,7 @@ use godot::{ use crate::{ bgm::{Music, MusicManager}, + cli_parser::CliParser, game::{ Game, GameManager, opts::{GameOption, GameOptionValue, GameOptions}, @@ -73,8 +74,10 @@ impl IPanel for LobbyPanel { map_selection .signals() .item_selected() - .connect_other(self, |s, id| { - s.update_map_selection(id as u8); + .connect_other(self, |s, _| { + if let Some(map_selection) = &s.map_selection { + s.update_map_selection(map_selection.get_selected_id() as u8); + } }); } @@ -95,8 +98,26 @@ impl IPanel for LobbyPanel { } else { false }; - if let Some(map_selection) = &mut self.map_selection { - let prev_selection = game.bind().selected_map_idx as i32; + if let Some(mut map_selection) = self.map_selection.clone() { + let mut prev_selection = game.bind().selected_map_idx; + let prev_selection_hidden = + if let Some(map) = game.bind().maps.get(prev_selection as usize) { + map.bind().hidden + } else { + false + }; + if prev_selection_hidden && !game.bind().forced_map_selection { + if let Some((id, _)) = game + .bind() + .maps + .iter_shared() + .enumerate() + .find(|(_, m)| !m.bind().hidden) + { + prev_selection = id as u8; + } + } + for (id, map) in game.bind().maps.iter_shared().enumerate() { if map.bind().hidden { continue; @@ -113,7 +134,8 @@ impl IPanel for LobbyPanel { .done(); } } - map_selection.select(prev_selection); + let idx = self.find_idx(prev_selection); + map_selection.select(idx); if is_host { self.update_map_selection(prev_selection as u8); } @@ -350,24 +372,36 @@ impl LobbyPanel { } } - pub fn update_map_selection(&mut self, idx: u8) { + pub fn update_map_selection(&mut self, id: u8) { if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer { match peer { PeerKind::Client(peer, addr) => { - if let Some(map_selection) = &mut self.map_selection { - map_selection.select(idx as i32); + if let Some(mut map_selection) = self.map_selection.clone() { + map_selection.select(self.find_idx(id)); self.run_deferred(|s| s.update_players(false)); - peer.send_reliable(addr, Package::SelectMap(idx)); + peer.send_reliable(addr, Package::SelectMap(id)); } } PeerKind::Server(peer, _) => { if let Some(game) = &mut Game::singleton() { - game.bind_mut().change_map_selection(idx, false); + game.bind_mut().change_map_selection(id, false); } self.run_deferred(|s| s.update_players(false)); - peer.broadcast_reliable(Package::SelectMap(idx)); + peer.broadcast_reliable(Package::SelectMap(id)); } } } } + + pub fn find_idx(&self, map_id: u8) -> i32 { + let mut idx = 0; + if let Some(map_selection) = &self.map_selection { + for i in 0..map_selection.get_item_count() { + if map_selection.get_item_id(i) == map_id as i32 { + idx = i; + } + } + } + idx + } }