Compare commits

..

No commits in common. "789a41cb3f30409b6f7e1c093ec4efbea049e7dd" and "8ee142586b191cf29be5b64c230ad964db26b107" have entirely different histories.

3 changed files with 46 additions and 37 deletions

View File

@ -310,11 +310,11 @@ impl LevelGenerator {
for row in 0..ROOMS_VERTICAL { for row in 0..ROOMS_VERTICAL {
if let Some(connection) = self.rooms[col][row].connection_down { if let Some(connection) = self.rooms[col][row].connection_down {
// println!("down"); // println!("down");
connection.render(&mut self.rng, &mut structure); connection.render(&mut structure);
} }
if let Some(connection) = self.rooms[col][row].connection_right { if let Some(connection) = self.rooms[col][row].connection_right {
// println!("right"); // println!("right");
connection.render(&mut self.rng,&mut structure); connection.render(&mut structure);
} }
} }
} }
@ -331,22 +331,20 @@ impl LevelGenerator {
} }
} }
#[test] // #[test]
fn test_level_gen() { // fn test_level_gen() {
let mut rng = rand::thread_rng(); // for _ in 0..1000 {
for _ in 0..1000 { // LevelGenerator::generate(0, true, false).render();
LevelGenerator::generate(0, true, false).render(); // }
} // }
}
#[test] // #[test]
fn test_level_gen_respects_level() { // fn test_level_gen_respects_level() {
let mut rng = rand::thread_rng(); // let level = LevelGenerator::generate(0, true, false).render();
let level = LevelGenerator::generate(0, true, false).render(); // assert_eq!(0, level.level);
assert_eq!(0, level.level); // let level = LevelGenerator::generate(1, true, false).render();
let level = LevelGenerator::generate(1, true, false).render(); // assert_eq!(1, level.level);
assert_eq!(1, level.level); // }
}
#[cfg(test)] #[cfg(test)]
fn find_room_types(rooms: &Vec<Room>) -> (bool, bool, bool, bool) { fn find_room_types(rooms: &Vec<Room>) -> (bool, bool, bool, bool) {

View File

@ -56,7 +56,7 @@ create_monster!(
name:"spider".to_string(), name:"spider".to_string(),
life: 3, life: 3,
symbol: String::from("s"), symbol: String::from("s"),
color: Color::Yellow, color: Color::Blue,
experience_gain: 7, experience_gain: 7,
ticks_between_steps: 7, ticks_between_steps: 7,
damage_range: 2..=3, damage_range: 2..=3,

View File

@ -1,4 +1,4 @@
use std::{cmp::{max, min}, ops::RangeInclusive}; use std::cmp::{max, min};
use crate::{ use crate::{
constants::{LEVEL_HEIGHT, LEVEL_WIDTH, ROOM_HEIGHT, ROOM_WIDTH}, constants::{LEVEL_HEIGHT, LEVEL_WIDTH, ROOM_HEIGHT, ROOM_WIDTH},
@ -26,14 +26,7 @@ pub struct Connection {
} }
impl Connection { impl Connection {
pub fn render(&self, rng: &mut ThreadRng, tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH]) { pub fn render(&self, tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH]) {
fn calc_range(a: usize, b:usize) -> RangeInclusive<usize>{
if a > b {
b..=a
} else {
a..=b
}
}
// the tuples are (col, row) // the tuples are (col, row)
let abs_d_col = let abs_d_col =
max(self.start_pos.0, self.end_pos.0) - min(self.start_pos.0, self.end_pos.0); max(self.start_pos.0, self.end_pos.0) - min(self.start_pos.0, self.end_pos.0);
@ -42,11 +35,22 @@ impl Connection {
max(self.start_pos.1, self.end_pos.1) - min(self.start_pos.1, self.end_pos.1); max(self.start_pos.1, self.end_pos.1) - min(self.start_pos.1, self.end_pos.1);
if abs_d_col == 0 { if abs_d_col == 0 {
for row in calc_range(self.end_pos.1, self.start_pos.1 ) { let range = if self.end_pos.1 > self.start_pos.1 {
self.start_pos.1..=self.end_pos.1
} else {
self.end_pos.1..=self.start_pos.1
};
for row in range {
tgt[self.end_pos.0][row] = StructureElement::Floor; tgt[self.end_pos.0][row] = StructureElement::Floor;
} }
} else if abs_d_row == 0 { } else if abs_d_row == 0 {
for col in calc_range(self.end_pos.0, self.start_pos.0) { let range = if self.end_pos.0 > self.start_pos.0 {
self.start_pos.0..=self.end_pos.0
} else {
self.end_pos.0..=self.start_pos.0
};
for col in range {
tgt[col][self.end_pos.1] = StructureElement::Floor; tgt[col][self.end_pos.1] = StructureElement::Floor;
} }
} else { } else {
@ -54,16 +58,18 @@ impl Connection {
if d_row > 0 { if d_row > 0 {
// more up/down // more up/down
let d_row = self.end_pos.1 - self.start_pos.1; let d_row = self.end_pos.1 - self.start_pos.1;
let vert_offset = if d_row > 3 { for r in self.start_pos.1..=(self.start_pos.1 + d_row / 2) {
rng.gen_range(1..=d_row-1)
} else {d_row/2};
for r in self.start_pos.1..=(self.start_pos.1 + vert_offset) {
tgt[self.start_pos.0][r] = StructureElement::Floor; tgt[self.start_pos.0][r] = StructureElement::Floor;
} }
for c in calc_range(self.end_pos.0, self.start_pos.0) { let range = if self.end_pos.0 > self.start_pos.0 {
tgt[c][self.start_pos.1 + vert_offset] = StructureElement::Floor; self.start_pos.0..=self.end_pos.0
} else {
self.end_pos.0..=self.start_pos.0
};
for c in range {
tgt[c][self.start_pos.1 + d_row / 2] = StructureElement::Floor;
} }
for r in (self.start_pos.1 + vert_offset)..=self.end_pos.1 { for r in (self.start_pos.1 + d_row / 2)..=self.end_pos.1 {
tgt[self.end_pos.0][r] = StructureElement::Floor; tgt[self.end_pos.0][r] = StructureElement::Floor;
} }
} else { } else {
@ -72,7 +78,12 @@ impl Connection {
for tgt_col in tgt.iter_mut().skip(self.start_pos.0).take(d_col / 2 + 1) { for tgt_col in tgt.iter_mut().skip(self.start_pos.0).take(d_col / 2 + 1) {
tgt_col[self.start_pos.1] = StructureElement::Floor; tgt_col[self.start_pos.1] = StructureElement::Floor;
} }
for r in calc_range(self.end_pos.1, self.start_pos.1) { let range = if self.end_pos.1 > self.start_pos.1 {
self.start_pos.1..=self.end_pos.1
} else {
self.end_pos.1..=self.start_pos.1
};
for r in range {
tgt[self.start_pos.0 + d_col / 2][r] = StructureElement::Floor; tgt[self.start_pos.0 + d_col / 2][r] = StructureElement::Floor;
} }
for tgt_col in tgt for tgt_col in tgt