@@ -43,72 +43,61 @@ pub struct World {
43
43
}
44
44
45
45
impl World {
46
+ // gets all the chunks except the one passed in the index
47
+ pub fn get_other_chunks ( & self , chunk_index : usize ) -> Vec < Arc < Mutex < Chunk > > > {
48
+ self . chunks
49
+ . iter ( )
50
+ . enumerate ( )
51
+ . filter_map ( |( i, c) | {
52
+ return if i != chunk_index {
53
+ Some ( c. clone ( ) )
54
+ } else {
55
+ None
56
+ } ;
57
+ } )
58
+ . collect ( )
59
+ }
46
60
pub fn remove_block ( & mut self , block : Arc < Mutex < Block > > ) {
47
- let blockbrw = block. lock ( ) . unwrap ( ) ;
48
- // TODO: Handle block on chunk border case
49
- let chunk = blockbrw. get_chunk_coords ( ) ;
50
- let chunk = self
61
+ let block_borrow = block. lock ( ) . unwrap ( ) ;
62
+ let chunk_coords = block_borrow. get_chunk_coords ( ) ;
63
+ let ( chunk_index, chunk) = self
51
64
. chunks
52
65
. iter ( )
53
- . find ( |c| {
66
+ . enumerate ( )
67
+ . find ( |( i, c) | {
54
68
let c = c. lock ( ) . unwrap ( ) ;
55
- return c. x == chunk . 0 && c. y == chunk . 1 ;
69
+ return c. x == chunk_coords . 0 && c. y == chunk_coords . 1 ;
56
70
} )
57
71
. expect ( "Cannot delete a block from unloaded chunk" ) ;
58
72
59
- let mut chunklock = chunk. lock ( ) . unwrap ( ) ;
60
- let chunkcoords = ( chunklock. x , chunklock. y ) ;
61
- println ! (
62
- "\n \n JUST DELETED {:?} {:?}" ,
63
- blockbrw. position, chunkcoords
64
- ) ;
65
- chunklock. remove_block ( & ( blockbrw. position ) ) ;
66
- std:: mem:: drop ( chunklock) ;
73
+ let mut chunk_lock = chunk. lock ( ) . unwrap ( ) ;
74
+ chunk_lock. remove_block ( & ( block_borrow. position ) ) ;
67
75
68
- let other_chunks = self
69
- . chunks
70
- . iter ( )
71
- . filter ( |c| {
72
- let c = c. lock ( ) . unwrap ( ) ;
73
- c. x != chunkcoords. 0 && c. y != chunkcoords. 1
74
- } )
75
- . map ( |p| p. clone ( ) )
76
- . collect :: < Vec < _ > > ( ) ;
76
+ chunk_lock. build_mesh ( self . get_other_chunks ( chunk_index) ) ;
77
+ let block_neighbour_chunks = block_borrow. get_neighbour_chunks_coords ( ) ;
77
78
78
- let mut chunklock = chunk. lock ( ) . unwrap ( ) ;
79
- chunklock. build_mesh ( other_chunks) ;
80
79
// I hate this so much
81
-
82
- let neighbour_chunks = blockbrw. get_neighbour_chunks_coords ( ) ;
83
- std:: mem:: drop ( chunklock) ;
84
-
85
- println ! ( "NEIGHBOUR CHUNKS {:?}" , neighbour_chunks) ;
86
-
87
- if neighbour_chunks. len ( ) > 0 {
88
- for neighbour_chunk in neighbour_chunks {
89
- let neigh_chunk = self . chunks . iter ( ) . enumerate ( ) . find_map ( |( i, o) | {
90
- let c = o. lock ( ) . unwrap ( ) ;
91
- return if c. x == neighbour_chunk. 0 && c. y == neighbour_chunk. 1 {
92
- Some ( ( i, o) )
93
- } else {
94
- None
95
- } ;
96
- } ) ;
97
-
98
- if let Some ( ( pi, chunk) ) = neigh_chunk {
99
- let mut neighbour_chunk = chunk. lock ( ) . unwrap ( ) ;
100
- let other_chunks = self
101
- . chunks
102
- . iter ( )
103
- . enumerate ( )
104
- . filter_map ( |( i, o) | {
105
- return if i != pi { Some ( o. clone ( ) ) } else { None } ;
106
- } )
107
- . collect :: < Vec < _ > > ( ) ;
108
- println ! ( "OTHER CHUNKS {}" , other_chunks. len( ) ) ;
109
-
110
- neighbour_chunk. build_mesh ( other_chunks) ;
111
- }
80
+ std:: mem:: drop ( chunk_lock) ;
81
+
82
+ if block_neighbour_chunks. len ( ) > 0 {
83
+ for neighbour_chunk in block_neighbour_chunks {
84
+ let ( neighbour_index, neighbour_chunk) = self
85
+ . chunks
86
+ . iter ( )
87
+ . enumerate ( )
88
+ . find_map ( |( i, o) | {
89
+ let c = o. lock ( ) . unwrap ( ) ;
90
+ return if c. x == neighbour_chunk. 0 && c. y == neighbour_chunk. 1 {
91
+ Some ( ( i, o) )
92
+ } else {
93
+ None
94
+ } ;
95
+ } )
96
+ . expect ( "Cannot destroy a block without neighbour being loaded" ) ;
97
+
98
+ let mut neighbour_chunk = neighbour_chunk. lock ( ) . unwrap ( ) ;
99
+
100
+ neighbour_chunk. build_mesh ( self . get_other_chunks ( neighbour_index) ) ;
112
101
}
113
102
}
114
103
}
0 commit comments