Finish port to Bevy 0.6.

This commit is contained in:
Nolan Darilek 2022-01-12 11:05:12 -06:00
parent 65e7c0f870
commit ae7a0d13fa
5 changed files with 165 additions and 166 deletions

View File

@ -265,31 +265,30 @@ fn exploration_focus<S, A: 'static>(
config.action_explore_right.clone(), config.action_explore_right.clone(),
) { ) {
for map in map.iter() { for map in map.iter() {
if let (entity, coordinates, exploring) = explorers.single() { let (entity, coordinates, exploring) = explorers.single();
let coordinates = **coordinates; let coordinates = **coordinates;
let mut exploring = if let Some(exploring) = exploring { let mut exploring = if let Some(exploring) = exploring {
**exploring **exploring
} else { } else {
coordinates.floor() coordinates.floor()
}; };
let orig = exploring; let orig = exploring;
if input.just_active(explore_forward.clone()) { if input.just_active(explore_forward.clone()) {
exploring.1 += 1.; exploring.1 += 1.;
} else if input.just_active(explore_backward.clone()) { } else if input.just_active(explore_backward.clone()) {
exploring.1 -= 1.; exploring.1 -= 1.;
} else if input.just_active(explore_left.clone()) { } else if input.just_active(explore_left.clone()) {
exploring.0 -= 1.; exploring.0 -= 1.;
} else if input.just_active(explore_right.clone()) { } else if input.just_active(explore_right.clone()) {
exploring.0 += 1.; exploring.0 += 1.;
} }
if orig != exploring if orig != exploring
&& exploring.0 >= 0. && exploring.0 >= 0.
&& exploring.0 < map.width as f32 && exploring.0 < map.width as f32
&& exploring.1 >= 0. && exploring.1 >= 0.
&& exploring.1 < map.height as f32 && exploring.1 < map.height as f32
{ {
commands.entity(entity).insert(Exploring(exploring)); commands.entity(entity).insert(Exploring(exploring));
}
} }
} }
} }
@ -334,66 +333,65 @@ fn exploration_changed_announcement(
query_pipeline: Res<QueryPipeline>, query_pipeline: Res<QueryPipeline>,
collider_query: QueryPipelineColliderComponentsQuery, collider_query: QueryPipelineColliderComponentsQuery,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
if let (coordinates, exploring, viewshed) = explorer.single() { let (coordinates, exploring, viewshed) = explorer.single();
let collider_set = QueryPipelineColliderComponentsSet(&collider_query); let collider_set = QueryPipelineColliderComponentsSet(&collider_query);
let coordinates = coordinates.floor(); let coordinates = coordinates.floor();
for (map, revealed_tiles) in map.iter() { for (map, revealed_tiles) in map.iter() {
let point = **exploring; let point = **exploring;
let idx = point.to_index(map.width); let idx = point.to_index(map.width);
let shape = Cuboid::new(Vec2::new(0.49, 0.49).into()); let shape = Cuboid::new(Vec2::new(0.49, 0.49).into());
let known = revealed_tiles[idx]; let known = revealed_tiles[idx];
let visible = viewshed.is_point_visible(exploring); let visible = viewshed.is_point_visible(exploring);
let fog_of_war = known && !visible; let fog_of_war = known && !visible;
let description = if known { let description = if known {
let mut tokens: Vec<&str> = vec![]; let mut tokens: Vec<&str> = vec![];
for entity in focused.iter() { for entity in focused.iter() {
commands.entity(entity).remove::<ExplorationFocused>(); commands.entity(entity).remove::<ExplorationFocused>();
} }
let shape_pos = (Vec2::new(exploring.x(), exploring.y()), 0.); let shape_pos = (Vec2::new(exploring.x(), exploring.y()), 0.);
query_pipeline.intersections_with_shape( query_pipeline.intersections_with_shape(
&collider_set, &collider_set,
&shape_pos.into(), &shape_pos.into(),
&shape, &shape,
InteractionGroups::all(), InteractionGroups::all(),
Some(&|v| explorable.get(v.entity()).is_ok()), Some(&|v| explorable.get(v.entity()).is_ok()),
|handle| { |handle| {
let entity = handle.entity(); let entity = handle.entity();
commands.entity(entity).insert(ExplorationFocused); commands.entity(entity).insert(ExplorationFocused);
if visible || mappables.get(entity).is_ok() { if visible || mappables.get(entity).is_ok() {
if let Ok(name) = names.get(entity) { if let Ok(name) = names.get(entity) {
tokens.push(name.as_str()); tokens.push(name.as_str());
} }
if tokens.is_empty() { if tokens.is_empty() {
if let Ok(t) = types.get(entity) { if let Ok(t) = types.get(entity) {
tokens.push((*t).into()); 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 { } else {
tokens.join(": ") "floor".to_string()
} }
} else { } else {
"Unknown".to_string() tokens.join(": ")
};
let mut tokens: Vec<String> = vec![
description,
coordinates.direction_and_distance(exploring, None),
];
if fog_of_war {
tokens.push("in the fog of war".into());
} }
tts.speak(format!("{}", tokens.join(", ")), true)?; } else {
"Unknown".to_string()
};
let mut tokens: Vec<String> = 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(()) Ok(())
} }

View File

@ -274,8 +274,8 @@ fn spawn_colliders(
} }
if let (Some(bl), Some(br)) = (bottom_left, bottom_right) { if let (Some(bl), Some(br)) = (bottom_left, bottom_right) {
//println!("Got bottom, checking if can extend up"); //println!("Got bottom, checking if can extend up");
let mut top_left = bl.clone(); let mut top_left = bl;
let mut top_right = br.clone(); let mut top_right = br;
if y != map.height - 1 { if y != map.height - 1 {
let mut can_extend_up = true; let mut can_extend_up = true;
for y in bl.1 + 1..map.height { for y in bl.1 + 1..map.height {
@ -322,7 +322,10 @@ fn spawn_colliders(
);*/ );*/
let id = commands let id = commands
.spawn_bundle(ColliderBundle { .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(), position: Vec2::new(x, y).into(),
..Default::default() ..Default::default()
}) })
@ -454,21 +457,20 @@ fn area_description(
}) })
{ {
if players.get(other).is_ok() { if players.get(other).is_ok() {
if let mut log = log.single_mut() { let mut log = log.single_mut();
if let Ok((aabb, area_name)) = areas.get(area) { if let Ok((aabb, area_name)) = areas.get(area) {
let name = if let Some(name) = area_name { let name = if let Some(name) = area_name {
Some(name.to_string()) Some(name.to_string())
} else if config.describe_undescribed_areas { } else if config.describe_undescribed_areas {
Some(format!("{}-by-{} area", aabb.extents().x, aabb.extents().y)) 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 { } else {
None log.push(format!("Leaving {}.", name));
};
if let Some(name) = name {
if event.intersecting {
log.push(format!("Entering {}.", name));
} else {
log.push(format!("Leaving {}.", name));
}
} }
} }
} }

View File

@ -195,10 +195,9 @@ fn speak_direction(
mut tts: ResMut<Tts>, mut tts: ResMut<Tts>,
player: Query<&CardinalDirection, (With<Player>, Changed<CardinalDirection>)>, player: Query<&CardinalDirection, (With<Player>, Changed<CardinalDirection>)>,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
if let direction = player.single() { let direction = player.single();
let direction: String = (*direction).into(); let direction: String = (*direction).into();
tts.speak(direction, true)?; tts.speak(direction, true)?;
}
Ok(()) Ok(())
} }

View File

@ -219,30 +219,29 @@ fn calculate_path(
.remove::<Speed>(); .remove::<Speed>();
continue; continue;
} }
if let map = map.single() { let map = map.single();
let coordinates_clone = *coordinates; let coordinates_clone = *coordinates;
let destination_clone = *destination; let destination_clone = *destination;
let query_pipeline_clone = query_pipeline.clone(); let query_pipeline_clone = query_pipeline.clone();
let map_clone = map.clone(); let map_clone = map.clone();
let shape_clone = shape.clone(); let shape_clone = (*shape).clone();
let collider_set: StaticColliderComponentsSet = (&collider_query, &obstructions).into(); let collider_set: StaticColliderComponentsSet = (&collider_query, &obstructions).into();
let task = pool.spawn(async move { let task = pool.spawn(async move {
find_path_for_shape( find_path_for_shape(
entity, entity,
coordinates_clone, coordinates_clone,
destination_clone, destination_clone,
query_pipeline_clone, query_pipeline_clone,
map_clone, map_clone,
collider_set, collider_set,
**shape_clone, shape_clone,
) )
}); });
commands commands
.entity(entity) .entity(entity)
.insert(Calculating(task)) .insert(Calculating(task))
.remove::<Path>() .remove::<Path>()
.remove::<NoPath>(); .remove::<NoPath>();
}
} }
} }

View File

@ -61,7 +61,7 @@ impl Viewshed {
let shape = ColliderShape::cuboid(0.49, 0.49); let shape = ColliderShape::cuboid(0.49, 0.49);
let origin = point!(start.x(), start.y()); let origin = point!(start.x(), start.y());
let coord = Coord::new(start.x_i32(), start.y_i32()); 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::<Entity>::new(); let mut new_visible_entities = HashSet::<Entity>::new();
let visibility_grid = VisibilityGrid( let visibility_grid = VisibilityGrid(
map, map,
@ -281,20 +281,19 @@ fn update_viewshed(
if let Ok((viewer_entity, mut viewshed, mut visible_entities, viewer_coordinates)) = if let Ok((viewer_entity, mut viewshed, mut visible_entities, viewer_coordinates)) =
viewers.get_mut(*entity) viewers.get_mut(*entity)
{ {
if let map = map.single() { let map = map.single();
let mut cache = HashMap::new(); let mut cache = HashMap::new();
viewshed.update( viewshed.update(
&viewer_entity, &viewer_entity,
&mut visible_entities, &mut visible_entities,
viewer_coordinates, viewer_coordinates,
&*query_pipeline, &*query_pipeline,
&collider_query, &collider_query,
map, map,
&visible, &visible,
&mut cache, &mut cache,
&mut changed, &mut changed,
); );
}
} }
} }
} }
@ -314,20 +313,19 @@ fn remove_visible(
continue; continue;
} }
visible_entities.remove(&removed); visible_entities.remove(&removed);
if let map = map.single() { let map = map.single();
let mut cache = HashMap::new(); let mut cache = HashMap::new();
viewshed.update( viewshed.update(
&viewer_entity, &viewer_entity,
&mut visible_entities, &mut visible_entities,
start, start,
&*query_pipeline, &*query_pipeline,
&collider_query, &collider_query,
map, map,
&visible, &visible,
&mut cache, &mut cache,
&mut changed, &mut changed,
); );
}
} }
} }
} }
@ -356,7 +354,11 @@ fn log_visible(
mut log: Query<&mut Log>, mut log: Query<&mut Log>,
viewers: Query<(Entity, &Coordinates, &Transform), (With<Player>, With<Viewshed>)>, viewers: Query<(Entity, &Coordinates, &Transform), (With<Player>, With<Viewshed>)>,
visible: Query< visible: Query<
(&Name, Option<&RigidBodyPositionComponent>, Option<&ColliderPositionComponent>), (
&Name,
Option<&RigidBodyPositionComponent>,
Option<&ColliderPositionComponent>,
),
Without<DontLogWhenVisible>, Without<DontLogWhenVisible>,
>, >,
) { ) {
@ -375,20 +377,19 @@ fn log_visible(
continue; continue;
} }
if let Ok((name, body_position, collider_position)) = visible.get(*viewed) { if let Ok((name, body_position, collider_position)) = visible.get(*viewed) {
if let mut log = log.single_mut() { let mut log = log.single_mut();
let viewed_coordinates = if let Some(p) = body_position { let viewed_coordinates = if let Some(p) = body_position {
(p.position.translation.x, p.position.translation.y) (p.position.translation.x, p.position.translation.y)
} else if let Some(p) = collider_position { } else if let Some(p) = collider_position {
(p.translation.x, p.translation.y) (p.translation.x, p.translation.y)
} else { } else {
(0., 0.) (0., 0.)
}; };
let forward = viewer_transform.local_x(); let forward = viewer_transform.local_x();
let yaw = Angle::Radians(forward.y.atan2(forward.x)); let yaw = Angle::Radians(forward.y.atan2(forward.x));
let location = viewer_coordinates let location =
.direction_and_distance(&viewed_coordinates, Some(yaw)); viewer_coordinates.direction_and_distance(&viewed_coordinates, Some(yaw));
log.push(format!("{}: {}", **name, location)); log.push(format!("{}: {}", **name, location));
}
} }
} else if let VisibilityChanged::Lost { viewed, .. } = event { } else if let VisibilityChanged::Lost { viewed, .. } = event {
recently_lost.insert(*viewed, Timer::from_seconds(2., false)); recently_lost.insert(*viewed, Timer::from_seconds(2., false));