@@ -42,7 +42,7 @@ pub struct World {
42
42
43
43
impl World {
44
44
// gets all the chunks except the one passed in the index
45
- pub fn get_other_chunks ( & self , chunk_index : usize ) -> Vec < Arc < Mutex < Chunk > > > {
45
+ pub fn get_other_chunks_by_index ( & self , chunk_index : usize ) -> Vec < Arc < Mutex < Chunk > > > {
46
46
self . chunks
47
47
. iter ( )
48
48
. enumerate ( )
@@ -55,14 +55,26 @@ impl World {
55
55
} )
56
56
. collect ( )
57
57
}
58
+ // gets all the chunks except the one passed
59
+ pub fn get_other_chunks ( & self , chunk_ptr : & Arc < Mutex < Chunk > > ) -> Vec < Arc < Mutex < Chunk > > > {
60
+ self . chunks
61
+ . iter ( )
62
+ . filter_map ( |c| {
63
+ if !Arc :: ptr_eq ( & chunk_ptr, c) {
64
+ Some ( c. clone ( ) )
65
+ } else {
66
+ None
67
+ }
68
+ } )
69
+ . collect ( )
70
+ }
58
71
pub fn place_block ( & mut self , block : Arc < Mutex < Block > > ) {
59
72
let block_borrow = block. lock ( ) . unwrap ( ) ;
60
73
let chunk_coords = block_borrow. get_chunk_coords ( ) ;
61
- let ( chunk_index , chunk) = self
74
+ let chunk = self
62
75
. chunks
63
76
. iter ( )
64
- . enumerate ( )
65
- . find ( |( _, c) | {
77
+ . find ( |c| {
66
78
let c = c. lock ( ) . unwrap ( ) ;
67
79
c. x == chunk_coords. 0 && c. y == chunk_coords. 1
68
80
} )
@@ -71,31 +83,25 @@ impl World {
71
83
let mut chunk_lock = chunk. lock ( ) . unwrap ( ) ;
72
84
std:: mem:: drop ( block_borrow) ;
73
85
chunk_lock. add_block ( block. clone ( ) ) ;
74
- chunk_lock. build_mesh ( self . get_other_chunks ( chunk_index ) ) ;
86
+ chunk_lock. build_mesh ( self . get_other_chunks ( chunk ) ) ;
75
87
76
88
let block_borrow = block. lock ( ) . unwrap ( ) ;
77
89
let block_neighbour_chunks = block_borrow. get_neighbour_chunks_coords ( ) ;
78
90
std:: mem:: drop ( chunk_lock) ;
79
91
80
92
if block_neighbour_chunks. len ( ) > 0 {
81
93
for neighbour_chunk in block_neighbour_chunks {
82
- let ( neighbour_index , neighbour_chunk) = self
94
+ let neighbour_chunk = self
83
95
. chunks
84
96
. iter ( )
85
- . enumerate ( )
86
- . find_map ( |( i, o) | {
97
+ . find ( |o| {
87
98
let c = o. lock ( ) . unwrap ( ) ;
88
- return if c. x == neighbour_chunk. 0 && c. y == neighbour_chunk. 1 {
89
- Some ( ( i, o) )
90
- } else {
91
- None
92
- } ;
99
+ return c. x == neighbour_chunk. 0 && c. y == neighbour_chunk. 1 ;
93
100
} )
94
101
. expect ( "Cannot destroy a block without neighbour being loaded" ) ;
95
102
96
- let mut neighbour_chunk = neighbour_chunk. lock ( ) . unwrap ( ) ;
97
-
98
- neighbour_chunk. build_mesh ( self . get_other_chunks ( neighbour_index) ) ;
103
+ let mut neighbour_chunkbrw = neighbour_chunk. lock ( ) . unwrap ( ) ;
104
+ neighbour_chunkbrw. build_mesh ( self . get_other_chunks ( neighbour_chunk) ) ;
99
105
}
100
106
}
101
107
@@ -104,11 +110,10 @@ impl World {
104
110
pub fn remove_block ( & mut self , block : Arc < Mutex < Block > > ) {
105
111
let block_borrow = block. lock ( ) . unwrap ( ) ;
106
112
let chunk_coords = block_borrow. get_chunk_coords ( ) ;
107
- let ( chunk_index , chunk) = self
113
+ let chunk = self
108
114
. chunks
109
115
. iter ( )
110
- . enumerate ( )
111
- . find ( |( _, c) | {
116
+ . find ( |c| {
112
117
let c = c. lock ( ) . unwrap ( ) ;
113
118
c. x == chunk_coords. 0 && c. y == chunk_coords. 1
114
119
} )
@@ -117,31 +122,26 @@ impl World {
117
122
let mut chunk_lock = chunk. lock ( ) . unwrap ( ) ;
118
123
chunk_lock. remove_block ( & ( block_borrow. position ) ) ;
119
124
120
- chunk_lock. build_mesh ( self . get_other_chunks ( chunk_index ) ) ;
125
+ chunk_lock. build_mesh ( self . get_other_chunks ( chunk ) ) ;
121
126
let block_neighbour_chunks = block_borrow. get_neighbour_chunks_coords ( ) ;
122
127
123
128
// I hate this so much
124
129
std:: mem:: drop ( chunk_lock) ;
125
130
126
131
if block_neighbour_chunks. len ( ) > 0 {
127
132
for neighbour_chunk in block_neighbour_chunks {
128
- let ( neighbour_index , neighbour_chunk) = self
133
+ let neighbour_chunk = self
129
134
. chunks
130
135
. iter ( )
131
- . enumerate ( )
132
- . find_map ( |( i, o) | {
136
+ . find ( |o| {
133
137
let c = o. lock ( ) . unwrap ( ) ;
134
- return if c. x == neighbour_chunk. 0 && c. y == neighbour_chunk. 1 {
135
- Some ( ( i, o) )
136
- } else {
137
- None
138
- } ;
138
+ c. x == neighbour_chunk. 0 && c. y == neighbour_chunk. 1
139
139
} )
140
140
. expect ( "Cannot destroy a block without neighbour being loaded" ) ;
141
141
142
- let mut neighbour_chunk = neighbour_chunk. lock ( ) . unwrap ( ) ;
142
+ let mut neighbour_chunkbrw = neighbour_chunk. lock ( ) . unwrap ( ) ;
143
143
144
- neighbour_chunk . build_mesh ( self . get_other_chunks ( neighbour_index ) ) ;
144
+ neighbour_chunkbrw . build_mesh ( self . get_other_chunks ( neighbour_chunk ) ) ;
145
145
}
146
146
}
147
147
}
@@ -277,6 +277,7 @@ impl World {
277
277
let chunk = receiver. recv ( ) . unwrap ( ) ;
278
278
self . chunks . push ( Arc :: new ( Mutex :: new ( chunk) ) ) ;
279
279
}
280
+ self . handle_outside_blocks ( ) ;
280
281
}
281
282
282
283
player. current_chunk = current_chunk;
@@ -306,14 +307,45 @@ impl World {
306
307
} ) ;
307
308
}
308
309
}
309
- println ! ( "END CHUNK" ) ;
310
310
311
311
let mut chunks = vec ! [ ] ;
312
312
for _ in 0 ..CHUNKS_PER_ROW * CHUNKS_PER_ROW {
313
313
let chunk = receiver. recv ( ) . unwrap ( ) ;
314
314
chunks. push ( Arc :: new ( Mutex :: new ( chunk) ) ) ;
315
315
}
316
- self . chunks . append ( & mut chunks) ;
316
+ self . chunks . append ( & mut chunks) ; // Add chunks to self
317
+
318
+ self . handle_outside_blocks ( ) ;
319
+ }
320
+ fn handle_outside_blocks ( & mut self ) {
321
+ let mut blocks_to_add = vec ! [ ] ;
322
+ for chunk in self . chunks . iter ( ) {
323
+ let mut chunkbrw = chunk. lock ( ) . unwrap ( ) ;
324
+ blocks_to_add. append ( & mut chunkbrw. outside_blocks ) ;
325
+ }
326
+
327
+ let mut chunks_to_rerender = vec ! [ ] ;
328
+
329
+ for block in blocks_to_add. iter ( ) {
330
+ let chunk_coords = block. lock ( ) . unwrap ( ) . get_chunk_coords ( ) ;
331
+ if let Some ( chunkptr) = self . chunks . iter ( ) . find ( |c| {
332
+ let c = c. lock ( ) . unwrap ( ) ;
333
+ c. x == chunk_coords. 0 && c. y == chunk_coords. 1
334
+ } ) {
335
+ let mut chunkbrw = chunkptr. lock ( ) . unwrap ( ) ;
336
+ chunkbrw. add_block ( block. clone ( ) ) ;
337
+ if let None = chunks_to_rerender. iter ( ) . find ( |c| Arc :: ptr_eq ( c, chunkptr) ) {
338
+ chunks_to_rerender. push ( chunkptr. clone ( ) ) ;
339
+ } ;
340
+ }
341
+ }
342
+
343
+ // This should really be multi threadable imo..
344
+ for chunk in chunks_to_rerender. iter ( ) {
345
+ let other_chunks = self . get_other_chunks ( chunk) ;
346
+ let mut chunk = chunk. lock ( ) . unwrap ( ) ;
347
+ chunk. build_mesh ( other_chunks) ;
348
+ }
317
349
}
318
350
pub fn init_world ( device : Arc < wgpu:: Device > , queue : Arc < wgpu:: Queue > ) -> Self {
319
351
let noise_data = Arc :: new ( crate :: utils:: noise:: create_world_noise_data (
0 commit comments