Added parsing level
This commit is contained in:
@@ -23,6 +23,15 @@ impl Tile {
|
||||
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.
|
||||
@@ -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.
|
||||
pub fn at(&self, row: usize, col: usize) -> PositionedTile {
|
||||
PositionedTile {
|
||||
|
||||
Reference in New Issue
Block a user