refactor board resizing in negative directions, fixing broken execution in negative zones
This commit is contained in:
parent
1b74f9c996
commit
355a5af15e
2 changed files with 83 additions and 89 deletions
|
@ -55,8 +55,6 @@ pub struct Board {
|
|||
rows: Vec<Vec<Tile>>,
|
||||
width: usize,
|
||||
height: usize,
|
||||
offset_x: isize,
|
||||
offset_y: isize,
|
||||
}
|
||||
|
||||
impl Board {
|
||||
|
@ -96,8 +94,6 @@ impl Board {
|
|||
rows,
|
||||
width,
|
||||
height,
|
||||
offset_x: 0,
|
||||
offset_y: 0,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -106,8 +102,6 @@ impl Board {
|
|||
width: rows[0].len(),
|
||||
height: rows.len(),
|
||||
rows,
|
||||
offset_x: 0,
|
||||
offset_y: 0,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -124,15 +118,7 @@ impl Board {
|
|||
sum
|
||||
}
|
||||
|
||||
fn transform(&self, p: Pos) -> Pos {
|
||||
Pos {
|
||||
x: p.x + self.offset_x,
|
||||
y: p.y + self.offset_y,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pos_in_bounds(&self, p: Pos) -> bool {
|
||||
let p = self.transform(p);
|
||||
self.in_bounds(p)
|
||||
}
|
||||
|
||||
|
@ -141,7 +127,6 @@ impl Board {
|
|||
}
|
||||
|
||||
pub fn get(&self, p: Pos) -> Option<Tile> {
|
||||
let p = self.transform(p);
|
||||
if self.in_bounds(p) {
|
||||
Some(self.rows[p.y as usize][p.x as usize])
|
||||
} else {
|
||||
|
@ -150,7 +135,6 @@ impl Board {
|
|||
}
|
||||
|
||||
pub fn get_or_blank(&self, p: Pos) -> Tile {
|
||||
let p = self.transform(p);
|
||||
if self.in_bounds(p) {
|
||||
self.rows[p.y as usize][p.x as usize]
|
||||
} else {
|
||||
|
@ -159,7 +143,6 @@ impl Board {
|
|||
}
|
||||
|
||||
pub fn get_mut(&mut self, p: Pos) -> Option<&mut Tile> {
|
||||
let p = self.transform(p);
|
||||
if self.in_bounds(p) {
|
||||
Some(&mut self.rows[p.y as usize][p.x as usize])
|
||||
} else {
|
||||
|
@ -168,7 +151,6 @@ impl Board {
|
|||
}
|
||||
|
||||
pub fn get_blank_mut(&mut self, p: Pos) -> Option<&mut Tile> {
|
||||
let p = self.transform(p);
|
||||
if self.in_bounds(p) {
|
||||
let tile = &mut self.rows[p.y as usize][p.x as usize];
|
||||
if tile == &Tile::Blank {
|
||||
|
@ -179,25 +161,25 @@ impl Board {
|
|||
}
|
||||
|
||||
pub fn set(&mut self, p: Pos, tile: Tile) {
|
||||
let p = self.transform(p);
|
||||
if self.in_bounds(p) {
|
||||
self.rows[p.y as usize][p.x as usize] = tile;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn grow_to_include(&mut self, p: Pos) {
|
||||
let p = self.transform(p);
|
||||
pub fn grow_to_include(&mut self, p: Pos) -> (isize, isize) {
|
||||
let mut offset_x = 0;
|
||||
let mut offset_y = 0;
|
||||
if p.x < 0 {
|
||||
let len = p.x.unsigned_abs();
|
||||
let len = p.x.unsigned_abs() + 2;
|
||||
for row in &mut self.rows {
|
||||
let mut new_row = vec![Tile::Blank; len];
|
||||
new_row.append(row);
|
||||
*row = new_row;
|
||||
}
|
||||
self.offset_x += len as isize;
|
||||
offset_x = len;
|
||||
self.width += len;
|
||||
} else if p.x as usize >= self.width {
|
||||
let new_width = p.x as usize + 1;
|
||||
let new_width = p.x as usize + 3;
|
||||
for row in &mut self.rows {
|
||||
row.resize(new_width, Tile::Blank);
|
||||
}
|
||||
|
@ -205,20 +187,22 @@ impl Board {
|
|||
}
|
||||
|
||||
if p.y < 0 {
|
||||
let len = p.y.unsigned_abs();
|
||||
let len = p.y.unsigned_abs() + 2;
|
||||
let mut new_rows = vec![vec![Tile::Blank; self.width]; len];
|
||||
new_rows.append(&mut self.rows);
|
||||
self.rows = new_rows;
|
||||
self.offset_y += len as isize;
|
||||
offset_y = len;
|
||||
self.height += len;
|
||||
} else if p.y as usize >= self.height {
|
||||
let new_height = p.y as usize + 1;
|
||||
let new_height = p.y as usize + 3;
|
||||
self.rows.resize(new_height, vec![Tile::Blank; self.width]);
|
||||
self.height = new_height;
|
||||
}
|
||||
(offset_x as isize, offset_y as isize)
|
||||
}
|
||||
|
||||
pub fn trim_size(&mut self) {
|
||||
pub fn trim_size(&mut self) -> (usize, usize) {
|
||||
let (offset_x, offset_y);
|
||||
// top
|
||||
{
|
||||
let mut n = 0;
|
||||
|
@ -229,7 +213,7 @@ impl Board {
|
|||
for _ in 0..trim_top {
|
||||
self.rows.remove(0);
|
||||
}
|
||||
self.offset_y -= trim_top as isize;
|
||||
offset_y = trim_top;
|
||||
self.height -= trim_top;
|
||||
}
|
||||
// bottom
|
||||
|
@ -256,7 +240,7 @@ impl Board {
|
|||
row.remove(0);
|
||||
}
|
||||
}
|
||||
self.offset_x -= trim_left as isize;
|
||||
offset_x = trim_left;
|
||||
self.width -= trim_left;
|
||||
}
|
||||
// right
|
||||
|
@ -273,6 +257,7 @@ impl Board {
|
|||
}
|
||||
self.width -= trim_right;
|
||||
}
|
||||
(offset_x, offset_y)
|
||||
}
|
||||
|
||||
pub fn width(&self) -> usize {
|
||||
|
@ -288,10 +273,7 @@ impl Board {
|
|||
for y in 0..self.height {
|
||||
for x in 0..self.width {
|
||||
if let Tile::Marble { value: _, dir: _ } = self.rows[y][x] {
|
||||
out.push(Pos {
|
||||
x: x as isize - self.offset_x,
|
||||
y: y as isize - self.offset_y,
|
||||
});
|
||||
out.push((x, y).into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -308,8 +290,8 @@ impl Board {
|
|||
|
||||
for x in start_x..(start_x + tile_width) {
|
||||
for y in start_y..(start_y + tile_height) {
|
||||
let tx = (x as isize + self.offset_x) as usize;
|
||||
let ty = (y as isize + self.offset_y) as usize;
|
||||
let tx = x as usize;
|
||||
let ty = y as usize;
|
||||
let px = x * tile_size + offset.x as i32;
|
||||
let py = y * tile_size + offset.y as i32;
|
||||
if self.in_bounds((tx, ty).into()) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue