Added setup to house and WIP Furniture tiles
This commit is contained in:
parent
9f78a94c8d
commit
1cdb6156f8
6 changed files with 84 additions and 56 deletions
|
@ -1,14 +1,16 @@
|
|||
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>>>> =
|
||||
Lazy::new(|| Arc::new(Mutex::new(HashSet::new())));
|
||||
|
||||
#[derive(Debug)]
|
||||
struct FurnitureAsset {
|
||||
folder: String,
|
||||
name: String
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct FurnitureTextureHandles {
|
||||
human: Option<TextureHandle>,
|
||||
magnet: Option<TextureHandle>,
|
||||
|
@ -70,6 +72,12 @@ pub struct Furniture {
|
|||
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 {
|
||||
pub fn new<F: Into<String>, N: Into<String>>(
|
||||
folder: F,
|
||||
|
|
|
@ -3,6 +3,7 @@ mod grid;
|
|||
mod player;
|
||||
mod room;
|
||||
|
||||
use comfy::EngineContext;
|
||||
use grid::Grid;
|
||||
use player::Player;
|
||||
use room::Room;
|
||||
|
@ -14,33 +15,39 @@ pub struct HouseState {
|
|||
player: Player
|
||||
}
|
||||
|
||||
impl Default for HouseState {
|
||||
fn default() -> Self {
|
||||
let room = Room::default();
|
||||
pub fn setup(state: &mut crate::State, ctx: &mut EngineContext<'_>) {
|
||||
let house = {
|
||||
let room = Room::new(ctx);
|
||||
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
|
||||
state.house.room.draw();
|
||||
house.room.draw();
|
||||
|
||||
//Draw Grid
|
||||
//state.house.grid.draw();
|
||||
|
||||
//Draw Player
|
||||
state.house.player.draw();
|
||||
}
|
||||
|
||||
pub fn update(state: &mut crate::State, _engine: &mut comfy::EngineContext<'_>) {
|
||||
state.house.player.update(&state.house.room.grid);
|
||||
|
||||
if state.house.player.is_moving_to_right_room(&state.house.room) {
|
||||
state.house.room = Room::default();
|
||||
state.house.player.reset_on_room(&state.house.room, true);
|
||||
} else if state.house.player.is_moving_to_left_room(&state.house.room) {
|
||||
state.house.room = Room::default();
|
||||
state.house.player.reset_on_room(&state.house.room, false);
|
||||
house.player.draw();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(state: &mut crate::State, ctx: &mut comfy::EngineContext<'_>) {
|
||||
if let Some(house) = &mut state.house {
|
||||
house.player.update(&house.room.grid);
|
||||
|
||||
if house.player.is_moving_to_right_room(&house.room) {
|
||||
house.room = Room::new(ctx);
|
||||
house.player.reset_on_room(&house.room, true);
|
||||
} else if house.player.is_moving_to_left_room(&house.room) {
|
||||
house.room = Room::new(ctx);
|
||||
house.player.reset_on_room(&house.room, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 std::collections::HashSet;
|
||||
|
||||
|
@ -12,9 +12,9 @@ pub struct Player {
|
|||
|
||||
impl Player {
|
||||
pub fn new(room: &Room) -> Self {
|
||||
let scale = 4.0;
|
||||
|
||||
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,
|
||||
connection: 0,
|
||||
next_connections: vec![1],
|
||||
|
@ -36,24 +36,24 @@ impl Player {
|
|||
}
|
||||
|
||||
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 {
|
||||
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) {
|
||||
let scale = 4.0;
|
||||
|
||||
let offset = 0.1;
|
||||
let x = if place_left {
|
||||
(offset - room.size.0 as f32 / 2.0) * scale
|
||||
(offset - room.size.0 as f32 / 2.0) * SCALE
|
||||
} 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);
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
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 {
|
||||
Kitchen,
|
||||
Bath,
|
||||
|
@ -15,7 +17,14 @@ enum RoomType {
|
|||
pub struct Room {
|
||||
room_type: RoomType,
|
||||
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 {
|
||||
|
@ -31,18 +40,19 @@ impl RoomType {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for Room {
|
||||
fn default() -> Self {
|
||||
Room::load() //Just for testing purposes
|
||||
}
|
||||
}
|
||||
|
||||
impl Room {
|
||||
pub fn load() -> Self {
|
||||
pub fn new(ctx: &mut EngineContext<'_>) -> Self {
|
||||
let room_type = RoomType::random();
|
||||
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) {
|
||||
|
@ -63,7 +73,7 @@ impl Room {
|
|||
error!("START GRID CREATION!");
|
||||
let left_border = width as f32 / 2.0;
|
||||
let lower_border = height as f32 / 2.0;
|
||||
let scale = 4.0;
|
||||
|
||||
|
||||
//Lower Cable
|
||||
let lower_cable_y = height as f32 / 6.0;
|
||||
|
@ -73,11 +83,11 @@ 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() {
|
||||
|
@ -92,7 +102,7 @@ impl Room {
|
|||
|
||||
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));
|
||||
}
|
||||
|
||||
|
@ -100,9 +110,9 @@ impl Room {
|
|||
}
|
||||
|
||||
pub fn draw(&self) {
|
||||
let scale = 4.0;
|
||||
|
||||
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);
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use crate::{
|
||||
activities::{house, overworld, Activity},
|
||||
State
|
||||
activities::{house, overworld, Activity}, assets::Assets, State
|
||||
};
|
||||
use comfy::EngineContext;
|
||||
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<'_>) {
|
||||
match state.activity {
|
||||
Activity::House => house::update(state, engine),
|
||||
|
|
14
src/main.rs
14
src/main.rs
|
@ -28,7 +28,7 @@ struct State {
|
|||
activity: Activity,
|
||||
ghost: Ghost,
|
||||
overworld: Overworld,
|
||||
house: HouseState
|
||||
house: Option<HouseState>
|
||||
}
|
||||
|
||||
impl GameLoop for State {
|
||||
|
@ -36,14 +36,14 @@ impl GameLoop for State {
|
|||
Self::default()
|
||||
}
|
||||
|
||||
fn update(&mut self, engine: &mut EngineContext<'_>) {
|
||||
fn update(&mut self, ctx: &mut EngineContext<'_>) {
|
||||
if !self.setup_called {
|
||||
setup(engine);
|
||||
game::setup(self, ctx);
|
||||
self.setup_called = true;
|
||||
}
|
||||
|
||||
game::update(self, engine);
|
||||
game::draw(self, engine);
|
||||
game::update(self, ctx);
|
||||
game::draw(self, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -51,9 +51,7 @@ fn config(config: GameConfig) -> GameConfig {
|
|||
config
|
||||
}
|
||||
|
||||
fn setup(ctx: &mut EngineContext<'_>) {
|
||||
Assets::load(ctx);
|
||||
}
|
||||
|
||||
|
||||
async fn run() {
|
||||
init_game_config(GAME_NAME.to_string(), env!("CARGO_PKG_VERSION"), config);
|
||||
|
|
Loading…
Reference in a new issue