Finish 2.1, loading a map

This commit is contained in:
Timothy Warren 2020-07-23 14:01:14 -04:00
parent a318213a60
commit e94defda89

View File

@ -20,6 +20,16 @@ pub struct Position {
z: u8,
}
impl Position {
pub fn new(x: u8, y: u8) -> Self {
Position {
x,
y,
z: 0,
}
}
}
#[derive(Component)]
#[storage(VecStorage)]
pub struct Renderable {
@ -163,95 +173,61 @@ pub fn create_player(world: &mut World, position: Position) {
.build();
}
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),
}
}
}
}
pub fn initialize_level(world: &mut World) {
create_player(
world,
Position {
x: 0,
y: 0,
z: 0, // we will get the z from the factory functions
},
);
create_floor(
world,
Position {
x: 0,
y: 1,
z: 0,
}
);
create_player(
world,
Position {
x: 0,
y: 1,
z: 0, // we will get the z from the factory functions
},
);
create_wall(
world,
Position {
x: 1,
y: 0,
z: 0, // we will get the z from the factory functions
},
);
create_floor(
world,
Position {
x: 1,
y: 1,
z: 0,
}
);
create_box(
world,
Position {
x: 2,
y: 0,
z: 0, // we will get the z from the factory functions
},
);
create_floor(
world,
Position {
x: 2,
y: 1,
z: 0,
}
);
create_box(
world,
Position {
x: 2,
y: 1,
z: 0, // we will get the z from the factory functions
},
);
create_box_spot(
world,
Position {
x: 3,
y: 0,
z: 0, // we will get the z from the factory functions
},
);
create_box_spot(
world,
Position {
x: 3,
y: 1,
z: 0, // we will get the z from the factory functions
},
);
create_box(
world,
Position {
x: 3,
y: 1,
z: 0, // we will get the z from the factory functions
},
);
const MAP: &str = "
N N W W W W W W
W W W . . . . W
W . . . B . . W
W . . . . . . W
W . P . . . . W
W . . . . . . W
W . . S . . . W
W . . . . . . W
W W W W W W W W
";
load_map(world, MAP.to_string());
}