Skip to content

Commit

Permalink
added avatar url loading stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
MalekiRe committed Dec 11, 2023
1 parent 8621adf commit cd0e407
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 50 deletions.
83 changes: 54 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bevy-inspector-egui = "0.21.0"
bevy_web_asset = "0.7.0"
bevy_vrm = "0.0.6"
social-common.path = "apps/social/common"
lightyear = { git = "https://github.com/cBournhonesque/lightyear", features = ["webtransport"]}
lightyear = { version = "0.2.0", features = ["webtransport"]}
serde = "1.0.193"

[profile.dev]
Expand Down
1 change: 1 addition & 0 deletions apps/social/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ bevy_vrm.workspace = true
lightyear.workspace = true
social-common.workspace = true
portpicker = "0.1.1"
random-number = "0.1.8"
8 changes: 5 additions & 3 deletions apps/social/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const ASSET_FOLDER: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../../assets
pub fn main() {
color_eyre::install().unwrap();

let client_id: u16 = random_number::random!();

info!("Running `social-client`");
App::new()
.add_plugins(bevy_web_asset::WebAssetPlugin::default())
Expand All @@ -39,7 +41,7 @@ pub fn main() {
.add_plugins(VrmPlugin)
.add_systems(Startup, setup)
.add_plugins(networking::MyClientPlugin {
client_id: ClientId::default(),
client_id: client_id as u64,
client_port: portpicker::pick_unused_port().unwrap(),
server_port: SERVER_PORT,
transport: Transports::Udp,
Expand Down Expand Up @@ -69,7 +71,7 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
assets: Res<AssetServer>,
) {
let mut transform = Transform::from_xyz(0.0, -1.0, -4.0);
/*let mut transform = Transform::from_xyz(0.0, -1.0, -4.0);
transform.rotate_y(PI);
commands.spawn(VrmBundle {
Expand All @@ -78,7 +80,7 @@ fn setup(
transform,
..default()
},
});
});*/

// plane
commands.spawn(PbrBundle {
Expand Down
67 changes: 59 additions & 8 deletions apps/social/client/src/networking.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use bevy::prelude::*;
use bevy_vrm::VrmBundle;
use lightyear::_reexport::{ShouldBeInterpolated, ShouldBePredicted};
use lightyear::prelude::client::*;
use lightyear::prelude::*;
use social_common::shared::*;
use social_common::*;
use std::f32::consts::PI;
use std::net::{Ipv4Addr, SocketAddr};
use std::str::FromStr;
use std::time::Duration;
Expand All @@ -18,6 +20,8 @@ pub struct MyClientPlugin {

impl Plugin for MyClientPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(PlayerClientId(self.client_id));

let server_addr = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), self.server_port);
let auth = Authentication::Manual {
server_addr,
Expand Down Expand Up @@ -49,14 +53,11 @@ impl Plugin for MyClientPlugin {
sync: SyncConfig::default(),
prediction: PredictionConfig::default(),
// we are sending updates every frame (60fps), let's add a delay of 6 network-ticks
interpolation: InterpolationConfig::default().with_delay(
InterpolationDelay::default()
.with_min_delay(Duration::from_millis(50))
.with_send_interval_ratio(2.0),
),
interpolation: InterpolationConfig::default()
.with_delay(InterpolationDelay::Ratio(2.0)),
// .with_delay(InterpolationDelay::Ratio(2.0)),
};
let plugin_config = PluginConfig::new(config, io, MyProtocol::default(), auth);
let plugin_config = PluginConfig::new(config, io, protocol(), auth);
app.add_plugins(ClientPlugin::new(plugin_config));
app.add_plugins(shared::SharedPlugin);
app.insert_resource(self.clone());
Expand All @@ -73,11 +74,61 @@ impl Plugin for MyClientPlugin {
handle_predicted_spawn,
handle_interpolated_spawn,
log,
on_avatar_url_add,
on_avatar_url_changed,
change_pos,
),
);
}
}

#[derive(Resource)]
pub struct PlayerClientId(u64);

pub fn on_avatar_url_add(
mut query: Query<(&PlayerId, &mut PlayerAvatarUrl), Added<PlayerAvatarUrl>>,
player_client_id: Res<PlayerClientId>,
mut client: ResMut<Client<MyProtocol>>,
) {
for (player_id, mut player_avatar_url) in query.iter_mut() {
if player_id.0 == player_client_id.0 {
if player_avatar_url.0.is_none() {
client.buffer_send::<Channel1, _>(Message1("https://vipe.mypinata.cloud/ipfs/QmU7QeqqVMgnMtCAqZBpAYKSwgcjD4gnx4pxFNY9LqA7KQ/default_398.vrm".to_string())).unwrap();
//player_avatar_url.0.replace("https://vipe.mypinata.cloud/ipfs/QmU7QeqqVMgnMtCAqZBpAYKSwgcjD4gnx4pxFNY9LqA7KQ/default_398.vrm".to_string());
}
}
}
}

pub fn on_avatar_url_changed(
mut commands: Commands,
assets: Res<AssetServer>,
mut query: Query<(Entity, &PlayerAvatarUrl), Changed<PlayerAvatarUrl>>,
) {
for (entity, url) in query.iter() {
let url = match url.0.as_ref() {
None => continue,
Some(url) => url.as_str(),
};
let mut transform = Transform::from_xyz(0.0, -1.0, -4.0);
transform.rotate_y(PI);

commands.entity(entity).insert(VrmBundle {
vrm: assets.load(url.to_string()),
scene_bundle: SceneBundle {
transform,
..default()
},
});
}
}

pub fn change_pos(mut query: Query<(&PlayerPosition, &mut Transform), Changed<PlayerPosition>>) {
for (player_pos, mut transform) in query.iter_mut() {
transform.translation = player_pos.0;
}
}

// Startup system for the client
pub(crate) fn init(
mut commands: Commands,
Expand Down Expand Up @@ -159,9 +210,9 @@ pub(crate) fn movement(

// System to receive messages on the client
pub(crate) fn receive_message1(mut reader: EventReader<MessageEvent<Message1>>) {
for event in reader.read() {
/*for event in reader.read() {
info!("Received message: {:?}", event.message());
}
}*/
}

// When the predicted copy of the client-owned entity is spawned, do stuff
Expand Down
Loading

0 comments on commit cd0e407

Please sign in to comment.