Almost! Just need to fix the collision handling.
This commit is contained in:
122
src/main.rs
122
src/main.rs
@@ -184,13 +184,13 @@ fn main() {
|
|||||||
|
|
||||||
let mut current : u64;
|
let mut current : u64;
|
||||||
let mut elapsed : u64;
|
let mut elapsed : u64;
|
||||||
let mut previous : u64 = time::precise_time_ns();
|
let mut previous : u64 = time::precise_time_ns() / 1_000_000;
|
||||||
let mut lag : u64 = 0;
|
let mut lag : u64 = 0;
|
||||||
|
|
||||||
let mut event_pump = sdl_context.event_pump().unwrap();
|
let mut event_pump = sdl_context.event_pump().unwrap();
|
||||||
|
|
||||||
'main : loop {
|
'main : loop {
|
||||||
current = time::precise_time_ns();
|
current = time::precise_time_ns() / 1_000_000;
|
||||||
elapsed = current - previous;
|
elapsed = current - previous;
|
||||||
previous = current;
|
previous = current;
|
||||||
lag += elapsed;
|
lag += elapsed;
|
||||||
@@ -261,26 +261,29 @@ fn main() {
|
|||||||
|
|
||||||
player.on_ground = false;
|
player.on_ground = false;
|
||||||
|
|
||||||
if let Some(intersect) = layer.find_intersecting(&player.to_rect()) {
|
let px = player.x + player.w as f32;
|
||||||
if player.dx > 0.0 {
|
let mut dx = player.dx;
|
||||||
let p = player.x + player.w as f32;
|
|
||||||
let mut d = player.dx;
|
|
||||||
|
|
||||||
for y in intersect.y()..intersect.y() + intersect.height() as i32 + 1 {
|
let py = player.y + player.h as f32;
|
||||||
let mut x = intersect.x();
|
let mut dy = player.dy;
|
||||||
|
|
||||||
|
if let Some((a, b)) = layer.find_intersecting(&player.to_rect()) {
|
||||||
|
if player.dx > 0.0 {
|
||||||
|
for y in a.y()..b.y() + 1 {
|
||||||
|
let mut x = a.x();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let t = (x * TILE_WIDTH as i32) as f32 - p;
|
let t = (x * TILE_WIDTH as i32) as f32 - px;
|
||||||
|
|
||||||
if t > d {
|
if t > dx {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(tile) = layer.get_tile(x, y) {
|
if let Some(tile) = layer.get_tile(x, y) {
|
||||||
d = match *tile {
|
dx = match *tile {
|
||||||
Tile::Floor(_) => d.min(t),
|
Tile::Floor(_) => dx.min(t),
|
||||||
Tile::Static(_, solid) => if solid { d.min(t) } else { d },
|
Tile::Static(_, solid) => if solid { dx.min(t) } else { dx },
|
||||||
_ => d
|
_ => dx
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
@@ -290,33 +293,25 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if d > 0.0 {
|
if dx <= 0.0 {
|
||||||
player.x += d;
|
|
||||||
} else if d < 0.0 {
|
|
||||||
player.x += d;
|
|
||||||
player.dx = 0.0;
|
|
||||||
} else {
|
|
||||||
player.dx = 0.0;
|
player.dx = 0.0;
|
||||||
}
|
}
|
||||||
} else if player.dx < 0.0 {
|
} else if player.dx < 0.0 {
|
||||||
let p = player.x;
|
for y in a.y()..b.y() + 1 {
|
||||||
let mut d = player.dx;
|
let mut x = a.x();
|
||||||
|
|
||||||
for y in intersect.y()..intersect.y() + intersect.height() as i32 + 1 {
|
|
||||||
let mut x = intersect.x();
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let t = (x * TILE_WIDTH as i32 + TILE_WIDTH as i32) as f32 - p;
|
let t = (x * TILE_WIDTH as i32 + TILE_WIDTH as i32) as f32 - px;
|
||||||
|
|
||||||
if t < d {
|
if t < dx {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(tile) = layer.get_tile(x, y) {
|
if let Some(tile) = layer.get_tile(x, y) {
|
||||||
d = match *tile {
|
dx = match *tile {
|
||||||
Tile::Floor(_) => d.max(t),
|
Tile::Floor(_) => dx.max(t),
|
||||||
Tile::Static(_, solid) => if solid { d.max(t) } else { d },
|
Tile::Static(_, solid) => if solid { dx.max(t) } else { dx },
|
||||||
_ => d
|
_ => dx
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
@@ -326,35 +321,27 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if d < 0.0 {
|
if dx >= 0.0 {
|
||||||
player.x += d;
|
|
||||||
} else if d > 0.0 {
|
|
||||||
player.x += d;
|
|
||||||
player.dx = 0.0;
|
|
||||||
} else {
|
|
||||||
player.dx = 0.0;
|
player.dx = 0.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if player.dy > 0.0 {
|
if player.dy > 0.0 {
|
||||||
let p = player.y + player.h as f32;
|
for x in a.x()..b.x() + 1 {
|
||||||
let mut d = player.dy;
|
let mut y = a.y();
|
||||||
|
|
||||||
for x in intersect.x()..intersect.x() + intersect.width() as i32 + 1 {
|
|
||||||
let mut y = intersect.y();
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let t = (y * TILE_HEIGHT as i32) as f32 - p;
|
let t = (y * TILE_HEIGHT as i32) as f32 - py;
|
||||||
|
|
||||||
if t > d {
|
if t > dy {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(tile) = layer.get_tile(x, y) {
|
if let Some(tile) = layer.get_tile(x, y) {
|
||||||
d = match *tile {
|
dy = match *tile {
|
||||||
Tile::Floor(_) => d.min(t),
|
Tile::Floor(_) => dy.min(t),
|
||||||
Tile::Static(_, solid) => if solid { d.min(t) } else { d },
|
Tile::Static(_, solid) => if solid { dy.min(t) } else { dy },
|
||||||
_ => d
|
_ => dy
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
@@ -364,37 +351,26 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if d > 0.0 {
|
if dy <= 0.0 {
|
||||||
player.y += d;
|
|
||||||
} else if d < 0.0 {
|
|
||||||
player.y += d;
|
|
||||||
player.dy = 0.0;
|
player.dy = 0.0;
|
||||||
|
|
||||||
player.on_ground = true;
|
|
||||||
} else {
|
|
||||||
player.dy = 0.0;
|
|
||||||
|
|
||||||
player.on_ground = true;
|
player.on_ground = true;
|
||||||
}
|
}
|
||||||
} else if player.dy < 0.0 {
|
} else if player.dy < 0.0 {
|
||||||
let p = player.y;
|
for x in a.x()..b.x() + 1 {
|
||||||
let mut d = player.dy;
|
let mut y = a.y();
|
||||||
|
|
||||||
for x in intersect.x()..intersect.x() + intersect.width() as i32 + 1 {
|
|
||||||
let mut y = intersect.y();
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let t = (y * TILE_HEIGHT as i32 + TILE_HEIGHT as i32) as f32 - p;
|
let t = (y * TILE_HEIGHT as i32 + TILE_HEIGHT as i32) as f32 - py;
|
||||||
|
|
||||||
if t < d {
|
if t < dy {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(tile) = layer.get_tile(x, y) {
|
if let Some(tile) = layer.get_tile(x, y) {
|
||||||
d = match *tile {
|
dy = match *tile {
|
||||||
Tile::Floor(_) => d.max(t),
|
Tile::Floor(_) => dy.max(t),
|
||||||
Tile::Static(_, solid) => if solid { d.max(t) } else { d },
|
Tile::Static(_, solid) => if solid { dy.max(t) } else { dy },
|
||||||
_ => d
|
_ => dy
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
@@ -404,17 +380,15 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if d < 0.0 {
|
if dy >= 0.0 {
|
||||||
player.y += d;
|
|
||||||
} else if d > 0.0 {
|
|
||||||
player.y += d;
|
|
||||||
player.dy = 0.0;
|
|
||||||
} else {
|
|
||||||
player.dy = 0.0;
|
player.dy = 0.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
player.x += dx;
|
||||||
|
player.y += dy;
|
||||||
|
|
||||||
player_sprite.update(elapsed);
|
player_sprite.update(elapsed);
|
||||||
|
|
||||||
camera.center(&player.to_rect());
|
camera.center(&player.to_rect());
|
||||||
|
|||||||
15
src/tile.rs
15
src/tile.rs
@@ -1,7 +1,7 @@
|
|||||||
use std::iter::repeat;
|
use std::iter::repeat;
|
||||||
use std::cmp::{min, max};
|
use std::cmp::{min, max};
|
||||||
|
|
||||||
use sdl2::rect::Rect;
|
use sdl2::rect::{Rect, Point};
|
||||||
|
|
||||||
|
|
||||||
pub struct Layer<T> {
|
pub struct Layer<T> {
|
||||||
@@ -39,7 +39,7 @@ impl<T> Layer<T> where T: Clone {
|
|||||||
self.tiles[offset] = tile;
|
self.tiles[offset] = tile;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_intersecting(&self, rect: &Rect) -> Option<Rect> {
|
pub fn find_intersecting(&self, rect: &Rect) -> Option<(Point, Point)> {
|
||||||
let x1 = max(rect.x() / self.tile_width as i32, 0);
|
let x1 = max(rect.x() / self.tile_width as i32, 0);
|
||||||
let y1 = max(rect.y() / self.tile_height 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 x2 = min((rect.x() + rect.width() as i32 - 1) / self.tile_width as i32, self.width as i32 - 1);
|
||||||
@@ -51,18 +51,15 @@ impl<T> Layer<T> where T: Clone {
|
|||||||
else if y1 < 0 || y2 >= self.height as i32 {
|
else if y1 < 0 || y2 >= self.height as i32 {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
else if x2 - x1 <= 0 || y2 - y1 <= 0 {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
Some(Rect::new_unwrap(x1, y1, (x2 - x1) as u32, (y2 - y1) as u32))
|
Some((Point::new(x1, y1), Point::new(x2, y2)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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((a, b)) = self.find_intersecting(rect) {
|
||||||
for y in intersect.y()..intersect.y() + intersect.height() as i32 + 1 {
|
for y in a.y()..b.y() + 1 {
|
||||||
for x in intersect.x()..intersect.x() + intersect.width() as i32 + 1 {
|
for x in a.x()..b.x() + 1 {
|
||||||
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