Skip to content

Commit

Permalink
fix ci
Browse files Browse the repository at this point in the history
  • Loading branch information
ValorZard committed Sep 25, 2024
1 parent 9cd0859 commit 1f0dd45
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 141 deletions.
34 changes: 19 additions & 15 deletions examples/multiplayer-lan/rust/src/bullet.rs
Original file line number Diff line number Diff line change
@@ -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<CharacterBody2D>
time_left: f64,
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");
let gravity: f64 = Result::expect(
ProjectSettings::singleton()
.get_setting("physics/2d/default_gravity".into())
.try_to::<f64>(),
"default setting in Godot",
);

Self {
gravity,
Expand All @@ -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);
Expand All @@ -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();
}
}
}
9 changes: 4 additions & 5 deletions examples/multiplayer-lan/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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 { }
unsafe impl ExtensionLibrary for MultiplayerLan {}
114 changes: 70 additions & 44 deletions examples/multiplayer-lan/rust/src/multiplayer_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<LineEdit>("UsernameLineEdit").get_text();
let username = self
.base()
.get_node_as::<LineEdit>("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
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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::<SceneManager>();
// 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::<SceneManager>();
// 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::<NetworkId>::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::<NetworkId>::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;
Expand All @@ -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::<SceneManager>("Game");
let mut game_scene = self
.base_mut()
.get_tree()
.unwrap()
.get_root()
.unwrap()
.get_node_as::<SceneManager>("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();
Expand All @@ -179,7 +199,10 @@ impl MultiplayerController {
.get_node_as::<Button>("JoinButton")
.set_visible(false);
self.host_game();
let username = self.base().get_node_as::<LineEdit>("UsernameLineEdit").get_text();
let username = self
.base()
.get_node_as::<LineEdit>("UsernameLineEdit")
.get_text();
let network_id = self.multiplayer.get_unique_id();
// in this instance, the host is also playing, so add their information to player_database
self.send_player_information(username, network_id);
Expand All @@ -192,7 +215,10 @@ impl MultiplayerController {
.get_node_as::<Button>("HostButton")
.set_visible(false);
let mut peer = ENetMultiplayerPeer::new_gd();
self.address = self.base().get_node_as::<LineEdit>("AddressLineEdit").get_text();
self.address = self
.base()
.get_node_as::<LineEdit>("AddressLineEdit")
.get_text();
let error = peer.create_client(self.address.clone(), self.port);
if error != Error::OK {
godot_print!("cannot join");
Expand All @@ -209,7 +235,7 @@ impl MultiplayerController {
#[func]
fn on_start_button_down(&mut self) {
// have client call server to start up game
self.base_mut().rpc_id(1,"load_game".into(), &[]);
self.base_mut().rpc_id(1, "load_game".into(), &[]);
}
}

Expand Down
Loading

0 comments on commit 1f0dd45

Please sign in to comment.