rename get_rect to to_rect, and there is scrolling!

This commit is contained in:
2015-01-06 18:16:33 +01:00
parent db4b16c08d
commit b5f94170f7
4 changed files with 56 additions and 25 deletions

View File

@@ -2,23 +2,45 @@ use sdl2::rect::Rect;
pub struct Camera {
pub x: i32,
pub y: i32,
pub w: i32,
pub h: i32
x: i32,
y: i32,
w: i32,
h: i32,
bounding: Rect
}
impl Camera {
pub fn new(x: i32, y: i32, w: i32, h: i32) -> Camera {
pub fn new(x: i32, y: i32, w: i32, h: i32, bounding: Rect) -> Camera {
Camera {
x: x,
y: y,
w: w,
h: h
h: h,
bounding: bounding
}
}
pub fn get_rect(&self) -> Rect {
pub fn center(&mut self, object: &Rect) {
let mut x = (object.x + object.w / 2) - (self.w / 2);
let mut y = (object.y + object.h / 2) - (self.h / 2);
if x < self.bounding.x {
x = self.bounding.x;
} else if x + self.w > self.bounding.w {
x = self.bounding.w - self.w;
}
if y < self.bounding.y {
y = self.bounding.y;
} else if y + self.h > self.bounding.h {
y = self.bounding.h - self.h;
}
self.x = x;
self.y = y;
}
pub fn to_rect(&self) -> Rect {
Rect::new(self.x, self.y, self.w, self.h)
}
}

View File

@@ -48,17 +48,16 @@ fn main() {
Err(err) => panic!("failed to create renderer: {}", err)
};
let mut camera = Camera::new(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
let mut layer = Layer::new(120, 20, TILE_WIDTH, TILE_HEIGHT, Tile::Empty);
camera.x = 16;
let colors = vec![Color::RGB(0, 0, 255), Color::RGB(0, 128, 255)];
for x in range(0, 120) {
layer.set_tile(x, 14, Tile::Floor(colors[(x % 2) as uint]));
}
let mut camera = Camera::new(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, layer.to_rect());
let mut player = Player::new(290.0, 390.0);
let mut current : uint;
@@ -110,12 +109,12 @@ fn main() {
while lag >= MS_PER_UPDATE {
player.update();
layer.for_each_intersecting(&player.get_rect(), |tile: &Tile, position: &Rect| {
layer.for_each_intersecting(&player.to_rect(), |tile: &Tile, position: &Rect| {
match *tile {
Tile::Empty => (),
Tile::Floor(_) => {
if position.has_intersection(&player.get_rect()) {
match position.intersection(&player.get_rect()) {
if position.has_intersection(&player.to_rect()) {
match position.intersection(&player.to_rect()) {
Some(intersect) => {
if intersect.w >= intersect.h {
if player.velocity.y < 0.0 {
@@ -146,7 +145,7 @@ fn main() {
player.on_ground = false;
let mut ground = player.get_rect();
let mut ground = player.to_rect();
ground.y += ground.h;
ground.h = 1;
@@ -162,23 +161,30 @@ fn main() {
}
});
camera.center(&player.to_rect());
lag -= MS_PER_UPDATE;
}
let _ = renderer.set_draw_color(Color::RGB(0, 0, 0));
let _ = renderer.clear();
layer.for_each_intersecting(&camera.get_rect(), |tile: &Tile, position: &Rect| {
layer.for_each_intersecting(&camera.to_rect(), |tile: &Tile, position: &Rect| {
let object = camera_relative_rect(&camera.to_rect(), position);
match *tile {
Tile::Empty => (),
Tile::Floor(c) => {
let _ = renderer.set_draw_color(c);
let _ = renderer.fill_rect(position);
Tile::Floor(color) => {
let _ = renderer.set_draw_color(color);
let _ = renderer.fill_rect(&object);
}
}
});
player.render(&renderer);
let player_rect = camera_relative_rect(&camera.to_rect(), &player.to_rect());
let _ = renderer.set_draw_color(Color::RGB(0, 255, 0));
let _ = renderer.fill_rect(&player_rect);
renderer.present();
@@ -187,3 +193,7 @@ fn main() {
sdl2::quit();
}
fn camera_relative_rect(camera: &Rect, other: &Rect) -> Rect {
Rect::new(other.x - camera.x, other.y - camera.y, other.w, other.h)
}

View File

@@ -35,12 +35,7 @@ impl Player {
self.position.y += self.velocity.y;
}
pub fn render(&self, renderer: &Renderer) {
let _ = renderer.set_draw_color(Color::RGB(0, 255, 0));
let _ = renderer.fill_rect(&self.get_rect());
}
pub fn get_rect(&self) -> Rect {
pub fn to_rect(&self) -> Rect {
Rect::new(self.position.x as i32 - 16, self.position.y as i32 - 16, 32, 32)
}
}

View File

@@ -62,4 +62,8 @@ impl<T> Layer<T> where T: Clone {
}
}
}
pub fn to_rect(&self) -> Rect {
Rect::new(0, 0, self.width * self.tile_width, self.height * self.tile_height)
}
}