generator except paths
This commit is contained in:
parent
95f5fd4944
commit
a9c22f0944
|
@ -25,7 +25,9 @@ pub enum RoomType {
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||||
pub struct Level {
|
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 {
|
impl Level {
|
||||||
|
@ -57,7 +59,9 @@ impl Level {
|
||||||
pub fn new() -> Level {
|
pub fn new() -> Level {
|
||||||
let mut s = [[LevelElement::Wall; LEVEL_HEIGHT]; LEVEL_WIDTH];
|
let mut s = [[LevelElement::Wall; LEVEL_HEIGHT]; LEVEL_WIDTH];
|
||||||
Level {
|
Level {
|
||||||
structure: s
|
structure: s,
|
||||||
|
start_x: 1,
|
||||||
|
start_y: 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn generate(&mut self, level: usize) {
|
pub fn generate(&mut self, level: usize) {
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
|
use std::any::Any;
|
||||||
use rand::prelude::SliceRandom;
|
use rand::prelude::SliceRandom;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
use crate::level::RoomType;
|
use crate::level::{Level, LevelElement, RoomType};
|
||||||
|
use crate::position::Position;
|
||||||
|
|
||||||
const ROOMS_VERTICAL: usize = 7;
|
const ROOMS_VERTICAL: usize = 7;
|
||||||
const ROOMS_HORIZONTAL: usize = 4;
|
const ROOMS_HORIZONTAL: usize = 4;
|
||||||
|
@ -36,12 +38,12 @@ impl Room {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Level {
|
pub struct LevelGenerator {
|
||||||
rooms: [[Room; ROOMS_HORIZONTAL]; ROOMS_VERTICAL],
|
rooms: [[Room; ROOMS_HORIZONTAL]; ROOMS_VERTICAL],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Level {
|
impl LevelGenerator {
|
||||||
fn generate(level: usize) -> Self {
|
pub fn generate(level: usize) -> Self {
|
||||||
let mut room_types: Vec<RoomType> = Vec::with_capacity(ROOMS_HORIZONTAL * ROOMS_VERTICAL);
|
let mut room_types: Vec<RoomType> = Vec::with_capacity(ROOMS_HORIZONTAL * ROOMS_VERTICAL);
|
||||||
if level > 0 {
|
if level > 0 {
|
||||||
room_types.push(RoomType::StairUp);
|
room_types.push(RoomType::StairUp);
|
||||||
|
@ -106,15 +108,48 @@ impl Level {
|
||||||
}
|
}
|
||||||
println!("Unconnected: {}", unconnected_rooms);
|
println!("Unconnected: {}", unconnected_rooms);
|
||||||
|
|
||||||
Level {
|
LevelGenerator {
|
||||||
rooms
|
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]
|
#[test]
|
||||||
fn test_level_gen() {
|
fn test_level_gen() {
|
||||||
let level = Level::generate(0);
|
let level = LevelGenerator::generate(0);
|
||||||
println!("{:?}", level);
|
println!("{:?}", level);
|
||||||
assert_eq!(1, 2);
|
assert_eq!(1, 2);
|
||||||
}
|
}
|
12
src/main.rs
12
src/main.rs
|
@ -15,6 +15,7 @@ use crate::game::Game;
|
||||||
use crate::level::{Level, LevelElement};
|
use crate::level::{Level, LevelElement};
|
||||||
use crate::level_widget::LevelWidget;
|
use crate::level_widget::LevelWidget;
|
||||||
use crate::player::Player;
|
use crate::player::Player;
|
||||||
|
use crate::level_generator::LevelGenerator;
|
||||||
|
|
||||||
mod game;
|
mod game;
|
||||||
mod player;
|
mod player;
|
||||||
|
@ -26,21 +27,14 @@ mod level_generator;
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let mut levels: [Level; 25] = [Level::new(); 25];
|
let mut levels: [Level; 25] = [Level::new(); 25];
|
||||||
for l in 0..25 {
|
for l in 0..25 {
|
||||||
levels[l].generate(l);
|
levels[l] = LevelGenerator::generate(l).render();
|
||||||
}
|
}
|
||||||
let mut g = Game {
|
let mut g = Game {
|
||||||
player: Player::new("Teddy Tester", 10),
|
player: Player::new("Teddy Tester", 10),
|
||||||
levels,
|
levels,
|
||||||
};
|
};
|
||||||
|
|
||||||
// place player on first upper left floor
|
g.player.change_position((levels[0].start_x) as i8, (levels[0].start_y) as i8);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stdout().execute(EnterAlternateScreen)?;
|
stdout().execute(EnterAlternateScreen)?;
|
||||||
enable_raw_mode()?;
|
enable_raw_mode()?;
|
||||||
|
|
|
@ -12,7 +12,7 @@ impl Player {
|
||||||
pub fn new(name: &str, max_life: i16) -> Player {
|
pub fn new(name: &str, max_life: i16) -> Player {
|
||||||
Player {
|
Player {
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
position: Position::new(0, 1, 1),
|
position: Position::new(0, 0, 0),
|
||||||
life: max_life,
|
life: max_life,
|
||||||
max_life,
|
max_life,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue