@@ -75,22 +75,53 @@ impl World {
75
75
self . render_chunks ( chunks_to_rerender)
76
76
}
77
77
pub fn remove_block ( & mut self , block : Arc < RwLock < Block > > ) {
78
- let block_borrow = block. read ( ) . unwrap ( ) ;
79
- let mut chunks_to_rerender = vec ! [ block_borrow. get_chunk_coords( ) ] ;
80
- chunks_to_rerender. append ( & mut block_borrow. get_neighbour_chunks_coords ( ) ) ;
81
-
82
- let chunk_map = self . chunks . read ( ) . unwrap ( ) ;
83
- let chunk = chunk_map
84
- . get ( & chunks_to_rerender[ 0 ] )
85
- . expect ( "Cannot delete a block from unloaded chunk" ) ;
86
-
78
+ let mut has_adjacent_water = false ;
79
+ let mut chunks_to_rerender = vec ! [ ] ;
87
80
{
88
- let mut chunk_lock = chunk. write ( ) . unwrap ( ) ;
89
- chunk_lock. remove_block ( & ( block_borrow. position ) ) ;
90
- // Drop chunk lock write
81
+ let block_borrow = block. read ( ) . unwrap ( ) ;
82
+ chunks_to_rerender. push ( block_borrow. get_chunk_coords ( ) ) ;
83
+ chunks_to_rerender. append ( & mut block_borrow. get_neighbour_chunks_coords ( ) ) ;
84
+
85
+ let chunk_map = self . chunks . read ( ) . unwrap ( ) ;
86
+ let chunk = chunk_map
87
+ . get ( & chunks_to_rerender[ 0 ] )
88
+ . expect ( "Cannot delete a block from unloaded chunk" ) ;
89
+
90
+ {
91
+ let mut chunk_lock = chunk. write ( ) . unwrap ( ) ;
92
+ chunk_lock. remove_block ( & ( block_borrow. position ) ) ;
93
+ // Drop chunk lock write
94
+ }
95
+
96
+ for offset in [
97
+ glam:: vec3 ( 1.0 , 0.0 , 0.0 ) ,
98
+ glam:: vec3 ( -1.0 , 0.0 , 0.0 ) ,
99
+ glam:: vec3 ( 0.0 , 0.0 , 1.0 ) ,
100
+ glam:: vec3 ( 0.0 , 0.0 , -1.0 ) ,
101
+ ] {
102
+ let position = block_borrow. absolute_position + offset;
103
+ let chunk_pos = position. get_chunk_from_position_absolute ( ) ;
104
+ let chunk = chunk_map
105
+ . get ( & chunk_pos)
106
+ . expect ( "Should be loaded chunk" )
107
+ . read ( )
108
+ . unwrap ( ) ;
109
+
110
+ if chunk. block_type_at ( & position) == Some ( BlockType :: Water ) {
111
+ has_adjacent_water = true ;
112
+ }
113
+ }
91
114
}
92
115
93
- self . render_chunks ( chunks_to_rerender) ;
116
+ // if it has a nearby block of water, replace the removed block with a water block.
117
+ if has_adjacent_water {
118
+ let mut blockbrw = block. write ( ) . unwrap ( ) ;
119
+ blockbrw. block_type = BlockType :: Water ;
120
+ std:: mem:: drop ( blockbrw) ;
121
+ self . place_block ( block) ;
122
+ } else {
123
+ self . render_chunks ( chunks_to_rerender) ;
124
+ }
94
125
}
95
126
pub fn get_blocks_absolute ( & self , position : & Vec3 ) -> Option < Arc < RwLock < Block > > > {
96
127
let ( chunk_x, chunk_y) = position. get_chunk_from_position_absolute ( ) ;
0 commit comments