More work

This commit is contained in:
User Jlusiardi 2023-12-09 22:43:06 +01:00
parent 206a49fb08
commit 643f3b76f9
4 changed files with 50 additions and 37 deletions

View File

@ -16,16 +16,15 @@ impl Game {
pub fn new(p: Player) -> Game {
let mut v: Vec<Level> = Vec::with_capacity(LEVELS);
for d in 0..LEVELS {
v.push(LevelGenerator::generate(d).render());
v.push(LevelGenerator::generate(d, d==0, d==LEVELS-1).render());
}
let mut g = Game {
player: p,
levels: v,
};
let mut start = (0, 0);
{
start = g.get_level(0).start;
}
let start = {
g.get_level(0).start
};
g.get_mutable_player().get_position().set(0, start.0, start.1);
g.get_mutable_level(0).discover(&Position::new(0, start.0, start.1));
g
@ -60,6 +59,11 @@ impl Game {
let (x, y) = self.levels[level].start;
(level, x, y)
}
fn prev_end(&self) -> (usize, usize, usize) {
let level = self.player.get_immutable_position().get_level() - 1;
let (x, y) = self.levels[level].end;
(level, x, y)
}
fn next_element(&mut self, dx: i16, dy: i16) -> (Option<StructureElement>, Option<&mut Monster>, Option<&mut Box<(dyn Artifact + 'static)>>) {
let player_pos = &self.player.get_position();
let new_x: i16 = player_pos.get_x() as i16 + dx;
@ -68,34 +72,40 @@ impl Game {
let level = &mut self.levels[player_level];
level.get_element(new_x, new_y)
}
pub fn move_player(&mut self, dx: i16, dy: i16) {
pub fn move_player(&mut self, mut dx: i16, mut dy: i16) {
// verify the player can do the move
if !self.can_move(dx, dy) {
return;
}
// // let player_pos = &self.player.get_position();
let player_pos = &self.player.get_position();
// // let new_x: i16 = player_pos.get_x() as i16 + dx;
// // let new_y: i16 = player_pos.get_y() as i16 + dy;
// // let player_level = player_pos.get_level();
let mut player_level = player_pos.get_level();
// // let level = &mut self.levels[player_level];
// let element = self.next_element(dx, dy);
let element = self.next_element(dx, dy);
//
// // use stairs if walked onto one
// match element {
// (Some(e), _, _) => {
// match e {
// StructureElement::StairDown => {
// let (next_level, x, y) = self.next_start();
// self.get_mutable_player().get_position().set(next_level, x, y);
// self.levels[next_level].discover(self.player.get_position());
// }
// StructureElement::StairUp => {}
// _ => {}
// }
// }
// (_, _, _) => {}
// }
match element {
(Some(e), None, None) => {
match e {
StructureElement::StairDown => {
(dx, dy) = (0, 0);
let (next_level, x, y) = self.next_start();
player_level = next_level;
self.get_mutable_player().get_position().set(next_level, x, y);
}
StructureElement::StairUp => {
(dx, dy) = (0, 0);
let (next_level, x, y) = self.prev_end();
player_level = next_level;
self.get_mutable_player().get_position().set(next_level, x, y);
}
_ => {}
}
}
(_, _, _) => {}
}
//
// // interact with monsters / artifacts
// match element {
@ -108,7 +118,7 @@ impl Game {
// update position and discover area on new position
let (new_x, new_y) = self.get_mutable_player().get_position().change(dx, dy);
self.get_mutable_level(0).discover(&Position::new(0, new_x, new_y));
self.get_mutable_level(player_level).discover(&Position::new(player_level, new_x, new_y));
}
}

View File

@ -1,5 +1,7 @@
use std::cmp::{max, min};
use crate::artifacts::{Artifact, Chest, Potion};
use crate::artifacts::Artifact;
#[cfg(test)]
use crate::artifacts::{Chest, Potion};
use crate::monster::Monster;
use crate::position::Position;

View File

@ -84,7 +84,7 @@ pub struct LevelGenerator {
}
impl LevelGenerator {
pub fn generate(level: usize) -> Self {
pub fn generate(level: usize, first: bool, last: bool) -> Self {
let mut rng = rand::thread_rng();
let mut rooms = [[Room::new(); ROOMS_HORIZONTAL]; ROOMS_VERTICAL];
let mut graph = UnGraph::<(usize, usize), u16>::default();
@ -95,13 +95,13 @@ impl LevelGenerator {
while !LevelGenerator::rooms_connectable(&rooms) {
let mut room_types: Vec<RoomType> = Vec::with_capacity(ROOMS_HORIZONTAL * ROOMS_VERTICAL);
// level 0 contains a start room, all others contain a stair up
if level == 0 {
if first {
room_types.push(RoomType::Start);
} else {
room_types.push(RoomType::StairUp);
}
// level 24 (the last) contains an end room, all others a stair down
if level == 24 {
if last {
room_types.push(RoomType::End);
} else {
room_types.push(RoomType::StairDown);
@ -276,7 +276,8 @@ impl LevelGenerator {
// TODO randomize enemies here
enemies.push(Monster::new_with_position(10, Position::new(self.level, t_x, t_y)));
}
if room.kind == RoomType::StairDown {
if room.kind == RoomType::End || room.kind == RoomType::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);
}
@ -286,6 +287,7 @@ impl LevelGenerator {
if room.kind == RoomType::End {
structure[end_x][end_y] = StructureElement::End;
}
if room.kind == RoomType::Start || room.kind == RoomType::StairUp {
start_x = left + room.offset_x + rng.gen_range(0..room.width);
start_y = top + room.offset_y + rng.gen_range(0..room.height);
@ -341,7 +343,7 @@ impl LevelGenerator {
#[test]
fn test_level_gen() {
let level = LevelGenerator::generate(23).render();
let level = LevelGenerator::generate(23, false, false).render();
assert_eq!(level.level, 23);
assert_ne!(level.start, (0, 0));
assert_ne!(level.end, (0, 0));

View File

@ -3,7 +3,6 @@ use ratatui::layout::Rect;
use ratatui::style::Color;
use ratatui::widgets::{StatefulWidget, Widget};
use crate::artifacts::Artifact;
use crate::game::Game;
use crate::level::StructureElement;
@ -66,7 +65,7 @@ impl StatefulWidget for LevelWidget {
}
}
}
(_, Some(t), _) => {
(_, Some(_), _) => {
self.set_cell(buf, x, y, "M", Color::Red, Color::Gray);
}
(_, _, Some(t)) => {
@ -89,5 +88,5 @@ impl StatefulWidget for LevelWidget {
}
impl Widget for LevelWidget {
fn render(self, area: Rect, buf: &mut Buffer) {}
fn render(self, _area: Rect, _buf: &mut Buffer) {}
}