improve paths

This commit is contained in:
Joachim Lusiardi 2024-10-31 21:52:04 +01:00
parent f5d257e826
commit 3096386ad1
2 changed files with 74 additions and 29 deletions

View File

@ -203,12 +203,13 @@ impl LevelGenerator {
});
} else {
// println!("Right");
let start_col = 1 + (src_node_col * ROOM_WIDTH) + src_room.offset_x + src_room.width;
let start_col =
1 + (src_node_col * ROOM_WIDTH) + src_room.offset_x + src_room.width;
let start_row =
1 + src_node_row * ROOM_HEIGHT + src_room.offset_y + (src_room.height-1) / 2;
1 + src_node_row * ROOM_HEIGHT + src_room.offset_y + (src_room.height - 1) / 2;
let end_col = 1 + tgt_node_col * ROOM_WIDTH - 1 + tgt_room.offset_x;
let end_row =
1 + tgt_node_row * ROOM_HEIGHT + tgt_room.offset_y + (tgt_room.height-1) / 2;
1 + tgt_node_row * ROOM_HEIGHT + tgt_room.offset_y + (tgt_room.height - 1) / 2;
rooms[src_node_col][src_node_row].connection_right = Some(Connection {
start_pos: (start_col, start_row),
end_pos: (end_col, end_row),
@ -253,7 +254,7 @@ impl LevelGenerator {
let mst: Graph<(usize, usize), u16, petgraph::Undirected> =
LevelGenerator::create_mst(&rooms);
println!("{:?}", mst);
LevelGenerator::create_connections(&mut rooms, &mst);
LevelGenerator { level, rooms, rng }
}

View File

@ -1,7 +1,4 @@
use std::{
cmp::{max, min},
ops::Range,
};
use std::cmp::{max, min};
use crate::{
constants::{LEVEL_HEIGHT, LEVEL_WIDTH, ROOM_HEIGHT, ROOM_WIDTH},
@ -31,21 +28,68 @@ pub struct Connection {
impl Connection {
pub fn render(&self, tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH]) {
// the tuples are (col, row)
println!("{:?} -> {:?}",self.start_pos, self.end_pos);
let d_col = max(self.start_pos.0, self.end_pos.0) - min(self.start_pos.0, self.end_pos.0);
let abs_d_col =
max(self.start_pos.0, self.end_pos.0) - min(self.start_pos.0, self.end_pos.0);
let d_row = max(self.start_pos.1, self.end_pos.1) - min(self.start_pos.1, self.end_pos.1);
let abs_d_row =
max(self.start_pos.1, self.end_pos.1) - min(self.start_pos.1, self.end_pos.1);
if d_col == 0 {
for row in self.start_pos.1..=self.end_pos.1 {
if abs_d_col == 0 {
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;
}
}
if d_row == 0 {
for col in self.start_pos.0..=self.end_pos.0 {
} else if abs_d_row == 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;
}
} else {
let d_row = self.end_pos.1 as i32 - self.start_pos.1 as i32;
if d_row > 0 {
// more up/down
let d_row = self.end_pos.1 - self.start_pos.1;
for r in self.start_pos.1..=(self.start_pos.1 + d_row / 2) {
tgt[self.start_pos.0][r] = StructureElement::Floor;
}
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 c in range {
tgt[c][self.start_pos.1 + d_row / 2] = StructureElement::Floor;
}
for r in (self.start_pos.1 + d_row / 2)..=self.end_pos.1 {
tgt[self.end_pos.0][r] = StructureElement::Floor;
}
} else {
// more left/right
let d_col = self.end_pos.0 - self.start_pos.0;
for c in self.start_pos.0..=(self.start_pos.0 + d_col / 2) {
tgt[c][self.start_pos.1] = StructureElement::Floor;
}
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;
}
for c in (self.start_pos.0 + d_col / 2)..=self.end_pos.0 {
tgt[c][self.end_pos.1] = StructureElement::Floor;
}
}
}
}
}
@ -64,12 +108,12 @@ pub struct Room {
impl Room {
pub fn new(rng: &mut ThreadRng) -> Self {
let width = ROOM_WIDTH-1; //rng.gen_range(3..ROOM_WIDTH);
let height = ROOM_HEIGHT-1; //rng.gen_range(3..ROOM_HEIGHT);
let offset_x = 0; //rng.gen_range(0..(ROOM_WIDTH - width));
let offset_y = 0; //rng.gen_range(0..(ROOM_HEIGHT - height));
let sx = offset_x + rng.gen_range(1..width-1);
let sy = offset_y + rng.gen_range(1..height-1);
let width = rng.gen_range(3..ROOM_WIDTH);
let height = rng.gen_range(3..ROOM_HEIGHT);
let offset_x = rng.gen_range(0..(ROOM_WIDTH - width));
let offset_y = rng.gen_range(0..(ROOM_HEIGHT - height));
let sx = offset_x + rng.gen_range(1..width - 1);
let sy = offset_y + rng.gen_range(1..height - 1);
Self {
kind: RoomType::EmptyRoom,
@ -83,13 +127,13 @@ impl Room {
}
}
pub fn get_x_range(&self) -> Range<usize> {
self.offset_x..self.offset_x + self.width
}
// pub fn get_x_range(&self) -> Range<usize> {
// self.offset_x..self.offset_x + self.width
// }
pub fn get_y_range(&self) -> Range<usize> {
self.offset_y..self.offset_y + self.height
}
// pub fn get_y_range(&self) -> Range<usize> {
// self.offset_y..self.offset_y + self.height
// }
pub fn render(
&self,