rust-sokoban/src/map.rs

49 lines
1.7 KiB
Rust
Raw Permalink Normal View History

2020-07-24 17:55:38 -04:00
use crate::components::{BoxColor, Position};
2020-07-23 18:12:52 -04:00
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),
2020-07-27 12:09:55 -04:00
"X" => {
2020-07-23 18:12:52 -04:00
create_floor(world, position);
create_wall(world, position);
}
2020-07-27 12:09:55 -04:00
"@" => {
2020-07-23 18:12:52 -04:00
create_floor(world, position);
create_player(world, position);
}
2020-07-27 12:09:55 -04:00
"*" => {
2020-07-23 18:12:52 -04:00
create_floor(world, position);
2020-07-28 10:04:44 -04:00
create_box(world, position, BoxColor::Brown);
2020-07-23 18:12:52 -04:00
}
"+" => {
2020-07-23 18:12:52 -04:00
create_floor(world, position);
2020-07-24 17:55:38 -04:00
create_box(world, position, BoxColor::Red);
}
2020-07-27 12:09:55 -04:00
"%" => {
2020-07-24 17:55:38 -04:00
create_floor(world, position);
2020-07-28 10:04:44 -04:00
create_box_spot(world, position, BoxColor::Brown);
2020-07-24 17:55:38 -04:00
}
"=" => {
2020-07-24 17:55:38 -04:00
create_floor(world, position);
create_box_spot(world, position, BoxColor::Red);
2020-07-23 18:12:52 -04:00
}
"-" => (),
2020-07-23 18:12:52 -04:00
c => panic!("unrecognized map item {}", c),
}
}
}
}