@@ -9,12 +9,13 @@ use crate::net::packets::outgoing::set_center_chunk::SetCenterChunk;
99use crate :: net:: systems:: System ;
1010use crate :: net:: { Connection , ConnectionWrapper } ;
1111use crate :: state:: GlobalState ;
12+ use crate :: utils:: components:: last_chunk_tx_pos:: LastChunkTxPos ;
1213use crate :: utils:: components:: player:: Player ;
1314use crate :: utils:: encoding:: position:: Position ;
1415use crate :: utils:: prelude:: * ;
1516use ferrumc_macros:: AutoGenName ;
1617
17- pub const CHUNK_RADIUS : i32 = 8 ;
18+ pub const CHUNK_RADIUS : i32 = 32 ;
1819const CHUNK_TX_INTERVAL_MS : u64 = 50000 ;
1920
2021#[ derive( AutoGenName ) ]
@@ -51,6 +52,39 @@ impl System for ChunkSender {
5152}
5253
5354impl ChunkSender {
55+ pub async fn send_chunks_to_player_if_needed (
56+ state : GlobalState ,
57+ entity_id : impl TryInto < usize > ,
58+ current_pos : ( i32 , i32 ) ,
59+ ) -> Result < ( ) > {
60+ let entity_id = entity_id. try_into ( ) . map_err ( |_| Error :: ConversionError ) ?;
61+
62+ let mut last_chunk_tx_pos = state
63+ . world
64+ . get_component_storage ( )
65+ . get_mut_or_insert_with :: < LastChunkTxPos > ( entity_id, Default :: default)
66+ . await ;
67+
68+ let distance = last_chunk_tx_pos. distance_to ( current_pos. 0 , current_pos. 1 ) ;
69+
70+ if distance < ( CHUNK_RADIUS as f64 / 4f64 ) {
71+ return Ok ( ( ) ) ;
72+ }
73+
74+ last_chunk_tx_pos. set_last_chunk_tx_pos ( current_pos. 0 , current_pos. 1 ) ;
75+
76+ let state_clone = state. clone ( ) ;
77+ tokio:: spawn (
78+ async move {
79+ ChunkSender :: send_chunks_to_player ( state_clone, entity_id) . await ?;
80+
81+ Ok :: < ( ) , Error > ( ( ) )
82+ }
83+ ) ;
84+
85+
86+ Ok ( ( ) )
87+ }
5488 pub async fn send_chunks_to_player (
5589 state : GlobalState ,
5690 entity_id : impl TryInto < usize > ,
0 commit comments