Living room

This commit is contained in:
Fredi 2024-07-07 16:19:57 +02:00
parent d3a54938af
commit 84030e5b6c

View file

@ -1,8 +1,7 @@
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
draw_rect, draw_rect_outline, draw_sprite, error, random_i32, vec2, EngineContext, HashSet, Index, RandomRange as _, Vec2, GREEN, PURPLE, RED, WHITE
};
use indexmap::IndexSet;
@ -88,6 +87,34 @@ impl Room {
let random_idx = usize::gen_range(0, empty_spots.len());
empty_spots.swap_remove_index(random_idx)
}
fn random_empty_spot_size(empty_spots: &mut IndexSet<u8>, size: u8) -> Option<u8> {
let mut empty_spots_size = IndexSet::<u8>::new();
for &index in empty_spots.iter() {
let mut is_valid = true;
for offset in 0..size{
if !empty_spots.contains(&(index + offset)){
is_valid = false;
break;
}
}
if is_valid {
empty_spots_size.insert(index);
}
}
if empty_spots_size.is_empty() {
return None;
}
let random_idx = usize::gen_range(0, empty_spots_size.len());
for offset in (0..size).rev() {
empty_spots.swap_remove_index(random_idx + offset as usize);
}
Some(random_idx as u8)
}
fn random_appliance<T>(empty_spots: &mut Vec<T>) -> Option<T> {
if empty_spots.is_empty() {
return None;
@ -254,6 +281,44 @@ impl Room {
}
},
RoomType::LivingRoom => {
let has_couch = match u8::gen_range(0, 2) {
0 => false,
1 => true,
_ => unreachable!()
};
if has_couch {
if let Some(pos) = random_empty_spot_size(&mut empty_spots, 3) {
furnitures.push(Tile {
pos: vec2(pos as f32, 0.0),
size: vec2(3.0, 1.0),
f: Furniture::new("bedroom", "couch", ctx),
z:0
});
}
}
if let Some(pos) = random_empty_spot(&mut empty_spots) {
furnitures.push(Tile {
pos: vec2(pos as f32, 0.0),
size: vec2(1.0, 2.0),
f: Furniture::new("bedroom", "bookshelf", ctx),
z:0
});
}
if let Some(pos) = random_empty_spot(&mut empty_spots) {
furnitures.push(Tile {
pos: vec2(pos as f32, 0.0),
size: vec2(0.5, 0.9),
f: Furniture::new("bedroom", "mini_ac", ctx),
z:0
});
}
},
_ => {}
}