Added setup to house and WIP Furniture tiles

This commit is contained in:
Glaeder 2024-07-07 10:04:29 +02:00
parent 9f78a94c8d
commit 1cdb6156f8
6 changed files with 84 additions and 56 deletions

View file

@ -1,14 +1,16 @@
use comfy::{error, texture_id, EngineContext, HashSet, Lazy, Mutex, TextureHandle}; use comfy::{error, texture_id, EngineContext, HashSet, Lazy, Mutex, TextureHandle};
use std::{fs, io, sync::Arc}; use std::{fmt::Debug, fs, io, sync::Arc};
static ASSETS_LOADED: Lazy<Arc<Mutex<HashSet<String>>>> = static ASSETS_LOADED: Lazy<Arc<Mutex<HashSet<String>>>> =
Lazy::new(|| Arc::new(Mutex::new(HashSet::new()))); Lazy::new(|| Arc::new(Mutex::new(HashSet::new())));
#[derive(Debug)]
struct FurnitureAsset { struct FurnitureAsset {
folder: String, folder: String,
name: String name: String
} }
#[derive(Debug)]
struct FurnitureTextureHandles { struct FurnitureTextureHandles {
human: Option<TextureHandle>, human: Option<TextureHandle>,
magnet: Option<TextureHandle>, magnet: Option<TextureHandle>,
@ -70,6 +72,12 @@ pub struct Furniture {
on: Box<dyn Fn() -> bool> on: Box<dyn Fn() -> bool>
} }
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()
}
}
impl Furniture { impl Furniture {
pub fn new<F: Into<String>, N: Into<String>>( pub fn new<F: Into<String>, N: Into<String>>(
folder: F, folder: F,

View file

@ -3,6 +3,7 @@ mod grid;
mod player; mod player;
mod room; mod room;
use comfy::EngineContext;
use grid::Grid; use grid::Grid;
use player::Player; use player::Player;
use room::Room; use room::Room;
@ -14,33 +15,39 @@ pub struct HouseState {
player: Player player: Player
} }
impl Default for HouseState { pub fn setup(state: &mut crate::State, ctx: &mut EngineContext<'_>) {
fn default() -> Self { let house = {
let room = Room::default(); let room = Room::new(ctx);
let player = Player::new(&room); let player = Player::new(&room);
Self { room, player} HouseState { room, player}
} };
state.house = Some(house);
} }
pub fn draw(state: &crate::State, _engine: &comfy::EngineContext<'_>) { pub fn draw(state: &crate::State, ctx: &comfy::EngineContext<'_>) {
if let Some(house) = &state.house {
//Draw House //Draw House
state.house.room.draw(); house.room.draw();
//Draw Grid //Draw Grid
//state.house.grid.draw(); //state.house.grid.draw();
//Draw Player //Draw Player
state.house.player.draw(); house.player.draw();
} }
}
pub fn update(state: &mut crate::State, _engine: &mut comfy::EngineContext<'_>) {
state.house.player.update(&state.house.room.grid); pub fn update(state: &mut crate::State, ctx: &mut comfy::EngineContext<'_>) {
if let Some(house) = &mut state.house {
if state.house.player.is_moving_to_right_room(&state.house.room) { house.player.update(&house.room.grid);
state.house.room = Room::default();
state.house.player.reset_on_room(&state.house.room, true); if house.player.is_moving_to_right_room(&house.room) {
} else if state.house.player.is_moving_to_left_room(&state.house.room) { house.room = Room::new(ctx);
state.house.room = Room::default(); house.player.reset_on_room(&house.room, true);
state.house.player.reset_on_room(&state.house.room, false); } else if house.player.is_moving_to_left_room(&house.room) {
house.room = Room::new(ctx);
house.player.reset_on_room(&house.room, false);
}
} }
} }

View file

@ -1,4 +1,4 @@
use super::{room::Room, Grid}; use super::{room::Room, Grid, room::SCALE};
use comfy::{delta, draw_circle, is_key_down, vec2, KeyCode, Vec2, RED}; use comfy::{delta, draw_circle, is_key_down, vec2, KeyCode, Vec2, RED};
use std::collections::HashSet; use std::collections::HashSet;
@ -12,9 +12,9 @@ pub struct Player {
impl Player { impl Player {
pub fn new(room: &Room) -> Self { pub fn new(room: &Room) -> Self {
let scale = 4.0;
Player { Player {
position: vec2(((0.25) - room.size.0 as f32 / 2.0) * scale, room.grid.nodes.get(0).unwrap().y), position: vec2(((0.25) - room.size.0 as f32 / 2.0) * SCALE, room.grid.nodes.get(0).unwrap().y),
speed: 10.0, speed: 10.0,
connection: 0, connection: 0,
next_connections: vec![1], next_connections: vec![1],
@ -36,24 +36,24 @@ impl Player {
} }
pub fn is_moving_to_right_room(&self, room: &Room) -> bool { pub fn is_moving_to_right_room(&self, room: &Room) -> bool {
let scale = 4.0;
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 { pub fn is_moving_to_left_room(&self, room: &Room) -> bool {
let scale = 4.0;
self.position.x < -(room.size.0 as f32 / 2.0) * scale self.position.x < -(room.size.0 as f32 / 2.0) * SCALE
} }
pub fn reset_on_room(&mut self, room: &Room, place_left: bool) { pub fn reset_on_room(&mut self, room: &Room, place_left: bool) {
let scale = 4.0;
let offset = 0.1; let offset = 0.1;
let x = if place_left { let x = if place_left {
(offset - room.size.0 as f32 / 2.0) * scale (offset - room.size.0 as f32 / 2.0) * SCALE
} else { } else {
(room.size.0 as f32 / 2.0 - offset) * scale (room.size.0 as f32 / 2.0 - offset) * SCALE
}; };
self.position = vec2(x, room.grid.nodes.get(0).unwrap().y); self.position = vec2(x, room.grid.nodes.get(0).unwrap().y);

View file

@ -1,8 +1,10 @@
use comfy::*; use comfy::*;
use super::grid::Grid; use super::{furniture::Furniture, grid::Grid};
#[derive(Debug)] pub const SCALE: f32 = 4.0;
#[derive(Debug, PartialEq)]
enum RoomType { enum RoomType {
Kitchen, Kitchen,
Bath, Bath,
@ -15,7 +17,14 @@ enum RoomType {
pub struct Room { pub struct Room {
room_type: RoomType, room_type: RoomType,
pub size: (u8, u8), //(width, height) pub size: (u8, u8), //(width, height)
pub grid: Grid pub grid: Grid,
tiles: Vec<Tile>
}
#[derive(Debug)]
enum Tile {
Single(Furniture),
Double(Furniture, Furniture)
} }
impl RoomType { impl RoomType {
@ -31,18 +40,19 @@ impl RoomType {
} }
} }
impl Default for Room {
fn default() -> Self {
Room::load() //Just for testing purposes
}
}
impl Room { impl Room {
pub fn load() -> Self { pub fn new(ctx: &mut EngineContext<'_>) -> Self {
let room_type = RoomType::random(); let room_type = RoomType::random();
let size = Self::random_size(&room_type); let size = Self::random_size(&room_type);
Room { room_type, size, grid: Self::create_grid(size.0, size.1)} let mut tiles = Vec::new();
if room_type == RoomType::Kitchen {
tiles.push(Tile::Single(Furniture::new("kitchen", "fridge", ctx)));
}
Room { room_type, size, grid: Self::create_grid(size.0, size.1), tiles}
} }
fn random_size(room_type: &RoomType) -> (u8, u8) { fn random_size(room_type: &RoomType) -> (u8, u8) {
@ -63,7 +73,7 @@ impl Room {
error!("START GRID CREATION!"); error!("START GRID CREATION!");
let left_border = width as f32 / 2.0; let left_border = width as f32 / 2.0;
let lower_border = height as f32 / 2.0; let lower_border = height as f32 / 2.0;
let scale = 4.0;
//Lower Cable //Lower Cable
let lower_cable_y = height as f32 / 6.0; let lower_cable_y = height as f32 / 6.0;
@ -73,11 +83,11 @@ impl Room {
let max_offset = ((width / 2) as i32).max(1); let max_offset = ((width / 2) as i32).max(1);
while current_x < width as f32 { 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 += random_i32(1, max_offset) as f32;
} }
current_x = width as f32 + 0.5; 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(); let mut connections = Vec::new();
for i in 1..nodes.len() { for i in 1..nodes.len() {
@ -92,7 +102,7 @@ impl Room {
let last_lower_node_index = nodes.len(); let last_lower_node_index = nodes.len();
for (i, index) in node_indices.iter().enumerate() { 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)); connections.push((*index, last_lower_node_index + i));
} }
@ -100,9 +110,9 @@ impl Room {
} }
pub fn draw(&self) { pub fn draw(&self) {
let scale = 4.0;
let (width, height) = self.size; let (width, height) = self.size;
let (width, height) = (width as f32 * scale, height as f32 * scale); 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, height), 0.3, RED, 0);

View file

@ -1,6 +1,5 @@
use crate::{ use crate::{
activities::{house, overworld, Activity}, activities::{house, overworld, Activity}, assets::Assets, State
State
}; };
use comfy::EngineContext; use comfy::EngineContext;
use std::ops::Sub; use std::ops::Sub;
@ -43,6 +42,12 @@ impl Sub<i32> for ZLayer {
} }
} }
pub fn setup(state: &mut State, ctx: &mut EngineContext<'_>) {
Assets::load(ctx);
house::setup(state, ctx);
}
pub fn update(state: &mut State, engine: &mut EngineContext<'_>) { pub fn update(state: &mut State, engine: &mut EngineContext<'_>) {
match state.activity { match state.activity {
Activity::House => house::update(state, engine), Activity::House => house::update(state, engine),

View file

@ -28,7 +28,7 @@ struct State {
activity: Activity, activity: Activity,
ghost: Ghost, ghost: Ghost,
overworld: Overworld, overworld: Overworld,
house: HouseState house: Option<HouseState>
} }
impl GameLoop for State { impl GameLoop for State {
@ -36,14 +36,14 @@ impl GameLoop for State {
Self::default() Self::default()
} }
fn update(&mut self, engine: &mut EngineContext<'_>) { fn update(&mut self, ctx: &mut EngineContext<'_>) {
if !self.setup_called { if !self.setup_called {
setup(engine); game::setup(self, ctx);
self.setup_called = true; self.setup_called = true;
} }
game::update(self, engine); game::update(self, ctx);
game::draw(self, engine); game::draw(self, ctx);
} }
} }
@ -51,9 +51,7 @@ fn config(config: GameConfig) -> GameConfig {
config config
} }
fn setup(ctx: &mut EngineContext<'_>) {
Assets::load(ctx);
}
async fn run() { async fn run() {
init_game_config(GAME_NAME.to_string(), env!("CARGO_PKG_VERSION"), config); init_game_config(GAME_NAME.to_string(), env!("CARGO_PKG_VERSION"), config);