From 8b4ca56a2ff02f2267c6c81241ea7308083d6cd7 Mon Sep 17 00:00:00 2001 From: logaritmisk Date: Mon, 16 Nov 2015 21:17:35 +0100 Subject: [PATCH] Add tests for Layer::find_intersecting. --- src/main.rs | 18 +++++------ src/tile.rs | 90 ++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 88 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index fbb2f92..cb58e61 100644 --- a/src/main.rs +++ b/src/main.rs @@ -267,10 +267,10 @@ fn main() { let py = player.y + player.h as f32; let mut dy = player.dy; - if let Some((a, b)) = layer.find_intersecting(&player.to_rect()) { + if let Some(intersect) = layer.find_intersecting(&player.to_rect()) { if player.dx > 0.0 { - for y in a.y()..b.y() + 1 { - let mut x = a.x(); + for y in intersect.y()..intersect.height() as i32 + 1 { + let mut x = intersect.x(); loop { let t = (x * TILE_WIDTH as i32) as f32 - px; @@ -297,8 +297,8 @@ fn main() { player.dx = 0.0; } } else if player.dx < 0.0 { - for y in a.y()..b.y() + 1 { - let mut x = a.x(); + for y in intersect.y()..intersect.height() as i32 + 1 { + let mut x = intersect.x(); loop { let t = (x * TILE_WIDTH as i32 + TILE_WIDTH as i32) as f32 - px; @@ -327,8 +327,8 @@ fn main() { } if player.dy > 0.0 { - for x in a.x()..b.x() + 1 { - let mut y = a.y(); + for x in intersect.x()..intersect.width() as i32 + 1 { + let mut y = intersect.y(); loop { let t = (y * TILE_HEIGHT as i32) as f32 - py; @@ -356,8 +356,8 @@ fn main() { player.on_ground = true; } } else if player.dy < 0.0 { - for x in a.x()..b.x() + 1 { - let mut y = a.y(); + for x in intersect.x()..intersect.width() as i32 + 1 { + let mut y = intersect.y(); loop { let t = (y * TILE_HEIGHT as i32 + TILE_HEIGHT as i32) as f32 - py; diff --git a/src/tile.rs b/src/tile.rs index 3618a4f..0770a06 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -1,7 +1,7 @@ use std::iter::repeat; -use std::cmp::{min, max}; +//use std::cmp::{min, max}; -use sdl2::rect::{Rect, Point}; +use sdl2::rect::Rect; pub struct Layer { @@ -39,11 +39,18 @@ impl Layer where T: Clone { self.tiles[offset] = tile; } - pub fn find_intersecting(&self, rect: &Rect) -> Option<(Point, Point)> { - let x1 = max(rect.x() / self.tile_width as i32, 0); - let y1 = max(rect.y() / self.tile_height as i32, 0); - let x2 = min((rect.x() + rect.width() as i32 - 1) / self.tile_width as i32, self.width as i32 - 1); - let y2 = min((rect.y() + rect.height() as i32 - 1) / self.tile_height as i32, self.height as i32 - 1); + pub fn find_intersecting(&self, rect: &Rect) -> Option { + if rect.x() + rect.width() as i32 <= 0 { + return None; + } + if rect.y() + rect.height() as i32 <= 0 { + return None; + } + + let x1 = rect.x() / self.tile_width as i32; + let y1 = rect.y() / self.tile_height as i32; + let x2 = (rect.x() + rect.width() as i32) / self.tile_width as i32; + let y2 = (rect.y() + rect.height() as i32) / self.tile_height as i32; if x1 < 0 || x2 >= self.width as i32 { None @@ -52,14 +59,14 @@ impl Layer where T: Clone { None } else { - Some((Point::new(x1, y1), Point::new(x2, y2))) + Some(Rect::new_unwrap(x1, y1, (x2 - x1 + 1) as u32, (y2 - y1 + 1) as u32)) } } pub fn for_each_intersecting(&self, rect: &Rect, mut f: F) { - if let Some((a, b)) = self.find_intersecting(rect) { - for y in a.y()..b.y() + 1 { - for x in a.x()..b.x() + 1 { + if let Some(intersect) = self.find_intersecting(rect) { + for y in intersect.y()..intersect.height() as i32 + 1 { + for x in intersect.x()..intersect.x() as i32 + 1 { let position = Rect::new_unwrap(x * self.tile_width as i32, y * self.tile_height as i32, self.tile_width, self.tile_height); f(self.get_tile(x, y).unwrap(), &position); @@ -72,3 +79,64 @@ impl Layer where T: Clone { Rect::new_unwrap(0, 0, self.width * self.tile_width, self.height * self.tile_height) } } + + +#[cfg(test)] +mod tests { + use super::Layer; + + use sdl2::rect::Rect; + + #[test] + fn layer_find_intersecting() { + let layer = Layer::new(3, 3, 3, 3, ()); + + // out of bounds. + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(-1, 1, 1, 1)), None); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap( 1, -1, 1, 1)), None); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(-1, -1, 1, 1)), None); + + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(9, 7, 1, 1)), None); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(7, 9, 1, 1)), None); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(9, 9, 1, 1)), None); + + // middle of tile. + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(1, 1, 1, 1)), Some(Rect::new_unwrap(0, 0, 1, 1))); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(4, 1, 1, 1)), Some(Rect::new_unwrap(1, 0, 1, 1))); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(7, 1, 1, 1)), Some(Rect::new_unwrap(2, 0, 1, 1))); + + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(1, 4, 1, 1)), Some(Rect::new_unwrap(0, 1, 1, 1))); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(4, 4, 1, 1)), Some(Rect::new_unwrap(1, 1, 1, 1))); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(7, 4, 1, 1)), Some(Rect::new_unwrap(2, 1, 1, 1))); + + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(1, 7, 1, 1)), Some(Rect::new_unwrap(0, 2, 1, 1))); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(4, 7, 1, 1)), Some(Rect::new_unwrap(1, 2, 1, 1))); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(7, 7, 1, 1)), Some(Rect::new_unwrap(2, 2, 1, 1))); + + // interlaps 4 tiles. + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(2, 2, 2, 2)), Some(Rect::new_unwrap(0, 0, 2, 2))); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(5, 2, 2, 2)), Some(Rect::new_unwrap(1, 0, 2, 2))); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(2, 5, 2, 2)), Some(Rect::new_unwrap(0, 1, 2, 2))); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(5, 5, 2, 2)), Some(Rect::new_unwrap(1, 1, 2, 2))); + + // interlaps 2 tiles horizontal. + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(2, 1, 2, 1)), Some(Rect::new_unwrap(0, 0, 2, 1))); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(5, 1, 2, 1)), Some(Rect::new_unwrap(1, 0, 2, 1))); + + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(2, 4, 2, 1)), Some(Rect::new_unwrap(0, 1, 2, 1))); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(5, 4, 2, 1)), Some(Rect::new_unwrap(1, 1, 2, 1))); + + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(2, 7, 2, 1)), Some(Rect::new_unwrap(0, 2, 2, 1))); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(5, 7, 2, 1)), Some(Rect::new_unwrap(1, 2, 2, 1))); + + // interlaps 2 tiles vertical. + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(1, 2, 1, 2)), Some(Rect::new_unwrap(0, 0, 1, 2))); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(1, 5, 1, 2)), Some(Rect::new_unwrap(0, 1, 1, 2))); + + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(4, 2, 1, 2)), Some(Rect::new_unwrap(1, 0, 1, 2))); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(4, 5, 1, 2)), Some(Rect::new_unwrap(1, 1, 1, 2))); + + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(7, 2, 1, 2)), Some(Rect::new_unwrap(2, 0, 1, 2))); + assert_eq!(layer.find_intersecting(&Rect::new_unwrap(7, 5, 1, 2)), Some(Rect::new_unwrap(2, 1, 1, 2))); + } +}