Add goals needed to win option

This commit is contained in:
Sofia 2026-07-22 21:56:00 +03:00
parent 5423b9ff90
commit 6fa07c2f73
2 changed files with 38 additions and 3 deletions

View File

@ -111,6 +111,7 @@ impl Default for GameOptions {
opts.insert(GameOption::AllowAnyMap, GameOptionValue::Boolean(false));
opts.insert(GameOption::AllowAnyTeam, GameOptionValue::Boolean(false));
opts.insert(GameOption::FriendlyFire, GameOptionValue::Boolean(true));
opts.insert(GameOption::GoalsNeededToWin, GameOptionValue::Number(10.));
Self { values: opts }
}
}
@ -137,6 +138,8 @@ pub enum GameOption {
AllowAnyTeam,
/// Allow anyone to shoot anyone, not just other team's members
FriendlyFire,
/// The number of goals needed to win a match
GoalsNeededToWin,
}
impl ToString for GameOption {
@ -145,6 +148,7 @@ impl ToString for GameOption {
GameOption::AllowAnyMap => "Allow anyone to select the map",
GameOption::AllowAnyTeam => "Allow anyone to select any teams",
GameOption::FriendlyFire => "Friendly fire",
GameOption::GoalsNeededToWin => "Goals needed to win",
}
.to_owned()
}
@ -153,6 +157,7 @@ impl ToString for GameOption {
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub enum GameOptionValue {
Boolean(bool),
Number(f64),
}
#[derive(GodotClass)]
@ -599,7 +604,16 @@ impl Game {
}
if NetworkManager::singleton().bind().is_host() {
let max_goals = s.goals.values().max().copied().unwrap_or(0);
if max_goals >= 1 {
let needed_goals = match s
.opts
.values
.get(&GameOption::GoalsNeededToWin)
.unwrap_or(&&GameOptionValue::Number(10.))
{
GameOptionValue::Boolean(_) => 10,
GameOptionValue::Number(value) => *value as u16,
};
if max_goals >= needed_goals {
s.finish_game();
}
}

View File

@ -2,8 +2,8 @@ use std::collections::HashMap;
use godot::{
classes::{
Button, CheckBox, Control, GridContainer, IPanel, Label, OptionButton, Panel, TextEdit,
VBoxContainer,
Button, CheckBox, Control, GridContainer, IPanel, Label, OptionButton, Panel, SpinBox,
TextEdit, VBoxContainer,
},
prelude::*,
};
@ -162,6 +162,22 @@ impl IPanel for LobbyPanel {
checkbox.upcast::<Control>()
}
GameOptionValue::Number(value) => {
let mut spinbox = SpinBox::new_alloc();
spinbox.set_value(*value);
spinbox.set_editable(NetworkManager::singleton().bind().is_host());
let spinbox_clone = spinbox.clone();
let option = option.clone();
spinbox.signals().changed().connect_other(self, move |s| {
s.option_changed(
option,
GameOptionValue::Number(spinbox_clone.get_value()),
);
});
spinbox.upcast()
}
};
nodes.push(value_node);
}
@ -209,6 +225,11 @@ impl LobbyPanel {
checkbox.set_pressed(*value);
}
}
GameOptionValue::Number(value) => {
if let Ok(mut spinbox) = control.try_cast::<SpinBox>() {
spinbox.set_value(*value);
}
}
}
}
}