Fixed room-movement and rembering rooms (#12)

Reviewed-on: #12
Co-authored-by: Glaeder <niklas@vousten.dev>
Co-committed-by: Glaeder <niklas@vousten.dev>
This commit is contained in:
Glaeder 2024-07-07 13:18:18 +00:00 committed by Glaeder
parent 2498152b21
commit 6da8b9d71f
2 changed files with 54 additions and 15 deletions

View file

@ -3,23 +3,34 @@ mod grid;
mod player;
mod room;
use comfy::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
}
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 }
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 }
};
state.house = Some(house);
@ -28,7 +39,7 @@ pub fn setup(state: &mut crate::State, ctx: &mut EngineContext<'_>) {
pub fn draw(state: &crate::State, _ctx: &comfy::EngineContext<'_>) {
if let Some(house) = &state.house {
//Draw House
house.room.draw();
house.rooms.get(house.current_room_id).unwrap().draw();
//Draw Grid
//state.house.grid.draw();
@ -40,14 +51,27 @@ pub fn draw(state: &crate::State, _ctx: &comfy::EngineContext<'_>) {
pub fn update(state: &mut crate::State, ctx: &mut comfy::EngineContext<'_>) {
if let Some(house) = &mut state.house {
house.player.update(&house.room.grid);
let current_room = house.rooms.get(house.current_room_id).unwrap();
house.player.update(&current_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);
}
}
}
}

View file

@ -57,8 +57,23 @@ impl Player {
};
self.position = vec2(x, room.grid.nodes.first().unwrap().y);
self.connection = 0;
self.next_connections = vec![1];
if place_left {
self.connection = 0;
self.next_connections = vec![1];
} else {
let mut current_index = 0;
for (i, node_pos) in room.grid.nodes.iter().enumerate().rev() {
if in_node_range(&vec2((room.size.0 as f32 / 2.0 + 0.5) * SCALE, self.position.y), node_pos, 1.0) {
current_index = i;
}
}
let connction_index = room.grid.connections.iter().enumerate().find(|(_i, (_node_1, node_2))| *node_2 == current_index).map(|(i, _)| i).unwrap();
self.connection = connction_index;
self.next_connections = vec![connction_index - 1];
}
}
}