Collision on Y axis seems to work
This commit is contained in:
+53
-7
@@ -1,10 +1,22 @@
|
||||
use sfml::system::Vector2;
|
||||
use sfml::graphics::IntRect;
|
||||
use sfml::graphics::{
|
||||
IntRect,
|
||||
FloatRect
|
||||
};
|
||||
|
||||
use engine::texture::Texture;
|
||||
use engine::renderer::Drawable;
|
||||
use engine::math::Matrix;
|
||||
|
||||
/// This enum represents if the collision happens on the X axis or the Y axis.
|
||||
pub enum CollisionAxis {
|
||||
/// The X axis.
|
||||
X,
|
||||
|
||||
/// The Y axis.
|
||||
Y,
|
||||
}
|
||||
|
||||
/// This struct represents the different sides from which a collision can occur.
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub struct CollisionTile {
|
||||
@@ -234,10 +246,6 @@ impl Drawable for PositionedTile {
|
||||
fn position(&self) -> Vector2<f32> {
|
||||
self.position.into()
|
||||
}
|
||||
|
||||
fn origin(&self) -> Vector2<f32> {
|
||||
Vector2::new(0.0, 0.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// The map represents the tiles contained in a level.
|
||||
@@ -351,10 +359,48 @@ impl Map {
|
||||
self.tiles.cols()
|
||||
}
|
||||
|
||||
/// Checks whether the bounding box collides with an element of the map
|
||||
pub fn collides_bbox(&self, old: FloatRect, new: FloatRect) -> Option<(CollisionAxis, Vector2<f32>)> {
|
||||
// Top left corner
|
||||
if let Some((axis, collision)) = self.collides_point(
|
||||
Vector2::new(old.left, old.top),
|
||||
Vector2::new(new.left, new.top),
|
||||
) {
|
||||
return Some((axis, collision));
|
||||
}
|
||||
|
||||
// Top right corner
|
||||
if let Some((axis, collision)) = self.collides_point(
|
||||
Vector2::new(old.left + old.width, old.top),
|
||||
Vector2::new(new.left + new.width, new.top),
|
||||
) {
|
||||
return Some((axis, collision - Vector2::new(new.width, 0.0)));
|
||||
}
|
||||
|
||||
// Bottom left corner
|
||||
if let Some((axis, collision)) = self.collides_point(
|
||||
Vector2::new(old.left, old.top + old.height),
|
||||
Vector2::new(new.left, new.top + new.height),
|
||||
) {
|
||||
return Some((axis, collision - Vector2::new(0.0, new.height)));
|
||||
}
|
||||
|
||||
// Bottom right corner
|
||||
if let Some((axis, collision)) = self.collides_point(
|
||||
Vector2::new(old.left + old.width, old.top + old.height),
|
||||
Vector2::new(new.left + new.width, new.top + new.height),
|
||||
) {
|
||||
return Some((axis, collision - Vector2::new(new.width, new.height)));
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Checks whether the vector (old, new) collides with an element of the map.
|
||||
///
|
||||
/// Returns the height of the collision if any.
|
||||
pub fn collides(&self, old: Vector2<f32>, new: Vector2<f32>) -> Option<f32> {
|
||||
pub fn collides_point(&self, old: Vector2<f32>, new: Vector2<f32>)
|
||||
-> Option<(CollisionAxis, Vector2<f32>)> {
|
||||
|
||||
let height = new.y - old.y;
|
||||
let mut y = (old.y / 16.0).ceil() * 16.0;
|
||||
@@ -372,7 +418,7 @@ impl Map {
|
||||
|
||||
if let Some((tile, _)) = self.tiles.get((row, col)) {
|
||||
if tile.from_top {
|
||||
return Some(y);
|
||||
return Some((CollisionAxis::Y, Vector2::new(x, y)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user