Fix map selection interface

This commit is contained in:
Sofia 2026-07-30 01:27:17 +03:00
parent cf68bd7887
commit 254aa640fa
3 changed files with 47 additions and 11 deletions

View File

@ -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);
}
}

View File

@ -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<Gd<MapResource>>,
pub maps: Array<Gd<MapResource>>,

View File

@ -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
}
}