Clean up and fixes for collision handling.

This commit is contained in:
2015-11-17 09:02:09 +01:00
parent 2ef29c4021
commit 3749e8d101
2 changed files with 14 additions and 21 deletions

View File

@@ -269,7 +269,7 @@ fn main() {
if let Some(intersect) = layer.find_intersecting(&player.to_rect()) {
if player.dx > 0.0 {
for y in intersect.y()..intersect.height() as i32 + 1 {
for y in intersect.y()..(intersect.y() + intersect.height() as i32) {
let mut x = intersect.x();
loop {
@@ -297,7 +297,7 @@ fn main() {
player.dx = 0.0;
}
} else if player.dx < 0.0 {
for y in intersect.y()..intersect.height() as i32 + 1 {
for y in intersect.y()..(intersect.y() + intersect.height() as i32) {
let mut x = intersect.x();
loop {
@@ -327,7 +327,7 @@ fn main() {
}
if player.dy > 0.0 {
for x in intersect.x()..intersect.width() as i32 + 1 {
for x in intersect.x()..(intersect.x() + intersect.width() as i32) {
let mut y = intersect.y();
loop {
@@ -356,7 +356,7 @@ fn main() {
player.on_ground = true;
}
} else if player.dy < 0.0 {
for x in intersect.x()..intersect.width() as i32 + 1 {
for x in intersect.x()..(intersect.x() + intersect.width() as i32) {
let mut y = intersect.y();
loop {
@@ -404,10 +404,10 @@ fn main() {
match *tile {
Tile::Background(src) => {
let _ = renderer.copy(&world_sprites, Some(src), Some(object));
renderer.copy(&world_sprites, Some(src), Some(object));
},
Tile::Floor(src) => {
let _ = renderer.copy(&world_sprites, Some(src), Some(object));
renderer.copy(&world_sprites, Some(src), Some(object));
},
Tile::Static(ref sprite, _) => sprite.render(&mut renderer, &object),
_ => ()

View File

@@ -40,33 +40,26 @@ impl<T> Layer<T> where T: Clone {
}
pub fn find_intersecting(&self, rect: &Rect) -> Option<Rect> {
if rect.x() + rect.width() as i32 <= 0 {
if rect.x() + rect.width() as i32 <= 0 || rect.x() >= (self.width * self.tile_width) as i32 {
return None;
}
if rect.y() + rect.height() as i32 <= 0 {
if rect.y() + rect.height() as i32 <= 0 || rect.y() >= (self.height * self.tile_height) as i32 {
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
}
else if y1 < 0 || y2 >= self.height as i32 {
None
}
else {
Some(Rect::new_unwrap(x1, y1, (x2 - x1 + 1) as u32, (y2 - y1 + 1) as u32))
}
let x2 = (rect.x() + rect.width() as i32 - 1) / self.tile_width as i32;
let y2 = (rect.y() + rect.height() as i32 - 1) / self.tile_height as i32;
Some(Rect::new_unwrap(x1, y1, (x2 - x1 + 1) as u32, (y2 - y1 + 1) as u32))
}
pub fn for_each_intersecting<F: FnMut(&T, &Rect)>(&self, rect: &Rect, mut f: F) {
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 {
for y in intersect.y()..(intersect.y() + intersect.height() as i32) {
for x in intersect.x()..(intersect.x() + intersect.width() as i32) {
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);