From d63c487b9d3632e6c41dee4d527e8c8095ea0e9c Mon Sep 17 00:00:00 2001 From: Dominic Date: Sun, 7 Jul 2024 12:33:22 +0200 Subject: [PATCH] start distributing kitchen furniture --- Cargo.lock | 1 + Cargo.toml | 1 + src/activities/house/room.rs | 127 +++++++++++++++++++++++++++++------ 3 files changed, 109 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e71edcb..c22856b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2129,6 +2129,7 @@ version = "0.1.0" dependencies = [ "comfy", "heck", + "indexmap", "log", "resvg", ] diff --git a/Cargo.toml b/Cargo.toml index 3dfe2bc..49f8e70 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ opt-level = 3 [dependencies] comfy = { version = "0.4.0", features = ["wayland"] } +indexmap = "2" log = "0.4.22" [build-dependencies] diff --git a/src/activities/house/room.rs b/src/activities/house/room.rs index 391db07..358d061 100644 --- a/src/activities/house/room.rs +++ b/src/activities/house/room.rs @@ -1,11 +1,10 @@ -use comfy::{ - draw_rect_outline, draw_sprite, error, random_i32, vec2, EngineContext, HashSet, - Vec2, GREEN, RED, WHITE -}; - -use crate::game; - use super::{furniture::Furniture, grid::Grid}; +use crate::game; +use comfy::{ + draw_rect, draw_rect_outline, draw_sprite, error, random_i32, vec2, EngineContext, + HashSet, RandomRange as _, Vec2, GREEN, PURPLE, RED, WHITE +}; +use indexmap::IndexSet; pub const SCALE: f32 = 4.0; @@ -25,7 +24,7 @@ pub struct Room { room_type: RoomType, pub size: (u8, u8), //(width, height) pub grid: Grid, - furnitures: Vec + furnitures: Vec } impl RoomType { @@ -46,7 +45,7 @@ impl Room { let room_type = RoomType::random(); let size = Self::random_size(&room_type); - let furnitures = Self::random_room_furniture(&room_type, size, ctx); + let furnitures = Self::random_room_furniture(&room_type, size, ctx); Room { room_type, @@ -67,28 +66,110 @@ impl Room { } } - fn random_room_furniture(room_type: &RoomType, size: (u8, u8), ctx: &mut EngineContext<'_>) -> Vec { + fn random_room_furniture( + room_type: &RoomType, + (width, _height): (u8, u8), + ctx: &mut EngineContext<'_> + ) -> Vec { let mut furnitures = Vec::new(); - let width = size.0; + let mut empty_spots: IndexSet = (0 .. width).collect(); + let mut assets_used: HashSet<&'static str> = HashSet::new(); + + fn random_empty_spot(empty_spots: &mut IndexSet) -> Option { + if empty_spots.is_empty() { + return None; + } + let random_idx = usize::gen_range(0, empty_spots.len()); + empty_spots.swap_remove_index(random_idx) + } + + const SIDEBOARD_HEIGHT: f32 = 0.1; + const STOVE_HEIGHT: f32 = 0.025; + const SINK_HEIGHT: f32 = 0.5; + + #[allow(clippy::single_match, unreachable_patterns)] // we'll add more stuff later match room_type { - RoomType::Kitchen => { + _ => { + // in a kitchen, we always add a fridge + let fridge_pos = u8::gen_range(0, 2) * (width - 1); + empty_spots.swap_remove(&fridge_pos); furnitures.push(( - vec2(0.0, 0.0), + vec2(fridge_pos as f32, 0.0), vec2(1.0, 2.0), Furniture::new("kitchen", "fridge", ctx) )); + + // and we always add an oven + let Some(oven_pos) = random_empty_spot(&mut empty_spots) else { + error!("How can I not fit an oven in a kitchen?!?"); + return furnitures; + }; furnitures.push(( - vec2(1.0, 0.0), + vec2(oven_pos as f32, 0.0), vec2(1.0, 1.0), - Furniture::new("kitchen", "dishwasher", ctx) + Furniture::new("kitchen", "oven", ctx) + )); + + // there's always sideboard above the oven with a stove + furnitures.push(( + vec2(oven_pos as f32, 1.0), + vec2(1.0, SIDEBOARD_HEIGHT), + Furniture::new("kitchen", "sideboard_1", ctx) )); furnitures.push(( - vec2(1.0, 1.0), - vec2(0.75, 0.75), - Furniture::new("kitchen", "blender", ctx) + vec2(oven_pos as f32, 1.0 + SIDEBOARD_HEIGHT), + vec2(1.0, STOVE_HEIGHT), + Furniture::new("kitchen", "stove", ctx) )); - } + + // and we always add a drawer that houses a sink + let Some(sink_pos) = random_empty_spot(&mut empty_spots) else { + error!("How can I not fit a sink in a kitchen?!?"); + return furnitures; + }; + furnitures.push(( + vec2(sink_pos as f32, 0.0), + vec2(1.0, 1.0), + Furniture::new("kitchen", "drawer_cupboard", ctx) + )); + + // there's always sideboard above that drawer with a sink **behind** it + furnitures.push(( + vec2(sink_pos as f32, 1.0), + vec2(1.0, SINK_HEIGHT), + Furniture::new("kitchen", "sink", ctx) + )); + furnitures.push(( + vec2(sink_pos as f32, 1.0), + vec2(1.0, SIDEBOARD_HEIGHT), + Furniture::new("kitchen", "sideboard_1", ctx) + )); + + // let's add half of the remaining positions as drawers + for _ in 0 .. empty_spots.len() / 2 { + let asset = match u8::gen_range(0, 2) { + 0 => "drawer", + 1 => "drawer_cupboard", + _ => unreachable!() + }; + let Some(spot) = random_empty_spot(&mut empty_spots) else { + error!("WTF I shouldn't've used more than half of the available spots"); + return furnitures; + }; + furnitures.push(( + vec2(spot as f32, 0.0), + vec2(1.0, 1.0), + Furniture::new("kitchen", asset, ctx) + )); + furnitures.push(( + vec2(spot as f32, 1.0), + vec2(1.0, SIDEBOARD_HEIGHT), + Furniture::new("kitchen", "sideboard_1", ctx) + )); + } + }, + _ => {} } @@ -148,12 +229,18 @@ impl Room { pub fn draw(&self) { let (width, height) = self.size; + draw_rect( + vec2(0.0, 0.0), + vec2(width as f32 * SCALE, height as f32 * SCALE), + PURPLE, + game::ZLayer::MapMax as i32 - 2 + ); draw_rect_outline( vec2(0.0, 0.0), vec2(width as f32 * SCALE, height as f32 * SCALE), 0.3, RED, - game::ZLayer::MapMax.into() + game::ZLayer::MapMax as i32 - 1 ); for (pos, size, furniture) in &self.furnitures {