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,9 +46,11 @@ pub fn find_path(
&start.into(),
|p| {
let mut successors: Vec<((i32, i32), u32)> = vec![];
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
},
|p| (p.distance_squared(destination) * 100.) as u32,
@ -147,6 +150,7 @@ fn find_path_for_shape(
&start.i32(),
|p| {
let mut successors: Vec<((i32, i32), u32)> = vec![];
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.);
@ -165,6 +169,7 @@ fn find_path_for_shape(
successors.push(((tile.0 as i32, tile.1 as i32), (tile.2 * 100.) as u32));
}
}
}
successors
},
|p| (p.distance_squared(destination) * 100.) as u32,
@ -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>();
}
}
}