Fix many errors tracing colliders in maps.

This commit is contained in:
Nolan Darilek 2022-02-14 22:51:15 -06:00
parent 6c97ff6be4
commit ea0b04da53

View File

@ -259,86 +259,74 @@ fn spawn_colliders(
}
}
} else {
let mut has_body = vec![vec![false; map.height as usize]; map.width as usize];
let mut has_body =
vec![vec![false; map.height as usize + 1]; map.width as usize + 1];
let mut bottom_left = None;
let mut bottom_right = None;
for y in 0..map.height {
for x in 0..map.width {
// println!("Checking ({}, {})", x, y);
trace!("Checking ({x}, {y})");
if bottom_left.is_some() {
if has_body[x][y] {
// println!("Hit another body, setting bottom right");
trace!("Hit another body, setting bottom right");
bottom_right = Some((x, y));
} else if map.at(x, y).is_walkable() {
/*println!(
"Hit an empty tile, setting bottom right to ({}, {})",
x - 1,
y
);*/
bottom_right = Some((x - 1, y));
trace!("Hit an empty tile, setting bottom right to ({x}, {y})",);
bottom_right = Some((x, y));
}
}
if map.at(x, y).is_blocked() && !has_body[x][y] {
//println!("Blocked, setting has_body");
trace!("Blocked, setting has_body");
has_body[x][y] = true;
if bottom_left.is_none() {
//println!("Setting bottom left");
trace!("Setting bottom left");
bottom_left = Some((x, y));
}
if bottom_left.is_some() && x == map.width - 1 {
//println!("Hit right edge, setting bottom right");
trace!("Hit right edge, setting bottom right");
bottom_right = Some((x, y));
}
}
if let (Some(bl), Some(br)) = (bottom_left, bottom_right) {
//println!("Got bottom, checking if can extend up");
let mut top_left = bl;
let mut top_right = br;
trace!("Got bottom, checking if can extend up");
let mut top_left = (bl.0, bl.1 + 1);
let mut top_right = (br.0, br.1 + 1);
if y != map.height - 1 {
let mut can_extend_up = true;
for y in bl.1 + 1..map.height {
for x in bl.0..=br.0 {
//println!("Extension check: ({}, {})", x, y);
for x in bl.0..br.0 {
trace!("Extension check: ({x}, {y})");
if map.at(x, y).is_walkable() {
//println!("Can't, empty tile");
trace!("Can't, empty tile");
can_extend_up = false;
break;
}
}
if can_extend_up {
//println!("Can extend up, setting has_body");
top_left.1 += 1;
top_right.1 += 1;
trace!("Can extend up, setting has_body");
for x in top_left.0..=top_right.0 {
has_body[x][top_left.1] = true;
}
top_left.1 += 1;
top_right.1 += 1;
} else {
break;
}
}
}
let mut width = br.0 as f32 - bl.0 as f32;
if width == 0. {
width = 1.;
}
let width = br.0 as f32 - bl.0 as f32;
let half_width = width / 2.;
let mut height = top_left.1 as f32 - bl.1 as f32;
if height == 0. {
height = 1.;
}
let height = top_left.1 as f32 - bl.1 as f32;
let half_height = height / 2.;
/*println!(
trace!(
"Top left: {:?}\ntop right: {:?}\nbottom left: {:?}\nbottom right: {:?}",
top_left, top_right, bl, br
);*/
let center = (
bl.0 as f32 + half_width + 0.5,
br.1 as f32 + half_height + 0.5,
);
/*println!(
let center = (bl.0 as f32 + half_width, br.1 as f32 + half_height);
trace!(
"Create shape at {:?} of width {:?} and height {:?}",
center, width, height
);*/
);
let id = commands
.spawn_bundle(ColliderBundle {
shape: ColliderShape::cuboid(half_width, half_height).into(),