From 8f67dad44c290f2799c9b7a0197ecd9a191cf989 Mon Sep 17 00:00:00 2001 From: Glaeder Date: Sun, 7 Jul 2024 11:38:13 +0200 Subject: [PATCH] Added furniture drawing to room-creation. Run clippy and fmt --- src/activities/house/furniture.rs | 17 +++- src/activities/house/grid.rs | 10 +-- src/activities/house/mod.rs | 16 ++-- src/activities/house/player.rs | 64 +++++++-------- src/activities/house/room.rs | 124 +++++++++++++++++++++--------- src/game.rs | 4 +- src/main.rs | 3 - 7 files changed, 150 insertions(+), 88 deletions(-) diff --git a/src/activities/house/furniture.rs b/src/activities/house/furniture.rs index 8682efb..a776c6b 100644 --- a/src/activities/house/furniture.rs +++ b/src/activities/house/furniture.rs @@ -74,7 +74,10 @@ pub struct Furniture { impl Debug for Furniture { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Furniture").field("asset", &self.asset).field("handles", &self.handles).finish_non_exhaustive() + f.debug_struct("Furniture") + .field("asset", &self.asset) + .field("handles", &self.handles) + .finish_non_exhaustive() } } @@ -95,4 +98,16 @@ impl Furniture { on: Box::new(|| false) } } + + pub fn get_human_texture_handle(&self) -> Option { + self.handles.human + } + + pub fn get_elec_texture_handle(&self) -> Option { + self.handles.elec + } + + pub fn get_magnet_texture_handle(&self) -> Option { + self.handles.magnet + } } diff --git a/src/activities/house/grid.rs b/src/activities/house/grid.rs index 55839f0..9cf5752 100644 --- a/src/activities/house/grid.rs +++ b/src/activities/house/grid.rs @@ -13,12 +13,12 @@ impl Default for Grid { } impl Grid { - pub fn new(nodes: Vec, connections: Vec<(usize, usize)>) -> Self { - let mut grid = Grid { nodes, connections }; - grid.sanitize(); + pub fn new(nodes: Vec, connections: Vec<(usize, usize)>) -> Self { + let mut grid = Grid { nodes, connections }; + grid.sanitize(); - grid - } + grid + } fn load() -> Self { let mut grid = Self { diff --git a/src/activities/house/mod.rs b/src/activities/house/mod.rs index 399a737..b6f34ed 100644 --- a/src/activities/house/mod.rs +++ b/src/activities/house/mod.rs @@ -19,22 +19,22 @@ pub fn setup(state: &mut crate::State, ctx: &mut EngineContext<'_>) { let house = { let room = Room::new(ctx); let player = Player::new(&room); - HouseState { room, player} + HouseState { room, player } }; state.house = Some(house); } -pub fn draw(state: &crate::State, ctx: &comfy::EngineContext<'_>) { +pub fn draw(state: &crate::State, _ctx: &comfy::EngineContext<'_>) { if let Some(house) = &state.house { - //Draw House - house.room.draw(); + //Draw House + house.room.draw(); - //Draw Grid - //state.house.grid.draw(); + //Draw Grid + //state.house.grid.draw(); - //Draw Player - house.player.draw(); + //Draw Player + house.player.draw(); } } diff --git a/src/activities/house/player.rs b/src/activities/house/player.rs index 900a943..23389fb 100644 --- a/src/activities/house/player.rs +++ b/src/activities/house/player.rs @@ -1,4 +1,7 @@ -use super::{room::Room, Grid, room::SCALE}; +use super::{ + room::{Room, SCALE}, + Grid +}; use comfy::{delta, draw_circle, is_key_down, vec2, KeyCode, Vec2, RED}; use std::collections::HashSet; @@ -11,15 +14,17 @@ pub struct Player { } impl Player { - pub fn new(room: &Room) -> Self { - - Player { - position: vec2(((0.25) - room.size.0 as f32 / 2.0) * SCALE, room.grid.nodes.get(0).unwrap().y), - speed: 10.0, - connection: 0, - next_connections: vec![1], - } - } + pub fn new(room: &Room) -> Self { + Player { + position: vec2( + ((0.25) - room.size.0 as f32 / 2.0) * SCALE, + room.grid.nodes.first().unwrap().y + ), + speed: 10.0, + connection: 0, + next_connections: vec![1] + } + } pub fn draw(&self) { draw_circle(self.position, 0.5, RED, 0); @@ -35,31 +40,26 @@ impl Player { } } - pub fn is_moving_to_right_room(&self, room: &Room) -> bool { - + pub fn is_moving_to_right_room(&self, room: &Room) -> bool { + self.position.x > (room.size.0 as f32 / 2.0) * SCALE + } - self.position.x > (room.size.0 as f32 / 2.0) * SCALE - } + pub fn is_moving_to_left_room(&self, room: &Room) -> bool { + self.position.x < -(room.size.0 as f32 / 2.0) * SCALE + } - pub fn is_moving_to_left_room(&self, room: &Room) -> bool { - + pub fn reset_on_room(&mut self, room: &Room, place_left: bool) { + let offset = 0.1; + let x = if place_left { + (offset - room.size.0 as f32 / 2.0) * SCALE + } else { + (room.size.0 as f32 / 2.0 - offset) * SCALE + }; - self.position.x < -(room.size.0 as f32 / 2.0) * SCALE - } - - pub fn reset_on_room(&mut self, room: &Room, place_left: bool) { - - let offset = 0.1; - let x = if place_left { - (offset - room.size.0 as f32 / 2.0) * SCALE - } else { - (room.size.0 as f32 / 2.0 - offset) * SCALE - }; - - self.position = vec2(x, room.grid.nodes.get(0).unwrap().y); - self.connection = 0; - self.next_connections = vec![1]; - } + self.position = vec2(x, room.grid.nodes.first().unwrap().y); + self.connection = 0; + self.next_connections = vec![1]; + } } fn move_player(player: &mut Player, allowed_movement: (bool, bool, bool, bool)) { diff --git a/src/activities/house/room.rs b/src/activities/house/room.rs index 03bb9a1..494ecfa 100644 --- a/src/activities/house/room.rs +++ b/src/activities/house/room.rs @@ -1,4 +1,9 @@ -use comfy::*; +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}; @@ -18,13 +23,7 @@ pub struct Room { room_type: RoomType, pub size: (u8, u8), //(width, height) pub grid: Grid, - tiles: Vec -} - -#[derive(Debug)] -enum Tile { - Single(Furniture), - Double(Furniture, Furniture) + furnitures: Vec<(Vec2, Vec2, Furniture)> //(pos, size, furniture) } impl RoomType { @@ -40,40 +39,51 @@ impl RoomType { } } - impl Room { - pub fn new(ctx: &mut EngineContext<'_>) -> Self { + pub fn new(ctx: &mut EngineContext<'_>) -> Self { let room_type = RoomType::random(); let size = Self::random_size(&room_type); - let mut tiles = Vec::new(); + let mut furnitures = Vec::new(); if room_type == RoomType::Kitchen { - tiles.push(Tile::Single(Furniture::new("kitchen", "fridge", ctx))); + furnitures.push(( + vec2(0.0, 0.0), + vec2(1.0, 2.0), + Furniture::new("kitchen", "fridge", ctx) + )); + furnitures.push(( + vec2(1.0, 0.0), + vec2(1.0, 1.0), + Furniture::new("kitchen", "dishwasher", ctx) + )); + furnitures.push(( + vec2(1.0, 1.0), + vec2(0.75, 0.75), + Furniture::new("kitchen", "blender", ctx) + )); } - Room { room_type, size, grid: Self::create_grid(size.0, size.1), tiles} - } - - 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(2, 3) as u8, 3) - } + Room { + room_type, + size, + grid: Self::create_grid(size.0, size.1), + furnitures } } - fn create_grid(width: u8, height: u8) -> Grid{ + 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(2, 3) as u8, 3) + } + } + + fn create_grid(width: u8, height: u8) -> Grid { error!("START GRID CREATION!"); let left_border = width as f32 / 2.0; let lower_border = height as f32 / 2.0; - //Lower Cable let lower_cable_y = height as f32 / 6.0; @@ -83,26 +93,37 @@ impl Room { let max_offset = ((width / 2) as i32).max(1); while current_x < width as f32 { - nodes.push(vec2((current_x - left_border) * SCALE, (lower_cable_y - lower_border) * SCALE)); + nodes.push(vec2( + (current_x - left_border) * SCALE, + (lower_cable_y - lower_border) * SCALE + )); current_x += random_i32(1, max_offset) as f32; } current_x = width as f32 + 0.5; - nodes.push(vec2((current_x - left_border) * SCALE, (lower_cable_y - lower_border) * SCALE)); + 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)); + 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 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)); + nodes.push(vec2( + nodes.get(*index).unwrap().x, + (upper_cable_y - lower_border) * SCALE + )); connections.push((*index, last_lower_node_index + i)); } @@ -110,11 +131,38 @@ impl Room { } pub fn draw(&self) { - 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); + draw_rect_outline( + vec2(0.0, 0.0), + vec2(width as f32 * SCALE, height as f32 * SCALE), + 0.3, + RED, + game::ZLayer::MapMax.into() + ); + + for (pos, size, furniture) in &self.furnitures { + let mut pos = *pos - vec2(width as f32 / 2.0, height as f32 / 2.0); + pos += *size * 0.5; + + if let Some(texture) = furniture.get_human_texture_handle() { + draw_sprite( + texture, + pos * SCALE, + WHITE, + game::ZLayer::MapMax.into(), + *size * SCALE + ); + } else { + draw_rect_outline( + pos * SCALE, + *size * SCALE, + 0.3, + GREEN, + game::ZLayer::MapMax.into() + ); + } + } self.grid.draw(); } diff --git a/src/game.rs b/src/game.rs index 4d25538..1a638c1 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,5 +1,7 @@ use crate::{ - activities::{house, overworld, Activity}, assets::Assets, State + activities::{house, overworld, Activity}, + assets::Assets, + State }; use comfy::EngineContext; use std::ops::Sub; diff --git a/src/main.rs b/src/main.rs index ead1564..0ecf53c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,6 @@ mod ui; use self::{ activities::{house::HouseState, overworld::worldgen::Overworld, Activity}, - assets::Assets, game::Ghost }; use comfy::{ @@ -51,8 +50,6 @@ fn config(config: GameConfig) -> GameConfig { config } - - async fn run() { init_game_config(GAME_NAME.to_string(), env!("CARGO_PKG_VERSION"), config); let mut engine = EngineState::new();