diff --git a/Cargo.lock b/Cargo.lock index 8651eea..c2575ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,11 +2,11 @@ name = "super-matte-bros" version = "0.0.1" dependencies = [ - "sdl2 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "sdl2 0.0.9 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "sdl2" -version = "0.0.8" +version = "0.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml index c79edb6..1949328 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.0.1" authors = ["logaritmisk "] [dependencies] -sdl2 = "0.0.8" +sdl2 = "0.0.9" [[bin]] name = "super-matte-bros" diff --git a/README.md b/README.md index b7d2bce..98e8356 100644 --- a/README.md +++ b/README.md @@ -1 +1,20 @@ -It time for me to learn Rust, and why not try to make a simple Super Mario Bros clone, called Super Matte Bros. Run Matte, RUN! \ No newline at end of file +# Super Matte Bros + +It time for me to learn Rust, and why not try to make a simple Super Mario Bros clone, called Super Matte Bros. Run Matte, RUN! + + +## Install + +### OS X + +```shell +$ curl -s https://static.rust-lang.org/rustup.sh | sudo sh +$ brew install sdl2 +``` + + +## Run + +```shell +$ cargo run +``` diff --git a/src/main.rs b/src/main.rs index cce1163..4de3e6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,34 @@ const SCREEN_HEIGHT : int = 600; const MS_PER_UPDATE : uint = 10; +struct Object { + position: vec::Vec2, + color: Color, + w: f32, + h: f32, +} + +impl Object { + fn new(x: f32, y: f32, w: f32, h: f32, color: Color) -> Object { + Object { + position: vec::Vec2 { x: x, y: y }, + color: color, + w: w, + h: h + } + } + + fn render(&self, renderer: &sdl2::render::Renderer) { + let _ = renderer.set_draw_color(self.color); + let _ = renderer.fill_rect(&self.get_rect()); + } + + fn get_rect(&self) -> Rect { + Rect::new((self.position.x - (self.w / 2.0)) as i32, (self.position.y - (self.h / 2.0)) as i32, self.w as i32, self.h as i32) + } +} + + fn main() { sdl2::init(sdl2::INIT_EVERYTHING); @@ -34,8 +62,8 @@ fn main() { let mut player = player::Player::new(290.0, 390.0); let mut on_ground = true; - let ground1 = Rect::new(0, 400, 325, 5); - let ground2 = Rect::new(475, 400, 325, 5); + let ground1 = Object::new(162.5, 400.0, 325.0, 5.0, Color::RGB(0, 0, 255)); + let ground2 = Object::new(637.5, 395.0, 325.0, 5.0, Color::RGB(0, 0, 255)); let mut current : uint; let mut elapsed : uint; @@ -82,6 +110,8 @@ fn main() { while lag >= MS_PER_UPDATE { player.update(); + collision_detection(&player.get_rect(), &ground2.get_rect()); + if player.position.y >= 390.0 { player.position.y = 390.0; player.velocity.y = 0.0; @@ -95,9 +125,8 @@ fn main() { let _ = renderer.set_draw_color(Color::RGB(0, 0, 0)); let _ = renderer.clear(); - let _ = renderer.set_draw_color(Color::RGB(0, 0, 255)); - let _ = renderer.fill_rect(&ground1); - let _ = renderer.fill_rect(&ground2); + ground1.render(&renderer); + ground2.render(&renderer); player.render(&renderer); @@ -106,3 +135,15 @@ fn main() { sdl2::quit(); } + +fn collision_detection(lhs: &Rect, rhs: &Rect) -> bool { + if lhs.x + lhs.w < rhs.x || rhs.x + rhs.w < lhs.x { + false + } + else if lhs.y + lhs.h < rhs.y || rhs.y + rhs.h < lhs.y { + false + } + else { + true + } +} diff --git a/src/player.rs b/src/player.rs index 2554fd3..7b8300b 100644 --- a/src/player.rs +++ b/src/player.rs @@ -27,9 +27,11 @@ impl Player { } pub fn render(&self, renderer: &Renderer) { - let player = Rect::new(self.position.x as i32, self.position.y as i32, 10, 10); - let _ = renderer.set_draw_color(Color::RGB(0, 255, 0)); - let _ = renderer.fill_rect(&player); + let _ = renderer.fill_rect(&self.get_rect()); + } + + pub fn get_rect(&self) -> Rect { + Rect::new(self.position.x as i32 - 5, self.position.y as i32 - 5, 10, 10) } }