Simplify path negotiation.

This commit is contained in:
Nolan Darilek 2021-09-17 10:02:33 -05:00
parent 10fc85d384
commit 8f5b1b8f08

View File

@ -260,62 +260,43 @@ fn negotiate_path(
)>, )>,
) { ) {
for (entity, mut path, mut position, mut velocity, speed, rotation_speed) in query.iter_mut() { for (entity, mut path, mut position, mut velocity, speed, rotation_speed) in query.iter_mut() {
let mut new_path = path.0.clone();
let start_i32 = ( let start_i32 = (
position.position.translation.x, position.position.translation.x,
position.position.translation.y, position.position.translation.y,
) )
.i32(); .i32();
let new_path_clone = new_path.clone(); let mut new_path = path
let mut iter = new_path_clone.split(|p| *p == start_i32); .iter()
if iter.next().is_some() { .cloned()
if let Some(upcoming) = iter.next() { .skip_while(|v| *v == start_i32)
new_path = vec![start_i32]; .collect::<Vec<(i32, i32)>>();
new_path.append(&mut upcoming.to_vec()); new_path.retain(|v| *v != start_i32);
} else { **path = new_path;
if let Some(next) = path.first() {
let start = Vec2::new( let start = Vec2::new(
position.position.translation.x, position.position.translation.x,
position.position.translation.y, position.position.translation.y,
); );
let next = path[1];
let next = Vec2::new(next.0 as f32 + 0.5, next.1 as f32 + 0.5); let next = Vec2::new(next.0 as f32 + 0.5, next.1 as f32 + 0.5);
let mut direction = next - start; let mut direction = next - start;
direction = direction.normalize(); direction = direction.normalize();
direction *= **speed; direction *= **speed;
velocity.linvel = direction.into(); velocity.linvel = direction.into();
continue;
}
} else {
println!("Remove path1");
commands.entity(entity).remove::<Path>().remove::<Speed>();
velocity.linvel = Vec2::ZERO.into();
}
**path = new_path;
if path.len() >= 2 {
let start = Vec2::new(
position.position.translation.x,
position.position.translation.y,
);
let next = path[1];
let next = Vec2::new(next.0 as f32 + 0.5, next.1 as f32 + 0.5);
if rotation_speed.is_some() { if rotation_speed.is_some() {
let v = next - start; let v = next - start;
let angle = v.y.atan2(v.x); let angle = v.y.atan2(v.x);
position.position.rotation = UnitComplex::new(angle); position.position.rotation = UnitComplex::new(angle);
} }
let mut direction = next - start; continue;
direction = direction.normalize();
direction *= **speed;
velocity.linvel = direction.into();
} else { } else {
velocity.linvel = Vec2::ZERO.into(); println!("Remove path1");
println!("Remove path2");
commands commands
.entity(entity) .entity(entity)
.remove::<Path>() .remove::<Path>()
.remove::<NoPath>() .remove::<NoPath>()
.remove::<Destination>() .remove::<Destination>()
.remove::<Speed>(); .remove::<Speed>();
velocity.linvel = Vec2::ZERO.into();
} }
} }
} }