room-creation #15

Merged
Glaeder merged 18 commits from room-creation into main 2024-07-07 14:13:52 +00:00
3 changed files with 109 additions and 20 deletions
Showing only changes of commit d63c487b9d - Show all commits

1
Cargo.lock generated
View file

@ -2129,6 +2129,7 @@ version = "0.1.0"
dependencies = [
"comfy",
"heck",
"indexmap",
"log",
"resvg",
]

View file

@ -16,6 +16,7 @@ opt-level = 3
[dependencies]
comfy = { version = "0.4.0", features = ["wayland"] }
indexmap = "2"
log = "0.4.22"
[build-dependencies]

View file

@ -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<Tile>
furnitures: Vec<Tile>
}
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<Tile> {
fn random_room_furniture(
room_type: &RoomType,
(width, _height): (u8, u8),
ctx: &mut EngineContext<'_>
) -> Vec<Tile> {
let mut furnitures = Vec::new();
let width = size.0;
let mut empty_spots: IndexSet<u8> = (0 .. width).collect();
let mut assets_used: HashSet<&'static str> = HashSet::new();
fn random_empty_spot(empty_spots: &mut IndexSet<u8>) -> Option<u8> {
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 {