Clean up and fixes for collision handling.
This commit is contained in:
12
src/main.rs
12
src/main.rs
@@ -269,7 +269,7 @@ fn main() {
|
|||||||
|
|
||||||
if let Some(intersect) = layer.find_intersecting(&player.to_rect()) {
|
if let Some(intersect) = layer.find_intersecting(&player.to_rect()) {
|
||||||
if player.dx > 0.0 {
|
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();
|
let mut x = intersect.x();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@@ -297,7 +297,7 @@ fn main() {
|
|||||||
player.dx = 0.0;
|
player.dx = 0.0;
|
||||||
}
|
}
|
||||||
} else if 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();
|
let mut x = intersect.x();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@@ -327,7 +327,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if player.dy > 0.0 {
|
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();
|
let mut y = intersect.y();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@@ -356,7 +356,7 @@ fn main() {
|
|||||||
player.on_ground = true;
|
player.on_ground = true;
|
||||||
}
|
}
|
||||||
} else if player.dy < 0.0 {
|
} 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();
|
let mut y = intersect.y();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@@ -404,10 +404,10 @@ fn main() {
|
|||||||
|
|
||||||
match *tile {
|
match *tile {
|
||||||
Tile::Background(src) => {
|
Tile::Background(src) => {
|
||||||
let _ = renderer.copy(&world_sprites, Some(src), Some(object));
|
renderer.copy(&world_sprites, Some(src), Some(object));
|
||||||
},
|
},
|
||||||
Tile::Floor(src) => {
|
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),
|
Tile::Static(ref sprite, _) => sprite.render(&mut renderer, &object),
|
||||||
_ => ()
|
_ => ()
|
||||||
|
|||||||
23
src/tile.rs
23
src/tile.rs
@@ -40,33 +40,26 @@ impl<T> Layer<T> where T: Clone {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_intersecting(&self, rect: &Rect) -> Option<Rect> {
|
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;
|
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;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let x1 = rect.x() / self.tile_width as i32;
|
let x1 = rect.x() / self.tile_width as i32;
|
||||||
let y1 = rect.y() / self.tile_height 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 {
|
let x2 = (rect.x() + rect.width() as i32 - 1) / self.tile_width as i32;
|
||||||
None
|
let y2 = (rect.y() + rect.height() as i32 - 1) / self.tile_height as i32;
|
||||||
}
|
|
||||||
else if y1 < 0 || y2 >= self.height as i32 {
|
Some(Rect::new_unwrap(x1, y1, (x2 - x1 + 1) as u32, (y2 - y1 + 1) as u32))
|
||||||
None
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
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) {
|
pub fn for_each_intersecting<F: FnMut(&T, &Rect)>(&self, rect: &Rect, mut f: F) {
|
||||||
if let Some(intersect) = self.find_intersecting(rect) {
|
if let Some(intersect) = self.find_intersecting(rect) {
|
||||||
for y in intersect.y()..intersect.height() as i32 + 1 {
|
for y in intersect.y()..(intersect.y() + intersect.height() as i32) {
|
||||||
for x in intersect.x()..intersect.x() as i32 + 1 {
|
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);
|
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);
|
f(self.get_tile(x, y).unwrap(), &position);
|
||||||
|
|||||||
Reference in New Issue
Block a user