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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 diff --git a/assets/textures/overworld-raw.png b/assets/textures/overworld-raw.png index 19a368b..f56b851 100644 Binary files a/assets/textures/overworld-raw.png and b/assets/textures/overworld-raw.png differ diff --git a/assets/textures/overworld.png b/assets/textures/overworld.png index f27ff69..255680b 100644 Binary files a/assets/textures/overworld.png and b/assets/textures/overworld.png differ diff --git a/src/engine/map/mod.rs b/src/engine/map/mod.rs index 7018f4a..829e1ce 100644 --- a/src/engine/map/mod.rs +++ b/src/engine/map/mod.rs @@ -23,6 +23,15 @@ impl Tile { Tile::Solid => (16, 0), } } + + /// Creates a tile from a u8. + pub fn from_u8(id: u8) -> Option { + match id { + 0 => Some(Tile::Empty), + 1 => Some(Tile::Solid), + _ => None, + } + } } /// A tile and its position. @@ -80,6 +89,29 @@ impl Map { } + /// Creates a map from a txt file. + pub fn from_str(text: &str) -> Result { + let split = text.split('\n').collect::>(); + + // First two usize are the size of the map + let size = split[0] + .split_whitespace() + .map(|x| x.parse::().unwrap()) + .collect::>(); + + let mut tiles = Matrix::from_size(size[0], size[1], Tile::Empty); + + for (row, line) in split.iter().skip(1).enumerate() { + for (col, tile) in line.split_whitespace().enumerate() { + tiles[(row, col)] = Tile::from_u8(tile.parse::().unwrap()).unwrap(); + } + } + + Ok(Map { + tiles: tiles, + }) + } + /// Returns an iterator to the positioned tiles. pub fn at(&self, row: usize, col: usize) -> PositionedTile { PositionedTile { diff --git a/src/engine/renderer/mod.rs b/src/engine/renderer/mod.rs index ab213c8..2291cc1 100644 --- a/src/engine/renderer/mod.rs +++ b/src/engine/renderer/mod.rs @@ -19,6 +19,7 @@ use sfml::system::{ use engine::texture::{Texture, TextureManager}; use engine::scene::Scene; +use engine::map::Tile; /// Our custom drawable trait. pub trait Drawable { @@ -76,7 +77,7 @@ impl Renderer { /// Clears the window. pub fn clear(&mut self) { - self.window.clear(&Color::rgb(0, 0, 0)); + self.window.clear(&Color::rgb(135, 206, 235)); } /// Draws a drawable. @@ -99,7 +100,10 @@ impl Renderer { for i in 0 .. rows { for j in 0 .. cols { - self.draw(&map.at(i, j)); + let tile = map.at(i,j); + if tile.tile != Tile::Empty { + self.draw(&tile); + } } } diff --git a/src/engine/scene/mod.rs b/src/engine/scene/mod.rs index ea4253a..f69aff7 100644 --- a/src/engine/scene/mod.rs +++ b/src/engine/scene/mod.rs @@ -22,7 +22,7 @@ impl Scene { pub fn new() -> Scene { Scene { characters: vec![], - map: Map::new(30, 30), + map: Map::from_str(include_str!("../../../assets/levels/level1.txt")).unwrap(), } }