diff --git a/Cargo.lock b/Cargo.lock index 88ebf35..8b6e047 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,11 +2,11 @@ name = "super-matte-bros" version = "0.0.1" dependencies = [ - "sdl2 0.0.12 (registry+https://github.com/rust-lang/crates.io-index)", + "sdl2 0.0.13 (git+https://github.com/AngryLawyer/rust-sdl2)", ] [[package]] name = "sdl2" -version = "0.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" +version = "0.0.13" +source = "git+https://github.com/AngryLawyer/rust-sdl2#1e9a96755780b757ed8833c16b17396b33e5eb15" diff --git a/Cargo.toml b/Cargo.toml index 80c1c67..b6cd026 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,8 +3,8 @@ name = "super-matte-bros" version = "0.0.1" authors = ["logaritmisk "] -[dependencies] -sdl2 = "0.0.12" +[dependencies.sdl2] +git = "https://github.com/AngryLawyer/rust-sdl2" [[bin]] name = "super-matte-bros" diff --git a/src/main.rs b/src/main.rs index 2010b15..c2a2a62 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ extern crate sdl2; + +use std::num::SignedInt; use std::cmp::{max, min}; use sdl2::video::{Window, WindowPos, OPENGL}; @@ -9,49 +11,55 @@ use sdl2::rect::Rect; use sdl2::keycode::KeyCode; use sdl2::pixels::Color; +use vec::Vec2; +use tile::Layer; +use player::Player; + mod vec; +mod tile; mod player; -const SCREEN_WIDTH : int = 800; -const SCREEN_HEIGHT : int = 600; +const SCREEN_WIDTH : i32 = 960; +const SCREEN_HEIGHT : i32 = 640; + +const TILE_WIDTH : i32 = 32; +const TILE_HEIGHT : i32 = 32; const MS_PER_UPDATE : uint = 10; -struct Object { - position: vec::Vec2, - color: Color, - w: f32, - h: f32, +#[deriving(Clone)] +enum Tile { + Empty, + Floor } -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 + +struct Camera { + x: i32, + y: i32, + width: i32, + height: i32 +} + +impl Camera { + fn new(x: i32, y: i32, width: i32, height: i32) -> Camera { + Camera { + x: x, + y: y, + width: width, + height: height } } - - 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); - let window = match Window::new("Super Matte Bros", WindowPos::PosCentered, WindowPos::PosCentered, SCREEN_WIDTH, SCREEN_HEIGHT, OPENGL) { + let window = match Window::new("Super Matte Bros", WindowPos::PosCentered, WindowPos::PosCentered, SCREEN_WIDTH as int, SCREEN_HEIGHT as int, OPENGL) { Ok(window) => window, Err(err) => panic!("failed to create window: {}", err) }; @@ -61,12 +69,18 @@ fn main() { Err(err) => panic!("failed to create renderer: {}", err) }; - let mut objects = Vec::new(); + let mut camera = Camera::new(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); + let mut layer = Layer::new(30, 20, Tile::Empty); - objects.push(Object::new(162.5, 400.0, 325.0, 5.0, Color::RGB(0, 0, 255))); - objects.push(Object::new(637.5, 380.0, 325.0, 5.0, Color::RGB(0, 0, 255))); + for x in range(0, 14) { + layer.set_tile(x, 14, Tile::Floor); + } - let mut player = player::Player::new(290.0, 390.0); + for x in range(17, 30) { + layer.set_tile(x, 13, Tile::Floor); + } + + let mut player = Player::new(290.0, 390.0); let mut current : uint; let mut elapsed : uint; @@ -114,30 +128,39 @@ fn main() { player.on_ground = false; - for object in objects.iter() { - if collision_detection(&object.get_rect(), &player.get_rect()) { - let intersect = collision_intersect(&object.get_rect(), &player.get_rect()); + for y in range(0, 20) { + for x in range(0, 30) { + match *layer.get_tile(x, y) { + Tile::Empty => (), + Tile::Floor => { + let object = Rect::new(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); - if intersect.w >= intersect.h { - let mut delta = intersect.h as f32; + if collision_detection(&object, &player.get_rect()) { + let intersect = collision_intersect(&object, &player.get_rect()); - if player.velocity.y >= 0.0 { - delta *= -1.0; + if intersect.w >= intersect.h { + let mut delta = intersect.h as f32; + + if player.velocity.y >= 0.0 { + delta *= -1.0; + } + + player.position.y += delta; + player.velocity.y = 0.0; + + player.on_ground = true; + } else { + let mut delta = intersect.w as f32; + + if player.velocity.x >= 0.0 { + delta *= -1.0; + } + + player.position.x += delta; + player.velocity.x = 0.0; + } + } } - - player.position.y += delta; - player.velocity.y = 0.0; - - player.on_ground = true; - } else { - let mut delta = intersect.w as f32; - - if player.velocity.x >= 0.0 { - delta *= -1.0; - } - - player.position.x += delta; - player.velocity.x = 0.0; } } } @@ -148,8 +171,16 @@ fn main() { let _ = renderer.set_draw_color(Color::RGB(0, 0, 0)); let _ = renderer.clear(); - for object in objects.iter() { - object.render(&renderer); + for y in range(0, 20) { + for x in range(0, 30) { + match *layer.get_tile(x, y) { + Tile::Empty => (), + Tile::Floor => { + let _ = renderer.set_draw_color(Color::RGB(0, 0, 255)); + let _ = renderer.fill_rect(&Rect::new(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT)); + } + } + } } player.render(&renderer); diff --git a/src/player.rs b/src/player.rs index cb61c0e..aaa903a 100644 --- a/src/player.rs +++ b/src/player.rs @@ -5,19 +5,26 @@ use sdl2::render::Renderer; use sdl2::pixels::Color; use sdl2::rect::Rect; +use vec::Vec2; + use vec; pub struct Player { - pub position: vec::Vec2, - pub velocity: vec::Vec2, + pub position: Vec2, + pub velocity: Vec2, pub gravity: f32, pub on_ground: bool } impl Player { pub fn new(x: f32, y: f32) -> Player { - Player { position: vec::Vec2 { x: x, y: y }, velocity: vec::Vec2 { x: 0.0, y: 0.0 }, gravity: 0.3, on_ground: false } + Player { + position: Vec2 { x: x, y: y }, + velocity: Vec2 { x: 0.0, y: 0.0 }, + gravity: 0.3, + on_ground: false + } } pub fn update(&mut self) { @@ -39,6 +46,6 @@ impl Player { } pub fn get_rect(&self) -> Rect { - Rect::new(self.position.x as i32 - 5, self.position.y as i32 - 5, 10, 10) + Rect::new(self.position.x as i32 - 16, self.position.y as i32 - 16, 32, 32) } } diff --git a/src/tile.rs b/src/tile.rs new file mode 100644 index 0000000..5f8246c --- /dev/null +++ b/src/tile.rs @@ -0,0 +1,25 @@ +pub struct Layer { + tiles: Vec, + width: i32 +} + +impl Layer where T: Clone { + pub fn new(width: i32, height: i32, tile: T) -> Layer { + Layer { + tiles: Vec::from_elem((width * height) as uint, tile), + width: width + } + } + + pub fn get_tile(&self, x: i32, y: i32) -> &T { + let offset = (x + y * self.width) as uint; + + self.tiles.index(&offset) + } + + pub fn set_tile(&mut self, x: i32, y: i32, tile: T) { + let offset = (x + y * self.width) as uint; + + *self.tiles.index_mut(&offset) = tile; + } +} diff --git a/src/vec.rs b/src/vec.rs index f1ecaca..a17f379 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -1,4 +1,4 @@ -pub struct Vec2 { - pub x: f32, - pub y: f32, +pub struct Vec2 { + pub x: T, + pub y: T }