Move GameOptions to game/opts.rs

This commit is contained in:
Sofia 2026-07-29 20:16:48 +03:00
parent 897a1dbef6
commit 215c88cb49
9 changed files with 105 additions and 90 deletions

View File

@ -10,6 +10,7 @@ use crate::{
},
bgm::{Music, MusicManager},
chat::{Chat, ChatMessage},
game::opts::{GameOption, GameOptionValue, GameOptions},
map::Map,
map_resource::MapResource,
net::{
@ -32,6 +33,8 @@ use crate::{
ui::cli::{CliColor, Command, CommandLinePanel},
};
pub mod opts;
pub const GAME_MANAGER_GLOBAL: &str = "GameManagerGlobal";
#[derive(GodotClass)]
@ -152,89 +155,6 @@ impl GameManager {
}
}
#[derive(Debug)]
pub struct GameOptions {
pub values: HashMap<GameOption, GameOptionValue>,
}
impl Default for GameOptions {
fn default() -> Self {
let mut opts = HashMap::new();
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(5.));
opts.insert(GameOption::RespawnTimer, GameOptionValue::Number(5.));
opts.insert(
GameOption::TelegrenadesAllowed,
GameOptionValue::Boolean(true),
);
Self { values: opts }
}
}
impl GameOptions {
pub fn is_true(&self, opt: GameOption) -> bool {
let opt = self
.values
.get(&opt)
.copied()
.unwrap_or(GameOptionValue::Boolean(false));
match opt {
GameOptionValue::Boolean(value) => value,
_ => false,
}
}
pub fn get_float(&self, opt: GameOption) -> f64 {
let opt = self
.values
.get(&opt)
.copied()
.unwrap_or(GameOptionValue::Number(0.));
match opt {
GameOptionValue::Number(value) => value,
_ => 0.,
}
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Hash, Copy, Clone, Serialize, Deserialize)]
pub enum GameOption {
/// Allow anyone to select a map, not just the host
AllowAnyMap,
/// Allow anyone to select anyone's team, not just themselves and the host
AllowAnyTeam,
/// Allow anyone to shoot anyone, not just other team's members
FriendlyFire,
/// The number of goals needed to win a match
GoalsNeededToWin,
/// Whether telegrenades are allowed or not.
TelegrenadesAllowed,
/// How long in seconds does it take for a player to respawn
RespawnTimer,
}
impl ToString for GameOption {
fn to_string(&self) -> String {
match self {
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",
GameOption::TelegrenadesAllowed => "Telegrenades allowed",
GameOption::RespawnTimer => "Respawn Time (seconds)",
}
.to_owned()
}
}
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub enum GameOptionValue {
Boolean(bool),
Number(f64),
}
pub struct RespawnBallCommand;
impl Command for RespawnBallCommand {
fn execute(&self, _: &mut CommandLinePanel, _: &[&str]) {

86
rust/src/game/opts.rs Normal file
View File

@ -0,0 +1,86 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
#[derive(Debug)]
pub struct GameOptions {
pub values: HashMap<GameOption, GameOptionValue>,
}
impl Default for GameOptions {
fn default() -> Self {
let mut opts = HashMap::new();
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(5.));
opts.insert(GameOption::RespawnTimer, GameOptionValue::Number(5.));
opts.insert(
GameOption::TelegrenadesAllowed,
GameOptionValue::Boolean(true),
);
Self { values: opts }
}
}
impl GameOptions {
pub fn is_true(&self, opt: GameOption) -> bool {
let opt = self
.values
.get(&opt)
.copied()
.unwrap_or(GameOptionValue::Boolean(false));
match opt {
GameOptionValue::Boolean(value) => value,
_ => false,
}
}
pub fn get_float(&self, opt: GameOption) -> f64 {
let opt = self
.values
.get(&opt)
.copied()
.unwrap_or(GameOptionValue::Number(0.));
match opt {
GameOptionValue::Number(value) => value,
_ => 0.,
}
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Hash, Copy, Clone, Serialize, Deserialize)]
pub enum GameOption {
/// Allow anyone to select a map, not just the host
AllowAnyMap,
/// Allow anyone to select anyone's team, not just themselves and the host
AllowAnyTeam,
/// Allow anyone to shoot anyone, not just other team's members
FriendlyFire,
/// The number of goals needed to win a match
GoalsNeededToWin,
/// Whether telegrenades are allowed or not.
TelegrenadesAllowed,
/// How long in seconds does it take for a player to respawn
RespawnTimer,
}
impl ToString for GameOption {
fn to_string(&self) -> String {
match self {
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",
GameOption::TelegrenadesAllowed => "Telegrenades allowed",
GameOption::RespawnTimer => "Respawn Time (seconds)",
}
.to_owned()
}
}
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub enum GameOptionValue {
Boolean(bool),
Number(f64),
}

View File

@ -10,7 +10,10 @@ use teanet::{Peer, PeerConfig, PeerMessage};
use crate::{
announcer::Announcement,
game::{Game, GameManager, GameOption, GameOptionValue, NetPlayer},
game::{
Game, GameManager, NetPlayer,
opts::{GameOption, GameOptionValue},
},
game_settings::GameSettingsManager,
net::{net_stats::Stats, util::NetVector3},
player::{DamageSource, NetTransform, weapon::WeaponType},

View File

@ -4,7 +4,7 @@ use godot::obj::Singleton;
use teanet::PeerMessage;
use crate::{
game::{Game, GameManager, GameOption},
game::{Game, GameManager, opts::GameOption},
game_settings::GameSettingsManager,
net::network_manager::{NetworkManager, Package, PeerKind},
popup_queue::{Popup, PopupQueue},

View File

@ -10,7 +10,7 @@ use godot::{
};
use crate::{
game::{Game, GameManager, GameOption},
game::{Game, GameManager, opts::GameOption},
game_settings::{GameSettings, GameSettingsManager},
killbox::KillboxKind,
net::{

View File

@ -8,7 +8,7 @@ use godot::{
use serde::{Deserialize, Serialize};
use crate::{
game::{Game, GameOption},
game::{Game, opts::GameOption},
killbox::KillboxKind,
net::{
network_manager::NetworkManager,

View File

@ -8,7 +8,7 @@ use godot::{
};
use crate::{
game::{Game, GameOption},
game::{Game, opts::GameOption},
killbox::KillboxKind,
pickup::PickupKind,
player::{

View File

@ -10,7 +10,10 @@ use godot::{
use crate::{
bgm::{Music, MusicManager},
game::{Game, GameManager, GameOption, GameOptionValue, GameOptions},
game::{
Game, GameManager,
opts::{GameOption, GameOptionValue, GameOptions},
},
net::network_manager::{NetworkManager, Package, PeerKind},
ui::lobby_player_listing::LobbyPlayerListing,
};

View File

@ -4,7 +4,10 @@ use godot::{
};
use crate::{
game::{Game, GameOption, GameOptions},
game::{
Game,
opts::{GameOption, GameOptions},
},
net::network_manager::NetworkManager,
team_resource::TeamResource,
};