Add some team options
This commit is contained in:
parent
c11911f75f
commit
4aa60ba6a4
@ -12,7 +12,7 @@ use crate::{
|
|||||||
chat::{Chat, ChatMessage},
|
chat::{Chat, ChatMessage},
|
||||||
game::{
|
game::{
|
||||||
commands::{register_commands, unregister_commands},
|
commands::{register_commands, unregister_commands},
|
||||||
opts::{GameOption, GameOptionValue, GameOptions, PrivateGameOptions},
|
opts::{GameOption, GameOptionValue, GameOptions, PrivateGameOptions, TeamOptions},
|
||||||
},
|
},
|
||||||
map::Map,
|
map::Map,
|
||||||
map_resource::MapResource,
|
map_resource::MapResource,
|
||||||
@ -490,6 +490,20 @@ impl Game {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn change_team_option(&mut self, team: u8, opt: GameOption, value: GameOptionValue) {
|
||||||
|
if let Some(team) = self.opts.teams.get_mut(team as usize) {
|
||||||
|
team.values.insert(opt, value);
|
||||||
|
}
|
||||||
|
self.run_deferred(move |s| {
|
||||||
|
s.signals().options_changed().emit();
|
||||||
|
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer {
|
||||||
|
if let PeerKind::Server(peer, _) = peer {
|
||||||
|
peer.broadcast_reliable(Package::TeamGameOptionChanged(team, opt, value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
pub fn change_map_selection(&mut self, map_idx: u8, force: bool) {
|
pub fn change_map_selection(&mut self, map_idx: u8, force: bool) {
|
||||||
if self.selected_map_idx == map_idx && !force {
|
if self.selected_map_idx == map_idx && !force {
|
||||||
return;
|
return;
|
||||||
@ -497,6 +511,7 @@ impl Game {
|
|||||||
self.selected_map_idx = map_idx;
|
self.selected_map_idx = map_idx;
|
||||||
if let Some(map) = self.maps.get(map_idx as usize) {
|
if let Some(map) = self.maps.get(map_idx as usize) {
|
||||||
self.selected_map = Some(map);
|
self.selected_map = Some(map);
|
||||||
|
self.init_team_opts();
|
||||||
|
|
||||||
let teams_max = (self.get_teams().len() - 1) as u8;
|
let teams_max = (self.get_teams().len() - 1) as u8;
|
||||||
self.run_deferred(move |s| {
|
self.run_deferred(move |s| {
|
||||||
@ -512,6 +527,16 @@ impl Game {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn init_team_opts(&mut self) {
|
||||||
|
let prev_settings = self.opts.teams.clone();
|
||||||
|
let mut new_settings = Vec::with_capacity(self.get_teams().len());
|
||||||
|
for (id, _) in self.get_teams().iter_shared().enumerate() {
|
||||||
|
let prev = prev_settings.get(id).cloned().unwrap_or_default();
|
||||||
|
new_settings.push(prev);
|
||||||
|
}
|
||||||
|
self.opts.teams = new_settings;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_teams(&self) -> Array<Gd<TeamResource>> {
|
pub fn get_teams(&self) -> Array<Gd<TeamResource>> {
|
||||||
if let Some(map) = &self.selected_map {
|
if let Some(map) = &self.selected_map {
|
||||||
map.bind().teams.clone()
|
map.bind().teams.clone()
|
||||||
@ -524,6 +549,10 @@ impl Game {
|
|||||||
self.get_teams().get(team as usize)
|
self.get_teams().get(team as usize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_team_opts(&self, team: u8) -> Option<&TeamOptions> {
|
||||||
|
self.opts.teams.get(team as usize)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn try_set_player_team(&mut self, player_id: u16, team: u8) {
|
pub fn try_set_player_team(&mut self, player_id: u16, team: u8) {
|
||||||
if let Some(player) = self.find_player_mut(player_id) {
|
if let Some(player) = self.find_player_mut(player_id) {
|
||||||
if player.data.team == team {
|
if player.data.team == team {
|
||||||
|
|||||||
@ -10,6 +10,7 @@ pub struct PrivateGameOptions {
|
|||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct GameOptions {
|
pub struct GameOptions {
|
||||||
pub values: HashMap<GameOption, GameOptionValue>,
|
pub values: HashMap<GameOption, GameOptionValue>,
|
||||||
|
pub teams: Vec<TeamOptions>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for GameOptions {
|
impl Default for GameOptions {
|
||||||
@ -21,10 +22,26 @@ impl Default for GameOptions {
|
|||||||
opts.insert(GameOption::GoalsNeededToWin, GameOptionValue::Number(5.));
|
opts.insert(GameOption::GoalsNeededToWin, GameOptionValue::Number(5.));
|
||||||
opts.insert(GameOption::RespawnTimer, GameOptionValue::Number(5.));
|
opts.insert(GameOption::RespawnTimer, GameOptionValue::Number(5.));
|
||||||
opts.insert(GameOption::SpawnProtection, GameOptionValue::Number(3.));
|
opts.insert(GameOption::SpawnProtection, GameOptionValue::Number(3.));
|
||||||
|
Self {
|
||||||
|
values: opts,
|
||||||
|
teams: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct TeamOptions {
|
||||||
|
pub values: HashMap<GameOption, GameOptionValue>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for TeamOptions {
|
||||||
|
fn default() -> Self {
|
||||||
|
let mut opts = HashMap::new();
|
||||||
opts.insert(
|
opts.insert(
|
||||||
GameOption::TelegrenadesAllowed,
|
GameOption::TelegrenadesAllowed,
|
||||||
GameOptionValue::Boolean(true),
|
GameOptionValue::Boolean(true),
|
||||||
);
|
);
|
||||||
|
opts.insert(GameOption::PickupsAllowed, GameOptionValue::Boolean(true));
|
||||||
Self { values: opts }
|
Self { values: opts }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -71,6 +88,8 @@ pub enum GameOption {
|
|||||||
RespawnTimer,
|
RespawnTimer,
|
||||||
/// How long in seconds is spawn protected
|
/// How long in seconds is spawn protected
|
||||||
SpawnProtection,
|
SpawnProtection,
|
||||||
|
/// Whether pickups are allowed
|
||||||
|
PickupsAllowed,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToString for GameOption {
|
impl ToString for GameOption {
|
||||||
@ -83,6 +102,7 @@ impl ToString for GameOption {
|
|||||||
GameOption::TelegrenadesAllowed => "Telegrenades allowed",
|
GameOption::TelegrenadesAllowed => "Telegrenades allowed",
|
||||||
GameOption::RespawnTimer => "Respawn Time (seconds)",
|
GameOption::RespawnTimer => "Respawn Time (seconds)",
|
||||||
GameOption::SpawnProtection => "Spawn Protection",
|
GameOption::SpawnProtection => "Spawn Protection",
|
||||||
|
GameOption::PickupsAllowed => "Pickups Allowed",
|
||||||
}
|
}
|
||||||
.to_owned()
|
.to_owned()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -323,6 +323,7 @@ pub enum Package {
|
|||||||
SetTeam(u16, u8),
|
SetTeam(u16, u8),
|
||||||
GameOptions(GameOptions),
|
GameOptions(GameOptions),
|
||||||
GameOptionChanged(GameOption, GameOptionValue),
|
GameOptionChanged(GameOption, GameOptionValue),
|
||||||
|
TeamGameOptionChanged(u8, GameOption, GameOptionValue),
|
||||||
ChatMessage(u16, String),
|
ChatMessage(u16, String),
|
||||||
|
|
||||||
// Lobby packages
|
// Lobby packages
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user