forked from tutorials/rust-sokoban
41 lines
1.3 KiB
Rust
41 lines
1.3 KiB
Rust
|
use crate::components::Position;
|
||
|
use crate::entities::*;
|
||
|
use specs::World;
|
||
|
|
||
|
pub fn load_map(world: &mut World, map_string: String) {
|
||
|
// read all lines
|
||
|
let rows: Vec<&str> = map_string.trim().split('\n').map(|x| x.trim()).collect();
|
||
|
|
||
|
for (y, row) in rows.iter().enumerate() {
|
||
|
let columns: Vec<&str> = row.split(' ').collect();
|
||
|
|
||
|
for (x, column) in columns.iter().enumerate() {
|
||
|
// Create the position at which to create something on the map
|
||
|
let position = Position::new(x as u8, y as u8);
|
||
|
|
||
|
// Figure out which object to create
|
||
|
match *column {
|
||
|
"." => create_floor(world, position),
|
||
|
"W" => {
|
||
|
create_floor(world, position);
|
||
|
create_wall(world, position);
|
||
|
}
|
||
|
"P" => {
|
||
|
create_floor(world, position);
|
||
|
create_player(world, position);
|
||
|
}
|
||
|
"B" => {
|
||
|
create_floor(world, position);
|
||
|
create_box(world, position);
|
||
|
}
|
||
|
"S" => {
|
||
|
create_floor(world, position);
|
||
|
create_box_spot(world, position);
|
||
|
}
|
||
|
"N" => (),
|
||
|
c => panic!("unrecognized map item {}", c),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|