2024-07-07 11:54:42 +00:00
|
|
|
mod furniture;
|
2024-07-06 17:59:56 +00:00
|
|
|
mod grid;
|
|
|
|
mod player;
|
2024-07-07 11:54:42 +00:00
|
|
|
mod room;
|
2024-07-06 17:59:56 +00:00
|
|
|
|
2024-07-07 14:13:52 +00:00
|
|
|
use comfy::{random_i32, EngineContext};
|
|
|
|
use grid::Grid;
|
|
|
|
use log::error;
|
|
|
|
use player::Player;
|
|
|
|
use room::Room;
|
|
|
|
|
|
|
|
const MAX_ROOMS: i32 = 6;
|
2024-07-06 17:59:56 +00:00
|
|
|
|
2024-07-07 11:54:42 +00:00
|
|
|
#[derive(Debug)]
|
2024-07-06 17:59:56 +00:00
|
|
|
pub struct HouseState {
|
2024-07-07 14:13:52 +00:00
|
|
|
current_room_id: usize,
|
|
|
|
room_count: usize,
|
|
|
|
rooms: Vec<Room>,
|
2024-07-07 11:54:42 +00:00
|
|
|
//grid: Grid,
|
2024-07-07 14:13:52 +00:00
|
|
|
player: Player,
|
|
|
|
human_layer: bool //Human, magnetic, electric
|
2024-07-06 17:59:56 +00:00
|
|
|
}
|
|
|
|
|
2024-07-07 11:54:42 +00:00
|
|
|
impl HouseState {
|
|
|
|
pub fn generate_new_house(ctx: &mut EngineContext<'_>) -> Self {
|
2024-07-07 14:13:52 +00:00
|
|
|
let room_count = random_i32(2, MAX_ROOMS) as usize;
|
|
|
|
|
|
|
|
let mut rooms = Vec::new();
|
|
|
|
for _ in 0 .. room_count {
|
|
|
|
rooms.push(Room::new(ctx));
|
|
|
|
}
|
|
|
|
|
|
|
|
let player = Player::new(rooms.first().unwrap());
|
|
|
|
HouseState {
|
|
|
|
current_room_id: 0,
|
|
|
|
room_count,
|
|
|
|
rooms,
|
|
|
|
player,
|
|
|
|
human_layer: false
|
|
|
|
}
|
2024-07-07 11:21:08 +00:00
|
|
|
}
|
2024-07-06 17:59:56 +00:00
|
|
|
}
|
|
|
|
|
2024-07-07 14:13:52 +00:00
|
|
|
pub fn draw(state: &crate::State, _ctx: &comfy::EngineContext<'_>) {
|
2024-07-07 11:54:42 +00:00
|
|
|
let Some(house) = state.house() else {
|
|
|
|
error!("How can I render a house when I'm not in one?!?");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
//Draw House
|
2024-07-07 14:13:52 +00:00
|
|
|
house
|
|
|
|
.rooms
|
|
|
|
.get(house.current_room_id)
|
|
|
|
.unwrap()
|
|
|
|
.draw(house.human_layer, house.player.can_see_metal(0.1));
|
2024-07-07 11:54:42 +00:00
|
|
|
|
|
|
|
//Draw Grid
|
|
|
|
//state.house.grid.draw();
|
|
|
|
|
|
|
|
//Draw Player
|
|
|
|
house.player.draw();
|
|
|
|
}
|
|
|
|
|
2024-07-07 14:13:52 +00:00
|
|
|
pub fn update(state: &mut crate::State, ctx: &mut comfy::EngineContext<'_>) {
|
2024-07-07 11:54:42 +00:00
|
|
|
let house = state.house_mut(ctx);
|
2024-07-07 14:13:52 +00:00
|
|
|
let current_room = house.rooms.get(house.current_room_id).unwrap();
|
|
|
|
house.player.update(¤t_room.grid);
|
|
|
|
|
|
|
|
if house.player.is_moving_to_right_room(current_room) {
|
|
|
|
if house.current_room_id < (house.room_count - 1) {
|
|
|
|
house.current_room_id += 1;
|
|
|
|
|
|
|
|
let current_room = house.rooms.get(house.current_room_id).unwrap();
|
|
|
|
house.player.reset_on_room(current_room, true);
|
|
|
|
} else {
|
|
|
|
house.player.reset_on_room(current_room, false);
|
|
|
|
}
|
|
|
|
} else if house.player.is_moving_to_left_room(current_room) {
|
|
|
|
if house.current_room_id > 0 {
|
|
|
|
house.current_room_id -= 1;
|
|
|
|
|
|
|
|
let current_room = house.rooms.get(house.current_room_id).unwrap();
|
|
|
|
house.player.reset_on_room(current_room, false);
|
|
|
|
} else {
|
|
|
|
house.player.reset_on_room(current_room, true);
|
|
|
|
}
|
2024-07-07 11:54:42 +00:00
|
|
|
}
|
2024-07-06 17:59:56 +00:00
|
|
|
}
|