From 8b23c565b843b9896cdf5d8df9b62faa7cd9adfb Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Fri, 1 Nov 2024 12:06:50 +0100 Subject: [PATCH] make connections more variable --- src/room.rs | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/room.rs b/src/room.rs index a332c6d..ed10a6c 100644 --- a/src/room.rs +++ b/src/room.rs @@ -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{ + pub fn render( + &self, + rng: &mut ThreadRng, + tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH], + ) { + fn calc_range(a: usize, b: usize) -> RangeInclusive { 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; }