Skip to content

Commit

Permalink
Made it load chunks when player crosses chunk border
Browse files Browse the repository at this point in the history
  • Loading branch information
Sweattypalms committed Sep 9, 2024
1 parent 94c5a62 commit dff458f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/net/packets/incoming/set_player_pos_and_rotate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::net::packets::{ConnectionId, IncomingPacket};
use crate::net::systems::chunk_sender::{ChunkSender, CHUNK_RADIUS};
use crate::state::GlobalState;
use crate::utils::components::rotation::Rotation;
use crate::utils::encoding::position::Position;
Expand Down Expand Up @@ -26,6 +27,20 @@ impl IncomingPacket for SetPlayerPosAndRotate {
let mut position = component_storage.get_mut::<Position>(my_entity_id).await?;
let mut rotation = component_storage.get_mut::<Rotation>(my_entity_id).await?;

let old_chunk_pos = (position.x >> 4, position.z >> 4);
let new_chunk_pos = (self.x as i32 >> 4, self.z as i32 >> 4);

if old_chunk_pos != new_chunk_pos {
let state_clone = state.clone();
tokio::spawn(
async move {
ChunkSender::send_chunks_to_player(state_clone, my_entity_id).await?;

Ok::<(), Error>(())
}
);
}

*position = Position {
x: self.x as i32,
y: self.y as i16,
Expand Down
17 changes: 16 additions & 1 deletion src/net/packets/incoming/set_player_position.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use tracing::trace;
use tracing::{debug, trace};

use ferrumc_macros::{packet, NetDecode};

use crate::net::packets::{ConnectionId, IncomingPacket};
use crate::net::systems::chunk_sender::{ChunkSender, CHUNK_RADIUS};
use crate::state::GlobalState;
use crate::utils::encoding::position::Position;

Expand Down Expand Up @@ -33,6 +34,20 @@ impl IncomingPacket for SetPlayerPosition {

let mut position = component_storage.get_mut::<Position>(my_entity_id).await?;

let old_chunk_pos = (position.x >> 4, position.z >> 4);
let new_chunk_pos = (self.x as i32 >> 4, self.z as i32 >> 4);

if old_chunk_pos != new_chunk_pos {
let state_clone = state.clone();
tokio::spawn(
async move {
ChunkSender::send_chunks_to_player(state_clone, my_entity_id).await?;

Ok::<(), Error>(())
}
);
}

*position = Position {
x: self.x as i32,
y: self.y as i16,
Expand Down
4 changes: 2 additions & 2 deletions src/net/systems/chunk_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use crate::utils::encoding::position::Position;
use crate::utils::prelude::*;
use ferrumc_macros::AutoGenName;

const CHUNK_RADIUS: i32 = 16;
const CHUNK_TX_INTERVAL_MS: u64 = 150;
pub const CHUNK_RADIUS: i32 = 8;
const CHUNK_TX_INTERVAL_MS: u64 = 50000;

#[derive(AutoGenName)]
pub struct ChunkSender;
Expand Down

0 comments on commit dff458f

Please sign in to comment.