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