From f3e6afc1799f5979e1460ef33f9137f97632ea6d Mon Sep 17 00:00:00 2001 From: Glaeder Date: Sun, 7 Jul 2024 00:01:46 +0200 Subject: [PATCH] Added room and grid creation --- src/activities/house/grid.rs | 4 ++ src/activities/house/mod.rs | 12 +++- src/activities/house/room.rs | 110 +++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 src/activities/house/room.rs diff --git a/src/activities/house/grid.rs b/src/activities/house/grid.rs index 114eace..06d08cf 100644 --- a/src/activities/house/grid.rs +++ b/src/activities/house/grid.rs @@ -13,6 +13,10 @@ impl Default for Grid { } impl Grid { + pub fn new(nodes: Vec, connections: Vec<(usize, usize)>) -> Self { + Grid { nodes, connections } + } + fn load() -> Self { let mut grid = Self { nodes: vec![ diff --git a/src/activities/house/mod.rs b/src/activities/house/mod.rs index 643e666..32a0856 100644 --- a/src/activities/house/mod.rs +++ b/src/activities/house/mod.rs @@ -1,23 +1,29 @@ mod grid; mod player; +mod room; use grid::Grid; use player::Player; +use room::Room; #[derive(Debug, Default)] pub struct HouseState { - grid: Grid, + room: Room, + //grid: Grid, player: Player } pub fn draw(state: &crate::State, _engine: &comfy::EngineContext) { + //Draw House + state.house.room.draw(); + //Draw Grid - state.house.grid.draw(); + //state.house.grid.draw(); //Draw Player state.house.player.draw(); } pub fn update(state: &mut crate::State, _engine: &mut comfy::EngineContext) { - state.house.player.update(&state.house.grid); + state.house.player.update(&state.house.room.grid); } diff --git a/src/activities/house/room.rs b/src/activities/house/room.rs new file mode 100644 index 0000000..fd9dbb2 --- /dev/null +++ b/src/activities/house/room.rs @@ -0,0 +1,110 @@ +use comfy::*; + +use super::grid::Grid; + +#[derive(Debug)] +enum RoomType { + Kitchen, + Bath, + Toilett, + LivingRoom, + SleepingRoom +} + +#[derive(Debug)] +pub struct Room { + room_type: RoomType, + size: (u8, u8), //(width, height) + pub grid: Grid +} + +impl RoomType { + pub fn random() -> Self { + match random_i32(0, 4) { + 0 => RoomType::Kitchen, + 1 => RoomType::Bath, + 2 => RoomType::Toilett, + 3 => RoomType::LivingRoom, + 4 => RoomType::SleepingRoom, + _ => panic!("Somehow you where unlucky and got a random number out of range") + } + } +} + +impl Default for Room { + fn default() -> Self { + Room::load() //Just for testing purposes + } +} + +impl Room { + pub fn load() -> Self { + let room_type = RoomType::random(); + let size = Self::random_size(&room_type); + + + + Room { room_type, size, grid: Self::create_grid(size.0, size.1)} + } + + fn random_size(room_type: &RoomType) -> (u8, u8) { + match room_type { + RoomType::Kitchen | RoomType::LivingRoom => { + (random_i32(5, 8) as u8, 3) + } + RoomType::Bath | RoomType::SleepingRoom => { + (random_i32(4, 6) as u8, 3) + } + RoomType::Toilett => { + (random_i32(1, 3) as u8, 3) + } + } + } + + fn create_grid(width: u8, height: u8) -> Grid{ + let left_border = width as f32 / 2.0; + let lower_border = height as f32 / 2.0; + let scale = 4.0; + + //Lower Cable + let lower_cable_y = height as f32 / 6.0; + let mut current_x = -0.5; + + let mut nodes = Vec::new(); + while current_x < width as f32 { + nodes.push(vec2((current_x - left_border) * scale, (lower_cable_y - lower_border) * scale)); + current_x += random_i32(1, (width / 2) as i32) as f32; + } + current_x = width as f32 + 0.5; + nodes.push(vec2((current_x - left_border) * scale, (lower_cable_y - lower_border) * scale)); + + let mut connections = Vec::new(); + for i in 1..nodes.len() { + connections.push((i-1, i)); + } + + //Lamps + let upper_cable_y = height as f32 - 0.25; + let max_lamps = (width as f32 / 2.5).round() as i32; + let lamp_amount = random_i32(1, max_lamps + 1); + let node_indices: HashSet = (0..lamp_amount).map(|_| random_i32(1, nodes.len() as i32 - 1) as usize).collect(); + + let last_lower_node_index = nodes.len(); + for (i, index) in node_indices.iter().enumerate() { + nodes.push(vec2(nodes.get(*index).unwrap().x,(upper_cable_y - lower_border) * scale)); + connections.push((*index, last_lower_node_index + i)); + } + + Grid::new(nodes, connections) + } + + pub fn draw(&self) { + let scale = 4.0; + let (width, height) = self.size; + let (width, height) = (width as f32 * scale, height as f32 * scale); + + draw_rect_outline(vec2(0.0, 0.0), vec2(width, height), 0.3, RED, 0); + + self.grid.draw(); + } +}