diff --git a/examples/multiplayer-lan/rust/src/bullet.rs b/examples/multiplayer-lan/rust/src/bullet.rs index 1a4b58dee..54c56fd4a 100644 --- a/examples/multiplayer-lan/rust/src/bullet.rs +++ b/examples/multiplayer-lan/rust/src/bullet.rs @@ -1,28 +1,33 @@ -use godot::prelude::*; use godot::classes::{CharacterBody2D, ICharacterBody2D, ProjectSettings}; +use godot::prelude::*; use crate::NetworkId; -const SPEED : f32 = 500.0; -const LIFETIME : f64 = 2.0; +const SPEED: f32 = 500.0; +const LIFETIME: f64 = 2.0; #[derive(GodotClass)] #[class(base=CharacterBody2D)] pub struct Bullet { - gravity : f64, - direction : Vector2, + gravity: f64, + direction: Vector2, // who shot the bullet #[var] - pub network_id : NetworkId, + pub network_id: NetworkId, // dont want the bullets to live forever - time_left : f64, - base: Base + time_left: f64, + base: Base, } #[godot_api] impl ICharacterBody2D for Bullet { fn init(base: Base) -> Self { - let gravity : f64 = Result::expect(ProjectSettings::singleton().get_setting("physics/2d/default_gravity".into()).try_to::(), "default setting in Godot"); + let gravity: f64 = Result::expect( + ProjectSettings::singleton() + .get_setting("physics/2d/default_gravity".into()) + .try_to::(), + "default setting in Godot", + ); Self { gravity, @@ -33,8 +38,7 @@ impl ICharacterBody2D for Bullet { } } - fn ready(&mut self) - { + fn ready(&mut self) { self.direction = self.direction.rotated(self.base().get_rotation()); let velocity = self.direction * SPEED; self.base_mut().set_velocity(velocity); @@ -47,10 +51,10 @@ impl ICharacterBody2D for Bullet { self.base_mut().queue_free(); } // have bullet fall down while flying - if !self.base().is_on_floor() { + if !self.base().is_on_floor() { self.base_mut().get_velocity().x += (self.gravity * 1. * delta) as f32; - } + } - self.base_mut().move_and_slide(); + self.base_mut().move_and_slide(); } -} \ No newline at end of file +} diff --git a/examples/multiplayer-lan/rust/src/lib.rs b/examples/multiplayer-lan/rust/src/lib.rs index 892f25f79..a7d510406 100644 --- a/examples/multiplayer-lan/rust/src/lib.rs +++ b/examples/multiplayer-lan/rust/src/lib.rs @@ -1,15 +1,14 @@ +use godot::prelude::*; use player::Player; -use godot::{prelude::*}; type NetworkId = i32; -mod player; mod bullet; -mod scene_manager; mod multiplayer_controller; +mod player; +mod scene_manager; struct MultiplayerLan; - #[gdextension] -unsafe impl ExtensionLibrary for MultiplayerLan { } \ No newline at end of file +unsafe impl ExtensionLibrary for MultiplayerLan {} diff --git a/examples/multiplayer-lan/rust/src/multiplayer_controller.rs b/examples/multiplayer-lan/rust/src/multiplayer_controller.rs index f079188d3..e118d2ff0 100644 --- a/examples/multiplayer-lan/rust/src/multiplayer_controller.rs +++ b/examples/multiplayer-lan/rust/src/multiplayer_controller.rs @@ -2,13 +2,15 @@ use core::time; use std::collections::HashMap; use std::thread; -use godot::classes::{Button, Control, ENetMultiplayerPeer, IControl, LineEdit, MultiplayerApi, RichTextLabel}; +use godot::classes::{ + Button, Control, ENetMultiplayerPeer, IControl, LineEdit, MultiplayerApi, RichTextLabel, +}; use godot::global::Error; use godot::obj::WithBaseField; use godot::prelude::*; use crate::scene_manager::SceneManager; -use crate::{NetworkId}; +use crate::NetworkId; const LOCALHOST: &str = "127.0.0.1"; const PORT: i32 = 8910; @@ -53,10 +55,17 @@ impl MultiplayerController { fn on_connected_to_server(&mut self) { godot_print!("Connected to Server!"); // send information to server - let username = self.base().get_node_as::("UsernameLineEdit").get_text(); + let username = self + .base() + .get_node_as::("UsernameLineEdit") + .get_text(); let network_id = self.multiplayer.get_unique_id(); // server always has peer id of 1 - self.base_mut().rpc_id(1, "send_player_information".into(), &[Variant::from(username), Variant::from(network_id)]); + self.base_mut().rpc_id( + 1, + "send_player_information".into(), + &[Variant::from(username), Variant::from(network_id)], + ); } // called only from clients @@ -66,12 +75,13 @@ impl MultiplayerController { } // utility function that converts our player database hashmap to a string - fn player_database_to_string(&self) -> String - { + fn player_database_to_string(&self) -> String { let mut string = String::from(""); for (network_id, data) in self.player_database.iter() { let username = &data.name; - string.push_str(&format!("network_id: {network_id}, username: {username} \n")); + string.push_str(&format!( + "network_id: {network_id}, username: {username} \n" + )); } string } @@ -82,7 +92,9 @@ impl MultiplayerController { #[rpc(any_peer)] fn send_player_information(&mut self, name: GString, network_id: NetworkId) { // insert new player data with network_id if it doesn't already exist - self.player_database.entry(network_id).or_insert(PlayerData{name}); + self.player_database + .entry(network_id) + .or_insert(PlayerData { name }); // print player information onto multiplayer log let mut multiplayer_log = self @@ -94,54 +106,56 @@ impl MultiplayerController { for (id, data) in self.player_database.clone().into_iter() { godot_print!("sending player {id} data"); let username = data.name; - self.base_mut().rpc("send_player_information".into(), &[Variant::from(username), Variant::from(id)]); + self.base_mut().rpc( + "send_player_information".into(), + &[Variant::from(username), Variant::from(id)], + ); } } } #[rpc(any_peer, call_local, reliable)] fn load_game(&mut self) { - // start up game scene - let mut scene = self.game_scene.instantiate_as::(); - // have to put this into a block to avoid borrowing self as immutable when its already mutable - { - let mut base = self.base_mut(); - base.get_tree() - .unwrap() - .get_root() - .unwrap() - .add_child(scene.clone()); - // hide multiplayer menu - base.hide(); - } + // start up game scene + let mut scene = self.game_scene.instantiate_as::(); + // have to put this into a block to avoid borrowing self as immutable when its already mutable + { + let mut base = self.base_mut(); + base.get_tree() + .unwrap() + .get_root() + .unwrap() + .add_child(scene.clone()); + // hide multiplayer menu + base.hide(); + } - // add players to scene - let mut player_ids = Vec::::new(); - for (&network_id, data) in &self.player_database { - scene.bind_mut().add_player(network_id, data.name.clone()); - player_ids.push(network_id); - } + // add players to scene + let mut player_ids = Vec::::new(); + for (&network_id, data) in &self.player_database { + scene.bind_mut().add_player(network_id, data.name.clone()); + player_ids.push(network_id); + } - if self.multiplayer.is_server() { - for id in player_ids { - // don't call rpc on server - if id == 1 { - continue; - } - // force other clients to also load the game up - self.base_mut().rpc_id(id.into(), "load_game".into(), &[]); + if self.multiplayer.is_server() { + for id in player_ids { + // don't call rpc on server + if id == 1 { + continue; } + // force other clients to also load the game up + self.base_mut().rpc_id(id.into(), "load_game".into(), &[]); } + } } // callback from scene_manager, tells the multiplayer_controller that this player has loaded in // Every peer will call this when they have loaded the game scene. #[rpc(any_peer, call_local, reliable)] - fn load_in_player(&mut self) - { + fn load_in_player(&mut self) { // if server, start up game on everyone else's client if self.multiplayer.is_server() { - let network_id= self.multiplayer.get_remote_sender_id(); + let network_id = self.multiplayer.get_remote_sender_id(); // only load in players that are actually in the player database if !self.player_database.contains_key(&network_id) { return; @@ -150,13 +164,19 @@ impl MultiplayerController { self.number_of_players_loaded += 1; // start game once everyone is loaded in if self.number_of_players_loaded == self.player_database.len() as u32 { - let mut game_scene = self.base_mut().get_tree().unwrap().get_root().unwrap().get_node_as::("Game"); + let mut game_scene = self + .base_mut() + .get_tree() + .unwrap() + .get_root() + .unwrap() + .get_node_as::("Game"); game_scene.bind_mut().start_game(); } } } - // set up server + // set up server #[func] fn host_game(&mut self) { let mut peer = ENetMultiplayerPeer::new_gd(); @@ -179,7 +199,10 @@ impl MultiplayerController { .get_node_as::