From c9db5ba0627e98ff1d109169a6599ee987caa7b2 Mon Sep 17 00:00:00 2001 From: logaritmisk Date: Tue, 27 Dec 2016 16:59:57 +0100 Subject: [PATCH] Small refactor of Layer. --- src/main.rs | 61 ++++++++++++++++------------------------ src/player_components.rs | 2 ++ src/tile.rs | 26 ++++++++--------- 3 files changed, 39 insertions(+), 50 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0c4aa96..44de581 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -#[deny(trivial_casts, trivial_numeric_casts)] extern crate sdl2; extern crate sdl2_image; @@ -17,6 +16,7 @@ use timer::Timer; use game_object::GameObject; use player_components::{PlayerPhysicsComponent, PlayerGraphicsComponent}; + mod timer; mod tile; mod camera; @@ -26,6 +26,7 @@ mod game_object; mod component; mod player_components; + const SCREEN_WIDTH : u32 = 960; const SCREEN_HEIGHT : u32 = 640; @@ -41,6 +42,7 @@ 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)] enum Tile<'a> { Empty, @@ -49,6 +51,7 @@ enum Tile<'a> { Floor(Rect) } + fn main() { let sdl_context = sdl2::init().unwrap(); let video_subsystem = sdl_context.video().unwrap(); @@ -249,15 +252,11 @@ fn main() { break; } - if let Some(tile) = layer.get_tile(x, y) { - d = match *tile { - Tile::Floor(_) => d.min(t), - Tile::Static(_, solid) => if solid { d.min(t) } else { d }, - _ => d - } - } else { - break; - } + d = match *layer.get_tile(x, y) { + Tile::Floor(_) => d.min(t), + Tile::Static(_, solid) => if solid { d.min(t) } else { d }, + _ => d + }; x += 1; } @@ -285,15 +284,11 @@ fn main() { break; } - if let Some(tile) = layer.get_tile(x, y) { - d = match *tile { - Tile::Floor(_) => d.max(t), - Tile::Static(_, solid) => if solid { d.max(t) } else { d }, - _ => d - } - } else { - break; - } + d = match *layer.get_tile(x, y) { + Tile::Floor(_) => d.max(t), + Tile::Static(_, solid) => if solid { d.max(t) } else { d }, + _ => d + }; x -= 1; } @@ -323,15 +318,11 @@ fn main() { break; } - if let Some(tile) = layer.get_tile(x, y) { - d = match *tile { - Tile::Floor(_) => d.min(t), - Tile::Static(_, solid) => if solid { d.min(t) } else { d }, - _ => d - } - } else { - break; - } + d = match *layer.get_tile(x, y) { + Tile::Floor(_) => d.min(t), + Tile::Static(_, solid) => if solid { d.min(t) } else { d }, + _ => d + }; y += 1; } @@ -363,15 +354,11 @@ fn main() { break; } - if let Some(tile) = layer.get_tile(x, y) { - d = match *tile { - Tile::Floor(_) => d.max(t), - Tile::Static(_, solid) => if solid { d.max(t) } else { d }, - _ => d - } - } else { - break; - } + d = match *layer.get_tile(x, y) { + Tile::Floor(_) => d.max(t), + Tile::Static(_, solid) => if solid { d.max(t) } else { d }, + _ => d + }; y -= 1; } diff --git a/src/player_components.rs b/src/player_components.rs index 6bd715e..8e2c2b4 100644 --- a/src/player_components.rs +++ b/src/player_components.rs @@ -7,6 +7,7 @@ use sprite::{Sprite, StaticSprite, AnimatedSprite}; use game_object::GameObject; use component::{Updatable, Renderable}; + pub struct PlayerPhysicsComponent { pub dx: f32, pub dy: f32, @@ -28,6 +29,7 @@ impl Updatable for PlayerPhysicsComponent { } } + pub struct PlayerGraphicsComponent<'a> { flip_horizontal: Cell, sprite_standing: RefCell>, diff --git a/src/tile.rs b/src/tile.rs index ff8a617..c8fafc3 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -1,10 +1,9 @@ -use std::iter::repeat; - use sdl2::rect::Rect; pub struct Layer { - tiles: Vec, + tiles: Vec>, + default: T, width: u32, height: u32, tile_width: u32, @@ -12,9 +11,11 @@ pub struct Layer { } 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 { - tiles: repeat(tile).take((width * height) as usize).collect(), + tiles: vec![None; (width * height) as usize], + default: tile, width: width, height: height, tile_width: tile_width, @@ -22,20 +23,19 @@ impl Layer where T: Clone { } } - pub fn get_tile(&self, x: i32, y: i32) -> Option<&T> { + pub fn get_tile(&self, x: i32, y: i32) -> &T { let offset = (x + y * self.width as i32) as usize; - if offset < self.tiles.len() { - Some(&self.tiles[offset]) - } else { - None + match *self.tiles.get(offset).unwrap() { + Some(ref tile) => tile, + None => &self.default } } pub fn set_tile(&mut self, x: i32, y: i32, tile: T) { let offset = (x + y * self.width as i32) as usize; - self.tiles[offset] = tile; + self.tiles[offset] = Some(tile); } pub fn find_intersecting(&self, rect: &Rect) -> Option { @@ -61,7 +61,7 @@ impl Layer where T: Clone { 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); - f(self.get_tile(x, y).unwrap(), &position); + f(self.get_tile(x, y), &position); } } } @@ -74,10 +74,10 @@ impl Layer where T: Clone { #[cfg(test)] mod tests { - use super::Layer; - use sdl2::rect::Rect; + use super::*; + #[test] fn layer_find_intersecting() { let layer = Layer::new(3, 3, 3, 3, ());