Run cargo fmt

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

author
Marijn Besseling <njirambem@gmail.com> · 2026-10-24 08:35 UTC
commit
97cc3d99d6019c978e6605836717a19f275cc1a0
parent
62af2829e2
tree
browse at this commit

8 files changed +144 -49

src/camera.rs +24 -5

@@ -96,7 +96,12 @@ fn keyboard_pan(
96 96 return;
97 97 }
98 98 let fast = keys.any_pressed([KeyCode::ShiftLeft, KeyCode::ShiftRight]);
99 - let speed = controller.pan_speed * if fast { controller.fast_multiplier } else { 1.0 };
99 + let speed = controller.pan_speed
100 + * if fast {
101 + controller.fast_multiplier
102 + } else {
103 + 1.0
104 + };
100 105 let scale = ortho(&mut projection).scale;
101 106 transform.translation += (dir.normalize() * speed * scale * time.delta_secs()).extend(0.0);
102 107 }
@@ -147,7 +152,12 @@ fn scroll_zoom(
147 152 .cursor_position()
148 153 .and_then(|cursor| camera.viewport_to_world_2d(global, cursor).ok());
149 154 if let Some(focus) = focus {
150 - let pos = zoom_towards(transform.translation.truncate(), focus, old_scale, new_scale);
155 + let pos = zoom_towards(
156 + transform.translation.truncate(),
157 + focus,
158 + old_scale,
159 + new_scale,
160 + );
151 161 transform.translation = pos.extend(transform.translation.z);
152 162 }
153 163 }
@@ -196,13 +206,22 @@ mod tests {
196 206
197 207 #[test]
198 208 fn dragging_moves_world_opposite_to_screen_y_because_screen_y_points_down() {
199 - assert_eq!(screen_to_world_delta(Vec2::new(3.0, 4.0)), Vec2::new(3.0, -4.0));
209 + assert_eq!(
210 + screen_to_world_delta(Vec2::new(3.0, 4.0)),
211 + Vec2::new(3.0, -4.0)
212 + );
200 213 }
201 214
202 215 #[test]
203 216 fn camera_center_cannot_leave_the_world_so_the_map_stays_on_screen() {
204 217 let max = Vec2::new(100.0, 50.0);
205 - assert_eq!(clamp_to_rect(Vec2::new(-5.0, 60.0), Vec2::ZERO, max), Vec2::new(0.0, 50.0));
206 - assert_eq!(clamp_to_rect(Vec2::new(5.0, 6.0), Vec2::ZERO, max), Vec2::new(5.0, 6.0));
218 + assert_eq!(
219 + clamp_to_rect(Vec2::new(-5.0, 60.0), Vec2::ZERO, max),
220 + Vec2::new(0.0, 50.0)
221 + );
222 + assert_eq!(
223 + clamp_to_rect(Vec2::new(5.0, 6.0), Vec2::ZERO, max),
224 + Vec2::new(5.0, 6.0)
225 + );
207 226 }
208 227 }

src/colonist.rs +26 -8

@@ -15,11 +15,14 @@ impl Plugin for ColonistPlugin {
15 15 fn build(&self, app: &mut App) {
16 16 app.insert_resource(SimRng::new(0x5EED))
17 17 .add_message::<MoveOrder>()
18 + .add_systems(Update, spawn_colonists.run_if(on_message::<WorldGenerated>))
18 19 .add_systems(
19 20 Update,
20 - spawn_colonists.run_if(on_message::<WorldGenerated>),
21 - )
22 - .add_systems(Update, (toggle_pause, (handle_move_orders, wander, follow_path).chain()));
21 + (
22 + toggle_pause,
23 + (handle_move_orders, wander, follow_path).chain(),
24 + ),
25 + );
23 26 }
24 27 }
25 28
@@ -50,7 +53,10 @@ pub struct Wander(Timer);
50 53
51 54 impl Default for Wander {
52 55 fn default() -> Self {
53 - Self(Timer::from_seconds(WANDER_PAUSE_SECS.start, TimerMode::Once))
56 + Self(Timer::from_seconds(
57 + WANDER_PAUSE_SECS.start,
58 + TimerMode::Once,
59 + ))
54 60 }
55 61 }
56 62
@@ -189,7 +195,10 @@ mod tests {
189 195
190 196 #[test]
191 197 fn step_towards_stops_exactly_on_target() {
192 - assert_eq!(step_towards(Vec2::ZERO, Vec2::new(3.0, 4.0), 10.0), (Vec2::new(3.0, 4.0), true));
198 + assert_eq!(
199 + step_towards(Vec2::ZERO, Vec2::new(3.0, 4.0), 10.0),
200 + (Vec2::new(3.0, 4.0), true)
201 + );
193 202 let (pos, arrived) = step_towards(Vec2::ZERO, Vec2::new(3.0, 4.0), 2.5);
194 203 assert!(!arrived);
195 204 assert!(pos.abs_diff_eq(Vec2::new(1.5, 2.0), 1e-5));
@@ -222,7 +231,9 @@ mod tests {
222 231 let colonist = app
223 232 .world_mut()
224 233 .spawn((
225 - Colonist { name: "Test".into() },
234 + Colonist {
235 + name: "Test".into(),
236 + },
226 237 TilePos(UVec2::new(1, 1)),
227 238 Path(VecDeque::from([UVec2::new(2, 1), UVec2::new(3, 1)])),
228 239 Transform::from_translation(WorldMap::tile_center(UVec2::new(1, 1)).extend(0.0)),
@@ -235,10 +246,17 @@ mod tests {
235 246 app.update();
236 247 }
237 248 let world = app.world();
238 - assert_eq!(world.get::<TilePos>(colonist), Some(&TilePos(UVec2::new(3, 1))));
249 + assert_eq!(
250 + world.get::<TilePos>(colonist),
251 + Some(&TilePos(UVec2::new(3, 1)))
252 + );
239 253 assert!(world.get::<Path>(colonist).unwrap().0.is_empty());
240 254 assert_eq!(
241 - world.get::<Transform>(colonist).unwrap().translation.truncate(),
255 + world
256 + .get::<Transform>(colonist)
257 + .unwrap()
258 + .translation
259 + .truncate(),
242 260 WorldMap::tile_center(UVec2::new(3, 1))
243 261 );
244 262 }

src/noise.rs +3 -1

@@ -85,7 +85,9 @@ mod tests {
85 85 #[test]
86 86 fn negative_coordinates_work() {
87 87 let noise = Noise::new(2);
88 - let values: Vec<f32> = (0..20).map(|i| noise.sample(Vec2::new(-i as f32 * 0.7 - 0.3, -2.4))).collect();
88 + let values: Vec<f32> = (0..20)
89 + .map(|i| noise.sample(Vec2::new(-i as f32 * 0.7 - 0.3, -2.4)))
90 + .collect();
89 91 assert!(values.iter().any(|v| *v != 0.0));
90 92 }
91 93 }

src/pathfinding.rs +7 -4

@@ -129,7 +129,12 @@ mod tests {
129 129 let path = find_path(&map, UVec2::new(0, 0), UVec2::new(4, 0)).unwrap();
130 130 assert_eq!(
131 131 path,
132 - vec![UVec2::new(1, 0), UVec2::new(2, 0), UVec2::new(3, 0), UVec2::new(4, 0)]
132 + vec![
133 + UVec2::new(1, 0),
134 + UVec2::new(2, 0),
135 + UVec2::new(3, 0),
136 + UVec2::new(4, 0)
137 + ]
133 138 );
134 139 }
135 140
@@ -143,9 +148,7 @@ mod tests {
143 148 fn path_goes_around_walls() {
144 149 let map = map_from(&[
145 150 ".....", //
146 - ".###.",
147 - ".#.#.",
148 - "...#.",
151 + ".###.", ".#.#.", "...#.",
149 152 ]);
150 153 let start = UVec2::new(2, 1);
151 154 let goal = UVec2::new(4, 0);

src/selection.rs +47 -15

@@ -14,10 +14,7 @@ impl Plugin for SelectionPlugin {
14 14 app.init_resource::<Cursor>()
15 15 .init_resource::<Selection>()
16 16 .add_systems(Startup, spawn_tile_info)
17 - .add_systems(
18 - Update,
19 - clear_selection.run_if(on_message::<WorldGenerated>),
20 - )
17 + .add_systems(Update, clear_selection.run_if(on_message::<WorldGenerated>))
21 18 .add_systems(
22 19 PostUpdate,
23 20 (
@@ -67,7 +64,11 @@ fn update_cursor(
67 64 let screen = window.cursor_position();
68 65 let world = screen.and_then(|s| camera.viewport_to_world_2d(global, s).ok());
69 66 let tile = world.and_then(|w| map.tile_at(w));
70 - cursor.set_if_neq(Cursor { screen, world, tile });
67 + cursor.set_if_neq(Cursor {
68 + screen,
69 + world,
70 + tile,
71 + });
71 72 }
72 73
73 74 fn clear_selection(mut selection: ResMut<Selection>) {
@@ -85,7 +86,10 @@ fn update_selection(
85 86 selection.set_if_neq(Selection::None);
86 87 } else if buttons.just_pressed(MouseButton::Left) {
87 88 let picked = cursor.world.and_then(|world| {
88 - pick_nearest(world, colonists.iter().map(|(e, t)| (e, t.translation.truncate())))
89 + pick_nearest(
90 + world,
91 + colonists.iter().map(|(e, t)| (e, t.translation.truncate())),
92 + )
89 93 });
90 94 let new = match (picked, cursor.tile) {
91 95 (Some(entity), _) => Selection::Colonist(entity),
@@ -96,7 +100,10 @@ fn update_selection(
96 100 }
97 101 }
98 102
99 -pub fn pick_nearest(point: Vec2, candidates: impl Iterator<Item = (Entity, Vec2)>) -> Option<Entity> {
103 +pub fn pick_nearest(
104 + point: Vec2,
105 + candidates: impl Iterator<Item = (Entity, Vec2)>,
106 +) -> Option<Entity> {
100 107 candidates
101 108 .map(|(e, pos)| (e, pos.distance(point)))
102 109 .filter(|&(_, d)| d <= PICK_RADIUS)
@@ -138,12 +145,20 @@ fn draw_highlights(
138 145 colonists: Query<(&Transform, &Path), With<Colonist>>,
139 146 ) {
140 147 if let Some(tile) = cursor.tile {
141 - gizmos.rect_2d(WorldMap::tile_center(tile), Vec2::splat(TILE_SIZE), HOVER_COLOR);
148 + gizmos.rect_2d(
149 + WorldMap::tile_center(tile),
150 + Vec2::splat(TILE_SIZE),
151 + HOVER_COLOR,
152 + );
142 153 }
143 154 match *selection {
144 155 Selection::None => {}
145 156 Selection::Tile(tile) => {
146 - gizmos.rect_2d(WorldMap::tile_center(tile), Vec2::splat(TILE_SIZE), SELECT_COLOR);
157 + gizmos.rect_2d(
158 + WorldMap::tile_center(tile),
159 + Vec2::splat(TILE_SIZE),
160 + SELECT_COLOR,
161 + );
147 162 }
148 163 Selection::Colonist(entity) => {
149 164 let Ok((transform, path)) = colonists.get(entity) else {
@@ -241,7 +256,9 @@ mod tests {
241 256 }
242 257
243 258 fn press(app: &mut App, button: MouseButton) {
244 - app.world_mut().resource_mut::<ButtonInput<MouseButton>>().press(button);
259 + app.world_mut()
260 + .resource_mut::<ButtonInput<MouseButton>>()
261 + .press(button);
245 262 app.update();
246 263 }
247 264
@@ -295,7 +312,10 @@ mod tests {
295 312
296 313 #[test]
297 314 fn escape_clears_the_selection() {
298 - let mut app = selection_app(cursor_on_tile(UVec2::ONE), Selection::Tile(UVec2::new(3, 4)));
315 + let mut app = selection_app(
316 + cursor_on_tile(UVec2::ONE),
317 + Selection::Tile(UVec2::new(3, 4)),
318 + );
299 319 app.world_mut()
300 320 .resource_mut::<ButtonInput<KeyCode>>()
301 321 .press(KeyCode::Escape);
@@ -306,7 +326,10 @@ mod tests {
306 326 #[test]
307 327 fn right_click_orders_selected_colonist_to_hovered_tile() {
308 328 let colonist = Entity::from_raw_u32(7).unwrap();
309 - let mut app = selection_app(cursor_on_tile(UVec2::new(5, 6)), Selection::Colonist(colonist));
329 + let mut app = selection_app(
330 + cursor_on_tile(UVec2::new(5, 6)),
331 + Selection::Colonist(colonist),
332 + );
310 333 press(&mut app, MouseButton::Right);
311 334 release(&mut app, MouseButton::Right);
312 335 assert_eq!(sent_orders(&mut app), vec![(colonist, UVec2::new(5, 6))]);
@@ -315,7 +338,10 @@ mod tests {
315 338 #[test]
316 339 fn right_drag_does_not_order_because_it_pans_the_camera() {
317 340 let colonist = Entity::from_raw_u32(7).unwrap();
318 - let mut app = selection_app(cursor_on_tile(UVec2::new(5, 6)), Selection::Colonist(colonist));
341 + let mut app = selection_app(
342 + cursor_on_tile(UVec2::new(5, 6)),
343 + Selection::Colonist(colonist),
344 + );
319 345 press(&mut app, MouseButton::Right);
320 346 app.world_mut().resource_mut::<Cursor>().screen = Some(Vec2::new(200.0, 50.0));
321 347 release(&mut app, MouseButton::Right);
@@ -328,7 +354,10 @@ mod tests {
328 354 let b = Entity::from_raw_u32(2).unwrap();
329 355 let candidates = [(a, Vec2::new(5.0, 0.0)), (b, Vec2::new(2.0, 0.0))];
330 356 assert_eq!(pick_nearest(Vec2::ZERO, candidates.into_iter()), Some(b));
331 - assert_eq!(pick_nearest(Vec2::new(100.0, 0.0), candidates.into_iter()), None);
357 + assert_eq!(
358 + pick_nearest(Vec2::new(100.0, 0.0), candidates.into_iter()),
359 + None
360 + );
332 361 }
333 362
334 363 #[test]
@@ -341,7 +370,10 @@ mod tests {
341 370 #[test]
342 371 fn describe_colonist_shows_activity() {
343 372 let ada = Colonist { name: "Ada".into() };
344 - assert_eq!(describe_colonist(&ada, UVec2::new(1, 2), 0), "Ada at (1, 2), idle");
373 + assert_eq!(
374 + describe_colonist(&ada, UVec2::new(1, 2), 0),
375 + "Ada at (1, 2), idle"
376 + );
345 377 assert_eq!(
346 378 describe_colonist(&ada, UVec2::new(1, 2), 3),
347 379 "Ada at (1, 2), moving, 3 steps left"

src/tilemap.rs +19 -7

@@ -17,10 +17,8 @@ pub struct TilemapPlugin;
17 17
18 18 impl Plugin for TilemapPlugin {
19 19 fn build(&self, app: &mut App) {
20 - app.add_systems(Startup, spawn_chunks).add_systems(
21 - Update,
22 - refresh_chunks.run_if(on_message::<WorldGenerated>),
23 - );
20 + app.add_systems(Startup, spawn_chunks)
21 + .add_systems(Update, refresh_chunks.run_if(on_message::<WorldGenerated>));
24 22 }
25 23 }
26 24
@@ -93,7 +91,12 @@ fn build_tileset() -> Image {
93 91 .map_or(Srgba::NONE, |t| t.color());
94 92 (0..TEXTURE_SIZE * TEXTURE_SIZE).flat_map(move |i| {
95 93 let jitter = (hash2(i, layer) % 16) as f32 / 255.0 - 8.0 / 255.0;
96 - let c = Srgba::new(base.red + jitter, base.green + jitter, base.blue + jitter, 1.0);
94 + let c = Srgba::new(
95 + base.red + jitter,
96 + base.green + jitter,
97 + base.blue + jitter,
98 + 1.0,
99 + );
97 100 c.to_u8_array()
98 101 })
99 102 })
@@ -143,7 +146,12 @@ mod tests {
143 146 let map = app.world().resource::<WorldMap>().clone();
144 147 let chunk = app
145 148 .world_mut()
146 - .spawn((Chunk { origin: UVec2::ZERO }, TilemapChunkTileData(chunk_tiles(&map, UVec2::ZERO))))
149 + .spawn((
150 + Chunk {
151 + origin: UVec2::ZERO,
152 + },
153 + TilemapChunkTileData(chunk_tiles(&map, UVec2::ZERO)),
154 + ))
147 155 .id();
148 156
149 157 *app.world_mut().resource_mut::<WorldMap>() =
@@ -152,7 +160,11 @@ mod tests {
152 160 app.update();
153 161
154 162 let data = app.world().get::<TilemapChunkTileData>(chunk).unwrap();
155 - assert!(data.0.iter().all(|t| t.unwrap().tileset_index == Terrain::Stone.index()));
163 + assert!(
164 + data.0
165 + .iter()
166 + .all(|t| t.unwrap().tileset_index == Terrain::Stone.index())
167 + );
156 168 }
157 169
158 170 #[test]

src/world.rs +0 -2

@@ -100,7 +100,6 @@ impl WorldMap {
100 100 let pos = (world / TILE_SIZE).floor().as_ivec2();
101 101 self.in_bounds(pos).then(|| pos.as_uvec2())
102 102 }
103 -
104 103 }
105 104
106 105 pub fn hash2(x: u32, y: u32) -> u32 {
@@ -153,5 +152,4 @@ mod tests {
153 152 assert_eq!(t.index() as usize, i);
154 153 }
155 154 }
156 -
157 155 }

src/worldgen.rs +18 -7

@@ -67,7 +67,10 @@ pub fn generate(size: UVec2, seed: u32) -> WorldMap {
67 67
68 68 let deep = quantile(&elevation, DEEP_WATER_SHARE);
69 69 let shallow = quantile(&elevation, DEEP_WATER_SHARE + SHALLOW_WATER_SHARE);
70 - let sand = quantile(&elevation, DEEP_WATER_SHARE + SHALLOW_WATER_SHARE + SAND_SHARE);
70 + let sand = quantile(
71 + &elevation,
72 + DEEP_WATER_SHARE + SHALLOW_WATER_SHARE + SAND_SHARE,
73 + );
71 74 let stone = quantile(&elevation, 1.0 - STONE_SHARE);
72 75 let soil = quantile(&fertility, 1.0 - SOIL_SHARE);
73 76
@@ -135,10 +138,11 @@ fn flood(map: &WorldMap, start: UVec2, mut visit: impl FnMut(UVec2) -> bool) ->
135 138
136 139 pub fn spawn_tiles(map: &WorldMap, count: usize) -> Vec<UVec2> {
137 140 let center = map.size.as_vec2() / 2.0;
138 - let Some(&origin) = largest_region(map)
139 - .iter()
140 - .min_by(|a, b| a.as_vec2().distance_squared(center).total_cmp(&b.as_vec2().distance_squared(center)))
141 - else {
141 + let Some(&origin) = largest_region(map).iter().min_by(|a, b| {
142 + a.as_vec2()
143 + .distance_squared(center)
144 + .total_cmp(&b.as_vec2().distance_squared(center))
145 + }) else {
142 146 return Vec::new();
143 147 };
144 148 let mut seen = std::collections::HashSet::new();
@@ -157,14 +161,21 @@ mod tests {
157 161 const SIZE: UVec2 = UVec2::new(96, 64);
158 162
159 163 fn share(map: &WorldMap, terrain: Terrain) -> f32 {
160 - let n = map.positions().filter(|p| map.get(p.as_ivec2()) == Some(terrain)).count();
164 + let n = map
165 + .positions()
166 + .filter(|p| map.get(p.as_ivec2()) == Some(terrain))
167 + .count();
161 168 n as f32 / map.size.element_product() as f32
162 169 }
163 170
164 171 #[test]
165 172 fn same_seed_generates_same_world() {
166 173 let (a, b, c) = (generate(SIZE, 7), generate(SIZE, 7), generate(SIZE, 8));
167 - let tiles = |m: &WorldMap| m.positions().map(|p| m.get(p.as_ivec2())).collect::<Vec<_>>();
174 + let tiles = |m: &WorldMap| {
175 + m.positions()
176 + .map(|p| m.get(p.as_ivec2()))
177 + .collect::<Vec<_>>()
178 + };
168 179 assert_eq!(tiles(&a), tiles(&b));
169 180 assert_ne!(tiles(&a), tiles(&c));
170 181 }