Skip to content

Commit

Permalink
Making player joining more reliable on client side
Browse files Browse the repository at this point in the history
Resolves #132
  • Loading branch information
AnthonyTornetta committed Jan 11, 2025
1 parent 4b6fd30 commit d67197a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 31 deletions.
39 changes: 38 additions & 1 deletion cosmos_client/src/entities/player/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,48 @@
//! Contains systems and components for the player
use bevy::prelude::App;
use bevy::{color::palettes::css, prelude::*};
use bevy_rapier3d::prelude::{ActiveEvents, CoefficientCombineRule, Collider, Friction, LockedAxes, ReadMassProperties, RigidBody};
use cosmos_core::{entities::player::Player, netty::system_sets::NetworkingSystemsSet, persistence::LoadingDistance, state::GameState};

pub mod player_movement;
pub mod render_distance;

fn on_add_player(
mut commands: Commands,
mut materials: ResMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
q_player: Query<(Entity, &Player), Added<Player>>,
) {
for (ent, player) in q_player.iter() {
commands.entity(ent).insert((
Mesh3d(meshes.add(Capsule3d::default())),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: css::GREEN.into(),
..Default::default()
})),
Collider::capsule_y(0.65, 0.25),
LockedAxes::ROTATION_LOCKED,
Name::new(format!("Player ({})", player.name())),
RigidBody::Dynamic,
Friction {
coefficient: 0.0,
combine_rule: CoefficientCombineRule::Min,
},
LoadingDistance::new(1, 2),
ReadMassProperties::default(),
ActiveEvents::COLLISION_EVENTS,
));
}
}

pub(super) fn register(app: &mut App) {
app.add_systems(
Update,
on_add_player
.in_set(NetworkingSystemsSet::Between)
.run_if(in_state(GameState::Playing).or(in_state(GameState::LoadingWorld))),
);

render_distance::register(app);
player_movement::register(app);
}
33 changes: 5 additions & 28 deletions cosmos_client/src/netty/gameplay/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ use bevy_rapier3d::prelude::*;
use bevy_renet2::renet2::{transport::NetcodeClientTransport, RenetClient};
use cosmos_core::{
block::Block,
ecs::{
bundles::{BundleStartingRotation, CosmosPbrBundle},
NeedsDespawned,
},
ecs::{bundles::BundleStartingRotation, NeedsDespawned},
entities::player::{render_distance::RenderDistance, Player},
events::{
block_events::{BlockChangedEvent, BlockDataChangedEvent},
Expand Down Expand Up @@ -189,8 +186,7 @@ fn lerp_towards(
/// TODO: super split this up
pub(crate) fn client_sync_players(
mut commands: Commands,
(mut meshes, mut client, transport, mut lobby, mut network_mapping): (
ResMut<Assets<Mesh>>,
(mut client, transport, mut lobby, mut network_mapping): (
ResMut<RenetClient>,
Res<NetcodeClientTransport>,
ResMut<ClientLobby>,
Expand Down Expand Up @@ -391,7 +387,7 @@ pub(crate) fn client_sync_players(
continue;
};

info!("Player {} ({}) connected!", name.as_str(), id);
info!("Player {} ({}) connected! {body:?}", name.as_str(), id);

// The player entity may have already been created if some of their components were already synced.
let mut entity_cmds = if let Some(player_entity) = network_mapping.client_from_server(&server_entity) {
Expand All @@ -409,31 +405,12 @@ pub(crate) fn client_sync_players(
}
};

error!("Player starting loc: {loc:?}");

// this will avoid any position mismatching
// loc.last_transform_loc = Some(loc.local);

entity_cmds.insert((
SetPosition::Location,
CosmosPbrBundle {
location: loc,
rotation: body.rotation.into(),
mesh: Mesh3d(meshes.add(Capsule3d::default())),
..default()
},
Collider::capsule_y(0.65, 0.25),
LockedAxes::ROTATION_LOCKED,
Name::new(format!("Player ({name})")),
RigidBody::Dynamic,
Friction {
coefficient: 0.0,
combine_rule: CoefficientCombineRule::Min,
},
Transform::from_rotation(body.rotation.into()),
loc,
body.create_velocity(),
Player::new(name, id),
ReadMassProperties::default(),
ActiveEvents::COLLISION_EVENTS,
ServerEntity(server_entity),
));

Expand Down
19 changes: 18 additions & 1 deletion cosmos_core/src/entities/player/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ pub mod render_distance;

use bevy::prelude::{App, Component};
use bevy_renet2::renet2::ClientId;
use serde::{Deserialize, Serialize};

#[derive(Component, Debug)]
use crate::netty::sync::{sync_component, IdentifiableComponent, SyncableComponent};

#[derive(Component, Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
/// Represents a player
pub struct Player {
name: String,
Expand All @@ -32,6 +35,20 @@ impl Player {
}
}

impl IdentifiableComponent for Player {
fn get_component_unlocalized_name() -> &'static str {
"cosmos:player"
}
}

impl SyncableComponent for Player {
fn get_sync_type() -> crate::netty::sync::SyncType {
crate::netty::sync::SyncType::ServerAuthoritative
}
}

pub(super) fn register(app: &mut App) {
sync_component::<Player>(app);

creative::register(app);
}
2 changes: 1 addition & 1 deletion cosmos_server/src/persistence/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::fs;
use bevy::{
ecs::schedule::{IntoSystemConfigs, IntoSystemSetConfigs, SystemSet},
hierarchy::BuildChildren,
log::{error, warn},
log::{error, info, warn},
prelude::{App, Commands, Component, Entity, Quat, Query, Update, With, Without},
reflect::Reflect,
};
Expand Down

0 comments on commit d67197a

Please sign in to comment.