Implement level change

This commit is contained in:
Joachim Lusiardi 2023-11-19 18:50:55 +01:00
parent 7e6b2b287b
commit d44c0fd53e
6 changed files with 37 additions and 5 deletions

View File

@ -24,7 +24,21 @@ impl Game {
} }
}; };
if can_go { if can_go {
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); self.player.change_position(dx, dy);
} }
} }
}
} }

View File

@ -24,6 +24,8 @@ pub struct Level {
pub(crate) structure: [[LevelElement; LEVEL_HEIGHT]; LEVEL_WIDTH], pub(crate) structure: [[LevelElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
pub(crate) start_x: usize, pub(crate) start_x: usize,
pub(crate) start_y: usize, pub(crate) start_y: usize,
pub(crate) end_x: usize,
pub(crate) end_y: usize,
} }
impl Level { impl Level {
@ -33,6 +35,8 @@ impl Level {
structure: s, structure: s,
start_x: 1, start_x: 1,
start_y: 1, start_y: 1,
end_x: 1,
end_y: 1
} }
} }
pub fn get_element(&self, x: i16, y: i16) -> Option<LevelElement> { pub fn get_element(&self, x: i16, y: i16) -> Option<LevelElement> {

View File

@ -44,7 +44,6 @@ impl Room {
height: 0, height: 0,
connection_down: None, connection_down: None,
connection_right: None, connection_right: None,
// connected: false,
} }
} }
/// change the size and position of a room randomly within its bounds /// 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 s = [[LevelElement::Wall; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH];
let mut start_x: usize = 0; let mut start_x: usize = 0;
let mut start_y: 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 c in 0..ROOMS_VERTICAL {
for r in 0..ROOMS_HORIZONTAL { for r in 0..ROOMS_HORIZONTAL {
let top = 1 + r * ROOM_HEIGHT; let top = 1 + r * ROOM_HEIGHT;
@ -239,7 +240,9 @@ impl LevelGenerator {
} }
} }
if room.kind == RoomType::StairDown { 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 { if room.kind == RoomType::Start || room.kind == RoomType::StairUp {
start_x = left + room.offset_x + rng.gen_range(0..room.width); start_x = left + room.offset_x + rng.gen_range(0..room.width);
@ -283,6 +286,8 @@ impl LevelGenerator {
structure: s, structure: s,
start_x, start_x,
start_y, start_y,
end_x,
end_y,
} }
} }
} }

View File

@ -96,10 +96,10 @@ impl<'a> Widget for LevelWidget<'a> {
self.set_cell(buf, x, y, " ", FG_BROWN, Color::Gray); self.set_cell(buf, x, y, " ", FG_BROWN, Color::Gray);
} }
LevelElement::StairDown => { LevelElement::StairDown => {
self.set_cell(buf, x, y, ">", Color::Yellow, Color::Gray); self.set_cell(buf, x, y, ">", Color::Black, Color::Gray);
} }
LevelElement::StairUp => { LevelElement::StairUp => {
self.set_cell(buf, x, y, "<", Color::Yellow, Color::Gray); self.set_cell(buf, x, y, "<", Color::Black, Color::Gray);
} }
} }
} }

View File

@ -32,6 +32,10 @@ impl Player {
pub fn change_position(&mut self, dx: i8, dy: i8) { pub fn change_position(&mut self, dx: i8, dy: i8) {
self.position.change(dx, dy); self.position.change(dx, dy);
} }
pub fn set_position(&mut self, level: u8, x: u16, y:u16) {
self.position.set(level, x, y);
}
} }
#[test] #[test]

View File

@ -29,4 +29,9 @@ impl Position {
self.x = ((self.x as i16) + dx as i16) as u16; self.x = ((self.x as i16) + dx as i16) as u16;
self.y = ((self.y as i16) + dy 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;
}
} }