generator except paths

This commit is contained in:
Joachim Lusiardi 2023-11-15 21:42:20 +01:00
parent 95f5fd4944
commit a9c22f0944
4 changed files with 54 additions and 21 deletions

View File

@ -25,7 +25,9 @@ pub enum RoomType {
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Level {
structure: [[LevelElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
pub(crate) structure: [[LevelElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
pub(crate) start_x: usize,
pub(crate) start_y: usize,
}
impl Level {
@ -57,7 +59,9 @@ impl Level {
pub fn new() -> Level {
let mut s = [[LevelElement::Wall; LEVEL_HEIGHT]; LEVEL_WIDTH];
Level {
structure: s
structure: s,
start_x: 1,
start_y: 1
}
}
pub fn generate(&mut self, level: usize) {

View File

@ -1,7 +1,9 @@
use std::any::Any;
use rand::prelude::SliceRandom;
use rand::Rng;
use crate::level::RoomType;
use crate::level::{Level, LevelElement, RoomType};
use crate::position::Position;
const ROOMS_VERTICAL: usize = 7;
const ROOMS_HORIZONTAL: usize = 4;
@ -36,12 +38,12 @@ impl Room {
}
#[derive(Debug)]
struct Level {
pub struct LevelGenerator {
rooms: [[Room; ROOMS_HORIZONTAL]; ROOMS_VERTICAL],
}
impl Level {
fn generate(level: usize) -> Self {
impl LevelGenerator {
pub fn generate(level: usize) -> Self {
let mut room_types: Vec<RoomType> = Vec::with_capacity(ROOMS_HORIZONTAL * ROOMS_VERTICAL);
if level > 0 {
room_types.push(RoomType::StairUp);
@ -90,31 +92,64 @@ impl Level {
}
let mut unconnected_rooms = 0;//ROOMS_HORIZONTAL * ROOMS_VERTICAL - 1;
rooms[0][0].connected=true;
rooms[0][0].connected = true;
for r in 0..ROOMS_VERTICAL {
for c in 0..ROOMS_HORIZONTAL {
if !rooms[r][c].connected {
continue;
}
if rooms[r][c].connection_right {
rooms[r][c+1].connected=true;
rooms[r][c + 1].connected = true;
}
if rooms[r][c].connection_down {
rooms[r+1][c].connected=true;
rooms[r + 1][c].connected = true;
}
}
}
println!("Unconnected: {}", unconnected_rooms);
Level {
LevelGenerator {
rooms
}
}
pub fn render(&self) -> Level {
let mut rng = rand::thread_rng();
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;
for c in 0..ROOMS_VERTICAL {
for r in 0..ROOMS_HORIZONTAL {
let top = 1 + r * ROOM_HEIGHT;
let left = 1 + c * ROOM_WIDTH;
let room = self.rooms[c][r];
for x in 0..room.width {
for y in 0..room.height {
s[left + room.offset_x + x][top + room.offset_y + y] = LevelElement::Floor;
}
}
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;
}
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);
}
if room.kind == RoomType::StairUp {
s[start_x][start_y] = LevelElement::StairUp;
}
}
}
Level {
structure: s,
start_x,
start_y,
}
}
}
#[test]
fn test_level_gen() {
let level = Level::generate(0);
let level = LevelGenerator::generate(0);
println!("{:?}", level);
assert_eq!(1, 2);
}

View File

@ -15,6 +15,7 @@ use crate::game::Game;
use crate::level::{Level, LevelElement};
use crate::level_widget::LevelWidget;
use crate::player::Player;
use crate::level_generator::LevelGenerator;
mod game;
mod player;
@ -26,21 +27,14 @@ mod level_generator;
fn main() -> Result<()> {
let mut levels: [Level; 25] = [Level::new(); 25];
for l in 0..25 {
levels[l].generate(l);
levels[l] = LevelGenerator::generate(l).render();
}
let mut g = Game {
player: Player::new("Teddy Tester", 10),
levels,
};
// place player on first upper left floor
for i in 0..level::LEVEL_HEIGHT {
let t = (i as usize).try_into().unwrap();
if g.levels[0].get_element(t, t) == Some(LevelElement::Floor) {
g.player.change_position((i - 1) as i8, (i - 1) as i8);
break;
}
}
g.player.change_position((levels[0].start_x) as i8, (levels[0].start_y) as i8);
stdout().execute(EnterAlternateScreen)?;
enable_raw_mode()?;

View File

@ -12,7 +12,7 @@ impl Player {
pub fn new(name: &str, max_life: i16) -> Player {
Player {
name: name.to_string(),
position: Position::new(0, 1, 1),
position: Position::new(0, 0, 0),
life: max_life,
max_life,
}