Fix off-by-1 errors in mapping.

This commit is contained in:
Nolan Darilek 2025-01-06 20:33:18 -05:00
parent ecec85d37c
commit b524fc39da

View File

@ -87,7 +87,7 @@ pub struct TileBundle {
pub transform: Transform, pub transform: Transform,
pub collider: Collider, pub collider: Collider,
pub rigid_body: RigidBody, pub rigid_body: RigidBody,
pub map_obstruction: Obstacle, pub obstacle: Obstacle,
} }
impl Default for TileBundle { impl Default for TileBundle {
@ -96,7 +96,7 @@ impl Default for TileBundle {
transform: default(), transform: default(),
collider: Collider::rectangle(1., 1.), collider: Collider::rectangle(1., 1.),
rigid_body: RigidBody::Static, rigid_body: RigidBody::Static,
map_obstruction: default(), obstacle: default(),
} }
} }
} }
@ -104,7 +104,7 @@ impl Default for TileBundle {
impl TileBundle { impl TileBundle {
pub fn new(x: i32, y: i32) -> Self { pub fn new(x: i32, y: i32) -> Self {
Self { Self {
transform: Transform::from_xyz(x as f32 + 0.5, y as f32 + 0.5, 0.).into(), transform: Transform::from_xyz(x as f32 + 0.5, y as f32 + 0.5, 0.),
..default() ..default()
} }
} }
@ -172,21 +172,24 @@ impl<D: Clone + Default> MapFilter<D> for GridBuilder {
let half_height = self.room_height / 2; let half_height = self.room_height / 2;
for y in 0..self.height_in_rooms { for y in 0..self.height_in_rooms {
for x in 0..self.width_in_rooms { for x in 0..self.width_in_rooms {
// println!("({x}, {y}): ");
let x_offset = x * (self.room_width + 1); let x_offset = x * (self.room_width + 1);
let y_offset = total_height - (y + 1) * (self.room_height + 1) - 1; let y_offset = total_height - (y + 1) * (self.room_height + 1) - 1;
// println!("Offsets: {x_offset}, {y_offset}");
let room = MRect::new_i32( let room = MRect::new_i32(
x_offset as i32 + 1, x_offset as i32 + 1,
y_offset as i32 + 1, y_offset as i32 + 1,
self.room_width as i32, self.room_width as i32,
self.room_height as i32, self.room_height as i32,
); );
// println!("{room:?}");
map.add_room(room); map.add_room(room);
let coords = maze_generator::prelude::Coordinates::new(x as i32, y as i32); let coords = maze_generator::prelude::Coordinates::new(x as i32, y as i32);
if let Some(field) = maze.get_field(&coords) { if let Some(field) = maze.get_field(&coords) {
use maze_generator::prelude::Direction::*; use maze_generator::prelude::Direction::*;
if field.has_passage(&North) { if field.has_passage(&North) {
let x = x_offset + half_width; let x = x_offset + half_width;
let y = y_offset + self.room_height; let y = y_offset + self.room_height + 1;
map.set_tile(x, y, Tile::floor()); map.set_tile(x, y, Tile::floor());
} }
if field.has_passage(&South) { if field.has_passage(&South) {
@ -195,7 +198,7 @@ impl<D: Clone + Default> MapFilter<D> for GridBuilder {
map.set_tile(x, y, Tile::floor()); map.set_tile(x, y, Tile::floor());
} }
if field.has_passage(&East) { if field.has_passage(&East) {
let x = x_offset + self.room_width; let x = x_offset + self.room_width + 1;
let y = y_offset + half_height; let y = y_offset + half_height;
map.set_tile(x, y, Tile::floor()); map.set_tile(x, y, Tile::floor());
} }