diff --git a/src/game.rs b/src/game.rs index deab1f3..7c50f7e 100644 --- a/src/game.rs +++ b/src/game.rs @@ -24,7 +24,21 @@ impl Game { } }; if can_go { - self.player.change_position(dx, dy); + if level.get_element(new_x as i16, new_y as i16).unwrap() == LevelElement::StairDown { + self.player.set_position( + player_pos.get_level() + 1, + self.levels[(player_pos.get_level() + 1) as usize].start_x as u16, + self.levels[(player_pos.get_level() + 1) as usize].start_y as u16, + ); + } else if level.get_element(new_x as i16, new_y as i16).unwrap() == LevelElement::StairUp { + self.player.set_position( + player_pos.get_level() - 1, + self.levels[(player_pos.get_level() - 1) as usize].end_x as u16, + self.levels[(player_pos.get_level() - 1) as usize].end_y as u16, + ); + } else { + self.player.change_position(dx, dy); + } } } } \ No newline at end of file diff --git a/src/level.rs b/src/level.rs index 3246e65..32f9753 100644 --- a/src/level.rs +++ b/src/level.rs @@ -24,6 +24,8 @@ pub struct Level { pub(crate) structure: [[LevelElement; LEVEL_HEIGHT]; LEVEL_WIDTH], pub(crate) start_x: usize, pub(crate) start_y: usize, + pub(crate) end_x: usize, + pub(crate) end_y: usize, } impl Level { @@ -33,6 +35,8 @@ impl Level { structure: s, start_x: 1, start_y: 1, + end_x: 1, + end_y: 1 } } pub fn get_element(&self, x: i16, y: i16) -> Option { diff --git a/src/level_generator.rs b/src/level_generator.rs index d9ee502..3c560d8 100644 --- a/src/level_generator.rs +++ b/src/level_generator.rs @@ -44,7 +44,6 @@ impl Room { height: 0, connection_down: None, connection_right: None, - // connected: false, } } /// change the size and position of a room randomly within its bounds @@ -228,6 +227,8 @@ impl LevelGenerator { let mut s = [[LevelElement::Wall; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH]; let mut start_x: usize = 0; let mut start_y: usize = 0; + let mut end_x: usize = 0; + let mut end_y: usize = 0; for c in 0..ROOMS_VERTICAL { for r in 0..ROOMS_HORIZONTAL { let top = 1 + r * ROOM_HEIGHT; @@ -239,7 +240,9 @@ impl LevelGenerator { } } if room.kind == RoomType::StairDown { - s[left + room.offset_x + rng.gen_range(0..room.width)][top + room.offset_y + rng.gen_range(0..room.height)] = LevelElement::StairDown; + end_x = left + room.offset_x + rng.gen_range(0..room.width); + end_y = top + room.offset_y + rng.gen_range(0..room.height); + s[end_x][end_y] = LevelElement::StairDown; } if room.kind == RoomType::Start || room.kind == RoomType::StairUp { start_x = left + room.offset_x + rng.gen_range(0..room.width); @@ -283,6 +286,8 @@ impl LevelGenerator { structure: s, start_x, start_y, + end_x, + end_y, } } } diff --git a/src/level_widget.rs b/src/level_widget.rs index 1f63915..8570d23 100644 --- a/src/level_widget.rs +++ b/src/level_widget.rs @@ -96,10 +96,10 @@ impl<'a> Widget for LevelWidget<'a> { self.set_cell(buf, x, y, " ", FG_BROWN, Color::Gray); } LevelElement::StairDown => { - self.set_cell(buf, x, y, ">", Color::Yellow, Color::Gray); + self.set_cell(buf, x, y, ">", Color::Black, Color::Gray); } LevelElement::StairUp => { - self.set_cell(buf, x, y, "<", Color::Yellow, Color::Gray); + self.set_cell(buf, x, y, "<", Color::Black, Color::Gray); } } } diff --git a/src/player.rs b/src/player.rs index 957a635..a17cf5e 100644 --- a/src/player.rs +++ b/src/player.rs @@ -32,6 +32,10 @@ impl Player { pub fn change_position(&mut self, dx: i8, dy: i8) { self.position.change(dx, dy); } + + pub fn set_position(&mut self, level: u8, x: u16, y:u16) { + self.position.set(level, x, y); + } } #[test] diff --git a/src/position.rs b/src/position.rs index d138431..20e50ef 100644 --- a/src/position.rs +++ b/src/position.rs @@ -29,4 +29,9 @@ impl Position { self.x = ((self.x as i16) + dx as i16) as u16; self.y = ((self.y as i16) + dy as i16) as u16; } + pub fn set(&mut self, level: u8, x: u16, y:u16) { + self.level = level; + self.x = x; + self.y = y; + } } \ No newline at end of file