Skip to content

Commit

Permalink
fix: world save files
Browse files Browse the repository at this point in the history
  • Loading branch information
AudranTourneur committed Jan 29, 2025
1 parent e871624 commit 8d3c667
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
2 changes: 0 additions & 2 deletions client/src/ui/hud/debug/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub fn inspector_ui(world: &mut World) {
let window = world.query::<&Window>().get_single(world).unwrap();
let width = window.width();

info!("Inspector UI width: {}", width);

let time = world.get_resource::<Time>().unwrap();

let mut res = egui::Window::new("Debug UI").default_open(false);
Expand Down
11 changes: 8 additions & 3 deletions server/src/init.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::network::dispatcher::{self, setup_resources_and_events};
use crate::network::{
cleanup::cleanup_all_players_from_world,
dispatcher::{self, setup_resources_and_events},
};
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
Expand Down Expand Up @@ -106,7 +109,7 @@ pub fn init(socket: UdpSocket, config: GameServerConfig, game_folder_path: Strin
setup_resources_and_events(&mut app);

// Load world from files
let world_map = match load_world_map(world_name, &app) {
let mut world_map = match load_world_map(world_name, &app) {
Ok(world) => world,
Err(e) => {
error!("Error loading world: {}. Generating a new one.", e);
Expand All @@ -116,7 +119,7 @@ pub fn init(socket: UdpSocket, config: GameServerConfig, game_folder_path: Strin

let world_seed = match load_world_seed(world_name, &app) {
Ok(seed) => {
info!("World seed loaded successfully: {}", seed.0); // Affiche la seed chargée
info!("World seed loaded successfully: {}", seed.0);
seed
}
Err(e) => {
Expand All @@ -133,6 +136,8 @@ pub fn init(socket: UdpSocket, config: GameServerConfig, game_folder_path: Strin
}
};

cleanup_all_players_from_world(&mut world_map);

// Insert world_map and seed into ressources
app.insert_resource(world_map);
app.insert_resource(world_seed);
Expand Down
7 changes: 7 additions & 0 deletions server/src/network/cleanup.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
use shared::{messages::PlayerId, world::ServerWorldMap};

pub fn cleanup_all_players_from_world(world_map: &mut ServerWorldMap) {
world_map.players.clear();
for (_, chunk) in world_map.chunks.map.iter_mut() {
chunk.sent_to_clients.clear();
}
}

pub fn cleanup_player_from_world(world_map: &mut ServerWorldMap, player_id: &PlayerId) {
world_map.players.remove(player_id);
for (_, chunk) in world_map.chunks.map.iter_mut() {
Expand Down
10 changes: 4 additions & 6 deletions server/src/world/load_from_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,17 @@ pub struct WorldData {
pub time: u64,
}

/// Charge les données combinées (carte et graine) d'un fichier
pub fn load_world_data(
file_name: &str,
app: &App,
) -> Result<WorldData, Box<dyn std::error::Error>> {
// Obtenir le chemin du dossier de jeu
let game_folder_path = app.world().get_resource::<GameFolderPaths>().unwrap();

// Construire le chemin complet du fichier
let file_path: PathBuf = get_game_folder(Some(game_folder_path))
.join(SAVE_PATH)
.join(format!("{file_name}.ron"));
let path: &Path = file_path.as_path();

// Vérifier si le fichier existe
if !path.exists() {
info!(
"World data file not found: {}. Generating default world and seed.",
Expand All @@ -47,9 +43,11 @@ pub fn load_world_data(
});
}

// Lire le contenu du fichier
let contents: String = fs::read_to_string(path)?;
let world_data: WorldData = from_str(&contents)?; // Désérialiser les données combinées
let world_data: WorldData = from_str(&contents)?;

info!("Found world data file from disk: {}", file_path.display());

Ok(world_data)
}

Expand Down
5 changes: 1 addition & 4 deletions server/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use ulid::Ulid;
#[derive(Event, Debug)]
pub struct BlockInteractionEvent {
pub position: IVec3,
pub block_type: Option<BlockData>, // None = suppression, Some = ajout
pub block_type: Option<BlockData>, // None = delete, Some = add
}

pub fn handle_block_interactions(
Expand All @@ -28,13 +28,10 @@ pub fn handle_block_interactions(
for event in events.read() {
match &event.block_type {
Some(block) => {
// Ajouter un bloc
world_map.chunks.set_block(&event.position, *block);
debug!("Block added at {:?}: {:?}", event.position, block);
}
None => {
// Supprimer un bloc

for (id, nb) in world_map
.chunks
.get_block_by_coordinates(&event.position)
Expand Down
1 change: 0 additions & 1 deletion server/src/world/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ pub struct WorldData {
pub time: u64,
}

// System to save the world when "L" is pressed
pub fn save_world_system(
world_map: Res<ServerWorldMap>,
world_seed: Res<WorldSeed>,
Expand Down

0 comments on commit 8d3c667

Please sign in to comment.