Added parsing level
This commit is contained in:
		
							parent
							
								
									805e6409a1
								
							
						
					
					
						commit
						8a81f6f469
					
				
							
								
								
									
										25
									
								
								assets/levels/level1.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								assets/levels/level1.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,25 @@ | |||||||
|  | 25 50 | ||||||
|  | 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 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 0 0 0 0 0 0 0 0 0 0 0 0 0 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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 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 | ||||||
										
											Binary file not shown.
										
									
								
							| Before Width: | Height: | Size: 56 KiB After Width: | Height: | Size: 74 KiB | 
										
											Binary file not shown.
										
									
								
							| Before Width: | Height: | Size: 2.4 KiB After Width: | Height: | Size: 3.8 KiB | 
| @ -23,6 +23,15 @@ impl Tile { | |||||||
|             Tile::Solid => (16, 0), |             Tile::Solid => (16, 0), | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     /// Creates a tile from a u8.
 | ||||||
|  |     pub fn from_u8(id: u8) -> Option<Tile> { | ||||||
|  |         match id { | ||||||
|  |             0 => Some(Tile::Empty), | ||||||
|  |             1 => Some(Tile::Solid), | ||||||
|  |             _ => None, | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /// A tile and its position.
 | /// 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<Map, ()> { | ||||||
|  |         let split = text.split('\n').collect::<Vec<_>>(); | ||||||
|  | 
 | ||||||
|  |         // First two usize are the size of the map
 | ||||||
|  |         let size = split[0] | ||||||
|  |             .split_whitespace() | ||||||
|  |             .map(|x| x.parse::<usize>().unwrap()) | ||||||
|  |             .collect::<Vec<_>>(); | ||||||
|  | 
 | ||||||
|  |         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::<u8>().unwrap()).unwrap(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         Ok(Map { | ||||||
|  |             tiles: tiles, | ||||||
|  |         }) | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     /// Returns an iterator to the positioned tiles.
 |     /// Returns an iterator to the positioned tiles.
 | ||||||
|     pub fn at(&self, row: usize, col: usize) -> PositionedTile { |     pub fn at(&self, row: usize, col: usize) -> PositionedTile { | ||||||
|         PositionedTile { |         PositionedTile { | ||||||
|  | |||||||
| @ -19,6 +19,7 @@ use sfml::system::{ | |||||||
| 
 | 
 | ||||||
| use engine::texture::{Texture, TextureManager}; | use engine::texture::{Texture, TextureManager}; | ||||||
| use engine::scene::Scene; | use engine::scene::Scene; | ||||||
|  | use engine::map::Tile; | ||||||
| 
 | 
 | ||||||
| /// Our custom drawable trait.
 | /// Our custom drawable trait.
 | ||||||
| pub trait Drawable { | pub trait Drawable { | ||||||
| @ -76,7 +77,7 @@ impl Renderer { | |||||||
| 
 | 
 | ||||||
|     /// Clears the window.
 |     /// Clears the window.
 | ||||||
|     pub fn clear(&mut self) { |     pub fn clear(&mut self) { | ||||||
|         self.window.clear(&Color::rgb(0, 0, 0)); |         self.window.clear(&Color::rgb(135, 206, 235)); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /// Draws a drawable.
 |     /// Draws a drawable.
 | ||||||
| @ -99,7 +100,10 @@ impl Renderer { | |||||||
| 
 | 
 | ||||||
|         for i in 0 .. rows { |         for i in 0 .. rows { | ||||||
|             for j in 0 .. cols { |             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); | ||||||
|  |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -22,7 +22,7 @@ impl Scene { | |||||||
|     pub fn new() -> Scene { |     pub fn new() -> Scene { | ||||||
|         Scene { |         Scene { | ||||||
|             characters: vec![], |             characters: vec![], | ||||||
|             map: Map::new(30, 30), |             map: Map::from_str(include_str!("../../../assets/levels/level1.txt")).unwrap(), | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user