From e94defda8946bc23448acf3c75f1b450e0bbc8f0 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 23 Jul 2020 14:01:14 -0400 Subject: [PATCH] Finish 2.1, loading a map --- src/main.rs | 152 ++++++++++++++++++++++------------------------------ 1 file changed, 64 insertions(+), 88 deletions(-) diff --git a/src/main.rs b/src/main.rs index 061b118..5e1fa6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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()); }