diff --git a/src/exploration.rs b/src/exploration.rs index f07a524..43d2320 100644 --- a/src/exploration.rs +++ b/src/exploration.rs @@ -265,31 +265,30 @@ fn exploration_focus( config.action_explore_right.clone(), ) { for map in map.iter() { - if let (entity, coordinates, exploring) = explorers.single() { - let coordinates = **coordinates; - let mut exploring = if let Some(exploring) = exploring { - **exploring - } else { - coordinates.floor() - }; - let orig = exploring; - if input.just_active(explore_forward.clone()) { - exploring.1 += 1.; - } else if input.just_active(explore_backward.clone()) { - exploring.1 -= 1.; - } else if input.just_active(explore_left.clone()) { - exploring.0 -= 1.; - } else if input.just_active(explore_right.clone()) { - exploring.0 += 1.; - } - if orig != exploring - && exploring.0 >= 0. - && exploring.0 < map.width as f32 - && exploring.1 >= 0. - && exploring.1 < map.height as f32 - { - commands.entity(entity).insert(Exploring(exploring)); - } + let (entity, coordinates, exploring) = explorers.single(); + let coordinates = **coordinates; + let mut exploring = if let Some(exploring) = exploring { + **exploring + } else { + coordinates.floor() + }; + let orig = exploring; + if input.just_active(explore_forward.clone()) { + exploring.1 += 1.; + } else if input.just_active(explore_backward.clone()) { + exploring.1 -= 1.; + } else if input.just_active(explore_left.clone()) { + exploring.0 -= 1.; + } else if input.just_active(explore_right.clone()) { + exploring.0 += 1.; + } + if orig != exploring + && exploring.0 >= 0. + && exploring.0 < map.width as f32 + && exploring.1 >= 0. + && exploring.1 < map.height as f32 + { + commands.entity(entity).insert(Exploring(exploring)); } } } @@ -334,66 +333,65 @@ fn exploration_changed_announcement( query_pipeline: Res, collider_query: QueryPipelineColliderComponentsQuery, ) -> Result<(), Box> { - if let (coordinates, exploring, viewshed) = explorer.single() { - let collider_set = QueryPipelineColliderComponentsSet(&collider_query); - let coordinates = coordinates.floor(); - for (map, revealed_tiles) in map.iter() { - let point = **exploring; - let idx = point.to_index(map.width); - let shape = Cuboid::new(Vec2::new(0.49, 0.49).into()); - let known = revealed_tiles[idx]; - let visible = viewshed.is_point_visible(exploring); - let fog_of_war = known && !visible; - let description = if known { - let mut tokens: Vec<&str> = vec![]; - for entity in focused.iter() { - commands.entity(entity).remove::(); - } - let shape_pos = (Vec2::new(exploring.x(), exploring.y()), 0.); - query_pipeline.intersections_with_shape( - &collider_set, - &shape_pos.into(), - &shape, - InteractionGroups::all(), - Some(&|v| explorable.get(v.entity()).is_ok()), - |handle| { - let entity = handle.entity(); - commands.entity(entity).insert(ExplorationFocused); - if visible || mappables.get(entity).is_ok() { - if let Ok(name) = names.get(entity) { - tokens.push(name.as_str()); - } - if tokens.is_empty() { - if let Ok(t) = types.get(entity) { - tokens.push((*t).into()); - } + let (coordinates, exploring, viewshed) = explorer.single(); + let collider_set = QueryPipelineColliderComponentsSet(&collider_query); + let coordinates = coordinates.floor(); + for (map, revealed_tiles) in map.iter() { + let point = **exploring; + let idx = point.to_index(map.width); + let shape = Cuboid::new(Vec2::new(0.49, 0.49).into()); + let known = revealed_tiles[idx]; + let visible = viewshed.is_point_visible(exploring); + let fog_of_war = known && !visible; + let description = if known { + let mut tokens: Vec<&str> = vec![]; + for entity in focused.iter() { + commands.entity(entity).remove::(); + } + let shape_pos = (Vec2::new(exploring.x(), exploring.y()), 0.); + query_pipeline.intersections_with_shape( + &collider_set, + &shape_pos.into(), + &shape, + InteractionGroups::all(), + Some(&|v| explorable.get(v.entity()).is_ok()), + |handle| { + let entity = handle.entity(); + commands.entity(entity).insert(ExplorationFocused); + if visible || mappables.get(entity).is_ok() { + if let Ok(name) = names.get(entity) { + tokens.push(name.as_str()); + } + if tokens.is_empty() { + if let Ok(t) = types.get(entity) { + tokens.push((*t).into()); } } - true - }, - ); - if tokens.is_empty() { - let tile = map.tiles[idx]; - if tile.is_blocked() { - "wall".to_string() - } else { - "floor".to_string() } + true + }, + ); + if tokens.is_empty() { + let tile = map.tiles[idx]; + if tile.is_blocked() { + "wall".to_string() } else { - tokens.join(": ") + "floor".to_string() } } else { - "Unknown".to_string() - }; - let mut tokens: Vec = vec![ - description, - coordinates.direction_and_distance(exploring, None), - ]; - if fog_of_war { - tokens.push("in the fog of war".into()); + tokens.join(": ") } - tts.speak(format!("{}", tokens.join(", ")), true)?; + } else { + "Unknown".to_string() + }; + let mut tokens: Vec = vec![ + description, + coordinates.direction_and_distance(exploring, None), + ]; + if fog_of_war { + tokens.push("in the fog of war".into()); } + tts.speak(tokens.join(", ").to_string(), true)?; } Ok(()) } diff --git a/src/map.rs b/src/map.rs index 06a6706..e28628f 100644 --- a/src/map.rs +++ b/src/map.rs @@ -274,8 +274,8 @@ fn spawn_colliders( } if let (Some(bl), Some(br)) = (bottom_left, bottom_right) { //println!("Got bottom, checking if can extend up"); - let mut top_left = bl.clone(); - let mut top_right = br.clone(); + let mut top_left = bl; + let mut top_right = br; if y != map.height - 1 { let mut can_extend_up = true; for y in bl.1 + 1..map.height { @@ -322,7 +322,10 @@ fn spawn_colliders( );*/ let id = commands .spawn_bundle(ColliderBundle { - shape: ColliderShapeComponent(ColliderShape::cuboid(half_width, half_height)), + shape: ColliderShapeComponent(ColliderShape::cuboid( + half_width, + half_height, + )), position: Vec2::new(x, y).into(), ..Default::default() }) @@ -454,21 +457,20 @@ fn area_description( }) { if players.get(other).is_ok() { - if let mut log = log.single_mut() { - if let Ok((aabb, area_name)) = areas.get(area) { - let name = if let Some(name) = area_name { - Some(name.to_string()) - } else if config.describe_undescribed_areas { - Some(format!("{}-by-{} area", aabb.extents().x, aabb.extents().y)) + let mut log = log.single_mut(); + if let Ok((aabb, area_name)) = areas.get(area) { + let name = if let Some(name) = area_name { + Some(name.to_string()) + } else if config.describe_undescribed_areas { + Some(format!("{}-by-{} area", aabb.extents().x, aabb.extents().y)) + } else { + None + }; + if let Some(name) = name { + if event.intersecting { + log.push(format!("Entering {}.", name)); } else { - None - }; - if let Some(name) = name { - if event.intersecting { - log.push(format!("Entering {}.", name)); - } else { - log.push(format!("Leaving {}.", name)); - } + log.push(format!("Leaving {}.", name)); } } } diff --git a/src/navigation.rs b/src/navigation.rs index 77a0a35..3524adb 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -195,10 +195,9 @@ fn speak_direction( mut tts: ResMut, player: Query<&CardinalDirection, (With, Changed)>, ) -> Result<(), Box> { - if let direction = player.single() { - let direction: String = (*direction).into(); - tts.speak(direction, true)?; - } + let direction = player.single(); + let direction: String = (*direction).into(); + tts.speak(direction, true)?; Ok(()) } diff --git a/src/pathfinding.rs b/src/pathfinding.rs index a962a6b..4dbaf5c 100644 --- a/src/pathfinding.rs +++ b/src/pathfinding.rs @@ -219,30 +219,29 @@ fn calculate_path( .remove::(); continue; } - if let map = map.single() { - let coordinates_clone = *coordinates; - let destination_clone = *destination; - let query_pipeline_clone = query_pipeline.clone(); - let map_clone = map.clone(); - let shape_clone = shape.clone(); - let collider_set: StaticColliderComponentsSet = (&collider_query, &obstructions).into(); - let task = pool.spawn(async move { - find_path_for_shape( - entity, - coordinates_clone, - destination_clone, - query_pipeline_clone, - map_clone, - collider_set, - **shape_clone, - ) - }); - commands - .entity(entity) - .insert(Calculating(task)) - .remove::() - .remove::(); - } + let map = map.single(); + let coordinates_clone = *coordinates; + let destination_clone = *destination; + let query_pipeline_clone = query_pipeline.clone(); + let map_clone = map.clone(); + let shape_clone = (*shape).clone(); + let collider_set: StaticColliderComponentsSet = (&collider_query, &obstructions).into(); + let task = pool.spawn(async move { + find_path_for_shape( + entity, + coordinates_clone, + destination_clone, + query_pipeline_clone, + map_clone, + collider_set, + shape_clone, + ) + }); + commands + .entity(entity) + .insert(Calculating(task)) + .remove::() + .remove::(); } } diff --git a/src/visibility.rs b/src/visibility.rs index bd33167..46e79eb 100644 --- a/src/visibility.rs +++ b/src/visibility.rs @@ -61,7 +61,7 @@ impl Viewshed { let shape = ColliderShape::cuboid(0.49, 0.49); let origin = point!(start.x(), start.y()); let coord = Coord::new(start.x_i32(), start.y_i32()); - let collider_set = QueryPipelineColliderComponentsSet(&collider_query); + let collider_set = QueryPipelineColliderComponentsSet(collider_query); let mut new_visible_entities = HashSet::::new(); let visibility_grid = VisibilityGrid( map, @@ -281,20 +281,19 @@ fn update_viewshed( if let Ok((viewer_entity, mut viewshed, mut visible_entities, viewer_coordinates)) = viewers.get_mut(*entity) { - if let map = map.single() { - let mut cache = HashMap::new(); - viewshed.update( - &viewer_entity, - &mut visible_entities, - viewer_coordinates, - &*query_pipeline, - &collider_query, - map, - &visible, - &mut cache, - &mut changed, - ); - } + let map = map.single(); + let mut cache = HashMap::new(); + viewshed.update( + &viewer_entity, + &mut visible_entities, + viewer_coordinates, + &*query_pipeline, + &collider_query, + map, + &visible, + &mut cache, + &mut changed, + ); } } } @@ -314,20 +313,19 @@ fn remove_visible( continue; } visible_entities.remove(&removed); - if let map = map.single() { - let mut cache = HashMap::new(); - viewshed.update( - &viewer_entity, - &mut visible_entities, - start, - &*query_pipeline, - &collider_query, - map, - &visible, - &mut cache, - &mut changed, - ); - } + let map = map.single(); + let mut cache = HashMap::new(); + viewshed.update( + &viewer_entity, + &mut visible_entities, + start, + &*query_pipeline, + &collider_query, + map, + &visible, + &mut cache, + &mut changed, + ); } } } @@ -356,7 +354,11 @@ fn log_visible( mut log: Query<&mut Log>, viewers: Query<(Entity, &Coordinates, &Transform), (With, With)>, visible: Query< - (&Name, Option<&RigidBodyPositionComponent>, Option<&ColliderPositionComponent>), + ( + &Name, + Option<&RigidBodyPositionComponent>, + Option<&ColliderPositionComponent>, + ), Without, >, ) { @@ -375,20 +377,19 @@ fn log_visible( continue; } if let Ok((name, body_position, collider_position)) = visible.get(*viewed) { - if let mut log = log.single_mut() { - let viewed_coordinates = if let Some(p) = body_position { - (p.position.translation.x, p.position.translation.y) - } else if let Some(p) = collider_position { - (p.translation.x, p.translation.y) - } else { - (0., 0.) - }; - let forward = viewer_transform.local_x(); - let yaw = Angle::Radians(forward.y.atan2(forward.x)); - let location = viewer_coordinates - .direction_and_distance(&viewed_coordinates, Some(yaw)); - log.push(format!("{}: {}", **name, location)); - } + let mut log = log.single_mut(); + let viewed_coordinates = if let Some(p) = body_position { + (p.position.translation.x, p.position.translation.y) + } else if let Some(p) = collider_position { + (p.translation.x, p.translation.y) + } else { + (0., 0.) + }; + let forward = viewer_transform.local_x(); + let yaw = Angle::Radians(forward.y.atan2(forward.x)); + let location = + viewer_coordinates.direction_and_distance(&viewed_coordinates, Some(yaw)); + log.push(format!("{}: {}", **name, location)); } } else if let VisibilityChanged::Lost { viewed, .. } = event { recently_lost.insert(*viewed, Timer::from_seconds(2., false));