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