@@ -9,12 +9,13 @@ use crate::net::packets::outgoing::set_center_chunk::SetCenterChunk;
9
9
use crate :: net:: systems:: System ;
10
10
use crate :: net:: { Connection , ConnectionWrapper } ;
11
11
use crate :: state:: GlobalState ;
12
+ use crate :: utils:: components:: last_chunk_tx_pos:: LastChunkTxPos ;
12
13
use crate :: utils:: components:: player:: Player ;
13
14
use crate :: utils:: encoding:: position:: Position ;
14
15
use crate :: utils:: prelude:: * ;
15
16
use ferrumc_macros:: AutoGenName ;
16
17
17
- pub const CHUNK_RADIUS : i32 = 8 ;
18
+ pub const CHUNK_RADIUS : i32 = 32 ;
18
19
const CHUNK_TX_INTERVAL_MS : u64 = 50000 ;
19
20
20
21
#[ derive( AutoGenName ) ]
@@ -51,6 +52,39 @@ impl System for ChunkSender {
51
52
}
52
53
53
54
impl 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
+ }
54
88
pub async fn send_chunks_to_player (
55
89
state : GlobalState ,
56
90
entity_id : impl TryInto < usize > ,
0 commit comments