make connections more variable

This commit is contained in:
Joachim Lusiardi 2024-11-01 12:06:50 +01:00
parent 789a41cb3f
commit 8b23c565b8

View File

@ -1,4 +1,7 @@
use std::{cmp::{max, min}, ops::RangeInclusive};
use std::{
cmp::{max, min},
ops::RangeInclusive,
};
use crate::{
constants::{LEVEL_HEIGHT, LEVEL_WIDTH, ROOM_HEIGHT, ROOM_WIDTH},
@ -26,12 +29,16 @@ pub struct Connection {
}
impl Connection {
pub fn render(&self, rng: &mut ThreadRng, tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH]) {
fn calc_range(a: usize, b:usize) -> RangeInclusive<usize>{
pub fn render(
&self,
rng: &mut ThreadRng,
tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
) {
fn calc_range(a: usize, b: usize) -> RangeInclusive<usize> {
if a > b {
b..=a
b..=a
} else {
a..=b
a..=b
}
}
// the tuples are (col, row)
@ -42,7 +49,7 @@ impl Connection {
max(self.start_pos.1, self.end_pos.1) - min(self.start_pos.1, self.end_pos.1);
if abs_d_col == 0 {
for row in calc_range(self.end_pos.1, self.start_pos.1 ) {
for row in calc_range(self.end_pos.1, self.start_pos.1) {
tgt[self.end_pos.0][row] = StructureElement::Floor;
}
} else if abs_d_row == 0 {
@ -55,8 +62,10 @@ impl Connection {
// more up/down
let d_row = self.end_pos.1 - self.start_pos.1;
let vert_offset = if d_row > 3 {
rng.gen_range(1..=d_row-1)
} else {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;
}
@ -69,16 +78,25 @@ impl Connection {
} else {
// more left/right
let d_col = self.end_pos.0 - self.start_pos.0;
for tgt_col in tgt.iter_mut().skip(self.start_pos.0).take(d_col / 2 + 1) {
let horizont_offset = if d_col > 3 {
rng.gen_range(1..=d_col - 1)
} else {
d_col / 2
};
for tgt_col in tgt
.iter_mut()
.skip(self.start_pos.0)
.take(horizont_offset + 1)
{
tgt_col[self.start_pos.1] = StructureElement::Floor;
}
for r in calc_range(self.end_pos.1, self.start_pos.1) {
tgt[self.start_pos.0 + d_col / 2][r] = StructureElement::Floor;
tgt[self.start_pos.0 + horizont_offset][r] = StructureElement::Floor;
}
for tgt_col in tgt
.iter_mut()
.take(self.end_pos.0 + 1)
.skip(self.start_pos.0 + d_col / 2)
.skip(self.start_pos.0 + horizont_offset)
{
tgt_col[self.end_pos.1] = StructureElement::Floor;
}