Skip to content

Commit

Permalink
feat: player position and camera persistence to save files (camera ro…
Browse files Browse the repository at this point in the history
…tation is slightly broken tho)
  • Loading branch information
AudranTourneur committed Feb 2, 2025
1 parent 8d3c667 commit 030f324
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 14 deletions.
18 changes: 16 additions & 2 deletions client/src/camera/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,27 @@ pub struct CameraController {
pub mouse_sensitivity: f32,
}

const DEFAULT_DISTANCE: f32 = 10.0;
const DEFAULT_MOUSE_SENSITIVITY: f32 = 0.003;

impl Default for CameraController {
fn default() -> Self {
Self {
distance: 10.0,
distance: DEFAULT_DISTANCE,
angle_x: 0.0,
angle_y: 20.0f32.to_radians(),
mouse_sensitivity: 0.003,
mouse_sensitivity: DEFAULT_MOUSE_SENSITIVITY,
}
}
}

impl From<Quat> for CameraController {
fn from(quat: Quat) -> Self {
Self {
distance: DEFAULT_DISTANCE,
angle_x: quat.to_euler(EulerRot::XYZ).0,
angle_y: quat.to_euler(EulerRot::XYZ).1,
mouse_sensitivity: DEFAULT_MOUSE_SENSITIVITY,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub fn game_plugin(app: &mut App) {
init_server_connection,
setup_materials,
setup_server_connect_loading_screen,
spawn_camera,
)
.chain(),
)
Expand All @@ -125,7 +126,6 @@ pub fn game_plugin(app: &mut App) {
OnEnter(GameState::Game),
(
setup_main_lighting,
spawn_camera,
spawn_reticle,
setup_hud,
setup_chat,
Expand Down
19 changes: 17 additions & 2 deletions client/src/player/update.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
camera::CameraController,
network::{CurrentPlayerProfile, TargetServer, TargetServerState, UnacknowledgedInputs},
player::{PlayerLabel, PlayerMaterialHandle},
world::ClientWorldMap,
Expand Down Expand Up @@ -40,6 +41,7 @@ pub fn spawn_players_system(
mut target_server: ResMut<TargetServer>,
players: Query<&Player>,
assets: Res<AssetServer>,
mut camera_query: Query<(&mut Transform, &mut CameraController), With<Camera>>,
) {
let current_id = player_profile.into_inner().id;
'event_loop: for event in ev_spawn.read() {
Expand All @@ -54,7 +56,12 @@ pub fn spawn_players_system(
}
}
let is_current_player = event.id == current_id;
let player = Player::new(event.id, event.name.clone(), event.position);
let player = Player::new(
event.id,
event.name.clone(),
event.position,
Transform::default(),
);

let color = if is_current_player {
Color::srgba(1.0, 0.0, 0.0, 1.0)
Expand Down Expand Up @@ -83,7 +90,6 @@ pub fn spawn_players_system(
Name::new(player_name.clone()),
));

// We need the full version of this font so we can use box drawing characters.
let text_style = TextFont {
font: assets.load("fonts/FiraMono-Medium.ttf"),
font_size: PLAYER_LABEL_FONT_SIZE,
Expand All @@ -96,6 +102,15 @@ pub fn spawn_players_system(
target_server.state = TargetServerState::FullyReady;
entity.insert(CurrentPlayerMarker {});
info!("Inserted current player marker");

info!("aaa ---");
for (transform, controller) in camera_query.iter_mut() {
*transform.into_inner() = event.camera_transform;
*controller.into_inner() = event.camera_transform.rotation.into();

info!("Setting camera transform: {:?}", event.camera_transform);
}
info!("bbb ---");
}

let entity_id = entity.id();
Expand Down
4 changes: 3 additions & 1 deletion server/src/network/cleanup.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use shared::{messages::PlayerId, world::ServerWorldMap};

pub fn cleanup_all_players_from_world(world_map: &mut ServerWorldMap) {
world_map.players.clear();
for p in world_map.players.values_mut() {
p.last_input_processed = 0;
}
for (_, chunk) in world_map.chunks.map.iter_mut() {
chunk.sent_to_clients.clear();
}
Expand Down
22 changes: 19 additions & 3 deletions server/src/network/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,24 @@ fn server_update_system(
.insert(client_id, LobbyPlayer::new(auth_req.username.clone()));
debug!("New lobby : {:?}", lobby);

let new_position = Vec3::new(0.0, 80.0, 0.0);
let maybe_existing_player = world_map.players.get(&client_id);

let player_data =
Player::new(client_id, auth_req.username.clone(), new_position);
let new_position = match maybe_existing_player {
Some(existing_player) => existing_player.position,
None => Vec3::new(0.0, 80.0, 0.0),
};

let camera_transform = match maybe_existing_player {
Some(existing_player) => existing_player.camera_transform,
None => Transform::default(),
};

let player_data = Player::new(
client_id,
auth_req.username.clone(),
new_position,
camera_transform,
);

world_map.players.insert(client_id, player_data);

Expand All @@ -119,6 +133,7 @@ fn server_update_system(
id: *id,
name: player.name.clone(),
position: player.position,
camera_transform: player.camera_transform,
})
.collect();

Expand All @@ -138,6 +153,7 @@ fn server_update_system(
id: *id,
name: player.name.clone(),
position: Vec3::new(0.0, 80.0, 0.0),
camera_transform: Transform::default(),
};

let spawn_message_wrapped =
Expand Down
1 change: 1 addition & 0 deletions shared/src/messages/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct PlayerSpawnEvent {
pub id: PlayerId,
pub name: String,
pub position: Vec3,
pub camera_transform: Transform,
}

#[derive(Event, Serialize, Deserialize, PartialEq, Debug, Clone)]
Expand Down
3 changes: 2 additions & 1 deletion shared/src/players/constants.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub const GRAVITY: f32 = -0.5;
pub const JUMP_VELOCITY: f32 = 10.0;
pub const SPEED: f32 = 3.0;
pub const FLY_SPEED_MULTIPLIER: f32 = 4.0;
pub const SPEED: f32 = 5.0;
4 changes: 2 additions & 2 deletions shared/src/players/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ pub struct Player {
}

impl Player {
pub fn new(id: PlayerId, name: String, position: Vec3) -> Self {
pub fn new(id: PlayerId, name: String, position: Vec3, camera_transform: Transform) -> Self {
Self {
id,
name,
position,
camera_transform: Transform::default(),
camera_transform,
velocity: Vec3::ZERO,
on_ground: true,
is_flying: false,
Expand Down
8 changes: 6 additions & 2 deletions shared/src/players/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
messages::{NetworkAction, PlayerFrameInput},
players::{
collision::check_player_collision,
constants::{GRAVITY, JUMP_VELOCITY, SPEED},
constants::{FLY_SPEED_MULTIPLIER, GRAVITY, JUMP_VELOCITY, SPEED},
},
world::WorldMap,
};
Expand Down Expand Up @@ -96,7 +96,11 @@ pub fn simulate_player_movement(
}
}

let speed = if player.is_flying { SPEED * 5. } else { SPEED };
let speed = if player.is_flying {
SPEED * FLY_SPEED_MULTIPLIER
} else {
SPEED
};
let speed = speed * delta;

// Attempt to move the player by the calculated direction
Expand Down

0 comments on commit 030f324

Please sign in to comment.