Compare commits
8 Commits
b6278cbc5a
...
3a0eaa58be
| Author | SHA1 | Date | |
|---|---|---|---|
| 3a0eaa58be | |||
| 0a1fdce192 | |||
| 5a04e82a11 | |||
| f32a43a826 | |||
| abc89c1e3e | |||
| 30b64ab475 | |||
| 791c879a13 | |||
| c910946151 |
@ -29,7 +29,9 @@ impl Chat {
|
||||
pub fn new_message(&mut self, message: ChatMessage) {
|
||||
self.messages.len() as i64;
|
||||
self.messages.push(message.clone());
|
||||
self.run_deferred(move |s| s.signals().on_new_message().emit(&message.as_label()));
|
||||
let label = message.as_label();
|
||||
godot_print_rich!("{}", label.get_text());
|
||||
self.run_deferred(move |s| s.signals().on_new_message().emit(&label));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -88,7 +88,7 @@ impl INode3D for CliParser {
|
||||
.connect_other(self, |s| s.on_join_lobby());
|
||||
|
||||
if let Some(port) = options.host {
|
||||
NetworkManager::singleton().bind_mut().host(port);
|
||||
NetworkManager::singleton().bind_mut().host(port, false);
|
||||
} else if let Some(addr) = options.join {
|
||||
NetworkManager::singleton().bind_mut().join_directly(addr);
|
||||
}
|
||||
|
||||
@ -37,7 +37,7 @@ use crate::{
|
||||
},
|
||||
replay::{ReplayEvent, ReplayPlayback, ReplayRecorder},
|
||||
team_resource::TeamResource,
|
||||
ui::cli::CommandLinePanel,
|
||||
ui::cli::{CliColor, CommandLinePanel},
|
||||
};
|
||||
|
||||
pub mod commands;
|
||||
@ -175,6 +175,7 @@ pub struct Game {
|
||||
pub private_opts: PrivateGameOptions,
|
||||
|
||||
is_host: bool,
|
||||
host_privileges: bool,
|
||||
player_counter: u16,
|
||||
pub self_id: Option<u16>,
|
||||
pub players: Vec<Player>,
|
||||
@ -351,8 +352,46 @@ impl Game {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_host_privileges(&mut self, privileges: bool) {
|
||||
if privileges {
|
||||
self.set_ready(true);
|
||||
}
|
||||
self.host_privileges = privileges;
|
||||
}
|
||||
|
||||
pub fn has_host_privileges(&self) -> bool {
|
||||
self.is_host || self.host_privileges
|
||||
}
|
||||
|
||||
pub fn is_host(&self) -> bool {
|
||||
self.is_host
|
||||
}
|
||||
|
||||
pub fn set_ready(&mut self, ready: bool) {
|
||||
if let Some(self_id) = self.self_id {
|
||||
self.run_deferred(move |s| {
|
||||
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer {
|
||||
match peer {
|
||||
PeerKind::Client(peer, socket_addr) => {
|
||||
godot_print!("Sent ready {}", ready);
|
||||
peer.send_reliable(&socket_addr, Package::SetReady(self_id, ready))
|
||||
}
|
||||
PeerKind::Server(peer, _) => {
|
||||
if let Some(player) = s.find_player_mut(self_id) {
|
||||
player.data.ready = ready;
|
||||
}
|
||||
peer.broadcast_reliable(Package::SetReady(self_id, ready));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_playback(&mut self, time_delta: f64, spectated_id: u16) {
|
||||
if let Some(recorder) = &self.replay_recorder {
|
||||
if let Some(recorder) = &self.replay_recorder
|
||||
&& self.self_id.is_some()
|
||||
{
|
||||
let mut playback = ReplayPlayback::new_alloc();
|
||||
|
||||
let frame = recorder
|
||||
@ -409,6 +448,11 @@ impl Game {
|
||||
|
||||
pub fn start_game(&mut self) {
|
||||
self.game_started = true;
|
||||
|
||||
CommandLinePanel::singleton()
|
||||
.bind_mut()
|
||||
.publish_message("Game started".to_owned(), CliColor::Info);
|
||||
|
||||
if let Some(map) = &self.selected_map {
|
||||
if let Some(scene) = &map.bind().scene {
|
||||
self.base().get_tree().change_scene_to_packed(scene);
|
||||
@ -430,9 +474,11 @@ impl Game {
|
||||
}
|
||||
});
|
||||
|
||||
let recorder = ReplayRecorder::new_alloc();
|
||||
self.replay_recorder = Some(recorder.clone());
|
||||
self.base_mut().add_child(&recorder);
|
||||
if self.self_id.is_some() {
|
||||
let recorder = ReplayRecorder::new_alloc();
|
||||
self.replay_recorder = Some(recorder.clone());
|
||||
self.base_mut().add_child(&recorder);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reset_game(&mut self) {
|
||||
@ -440,13 +486,10 @@ impl Game {
|
||||
recorder.queue_free();
|
||||
}
|
||||
|
||||
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer
|
||||
&& let PeerKind::Server(peer, _) = peer
|
||||
&& let Some(player) = self.find_player_mut(self.self_id.unwrap_or(0))
|
||||
{
|
||||
player.data.ready = true;
|
||||
peer.broadcast_reliable(Package::SetReady(player.id(), true));
|
||||
if self.has_host_privileges() {
|
||||
self.set_ready(true);
|
||||
}
|
||||
|
||||
for player in &mut self.players {
|
||||
player.stats = Default::default();
|
||||
}
|
||||
@ -496,27 +539,55 @@ impl Game {
|
||||
unregister_commands(&mut cli);
|
||||
}
|
||||
|
||||
pub fn change_option(&mut self, opt: GameOption, value: GameOptionValue) {
|
||||
pub fn change_option(&mut self, opt: GameOption, value: GameOptionValue, send: bool) {
|
||||
godot_print!("Option changed");
|
||||
self.opts.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::GameOptionChanged(opt, value));
|
||||
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer
|
||||
&& send
|
||||
{
|
||||
match peer {
|
||||
PeerKind::Client(peer, socket_addr) => {
|
||||
if s.has_host_privileges() {
|
||||
peer.send_reliable(socket_addr, Package::GameOptionChanged(opt, value));
|
||||
}
|
||||
}
|
||||
PeerKind::Server(peer, _) => {
|
||||
peer.broadcast_reliable(Package::GameOptionChanged(opt, value));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn change_team_option(&mut self, team: u8, opt: GameOption, value: GameOptionValue) {
|
||||
pub fn change_team_option(
|
||||
&mut self,
|
||||
team: u8,
|
||||
opt: GameOption,
|
||||
value: GameOptionValue,
|
||||
send: bool,
|
||||
) {
|
||||
if let Some(team) = self.opts.teams.get_mut(team as usize) {
|
||||
team.values.insert(opt, value);
|
||||
}
|
||||
self.run_deferred(move |s| {
|
||||
s.signals().team_options_changed().emit(team);
|
||||
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer {
|
||||
if let PeerKind::Server(peer, _) = peer {
|
||||
peer.broadcast_reliable(Package::TeamGameOptionChanged(team, opt, value));
|
||||
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer
|
||||
&& send
|
||||
{
|
||||
match peer {
|
||||
PeerKind::Client(peer, socket_addr) => {
|
||||
if s.has_host_privileges() {
|
||||
peer.send_reliable(
|
||||
socket_addr,
|
||||
Package::TeamGameOptionChanged(team, opt, value),
|
||||
);
|
||||
}
|
||||
}
|
||||
PeerKind::Server(peer, _) => {
|
||||
peer.broadcast_reliable(Package::TeamGameOptionChanged(team, opt, value));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -608,7 +679,13 @@ impl Game {
|
||||
self.self_id = Some(id);
|
||||
}
|
||||
|
||||
pub fn new_player(&mut self, addr: SocketAddr, name: Option<String>, id: Option<u16>) -> u16 {
|
||||
pub fn new_player(
|
||||
&mut self,
|
||||
addr: SocketAddr,
|
||||
name: Option<String>,
|
||||
id: Option<u16>,
|
||||
host_privileges: bool,
|
||||
) -> u16 {
|
||||
let id = if let Some(id) = id {
|
||||
id
|
||||
} else {
|
||||
@ -631,6 +708,7 @@ impl Game {
|
||||
character: None,
|
||||
last_held_weapon: WeaponType::Raygun,
|
||||
last_position: Vector3::ZERO,
|
||||
has_host_privileges: host_privileges,
|
||||
});
|
||||
self.run_deferred(move |s| s.signals().on_new_player().emit(id));
|
||||
|
||||
@ -655,6 +733,11 @@ impl Game {
|
||||
.unwrap_or(0);
|
||||
|
||||
self.try_set_player_team(id, team_with_least as u8);
|
||||
|
||||
CommandLinePanel::singleton()
|
||||
.bind_mut()
|
||||
.publish_message(format!("Player joined: {}", addr), CliColor::Info);
|
||||
|
||||
id
|
||||
}
|
||||
|
||||
@ -682,6 +765,17 @@ impl Game {
|
||||
}
|
||||
|
||||
pub fn remove_player(&mut self, id: u16) {
|
||||
if let Some(player) = self.find_player(id) {
|
||||
CommandLinePanel::singleton().bind_mut().publish_message(
|
||||
format!(
|
||||
"Player {} disconnected ({})",
|
||||
player.id(),
|
||||
player.connection_addr
|
||||
),
|
||||
CliColor::Info,
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(player) = self.find_player_mut(id)
|
||||
&& let Some(mut character) = player.character.take()
|
||||
{
|
||||
@ -696,6 +790,14 @@ impl Game {
|
||||
|
||||
pub fn update_player_nick(&mut self, player_id: u16, nick: String) {
|
||||
if let Some(player) = self.players.iter_mut().find(|p| p.id() == player_id) {
|
||||
CommandLinePanel::singleton().bind_mut().publish_message(
|
||||
format!(
|
||||
"Player nick changed {} -> {} ({})",
|
||||
player.data.name, nick, player.connection_addr
|
||||
),
|
||||
CliColor::Info,
|
||||
);
|
||||
|
||||
player.data.name = nick;
|
||||
}
|
||||
self.run_deferred(|s| s.signals().on_players_updated().emit());
|
||||
@ -901,10 +1003,23 @@ impl Game {
|
||||
}
|
||||
|
||||
pub fn spawn_player_server(&mut self, id: u16) -> Option<DynGd<CharacterBody3D, dyn IPlayer>> {
|
||||
if id == self.self_id.unwrap_or(0) {
|
||||
if let Some(self_id) = self.self_id
|
||||
&& id == self_id
|
||||
{
|
||||
self.stop_playback();
|
||||
}
|
||||
|
||||
if let Some(player) = self.find_player(id) {
|
||||
CommandLinePanel::singleton().bind_mut().publish_message(
|
||||
format!(
|
||||
"Player spawned {} ({})",
|
||||
player.id(),
|
||||
player.connection_addr
|
||||
),
|
||||
CliColor::Info,
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(player) = self.find_player(id)
|
||||
&& let Some(mut character) = player.character.clone()
|
||||
{
|
||||
@ -921,9 +1036,13 @@ impl Game {
|
||||
|
||||
if let Some(mut map) = self.current_map.clone() {
|
||||
let team = self.find_player(id).map(|p| p.data.team).unwrap_or(0);
|
||||
let mut character =
|
||||
map.bind_mut()
|
||||
.spawn_player(id, team, id == self.self_id.unwrap_or(0), None, true);
|
||||
let mut character = map.bind_mut().spawn_player(
|
||||
id,
|
||||
team,
|
||||
self.self_id.map(|self_id| self_id == id).unwrap_or(false),
|
||||
None,
|
||||
true,
|
||||
);
|
||||
|
||||
if let Some(character) = &character
|
||||
&& let Some(player) = self.find_player_mut(id)
|
||||
@ -995,7 +1114,9 @@ impl Game {
|
||||
|
||||
// Method called on the client-side whenever a player has spawned
|
||||
pub fn spawn_player_client(&mut self, id: u16, transform: NetTransform) {
|
||||
if id == self.self_id.unwrap_or(0) {
|
||||
if let Some(self_id) = self.self_id
|
||||
&& id == self_id
|
||||
{
|
||||
self.stop_playback();
|
||||
}
|
||||
|
||||
@ -1014,10 +1135,13 @@ impl Game {
|
||||
character.queue_free();
|
||||
}
|
||||
|
||||
let player_id = player.id();
|
||||
let mut character = map.bind_mut().spawn_player(
|
||||
player.id(),
|
||||
player.data.team,
|
||||
player.id() == self.self_id.unwrap_or(0),
|
||||
self.self_id
|
||||
.map(|self_id| self_id == player_id)
|
||||
.unwrap_or(false),
|
||||
Some(transform),
|
||||
true,
|
||||
);
|
||||
@ -1071,7 +1195,8 @@ impl Game {
|
||||
telegrenade.set_linear_velocity(telegrenade_sync.velocity.into());
|
||||
}
|
||||
|
||||
if player.id() != self.self_id.unwrap_or(0) {
|
||||
let player_id = player.id();
|
||||
if self.self_id.map(|id| id != player_id).unwrap_or(true) {
|
||||
// Sync remote players always
|
||||
player.set_transform(transform);
|
||||
player.set_move_dir(movement_direction);
|
||||
@ -1103,7 +1228,11 @@ impl Game {
|
||||
|
||||
// Handle a jump, either from the server or from a client
|
||||
pub fn handle_jump(&mut self, player_id: u16) {
|
||||
if player_id != self.self_id.unwrap_or(0) {
|
||||
if self
|
||||
.self_id
|
||||
.map(|self_id| self_id != player_id)
|
||||
.unwrap_or(true)
|
||||
{
|
||||
if let Some(recorder) = &mut self.replay_recorder {
|
||||
recorder
|
||||
.bind_mut()
|
||||
@ -1143,8 +1272,11 @@ impl Game {
|
||||
look_up: f32,
|
||||
deal_damage: bool,
|
||||
) -> bool {
|
||||
let self_id = self.self_id.clone().unwrap_or(0);
|
||||
if player_id != self_id {
|
||||
if self
|
||||
.self_id
|
||||
.map(|self_id| self_id != player_id)
|
||||
.unwrap_or(true)
|
||||
{
|
||||
if let Some(recorder) = &mut self.replay_recorder {
|
||||
recorder
|
||||
.bind_mut()
|
||||
@ -1170,8 +1302,11 @@ impl Game {
|
||||
look_up: f32,
|
||||
deal_damage: bool,
|
||||
) -> bool {
|
||||
let self_id = self.self_id.clone().unwrap_or(0);
|
||||
if player_id != self_id {
|
||||
if self
|
||||
.self_id
|
||||
.map(|self_id| self_id != player_id)
|
||||
.unwrap_or(true)
|
||||
{
|
||||
if let Some(recorder) = &mut self.replay_recorder {
|
||||
recorder
|
||||
.bind_mut()
|
||||
@ -1561,6 +1696,7 @@ pub struct Player {
|
||||
pub killing_spree_cd: f64,
|
||||
pub last_held_weapon: WeaponType,
|
||||
pub last_position: Vector3,
|
||||
pub has_host_privileges: bool,
|
||||
}
|
||||
|
||||
impl Player {
|
||||
@ -1747,6 +1883,7 @@ impl From<NetPlayer> for Player {
|
||||
killing_spree_cd: 0.,
|
||||
last_held_weapon: WeaponType::Raygun,
|
||||
last_position: Vector3::ZERO,
|
||||
has_host_privileges: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -85,7 +85,8 @@ impl Goal {
|
||||
if let Some(game) = Game::singleton()
|
||||
&& let Some(ball_holder) = game.bind().ball_holder
|
||||
&& let Some(player) = game.bind().find_player(ball_holder)
|
||||
&& ball_holder == game.bind().self_id.unwrap_or(0)
|
||||
&& let Some(self_id) = game.bind().self_id
|
||||
&& ball_holder == self_id
|
||||
&& player.data.team != self.team
|
||||
{
|
||||
icon.set_visible(true);
|
||||
|
||||
@ -62,7 +62,7 @@ impl INode for NetworkManager {
|
||||
cli.bind_mut()
|
||||
.signals()
|
||||
.on_host()
|
||||
.connect_other(self, |s, port| s.host(port));
|
||||
.connect_other(self, |s, port, headless| s.host(port, headless));
|
||||
cli.bind_mut()
|
||||
.signals()
|
||||
.on_join()
|
||||
@ -188,7 +188,7 @@ impl NetworkManager {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn host(&mut self, port: u16) {
|
||||
pub fn host(&mut self, port: u16, headless: bool) {
|
||||
if self.peer.is_none() {
|
||||
let mut cli = CommandLinePanel::singleton();
|
||||
cli.bind_mut()
|
||||
@ -216,11 +216,14 @@ impl NetworkManager {
|
||||
.default_name
|
||||
.clone();
|
||||
|
||||
if let Some(game) = &mut Game::singleton() {
|
||||
if let Some(game) = &mut Game::singleton()
|
||||
&& !headless
|
||||
{
|
||||
let id = game.bind_mut().new_player(
|
||||
SocketAddr::from(([0, 0, 0, 0], 0)),
|
||||
Some(default_name),
|
||||
None,
|
||||
true,
|
||||
);
|
||||
game.bind_mut().update_player_ready(id, true);
|
||||
game.bind_mut().set_self(id);
|
||||
@ -313,11 +316,13 @@ impl NetworkManager {
|
||||
}
|
||||
PeerKind::Server(peer, ..) => {
|
||||
let player_id = if let Some(game) = Game::singleton() {
|
||||
game.bind().self_id.unwrap_or(0)
|
||||
game.bind().self_id
|
||||
} else {
|
||||
0
|
||||
None
|
||||
};
|
||||
peer.broadcast_reliable(Package::SetNick(player_id, nick));
|
||||
if let Some(player_id) = player_id {
|
||||
peer.broadcast_reliable(Package::SetNick(player_id, nick));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -346,7 +351,7 @@ pub enum Package {
|
||||
Hello,
|
||||
|
||||
// Meta values
|
||||
Players(Vec<NetPlayer>, u16),
|
||||
Players(Vec<NetPlayer>, u16, bool),
|
||||
UpdatePings(HashMap<u16, u128>),
|
||||
SetNick(u16, String),
|
||||
SetSelfNick(String),
|
||||
|
||||
@ -95,7 +95,7 @@ impl NetworkManager {
|
||||
cli.bind_mut()
|
||||
.publish_message(format!("Got \"Hello\"!"), CliColor::Info);
|
||||
}
|
||||
Package::Players(players, self_id) => {
|
||||
Package::Players(players, self_id, host_privileges) => {
|
||||
cli.bind_mut()
|
||||
.publish_message(format!("Got players"), CliColor::Info);
|
||||
if let Some(game) = &mut Game::singleton() {
|
||||
@ -103,6 +103,9 @@ impl NetworkManager {
|
||||
players.into_iter().map(|p| p.clone().into()).collect(),
|
||||
);
|
||||
game.bind_mut().set_self(self_id);
|
||||
if host_privileges {
|
||||
game.bind_mut().set_host_privileges(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
Package::SetNick(id, nick) => {
|
||||
@ -119,18 +122,29 @@ impl NetworkManager {
|
||||
}
|
||||
Package::NewPlayer(player) => {
|
||||
if let Some(game) = &mut Game::singleton() {
|
||||
if player.id != game.bind().self_id.unwrap_or(u16::MAX) {
|
||||
if game
|
||||
.bind()
|
||||
.self_id
|
||||
.map(|id| player.id != id)
|
||||
.unwrap_or(true)
|
||||
{
|
||||
game.bind_mut().new_player(
|
||||
SocketAddr::from(([0, 0, 0, 0], 0)),
|
||||
Some(player.name),
|
||||
Some(player.id),
|
||||
false,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Package::PlayerLeft(player) => {
|
||||
if let Some(game) = &mut Game::singleton() {
|
||||
if player.id != game.bind().self_id.unwrap_or(u16::MAX) {
|
||||
if game
|
||||
.bind()
|
||||
.self_id
|
||||
.map(|id| player.id != id)
|
||||
.unwrap_or(true)
|
||||
{
|
||||
game.bind_mut().remove_player(player.id);
|
||||
}
|
||||
}
|
||||
@ -229,12 +243,12 @@ impl NetworkManager {
|
||||
}
|
||||
Package::GameOptionChanged(opt, value) => {
|
||||
if let Some(game) = &mut Game::singleton() {
|
||||
game.bind_mut().change_option(opt, value);
|
||||
game.bind_mut().change_option(opt, value, false);
|
||||
}
|
||||
}
|
||||
Package::TeamGameOptionChanged(team, opt, value) => {
|
||||
if let Some(game) = &mut Game::singleton() {
|
||||
game.bind_mut().change_team_option(team, opt, value);
|
||||
game.bind_mut().change_team_option(team, opt, value, false);
|
||||
}
|
||||
}
|
||||
Package::Goal(scorer, teams) => {
|
||||
@ -264,7 +278,12 @@ impl NetworkManager {
|
||||
}
|
||||
Package::ThrowTelegrenade(player_id, to) => {
|
||||
if let Some(game) = &mut Game::singleton() {
|
||||
if player_id != game.bind().self_id.unwrap_or(0) {
|
||||
if game
|
||||
.bind()
|
||||
.self_id
|
||||
.map(|id| player_id != id)
|
||||
.unwrap_or(true)
|
||||
{
|
||||
game.bind_mut().on_throw_telegrenade(player_id, to);
|
||||
}
|
||||
}
|
||||
@ -306,8 +325,13 @@ impl NetworkManager {
|
||||
);
|
||||
|
||||
if let Some(game) = &mut Game::singleton() {
|
||||
let self_id =
|
||||
game.bind_mut().new_player(connection.address, None, None);
|
||||
let is_first = game.bind().players.is_empty();
|
||||
let self_id = game.bind_mut().new_player(
|
||||
connection.address,
|
||||
None,
|
||||
None,
|
||||
is_first,
|
||||
);
|
||||
peer.send_reliable(
|
||||
&connection.address,
|
||||
Package::Players(
|
||||
@ -318,6 +342,7 @@ impl NetworkManager {
|
||||
.map(|p| p.into())
|
||||
.collect(),
|
||||
self_id,
|
||||
is_first,
|
||||
),
|
||||
);
|
||||
peer.send_reliable(
|
||||
@ -471,13 +496,12 @@ impl NetworkManager {
|
||||
}
|
||||
Package::SetTeam(player_id, team) => {
|
||||
if let Some(game) = &mut Game::singleton() {
|
||||
let self_id = game
|
||||
.bind()
|
||||
.find_player_by_addr(&conn.address)
|
||||
.map(|p| p.id());
|
||||
if let Some(self_id) = self_id {
|
||||
let player =
|
||||
game.bind().find_player_by_addr(&conn.address).cloned();
|
||||
if let Some(player) = player {
|
||||
if game.bind().opts.is_true(GameOption::AllowAnyTeam)
|
||||
|| self_id == player_id
|
||||
|| player.id() == player_id
|
||||
|| player.has_host_privileges
|
||||
{
|
||||
game.bind_mut().try_set_player_team(player_id, team);
|
||||
}
|
||||
@ -486,8 +510,14 @@ impl NetworkManager {
|
||||
}
|
||||
Package::SelectMap(map_idx) => {
|
||||
if let Some(mut game) = Game::singleton() {
|
||||
let player =
|
||||
game.bind().find_player_by_addr(&conn.address).cloned();
|
||||
if game.bind().opts.is_true(GameOption::AllowAnyMap) {
|
||||
game.bind_mut().change_map_selection(map_idx, false);
|
||||
} else if let Some(player) = player
|
||||
&& player.has_host_privileges
|
||||
{
|
||||
game.bind_mut().change_map_selection(map_idx, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -532,6 +562,39 @@ impl NetworkManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
Package::GameStarted => {
|
||||
if let Some(game) = &mut Game::singleton() {
|
||||
let player =
|
||||
game.bind().find_player_by_addr(&conn.address).cloned();
|
||||
if let Some(player) = player
|
||||
&& player.has_host_privileges
|
||||
{
|
||||
game.bind_mut().start_game();
|
||||
}
|
||||
}
|
||||
}
|
||||
Package::GameOptionChanged(key, value) => {
|
||||
if let Some(game) = &mut Game::singleton() {
|
||||
let player =
|
||||
game.bind().find_player_by_addr(&conn.address).cloned();
|
||||
if let Some(player) = player
|
||||
&& player.has_host_privileges
|
||||
{
|
||||
game.bind_mut().change_option(key, value, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Package::TeamGameOptionChanged(team, key, value) => {
|
||||
if let Some(game) = &mut Game::singleton() {
|
||||
let player =
|
||||
game.bind().find_player_by_addr(&conn.address).cloned();
|
||||
if let Some(player) = player
|
||||
&& player.has_host_privileges
|
||||
{
|
||||
game.bind_mut().change_team_option(team, key, value, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
},
|
||||
|
||||
@ -426,7 +426,8 @@ impl ICharacterBody3D for RemotePlayer {
|
||||
|
||||
self.run_deferred(|s| {
|
||||
if let Some(game) = Game::singleton()
|
||||
&& let Some(self_player) = game.bind().find_player(game.bind().self_id.unwrap_or(0))
|
||||
&& let Some(self_id) = game.bind().self_id
|
||||
&& let Some(self_player) = game.bind().find_player(self_id)
|
||||
&& let Some(remote_player) = game.bind().find_player(s.player_id.unwrap_or(0))
|
||||
&& self_player.data.team == remote_player.data.team
|
||||
&& let Some(sprite) = &mut s.shield_sprite
|
||||
|
||||
@ -145,9 +145,11 @@ impl ChatBox {
|
||||
PeerKind::Server(..) => {
|
||||
self.run_deferred(move |_| {
|
||||
if let Some(game) = &mut Game::singleton() {
|
||||
let self_id = game.bind().self_id;
|
||||
game.bind_mut()
|
||||
.on_chat_message(self_id.unwrap_or(0), message.to_string());
|
||||
let self_id = game.bind().self_id.clone();
|
||||
if let Some(self_id) = self_id {
|
||||
game.bind_mut()
|
||||
.on_chat_message(self_id, message.to_string());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -128,7 +128,7 @@ impl IPanel for CommandLinePanel {
|
||||
#[godot_api]
|
||||
impl CommandLinePanel {
|
||||
#[signal]
|
||||
pub fn on_host(port: u16);
|
||||
pub fn on_host(port: u16, headless: bool);
|
||||
#[signal]
|
||||
pub fn on_join(port: String);
|
||||
#[signal]
|
||||
@ -223,7 +223,14 @@ impl Command for HostCommand {
|
||||
fn execute(&self, cli: &mut CommandLinePanel, params: &[&str]) {
|
||||
if let Some(port_str) = params.first() {
|
||||
if let Ok(port) = u16::from_str_radix(*port_str, 10) {
|
||||
cli.signals().on_host().emit(port);
|
||||
let headless = if let Some(param) = params.get(1)
|
||||
&& *param == "--headless"
|
||||
{
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
cli.signals().on_host().emit(port, headless);
|
||||
} else {
|
||||
cli.publish_message(format!("Invalid port: {}", port_str), CliColor::Error);
|
||||
}
|
||||
|
||||
@ -108,10 +108,7 @@ impl IPanel for LobbyPanel {
|
||||
game.signals().options_changed().connect_other(self, |s| {
|
||||
s.run_deferred(|s| {
|
||||
if let Some(game) = Game::singleton() {
|
||||
s.on_game_opts_update(
|
||||
&game.bind().opts,
|
||||
NetworkManager::singleton().bind().is_host(),
|
||||
);
|
||||
s.on_game_opts_update(&game.bind().opts, game.bind().has_host_privileges());
|
||||
}
|
||||
});
|
||||
});
|
||||
@ -133,8 +130,8 @@ impl IPanel for LobbyPanel {
|
||||
});
|
||||
});
|
||||
|
||||
let is_host = if let Some(peer) = &NetworkManager::singleton().bind().peer {
|
||||
peer.is_host()
|
||||
let is_host = if let Some(game) = &Game::singleton() {
|
||||
game.bind().has_host_privileges()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
@ -273,13 +270,13 @@ impl LobbyPanel {
|
||||
|
||||
fn option_changed(&mut self, key: GameOption, value: GameOptionValue) {
|
||||
if let Some(game) = &mut Game::singleton() {
|
||||
game.bind_mut().change_option(key, value);
|
||||
game.bind_mut().change_option(key, value, true);
|
||||
}
|
||||
}
|
||||
|
||||
fn team_option_changed(&mut self, team: u8, key: GameOption, value: GameOptionValue) {
|
||||
if let Some(game) = &mut Game::singleton() {
|
||||
game.bind_mut().change_team_option(team, key, value);
|
||||
game.bind_mut().change_team_option(team, key, value, true);
|
||||
}
|
||||
}
|
||||
|
||||
@ -314,10 +311,8 @@ impl LobbyPanel {
|
||||
}
|
||||
|
||||
fn update_ready_button(&mut self) {
|
||||
if let Some(game) = Game::singleton()
|
||||
&& let Some(peer) = &NetworkManager::singleton().bind().peer
|
||||
{
|
||||
if let PeerKind::Server(_, _) = peer
|
||||
if let Some(game) = Game::singleton() {
|
||||
if game.bind().has_host_privileges()
|
||||
&& let Some(button) = &mut self.ready_button
|
||||
{
|
||||
let all_ready = game.bind().players.iter().all(|p| p.data.ready);
|
||||
@ -346,7 +341,8 @@ impl LobbyPanel {
|
||||
|
||||
pub fn update_players(&mut self, replace: bool) {
|
||||
if let Some(game) = &Game::singleton()
|
||||
&& let Some(player) = game.bind().find_player(game.bind().self_id.unwrap_or(0))
|
||||
&& let Some(self_id) = game.bind().self_id
|
||||
&& let Some(player) = game.bind().find_player(self_id)
|
||||
&& let Some(name_label) = &mut self.name_field
|
||||
{
|
||||
if player.data.name != name_label.get_text().to_string() {
|
||||
@ -387,8 +383,8 @@ impl LobbyPanel {
|
||||
}
|
||||
|
||||
pub fn on_ready(&mut self) {
|
||||
let is_host = if let Some(peer) = &NetworkManager::singleton().bind().peer {
|
||||
peer.is_host()
|
||||
let is_host = if let Some(game) = &Game::singleton() {
|
||||
game.bind().has_host_privileges()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
@ -396,14 +392,18 @@ impl LobbyPanel {
|
||||
self.is_ready = !self.is_ready;
|
||||
self.update_ready_button();
|
||||
|
||||
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer {
|
||||
if let PeerKind::Client(peer, socket_addr) = peer {
|
||||
peer.send_reliable(&socket_addr, Package::SetReady(0, self.is_ready));
|
||||
}
|
||||
if let Some(game) = &mut Game::singleton() {
|
||||
game.bind_mut().set_ready(self.is_ready);
|
||||
}
|
||||
} else {
|
||||
if let Some(game) = &mut Game::singleton() {
|
||||
game.bind_mut().start_game();
|
||||
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer
|
||||
&& let PeerKind::Client(peer, addr) = peer
|
||||
{
|
||||
peer.send_reliable(addr, Package::GameStarted);
|
||||
} else {
|
||||
game.bind_mut().start_game();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -490,7 +490,9 @@ impl LobbyPanel {
|
||||
GameOptionValue::Boolean(value) => {
|
||||
let mut checkbox = CheckBox::new_alloc();
|
||||
checkbox.set_pressed(*value);
|
||||
checkbox.set_disabled(!NetworkManager::singleton().bind().is_host());
|
||||
if let Some(game) = &Game::singleton() {
|
||||
checkbox.set_disabled(!game.bind().has_host_privileges());
|
||||
}
|
||||
|
||||
let checkbox_clone = checkbox.clone();
|
||||
let option = option.clone();
|
||||
@ -514,7 +516,9 @@ impl LobbyPanel {
|
||||
GameOptionValue::Number(value) => {
|
||||
let mut spinbox = SpinBox::new_alloc();
|
||||
spinbox.set_value(*value);
|
||||
spinbox.set_editable(NetworkManager::singleton().bind().is_host());
|
||||
if let Some(game) = &Game::singleton() {
|
||||
spinbox.set_editable(game.bind().has_host_privileges());
|
||||
}
|
||||
|
||||
let option = option.clone();
|
||||
spinbox
|
||||
|
||||
@ -75,9 +75,10 @@ impl IGridContainer for LobbyPlayerListing {
|
||||
PeerKind::Server(..) => {
|
||||
if let Some(player_id) = self.player_id
|
||||
&& let Some(game) = Game::singleton()
|
||||
&& let Some(self_id) = game.bind().self_id
|
||||
{
|
||||
// Hide kick button for self even if host
|
||||
if player_id == game.bind().self_id.unwrap_or(0) {
|
||||
if player_id == self_id {
|
||||
kick_btn.hide();
|
||||
}
|
||||
}
|
||||
|
||||
@ -211,7 +211,7 @@ impl MainMenu {
|
||||
if let Some(host_port) = &self.host_port {
|
||||
NetworkManager::singleton()
|
||||
.bind_mut()
|
||||
.host(host_port.get_value() as u16);
|
||||
.host(host_port.get_value() as u16, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -89,7 +89,7 @@ impl PlayerListing {
|
||||
pub fn update(&mut self) {
|
||||
if let Some(player_id) = self.player_id {
|
||||
if let Some(game) = Game::singleton() {
|
||||
let is_self = player_id == game.bind().self_id.unwrap_or(0);
|
||||
let is_self = game.bind().self_id.map(|s| s == player_id).unwrap_or(false);
|
||||
if let Some(player) = game.bind().find_player(player_id) {
|
||||
if let Some(name_label) = &mut self.name_label {
|
||||
name_label.set_text(&player.data.name);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user