53 |
53 |
54 fn get_bin(&mut self, index: Point) -> &mut GridBin { |
54 fn get_bin(&mut self, index: Point) -> &mut GridBin { |
55 &mut self.bins[index.y as usize * self.bins_count.width + index.x as usize] |
55 &mut self.bins[index.y as usize * self.bins_count.width + index.x as usize] |
56 } |
56 } |
57 |
57 |
|
58 fn try_get_bin(&mut self, index: Point) -> Option<&mut GridBin> { |
|
59 self.bins |
|
60 .get_mut(index.y as usize * self.bins_count.width + index.x as usize) |
|
61 } |
|
62 |
58 fn lookup_bin(&mut self, position: &FPPoint) -> &mut GridBin { |
63 fn lookup_bin(&mut self, position: &FPPoint) -> &mut GridBin { |
59 self.get_bin(self.bin_index(position)) |
64 self.get_bin(self.bin_index(position)) |
60 } |
65 } |
61 |
66 |
62 pub fn insert_static(&mut self, gear_id: GearId, bounds: &CircleBounds) { |
67 pub fn insert_static(&mut self, gear_id: GearId, bounds: &CircleBounds) { |
80 new_position: &FPPoint, |
85 new_position: &FPPoint, |
81 ) { |
86 ) { |
82 let old_bin_index = self.bin_index(old_position); |
87 let old_bin_index = self.bin_index(old_position); |
83 let new_bin_index = self.bin_index(new_position); |
88 let new_bin_index = self.bin_index(new_position); |
84 |
89 |
85 let old_bin = self.lookup_bin(old_position); |
90 if let Some(old_bin) = self.try_get_bin(old_bin_index) { |
86 if let Some(index) = old_bin.dynamic_refs.iter().position(|id| *id == gear_id) { |
91 let bounds = if let Some(index) = |
87 if old_bin_index == new_bin_index { |
92 old_bin.dynamic_refs.iter().position(|id| *id == gear_id) |
88 old_bin.dynamic_entries[index].center = *new_position |
93 { |
|
94 if old_bin_index == new_bin_index { |
|
95 old_bin.dynamic_entries[index].center = *new_position; |
|
96 None |
|
97 } else { |
|
98 Some(old_bin.dynamic_entries.swap_remove(index)) |
|
99 } |
|
100 } else if let Some(index) = old_bin.static_refs.iter().position(|id| *id == gear_id) { |
|
101 old_bin.static_refs.swap_remove(index); |
|
102 Some(old_bin.static_entries.swap_remove(index)) |
89 } else { |
103 } else { |
90 let bounds = old_bin.dynamic_entries.swap_remove(index); |
104 None |
91 let new_bin = self.get_bin(new_bin_index); |
|
92 |
|
93 new_bin.dynamic_refs.push(gear_id); |
|
94 new_bin.dynamic_entries.push(CircleBounds { |
|
95 center: *new_position, |
|
96 ..bounds |
|
97 }); |
|
98 } |
|
99 } else if let Some(index) = old_bin.static_refs.iter().position(|id| *id == gear_id) { |
|
100 let bounds = old_bin.static_entries.swap_remove(index); |
|
101 old_bin.static_refs.swap_remove(index); |
|
102 |
|
103 let new_bin = if old_bin_index == new_bin_index { |
|
104 old_bin |
|
105 } else { |
|
106 self.get_bin(new_bin_index) |
|
107 }; |
105 }; |
108 |
106 |
109 new_bin.dynamic_refs.push(gear_id); |
107 if let Some(bounds) = bounds { |
110 new_bin.dynamic_entries.push(CircleBounds { |
108 let new_bin = if old_bin_index == new_bin_index { |
111 center: *new_position, |
109 Some(old_bin) |
112 ..bounds |
110 } else { |
113 }); |
111 self.try_get_bin(new_bin_index) |
|
112 }; |
|
113 |
|
114 if let Some(new_bin) = new_bin { |
|
115 new_bin.dynamic_refs.push(gear_id); |
|
116 new_bin.dynamic_entries.push(CircleBounds { |
|
117 center: *new_position, |
|
118 ..bounds |
|
119 }); |
|
120 } |
|
121 } |
114 } |
122 } |
115 } |
123 } |
116 |
124 |
117 pub fn check_collisions(&self, collisions: &mut DetectedCollisions) { |
125 pub fn check_collisions(&self, collisions: &mut DetectedCollisions) { |
118 for bin in &self.bins { |
126 for bin in &self.bins { |