Various pathfinding tweaks.

This commit is contained in:
Nolan Darilek 2021-09-16 14:26:45 -05:00
parent 74e66dbd59
commit dc1c14fd23

View File

@ -11,6 +11,7 @@ use bevy_rapier2d::{
}; };
use derive_more::{Deref, DerefMut}; use derive_more::{Deref, DerefMut};
use futures_lite::future; use futures_lite::future;
use mapgen::Tile;
use pathfinding::prelude::*; use pathfinding::prelude::*;
use crate::{ use crate::{
@ -45,8 +46,10 @@ pub fn find_path(
&start.into(), &start.into(),
|p| { |p| {
let mut successors: Vec<((i32, i32), u32)> = vec![]; let mut successors: Vec<((i32, i32), u32)> = vec![];
for tile in map.get_available_exits(p.0 as usize, p.1 as usize) { if map.at(p.0 as usize, p.1 as usize).is_walkable() {
successors.push(((tile.0 as i32, tile.1 as i32), (tile.2 * 100.) as u32)); for tile in map.get_available_exits(p.0 as usize, p.1 as usize) {
successors.push(((tile.0 as i32, tile.1 as i32), (tile.2 * 100.) as u32));
}
} }
successors successors
}, },
@ -147,22 +150,24 @@ fn find_path_for_shape(
&start.i32(), &start.i32(),
|p| { |p| {
let mut successors: Vec<((i32, i32), u32)> = vec![]; let mut successors: Vec<((i32, i32), u32)> = vec![];
for tile in map.get_available_exits(p.0 as usize, p.1 as usize) { if map.at(p.0 as usize, p.1 as usize).is_walkable() {
let mut should_push = true; for tile in map.get_available_exits(p.0 as usize, p.1 as usize) {
let shape_pos = Isometry::new(vector![tile.0 as f32, tile.1 as f32], 0.); let mut should_push = true;
query_pipeline.intersections_with_shape( let shape_pos = Isometry::new(vector![tile.0 as f32, tile.1 as f32], 0.);
&collider_set, query_pipeline.intersections_with_shape(
&shape_pos, &collider_set,
&**shape, &shape_pos,
InteractionGroups::all(), &**shape,
Some(&|v| v.entity() != initiator), InteractionGroups::all(),
|_handle| { Some(&|v| v.entity() != initiator),
should_push = false; |_handle| {
false should_push = false;
}, false
); },
if should_push { );
successors.push(((tile.0 as i32, tile.1 as i32), (tile.2 * 100.) as u32)); if should_push {
successors.push(((tile.0 as i32, tile.1 as i32), (tile.2 * 100.) as u32));
}
} }
} }
successors successors
@ -193,7 +198,8 @@ fn calculate_path(
.remove::<Path>() .remove::<Path>()
.remove::<NoPath>() .remove::<NoPath>()
.remove::<Calculating>() .remove::<Calculating>()
.remove::<Destination>(); .remove::<Destination>()
.remove::<Speed>();
continue; continue;
} }
for map in map.iter() { for map in map.iter() {
@ -280,7 +286,8 @@ fn negotiate_path(
continue; continue;
} }
} else { } else {
commands.entity(entity).remove::<Path>(); println!("Remove path1");
commands.entity(entity).remove::<Path>().remove::<Speed>();
velocity.linvel = Vec2::ZERO.into(); velocity.linvel = Vec2::ZERO.into();
} }
**path = new_path; **path = new_path;
@ -302,10 +309,13 @@ fn negotiate_path(
velocity.linvel = direction.into(); velocity.linvel = direction.into();
} else { } else {
velocity.linvel = Vec2::ZERO.into(); velocity.linvel = Vec2::ZERO.into();
println!("Remove path2");
commands commands
.entity(entity) .entity(entity)
.remove::<Path>() .remove::<Path>()
.remove::<Destination>(); .remove::<NoPath>()
.remove::<Destination>()
.remove::<Speed>();
} }
} }
} }