diff --git a/Cargo.lock b/Cargo.lock index dad817b..f9422ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,7 +2,7 @@ name = "super-matte-bros" version = "0.0.1" dependencies = [ - "sdl2 0.27.2 (registry+https://github.com/rust-lang/crates.io-index)", + "sdl2 0.28.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -12,22 +12,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "lazy_static" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "libc" -version = "0.2.11" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "num" -version = "0.1.32" +version = "0.1.36" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -35,7 +35,7 @@ name = "num-integer" version = "0.1.32" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -44,32 +44,32 @@ version = "0.1.32" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "num-traits" -version = "0.1.32" +version = "0.1.36" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "rand" -version = "0.3.14" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "sdl2" -version = "0.27.2" +version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)", + "num 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", "sdl2-sys 0.27.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -78,17 +78,17 @@ name = "sdl2-sys" version = "0.27.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)", ] [metadata] "checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d" -"checksum lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "49247ec2a285bb3dcb23cbd9c35193c025e7251bfce77c1d5da97e6362dffe7f" -"checksum libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "c96061f0c8a2dc27482e394d82e23073569de41d73cd736672ccd3e5c7471bfd" -"checksum num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "c04bd954dbf96f76bab6e5bd6cef6f1ce1262d15268ce4f926d2b5b778fa7af2" +"checksum lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6abe0ee2e758cd6bc8a2cd56726359007748fbf4128da998b65d0b70f881e19b" +"checksum libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)" = "9e030dc72013ed68994d1b2cbf36a94dd0e58418ba949c4b0db7eeb70a7a6352" +"checksum num 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "bde7c03b09e7c6a301ee81f6ddf66d7a28ec305699e3d3b056d2fc56470e3120" "checksum num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "fb24d9bfb3f222010df27995441ded1e954f8f69cd35021f6bef02ca9552fb92" "checksum num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "287a1c9969a847055e1122ec0ea7a5c5d6f72aad97934e131c83d5c08ab4e45c" -"checksum num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "51eab148f171aefad295f8cece636fc488b9b392ef544da31ea4b8ef6b9e9c39" -"checksum rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "2791d88c6defac799c3f20d74f094ca33b9332612d9aef9078519c82e4fe04a5" -"checksum sdl2 0.27.2 (registry+https://github.com/rust-lang/crates.io-index)" = "55d39a8921d9cab08e229dccb574940c99b5819f872ff13335f7e087ffbbdf53" +"checksum num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "a16a42856a256b39c6d3484f097f6713e14feacd9bfb02290917904fae46c81c" +"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d" +"checksum sdl2 0.28.0 (registry+https://github.com/rust-lang/crates.io-index)" = "29301273acc73295e4864a069bf9ad791ef0f819f4fc2a4653aa55818f1cbb05" "checksum sdl2-sys 0.27.2 (registry+https://github.com/rust-lang/crates.io-index)" = "37cfe343db4cd2159cab098096fff52e92d513c2a5b1ee06abbfd5db5323a64d" diff --git a/Cargo.toml b/Cargo.toml index e54c933..6c57ace 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,9 +4,12 @@ version = "0.0.1" authors = ["logaritmisk "] [dependencies.sdl2] -version = "0.27" +version = "0.28" default-features = false features = ["image"] [[bin]] name = "super-matte-bros" + +[profile.release] +lto = true diff --git a/src/camera.rs b/src/camera.rs index 42843b2..cedffe2 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -6,7 +6,7 @@ pub struct Camera { y: i32, width: u32, height: u32, - bounding: Rect + bounding: Rect, } impl Camera { @@ -16,7 +16,7 @@ impl Camera { y: y, width: width, height: height, - bounding: bounding + bounding: bounding, } } @@ -41,7 +41,10 @@ impl Camera { } pub fn to_relative_rect(&self, rect: &Rect) -> Rect { - Rect::new(rect.x() - self.x, rect.y() - self.y, rect.width(), rect.height()) + Rect::new(rect.x() - self.x, + rect.y() - self.y, + rect.width(), + rect.height()) } pub fn to_rect(&self) -> Rect { diff --git a/src/command.rs b/src/command.rs new file mode 100644 index 0000000..0392275 --- /dev/null +++ b/src/command.rs @@ -0,0 +1,34 @@ +use std::cell::RefCell; + +use game_object::GameObject; + + +pub trait Command { + fn apply(&mut self); + fn revert(&mut self); +} + + +pub struct PlayerJumpCommand<'a> { + game_object: RefCell<&'a mut GameObject<'a>>, +} + +impl<'a> PlayerJumpCommand<'a> { + pub fn new(game_object: &'a mut GameObject<'a>) -> PlayerJumpCommand<'a> { + PlayerJumpCommand { game_object: RefCell::new(game_object) } + } +} + +impl<'a> Command for PlayerJumpCommand<'a> { + fn apply(&mut self) { + let mut game_object = self.game_object.borrow_mut(); + + game_object.dx += 5.0; + } + + fn revert(&mut self) { + let mut game_object = self.game_object.borrow_mut(); + + game_object.dx -= 5.0; + } +} diff --git a/src/ecs.rs b/src/ecs.rs new file mode 100644 index 0000000..1e138cd --- /dev/null +++ b/src/ecs.rs @@ -0,0 +1 @@ +type Entity = usize; diff --git a/src/game_object.rs b/src/game_object.rs index 2b78e22..f405727 100644 --- a/src/game_object.rs +++ b/src/game_object.rs @@ -3,6 +3,7 @@ use sdl2::render::Renderer; use component::{Updatable, Renderable}; + pub struct GameObject<'a> { pub x: f32, pub y: f32, @@ -13,11 +14,15 @@ pub struct GameObject<'a> { pub gravity: f32, // TODO moved to PlayerPhysicsComponent pub on_ground: bool, physics: Box, - graphics: Box + graphics: Box, } impl<'a> GameObject<'a> { - pub fn new(x: f32, y: f32, physics: Box, graphics: Box) -> GameObject<'a> { + pub fn new(x: f32, + y: f32, + physics: Box, + graphics: Box) + -> GameObject<'a> { GameObject { x: x, y: y, @@ -28,18 +33,21 @@ impl<'a> GameObject<'a> { gravity: 0.3, on_ground: false, physics: physics, - graphics: graphics + graphics: graphics, } } + #[inline] pub fn update(&self) { self.physics.update(self); } + #[inline] pub fn render(&self, elapsed: f64, renderer: &mut Renderer, destination: &Rect) { self.graphics.render(self, elapsed, renderer, destination); } + #[inline] pub fn to_rect(&self) -> Rect { Rect::new(self.x as i32, self.y as i32, 32, 32) } diff --git a/src/keyboard.rs b/src/keyboard.rs index 3fc3347..74be056 100644 --- a/src/keyboard.rs +++ b/src/keyboard.rs @@ -20,14 +20,14 @@ impl KeyboardHandler { pub fn process(&mut self, event: &Event) { match *event { - Event::KeyDown {keycode, repeat, ..} => { + Event::KeyDown { keycode, repeat, .. } => { if !repeat { self.key_down(keycode.unwrap()); } - }, - Event::KeyUp {keycode, ..} => { + } + Event::KeyUp { keycode, .. } => { self.key_up(keycode.unwrap()); - }, + } _ => (), } } @@ -50,21 +50,21 @@ impl KeyboardHandler { pub fn was_pressed(&self, keycode: Keycode) -> bool { match self.pressed_keys.get(&keycode) { Some(state) => *state, - None => false + None => false, } } pub fn was_released(&self, keycode: Keycode) -> bool { match self.released_keys.get(&keycode) { Some(state) => *state, - None => false + None => false, } } pub fn is_held(&self, keycode: Keycode) -> bool { match self.held_keys.get(&keycode) { Some(state) => *state, - None => false + None => false, } } } diff --git a/src/main.rs b/src/main.rs index a5ec04c..13deb82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,22 +24,24 @@ mod sprite; mod game_object; mod component; mod player_components; +mod tiler; +mod command; -const SCREEN_WIDTH : u32 = 960; -const SCREEN_HEIGHT : u32 = 640; +const SCREEN_WIDTH: u32 = 960; +const SCREEN_HEIGHT: u32 = 640; -const TILE_WIDTH : u32 = 32; -const TILE_HEIGHT : u32 = 32; +const TILE_WIDTH: u32 = 32; +const TILE_HEIGHT: u32 = 32; -const MS_PER_UPDATE : f64 = 10.0; +const MS_PER_UPDATE: f64 = 10.0; -const PLAYER_SPEED_X : f32 = 4.0; -const PLAYER_THRESHOLD_X : f32 = 0.2; +const PLAYER_SPEED_X: f32 = 4.0; +const PLAYER_THRESHOLD_X: f32 = 0.2; -const PLAYER_ACCELERATION_X_START : f32 = 0.02; -const PLAYER_ACCELERATION_X_STOP : f32 = 0.15; -const PLAYER_ACCELERATION_X_CHANGE : f32 = 0.06; +const PLAYER_ACCELERATION_X_START: f32 = 0.02; +const PLAYER_ACCELERATION_X_STOP: f32 = 0.15; +const PLAYER_ACCELERATION_X_CHANGE: f32 = 0.06; #[derive(Clone)] @@ -47,7 +49,7 @@ enum Tile<'a> { Empty, Static(&'a StaticSprite<'a>, bool), Background(Rect), - Floor(Rect) + Floor(Rect), } @@ -57,11 +59,32 @@ fn main() { let _ = sdl2::image::init(sdl2::image::INIT_PNG).unwrap(); - let window = video_subsystem.window("Super Matte Bros", SCREEN_WIDTH, SCREEN_HEIGHT).position_centered().build().unwrap(); - let mut renderer = window.renderer().software().build().unwrap(); + let window = video_subsystem.window("Super Matte Bros", SCREEN_WIDTH, SCREEN_HEIGHT) + .position_centered() + .build() + .unwrap(); + + let mut renderer = window.renderer() + .software() + .build() + .unwrap(); let world_sprites = renderer.load_texture(Path::new("gfx/world.png")).unwrap(); + let _foo = tiler::Set { + image: renderer.load_texture(Path::new("gfx/world.png")).unwrap(), + width: 16, + height: 16, + tiles: vec![tiler::Tile { + id: 0, + animation: None, + }, + tiler::Tile { + id: 1, + animation: None, + }], + }; + let floor_sprite = StaticSprite::new(&world_sprites, 16 * 0, 16 * 0); let brick_sprite = StaticSprite::new(&world_sprites, 16 * 1, 16 * 0); @@ -69,7 +92,10 @@ fn main() { let timer = Timer::new(); - let mut player = GameObject::new(390.0, 390.0, Box::new(PlayerPhysicsComponent::new()), Box::new(PlayerGraphicsComponent::new(&player_sprites))); + let mut player = GameObject::new(390.0, + 390.0, + Box::new(PlayerPhysicsComponent::new()), + Box::new(PlayerGraphicsComponent::new(&player_sprites))); let mut keyboard = KeyboardHandler::new(); @@ -183,8 +209,8 @@ fn main() { use sdl2::event::Event; match event { - Event::KeyDown {keycode, ..} if keycode == Some(Keycode::Escape) => break 'main, - Event::Quit {..} => break 'main, + Event::KeyDown { keycode, .. } if keycode == Some(Keycode::Escape) => break 'main, + Event::Quit { .. } => break 'main, _ => (), } @@ -254,7 +280,7 @@ fn main() { d = match *layer.get_tile(x, y) { Tile::Floor(_) => d.min(t), Tile::Static(_, solid) => if solid { d.min(t) } else { d }, - _ => d + _ => d, }; x += 1; @@ -286,7 +312,7 @@ fn main() { d = match *layer.get_tile(x, y) { Tile::Floor(_) => d.max(t), Tile::Static(_, solid) => if solid { d.max(t) } else { d }, - _ => d + _ => d, }; x -= 1; @@ -320,7 +346,7 @@ fn main() { d = match *layer.get_tile(x, y) { Tile::Floor(_) => d.min(t), Tile::Static(_, solid) => if solid { d.min(t) } else { d }, - _ => d + _ => d, }; y += 1; @@ -356,7 +382,7 @@ fn main() { d = match *layer.get_tile(x, y) { Tile::Floor(_) => d.max(t), Tile::Static(_, solid) => if solid { d.max(t) } else { d }, - _ => d + _ => d, }; y -= 1; @@ -388,9 +414,12 @@ fn main() { let object = camera.to_relative_rect(position); match *tile { - Tile::Background(src) | Tile::Floor(src) => renderer.copy(&world_sprites, Some(src), Some(object)).unwrap(), - Tile::Static(ref sprite, _) => sprite.render(lag / MS_PER_UPDATE, &mut renderer, &object), - _ => () + Tile::Background(src) | + Tile::Floor(src) => renderer.copy(&world_sprites, Some(src), Some(object)).unwrap(), + Tile::Static(ref sprite, _) => { + sprite.render(lag / MS_PER_UPDATE, &mut renderer, &object) + } + _ => (), } }); diff --git a/src/player_components.rs b/src/player_components.rs index 8e2c2b4..3592ae7 100644 --- a/src/player_components.rs +++ b/src/player_components.rs @@ -25,8 +25,7 @@ impl PlayerPhysicsComponent { } impl Updatable for PlayerPhysicsComponent { - fn update(&self, _: &GameObject) { - } + fn update(&self, _: &GameObject) {} } @@ -34,7 +33,7 @@ pub struct PlayerGraphicsComponent<'a> { flip_horizontal: Cell, sprite_standing: RefCell>, sprite_running: RefCell>, - sprite_jumping: RefCell> + sprite_jumping: RefCell>, } impl<'a> PlayerGraphicsComponent<'a> { @@ -43,13 +42,17 @@ impl<'a> PlayerGraphicsComponent<'a> { flip_horizontal: Cell::new(false), sprite_standing: RefCell::new(StaticSprite::new(texture, 80, 32)), sprite_running: RefCell::new(AnimatedSprite::new(texture, 96, 32, 3, 10.0)), - sprite_jumping: RefCell::new(StaticSprite::new(texture, 160, 32)) + sprite_jumping: RefCell::new(StaticSprite::new(texture, 160, 32)), } } } impl<'a> Renderable for PlayerGraphicsComponent<'a> { - fn render(&self, object: &GameObject, elapsed: f64, renderer: &mut Renderer, destination: &Rect) { + fn render(&self, + object: &GameObject, + elapsed: f64, + renderer: &mut Renderer, + destination: &Rect) { if !object.on_ground { let mut sprite = self.sprite_jumping.borrow_mut(); diff --git a/src/sprite.rs b/src/sprite.rs index 85ea9b8..4903ce2 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -12,7 +12,7 @@ pub struct StaticSprite<'a> { x: i32, y: i32, pub flip_horizontal: bool, - pub flip_vertical: bool + pub flip_vertical: bool, } impl<'a> StaticSprite<'a> { @@ -22,14 +22,20 @@ impl<'a> StaticSprite<'a> { x: x, y: y, flip_horizontal: false, - flip_vertical: false + flip_vertical: false, } } } impl<'a> Sprite for StaticSprite<'a> { fn render(&self, _: f64, drawer: &mut Renderer, destination: &Rect) { - let _ = drawer.copy_ex(self.texture, Some(Rect::new(self.x, self.y, 16, 16)), Some(*destination), 0.0, None, self.flip_horizontal, self.flip_vertical); + let _ = drawer.copy_ex(self.texture, + Some(Rect::new(self.x, self.y, 16, 16)), + Some(*destination), + 0.0, + None, + self.flip_horizontal, + self.flip_vertical); } } @@ -42,7 +48,7 @@ pub struct AnimatedSprite<'a> { frames: u32, frame_time: f64, frame: RefCell, - time: RefCell + time: RefCell, } impl<'a> AnimatedSprite<'a> { @@ -56,7 +62,7 @@ impl<'a> AnimatedSprite<'a> { frame: RefCell::new(0), frames: frames, time: RefCell::new(0.0), - frame_time: 1000.0 / fps as f64 + frame_time: 1000.0 / fps as f64, } } } @@ -73,6 +79,12 @@ impl<'a> Sprite for AnimatedSprite<'a> { let x = self.x + (*frame * 16) as i32; - let _ = drawer.copy_ex(self.texture, Some(Rect::new(x, self.y, 16, 16)), Some(*destination), 0.0, None, self.flip_horizontal, self.flip_vertical); + let _ = drawer.copy_ex(self.texture, + Some(Rect::new(x, self.y, 16, 16)), + Some(*destination), + 0.0, + None, + self.flip_horizontal, + self.flip_vertical); } } diff --git a/src/tile.rs b/src/tile.rs index c8fafc3..8e2b7b4 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -7,10 +7,12 @@ pub struct Layer { width: u32, height: u32, tile_width: u32, - tile_height: u32 + tile_height: u32, } -impl Layer where T: Clone { +impl Layer + where T: Clone +{ // TODO Change u32 to usize for width and height? pub fn new(width: u32, height: u32, tile_width: u32, tile_height: u32, tile: T) -> Layer { Layer { @@ -19,7 +21,7 @@ impl Layer where T: Clone { width: width, height: height, tile_width: tile_width, - tile_height: tile_height + tile_height: tile_height, } } @@ -28,7 +30,7 @@ impl Layer where T: Clone { match *self.tiles.get(offset).unwrap() { Some(ref tile) => tile, - None => &self.default + None => &self.default, } } @@ -39,10 +41,12 @@ impl Layer where T: Clone { } pub fn find_intersecting(&self, rect: &Rect) -> Option { - if rect.x() + rect.width() as i32 <= 0 || rect.x() >= (self.width * self.tile_width) as i32 { + if rect.x() + rect.width() as i32 <= 0 || + rect.x() >= (self.width * self.tile_width) as i32 { return None; } - if rect.y() + rect.height() as i32 <= 0 || rect.y() >= (self.height * self.tile_height) as i32 { + if rect.y() + rect.height() as i32 <= 0 || + rect.y() >= (self.height * self.tile_height) as i32 { return None; } @@ -57,9 +61,12 @@ impl Layer where T: Clone { pub fn for_each_intersecting(&self, rect: &Rect, mut f: F) { if let Some(intersect) = self.find_intersecting(rect) { - for y in intersect.y()..(intersect.y() + intersect.height() as i32) { + for y in intersect.y()..(intersect.y() + intersect.height() as i32) { for x in intersect.x()..(intersect.x() + intersect.width() as i32) { - let position = Rect::new(x * self.tile_width as i32, y * self.tile_height as i32, self.tile_width, self.tile_height); + let position = Rect::new(x * self.tile_width as i32, + y * self.tile_height as i32, + self.tile_width, + self.tile_height); f(self.get_tile(x, y), &position); } @@ -68,7 +75,10 @@ impl Layer where T: Clone { } pub fn to_rect(&self) -> Rect { - Rect::new(0, 0, self.width * self.tile_width, self.height * self.tile_height) + Rect::new(0, + 0, + self.width * self.tile_width, + self.height * self.tile_height) } } @@ -83,8 +93,8 @@ mod tests { let layer = Layer::new(3, 3, 3, 3, ()); // out of bounds. - assert_eq!(layer.find_intersecting(&Rect::new(-1, 1, 1, 1)), None); - assert_eq!(layer.find_intersecting(&Rect::new( 1, -1, 1, 1)), None); + assert_eq!(layer.find_intersecting(&Rect::new(-1, 1, 1, 1)), None); + assert_eq!(layer.find_intersecting(&Rect::new(1, -1, 1, 1)), None); assert_eq!(layer.find_intersecting(&Rect::new(-1, -1, 1, 1)), None); assert_eq!(layer.find_intersecting(&Rect::new(9, 7, 1, 1)), None); @@ -92,45 +102,71 @@ mod tests { assert_eq!(layer.find_intersecting(&Rect::new(9, 9, 1, 1)), None); // middle of tile. - assert_eq!(layer.find_intersecting(&Rect::new(1, 1, 1, 1)), Some(Rect::new(0, 0, 1, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(4, 1, 1, 1)), Some(Rect::new(1, 0, 1, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(7, 1, 1, 1)), Some(Rect::new(2, 0, 1, 1))); + assert_eq!(layer.find_intersecting(&Rect::new(1, 1, 1, 1)), + Some(Rect::new(0, 0, 1, 1))); + assert_eq!(layer.find_intersecting(&Rect::new(4, 1, 1, 1)), + Some(Rect::new(1, 0, 1, 1))); + assert_eq!(layer.find_intersecting(&Rect::new(7, 1, 1, 1)), + Some(Rect::new(2, 0, 1, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(1, 4, 1, 1)), Some(Rect::new(0, 1, 1, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(4, 4, 1, 1)), Some(Rect::new(1, 1, 1, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(7, 4, 1, 1)), Some(Rect::new(2, 1, 1, 1))); + assert_eq!(layer.find_intersecting(&Rect::new(1, 4, 1, 1)), + Some(Rect::new(0, 1, 1, 1))); + assert_eq!(layer.find_intersecting(&Rect::new(4, 4, 1, 1)), + Some(Rect::new(1, 1, 1, 1))); + assert_eq!(layer.find_intersecting(&Rect::new(7, 4, 1, 1)), + Some(Rect::new(2, 1, 1, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(1, 7, 1, 1)), Some(Rect::new(0, 2, 1, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(4, 7, 1, 1)), Some(Rect::new(1, 2, 1, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(7, 7, 1, 1)), Some(Rect::new(2, 2, 1, 1))); + assert_eq!(layer.find_intersecting(&Rect::new(1, 7, 1, 1)), + Some(Rect::new(0, 2, 1, 1))); + assert_eq!(layer.find_intersecting(&Rect::new(4, 7, 1, 1)), + Some(Rect::new(1, 2, 1, 1))); + assert_eq!(layer.find_intersecting(&Rect::new(7, 7, 1, 1)), + Some(Rect::new(2, 2, 1, 1))); // interlaps 4 tiles. - assert_eq!(layer.find_intersecting(&Rect::new(2, 2, 2, 2)), Some(Rect::new(0, 0, 2, 2))); - assert_eq!(layer.find_intersecting(&Rect::new(5, 2, 2, 2)), Some(Rect::new(1, 0, 2, 2))); - assert_eq!(layer.find_intersecting(&Rect::new(2, 5, 2, 2)), Some(Rect::new(0, 1, 2, 2))); - assert_eq!(layer.find_intersecting(&Rect::new(5, 5, 2, 2)), Some(Rect::new(1, 1, 2, 2))); + assert_eq!(layer.find_intersecting(&Rect::new(2, 2, 2, 2)), + Some(Rect::new(0, 0, 2, 2))); + assert_eq!(layer.find_intersecting(&Rect::new(5, 2, 2, 2)), + Some(Rect::new(1, 0, 2, 2))); + assert_eq!(layer.find_intersecting(&Rect::new(2, 5, 2, 2)), + Some(Rect::new(0, 1, 2, 2))); + assert_eq!(layer.find_intersecting(&Rect::new(5, 5, 2, 2)), + Some(Rect::new(1, 1, 2, 2))); // interlaps 2 tiles horizontal. - assert_eq!(layer.find_intersecting(&Rect::new(2, 1, 2, 1)), Some(Rect::new(0, 0, 2, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(5, 1, 2, 1)), Some(Rect::new(1, 0, 2, 1))); + assert_eq!(layer.find_intersecting(&Rect::new(2, 1, 2, 1)), + Some(Rect::new(0, 0, 2, 1))); + assert_eq!(layer.find_intersecting(&Rect::new(5, 1, 2, 1)), + Some(Rect::new(1, 0, 2, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(2, 4, 2, 1)), Some(Rect::new(0, 1, 2, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(5, 4, 2, 1)), Some(Rect::new(1, 1, 2, 1))); + assert_eq!(layer.find_intersecting(&Rect::new(2, 4, 2, 1)), + Some(Rect::new(0, 1, 2, 1))); + assert_eq!(layer.find_intersecting(&Rect::new(5, 4, 2, 1)), + Some(Rect::new(1, 1, 2, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(2, 7, 2, 1)), Some(Rect::new(0, 2, 2, 1))); - assert_eq!(layer.find_intersecting(&Rect::new(5, 7, 2, 1)), Some(Rect::new(1, 2, 2, 1))); + assert_eq!(layer.find_intersecting(&Rect::new(2, 7, 2, 1)), + Some(Rect::new(0, 2, 2, 1))); + assert_eq!(layer.find_intersecting(&Rect::new(5, 7, 2, 1)), + Some(Rect::new(1, 2, 2, 1))); // interlaps 2 tiles vertical. - assert_eq!(layer.find_intersecting(&Rect::new(1, 2, 1, 2)), Some(Rect::new(0, 0, 1, 2))); - assert_eq!(layer.find_intersecting(&Rect::new(1, 5, 1, 2)), Some(Rect::new(0, 1, 1, 2))); + assert_eq!(layer.find_intersecting(&Rect::new(1, 2, 1, 2)), + Some(Rect::new(0, 0, 1, 2))); + assert_eq!(layer.find_intersecting(&Rect::new(1, 5, 1, 2)), + Some(Rect::new(0, 1, 1, 2))); - assert_eq!(layer.find_intersecting(&Rect::new(4, 2, 1, 2)), Some(Rect::new(1, 0, 1, 2))); - assert_eq!(layer.find_intersecting(&Rect::new(4, 5, 1, 2)), Some(Rect::new(1, 1, 1, 2))); + assert_eq!(layer.find_intersecting(&Rect::new(4, 2, 1, 2)), + Some(Rect::new(1, 0, 1, 2))); + assert_eq!(layer.find_intersecting(&Rect::new(4, 5, 1, 2)), + Some(Rect::new(1, 1, 1, 2))); - assert_eq!(layer.find_intersecting(&Rect::new(7, 2, 1, 2)), Some(Rect::new(2, 0, 1, 2))); - assert_eq!(layer.find_intersecting(&Rect::new(7, 5, 1, 2)), Some(Rect::new(2, 1, 1, 2))); + assert_eq!(layer.find_intersecting(&Rect::new(7, 2, 1, 2)), + Some(Rect::new(2, 0, 1, 2))); + assert_eq!(layer.find_intersecting(&Rect::new(7, 5, 1, 2)), + Some(Rect::new(2, 1, 1, 2))); // exactly one tile. - assert_eq!(layer.find_intersecting(&Rect::new(0, 0, 3, 3)), Some(Rect::new(0, 0, 1, 1))); + assert_eq!(layer.find_intersecting(&Rect::new(0, 0, 3, 3)), + Some(Rect::new(0, 0, 1, 1))); } } diff --git a/src/tiler.rs b/src/tiler.rs new file mode 100644 index 0000000..03128b0 --- /dev/null +++ b/src/tiler.rs @@ -0,0 +1,39 @@ +use std::time::Duration; + +use sdl2::render::Texture; + + +type TileId = usize; + + +pub struct Map { + width: usize, + height: usize, + sets: Vec, + layers: Vec, +} + + +pub struct Set { + pub image: Texture, + pub width: usize, + pub height: usize, + pub tiles: Vec, +} + + +pub struct Tile { + pub id: TileId, + pub animation: Option>, +} + + +pub struct Frame { + tile: TileId, + duration: Duration, +} + + +pub struct Layer { + tiles: Vec, +} diff --git a/src/timer.rs b/src/timer.rs index 2959737..53ad1d3 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -1,14 +1,12 @@ use std::time::Instant; pub struct Timer { - time: Instant + time: Instant, } impl Timer { pub fn new() -> Timer { - Timer { - time: Instant::now() - } + Timer { time: Instant::now() } } pub fn current_time(&self) -> f64 {