room-creation (#15)
Co-authored-by: Dominic <git@msrd0.de> Co-authored-by: Fredi <fredrik.konrad@rwth-aachen.de> Reviewed-on: #15 Co-authored-by: Glaeder <niklas@vousten.dev> Co-committed-by: Glaeder <niklas@vousten.dev>
This commit is contained in:
parent
fa990bfadb
commit
a6d9e088ef
8 changed files with 195 additions and 59 deletions
|
@ -3,33 +3,56 @@ mod grid;
|
|||
mod player;
|
||||
mod room;
|
||||
|
||||
use self::{grid::Grid, player::Player, room::Room};
|
||||
use crate::State;
|
||||
use comfy::{error, EngineContext};
|
||||
use comfy::{random_i32, EngineContext};
|
||||
use grid::Grid;
|
||||
use log::error;
|
||||
use player::Player;
|
||||
use room::Room;
|
||||
|
||||
const MAX_ROOMS: i32 = 6;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct HouseState {
|
||||
room: Room,
|
||||
current_room_id: usize,
|
||||
room_count: usize,
|
||||
rooms: Vec<Room>,
|
||||
//grid: Grid,
|
||||
player: Player
|
||||
player: Player,
|
||||
human_layer: bool //Human, magnetic, electric
|
||||
}
|
||||
|
||||
impl HouseState {
|
||||
pub fn generate_new_house(ctx: &mut EngineContext<'_>) -> Self {
|
||||
let room = Room::new(ctx);
|
||||
let player = Player::new(&room);
|
||||
HouseState { room, player }
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(state: &State, _ctx: &comfy::EngineContext<'_>) {
|
||||
pub fn draw(state: &crate::State, _ctx: &comfy::EngineContext<'_>) {
|
||||
let Some(house) = state.house() else {
|
||||
error!("How can I render a house when I'm not in one?!?");
|
||||
return;
|
||||
};
|
||||
|
||||
//Draw House
|
||||
house.room.draw();
|
||||
house
|
||||
.rooms
|
||||
.get(house.current_room_id)
|
||||
.unwrap()
|
||||
.draw(house.human_layer, house.player.can_see_metal(0.1));
|
||||
|
||||
//Draw Grid
|
||||
//state.house.grid.draw();
|
||||
|
@ -38,15 +61,28 @@ pub fn draw(state: &State, _ctx: &comfy::EngineContext<'_>) {
|
|||
house.player.draw();
|
||||
}
|
||||
|
||||
pub fn update(state: &mut State, ctx: &mut comfy::EngineContext<'_>) {
|
||||
pub fn update(state: &mut crate::State, ctx: &mut comfy::EngineContext<'_>) {
|
||||
let house = state.house_mut(ctx);
|
||||
house.player.update(&house.room.grid);
|
||||
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(&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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue