just check intersecting tiles
This commit is contained in:
33
src/main.rs
33
src/main.rs
@@ -70,7 +70,7 @@ fn main() {
|
||||
};
|
||||
|
||||
let mut camera = Camera::new(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
let mut layer = Layer::new(30, 20, Tile::Empty);
|
||||
let mut layer = Layer::new(30, 20, TILE_WIDTH, TILE_HEIGHT, Tile::Empty);
|
||||
|
||||
for x in range(0, 14) {
|
||||
layer.set_tile(x, 14, Tile::Floor);
|
||||
@@ -104,7 +104,7 @@ fn main() {
|
||||
player.velocity.x = -4.0;
|
||||
} else if key == KeyCode::Up {
|
||||
if player.on_ground {
|
||||
player.velocity.y = -8.0;
|
||||
player.velocity.y = -12.0;
|
||||
player.on_ground = false;
|
||||
}
|
||||
}
|
||||
@@ -115,8 +115,8 @@ fn main() {
|
||||
} else if key == KeyCode::Left {
|
||||
player.velocity.x = 0.0;
|
||||
} else if key == KeyCode::Up {
|
||||
if player.velocity.y < -4.0 {
|
||||
player.velocity.y = -4.0;
|
||||
if player.velocity.y < -6.0 {
|
||||
player.velocity.y = -6.0;
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -128,15 +128,29 @@ fn main() {
|
||||
|
||||
player.on_ground = false;
|
||||
|
||||
for y in range(0, 20) {
|
||||
for x in range(0, 30) {
|
||||
let tiles_intersect = layer.get_intersecting(&player.get_rect());
|
||||
|
||||
let mut skip = false;
|
||||
|
||||
if tiles_intersect.x < 0 || tiles_intersect.x + tiles_intersect.w > 30 - 1 {
|
||||
skip = true;
|
||||
}
|
||||
else if tiles_intersect.y < 0 || tiles_intersect.y + tiles_intersect.h > 20 - 1 {
|
||||
skip = true;
|
||||
}
|
||||
|
||||
if skip == false {
|
||||
for y in range(tiles_intersect.y, tiles_intersect.y + tiles_intersect.h + 1) {
|
||||
for x in range(tiles_intersect.x, tiles_intersect.x + tiles_intersect.w + 1) {
|
||||
let player_rect = player.get_rect();
|
||||
|
||||
match *layer.get_tile(x, y) {
|
||||
Tile::Empty => (),
|
||||
Tile::Floor => {
|
||||
let object = Rect::new(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
|
||||
let object_rect = Rect::new(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
|
||||
|
||||
if collision_detection(&object, &player.get_rect()) {
|
||||
let intersect = collision_intersect(&object, &player.get_rect());
|
||||
if collision_detection(&object_rect, &player_rect) {
|
||||
let intersect = collision_intersect(&object_rect, &player_rect);
|
||||
|
||||
if intersect.w >= intersect.h {
|
||||
let mut delta = intersect.h as f32;
|
||||
@@ -164,6 +178,7 @@ fn main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lag -= MS_PER_UPDATE;
|
||||
}
|
||||
|
||||
@@ -1,14 +1,9 @@
|
||||
extern crate sdl2;
|
||||
|
||||
|
||||
use sdl2::render::Renderer;
|
||||
use sdl2::pixels::Color;
|
||||
use sdl2::rect::Rect;
|
||||
|
||||
use vec::Vec2;
|
||||
|
||||
use vec;
|
||||
|
||||
|
||||
pub struct Player {
|
||||
pub position: Vec2<f32>,
|
||||
|
||||
23
src/tile.rs
23
src/tile.rs
@@ -1,13 +1,20 @@
|
||||
use sdl2::rect::Rect;
|
||||
|
||||
|
||||
pub struct Layer<T> {
|
||||
tiles: Vec<T>,
|
||||
width: i32
|
||||
width: i32,
|
||||
tile_width: i32,
|
||||
tile_height: i32
|
||||
}
|
||||
|
||||
impl<T> Layer<T> where T: Clone {
|
||||
pub fn new(width: i32, height: i32, tile: T) -> Layer<T> {
|
||||
pub fn new(width: i32, height: i32, tile_width: i32, tile_height: i32, tile: T) -> Layer<T> {
|
||||
Layer {
|
||||
tiles: Vec::from_elem((width * height) as uint, tile),
|
||||
width: width
|
||||
width: width,
|
||||
tile_width: tile_width,
|
||||
tile_height: tile_height
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,4 +29,14 @@ impl<T> Layer<T> where T: Clone {
|
||||
|
||||
*self.tiles.index_mut(&offset) = tile;
|
||||
}
|
||||
|
||||
pub fn get_intersecting(&self, rect: &Rect) -> Rect {
|
||||
let x = rect.x / self.tile_width;
|
||||
let w = rect.w / self.tile_width;
|
||||
|
||||
let y = rect.y / self.tile_height;
|
||||
let h = rect.h / self.tile_height;
|
||||
|
||||
Rect::new(x, y, w, h)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user