Skip to content

Commit dff458f

Browse files
committed
Made it load chunks when player crosses chunk border
1 parent 94c5a62 commit dff458f

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/net/packets/incoming/set_player_pos_and_rotate.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::net::packets::{ConnectionId, IncomingPacket};
2+
use crate::net::systems::chunk_sender::{ChunkSender, CHUNK_RADIUS};
23
use crate::state::GlobalState;
34
use crate::utils::components::rotation::Rotation;
45
use crate::utils::encoding::position::Position;
@@ -26,6 +27,20 @@ impl IncomingPacket for SetPlayerPosAndRotate {
2627
let mut position = component_storage.get_mut::<Position>(my_entity_id).await?;
2728
let mut rotation = component_storage.get_mut::<Rotation>(my_entity_id).await?;
2829

30+
let old_chunk_pos = (position.x >> 4, position.z >> 4);
31+
let new_chunk_pos = (self.x as i32 >> 4, self.z as i32 >> 4);
32+
33+
if old_chunk_pos != new_chunk_pos {
34+
let state_clone = state.clone();
35+
tokio::spawn(
36+
async move {
37+
ChunkSender::send_chunks_to_player(state_clone, my_entity_id).await?;
38+
39+
Ok::<(), Error>(())
40+
}
41+
);
42+
}
43+
2944
*position = Position {
3045
x: self.x as i32,
3146
y: self.y as i16,

src/net/packets/incoming/set_player_position.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use tracing::trace;
1+
use tracing::{debug, trace};
22

33
use ferrumc_macros::{packet, NetDecode};
44

55
use crate::net::packets::{ConnectionId, IncomingPacket};
6+
use crate::net::systems::chunk_sender::{ChunkSender, CHUNK_RADIUS};
67
use crate::state::GlobalState;
78
use crate::utils::encoding::position::Position;
89

@@ -33,6 +34,20 @@ impl IncomingPacket for SetPlayerPosition {
3334

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

37+
let old_chunk_pos = (position.x >> 4, position.z >> 4);
38+
let new_chunk_pos = (self.x as i32 >> 4, self.z as i32 >> 4);
39+
40+
if old_chunk_pos != new_chunk_pos {
41+
let state_clone = state.clone();
42+
tokio::spawn(
43+
async move {
44+
ChunkSender::send_chunks_to_player(state_clone, my_entity_id).await?;
45+
46+
Ok::<(), Error>(())
47+
}
48+
);
49+
}
50+
3651
*position = Position {
3752
x: self.x as i32,
3853
y: self.y as i16,

src/net/systems/chunk_sender.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use crate::utils::encoding::position::Position;
1414
use crate::utils::prelude::*;
1515
use ferrumc_macros::AutoGenName;
1616

17-
const CHUNK_RADIUS: i32 = 16;
18-
const CHUNK_TX_INTERVAL_MS: u64 = 150;
17+
pub const CHUNK_RADIUS: i32 = 8;
18+
const CHUNK_TX_INTERVAL_MS: u64 = 50000;
1919

2020
#[derive(AutoGenName)]
2121
pub struct ChunkSender;

0 commit comments

Comments
 (0)