Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ValorZard committed Sep 27, 2024
1 parent 1f0dd45 commit f89291e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 103 deletions.
57 changes: 0 additions & 57 deletions examples/multiplayer-lan/godot/mulCBE9.tmp

This file was deleted.

26 changes: 7 additions & 19 deletions examples/multiplayer-lan/rust/src/bullet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use godot::classes::{CharacterBody2D, ICharacterBody2D, ProjectSettings};
use godot::classes::{CharacterBody2D, ICharacterBody2D, SceneTreeTimer};
use godot::prelude::*;

use crate::NetworkId;
Expand All @@ -9,31 +9,24 @@ const LIFETIME: f64 = 2.0;
#[derive(GodotClass)]
#[class(base=CharacterBody2D)]
pub struct Bullet {
gravity: f64,
direction: Vector2,
// who shot the bullet
#[var]
pub network_id: NetworkId,
// dont want the bullets to live forever
time_left: f64,
timer: OnReady<Gd<SceneTreeTimer>>,
base: Base<CharacterBody2D>,
}

#[godot_api]
impl ICharacterBody2D for Bullet {
fn init(base: Base<CharacterBody2D>) -> Self {
let gravity: f64 = Result::expect(
ProjectSettings::singleton()
.get_setting("physics/2d/default_gravity".into())
.try_to::<f64>(),
"default setting in Godot",
);

Self {
gravity,
direction: Vector2::new(1., 0.),
network_id: 1,
time_left: LIFETIME,
timer: OnReady::from_base_fn(|base| {
base.get_tree().unwrap().create_timer(LIFETIME).unwrap()
}),
base,
}
}
Expand All @@ -44,16 +37,11 @@ impl ICharacterBody2D for Bullet {
self.base_mut().set_velocity(velocity);
}

fn physics_process(&mut self, delta: f64) {
fn physics_process(&mut self, _delta: f64) {
// delete bullet once LIFETIME seconds have passed
self.time_left -= delta;
if self.time_left <= 0.0 {
if self.timer.get_time_left() <= 0. {
self.base_mut().queue_free();
}
// have bullet fall down while flying
if !self.base().is_on_floor() {
self.base_mut().get_velocity().x += (self.gravity * 1. * delta) as f32;
}

self.base_mut().move_and_slide();
}
Expand Down
1 change: 0 additions & 1 deletion examples/multiplayer-lan/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use godot::prelude::*;
use player::Player;

type NetworkId = i32;

Expand Down
38 changes: 17 additions & 21 deletions examples/multiplayer-lan/rust/src/multiplayer_controller.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use core::time;
use std::collections::HashMap;
use std::thread;

use godot::classes::{
Button, Control, ENetMultiplayerPeer, IControl, LineEdit, MultiplayerApi, RichTextLabel,
Expand All @@ -14,11 +12,6 @@ use crate::NetworkId;

const LOCALHOST: &str = "127.0.0.1";
const PORT: i32 = 8910;
#[derive(GodotClass, Clone)]
#[class(init)]
pub struct PlayerData {
pub name: GString,
}

#[derive(GodotClass)]
#[class(base=Control)]
Expand All @@ -27,8 +20,8 @@ pub struct MultiplayerController {
address: GString,
port: i32,
#[export]
game_scene: Gd<PackedScene>,
player_database: HashMap<NetworkId, PlayerData>,
game_scene: Option<Gd<PackedScene>>,
player_database: HashMap<NetworkId, GString>,
number_of_players_loaded: u32,
multiplayer: OnReady<Gd<MultiplayerApi>>,
base: Base<Control>,
Expand Down Expand Up @@ -76,9 +69,8 @@ impl MultiplayerController {

// utility function that converts our player database hashmap to a 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;
let mut string = String::default();
for (network_id, username) in self.player_database.iter() {
string.push_str(&format!(
"network_id: {network_id}, username: {username} \n"
));
Expand All @@ -94,7 +86,7 @@ impl MultiplayerController {
// insert new player data with network_id if it doesn't already exist
self.player_database
.entry(network_id)
.or_insert(PlayerData { name });
.or_insert(name);

// print player information onto multiplayer log
let mut multiplayer_log = self
Expand All @@ -103,9 +95,8 @@ impl MultiplayerController {
multiplayer_log.set_text(self.player_database_to_string().into());

if self.multiplayer.is_server() {
for (id, data) in self.player_database.clone().into_iter() {
for (id, username) 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)],
Expand All @@ -117,8 +108,9 @@ impl MultiplayerController {
#[rpc(any_peer, call_local, reliable)]
fn load_game(&mut self) {
// start up game scene
let mut scene = self.game_scene.instantiate_as::<SceneManager>();
// have to put this into a block to avoid borrowing self as immutable when its already mutable
let mut scene = self.game_scene.as_mut().unwrap().instantiate_as::<SceneManager>();
// have to put this into its own scope to avoid borrowing self as immutable when its already mutable
// note: you could also use drop(..) to drop reference to base
{
let mut base = self.base_mut();
base.get_tree()
Expand All @@ -132,8 +124,8 @@ impl MultiplayerController {

// add players to scene
let mut player_ids = Vec::<NetworkId>::new();
for (&network_id, data) in &self.player_database {
scene.bind_mut().add_player(network_id, data.name.clone());
for (&network_id, username) in &self.player_database {
scene.bind_mut().add_player(network_id, username.clone());
player_ids.push(network_id);
}

Expand Down Expand Up @@ -245,7 +237,7 @@ impl IControl for MultiplayerController {
Self {
address: LOCALHOST.into(),
port: PORT,
game_scene: PackedScene::new_gd(),
game_scene: None,
player_database: HashMap::new(),
number_of_players_loaded: 0,
multiplayer: OnReady::from_base_fn(|base| base.get_multiplayer().unwrap()),
Expand All @@ -271,7 +263,11 @@ impl IControl for MultiplayerController {
self.base().callable("on_start_button_down"),
);

// make clone to avoid borrowing errors
// make clone to avoid following borrowing error:
// cannot move out of `self.multiplayer` which is behind a mutable reference
// move occurs because `self.multiplayer` has type `godot::prelude::OnReady<godot::prelude::Gd<MultiplayerApi>>`,
// which does not implement the `Copy` trait

let mut multiplayer = self.multiplayer.clone();

// setup multiplayer signal callbacks
Expand Down
2 changes: 1 addition & 1 deletion examples/multiplayer-lan/rust/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl ICharacterBody2D for Player {
ProjectSettings::singleton()
.get_setting("physics/2d/default_gravity".into())
.try_to::<f64>(),
"default setting in Godot",
"not able to cast default setting in Godot to float",
);
Self {
speed: 300.0,
Expand Down
8 changes: 4 additions & 4 deletions examples/multiplayer-lan/rust/src/scene_manager.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::collections::HashMap;

use godot::{
classes::{RandomNumberGenerator, RichTextLabel},
classes::RichTextLabel,
prelude::*,
};

use crate::{
multiplayer_controller::{self, MultiplayerController},
multiplayer_controller::MultiplayerController,
player::Player,
NetworkId,
};
Expand Down Expand Up @@ -140,9 +140,9 @@ impl INode2D for SceneManager {
multiplayer_controller.rpc_id(1, "load_in_player".into(), &[]);
}

fn process(&mut self, delta: f64) {
fn process(&mut self, _delta: f64) {
let mut string = String::from("");
for (_, player) in self.player_list.iter() {
for player in self.player_list.values_mut() {
let player_bind = player.bind();
let username = &player_bind.username;
let position = player.get_global_position();
Expand Down

0 comments on commit f89291e

Please sign in to comment.