room-creation #15

Merged
Glaeder merged 18 commits from room-creation into main 2024-07-07 14:13:52 +00:00
2 changed files with 54 additions and 15 deletions
Showing only changes of commit 6da8b9d71f - Show all commits

View file

@ -3,23 +3,34 @@ mod grid;
mod player; mod player;
mod room; mod room;
use comfy::EngineContext; use comfy::{random_i32, EngineContext};
use grid::Grid; use grid::Grid;
use log::error;
use player::Player; use player::Player;
use room::Room; use room::Room;
const MAX_ROOMS: i32 = 6;
#[derive(Debug)] #[derive(Debug)]
pub struct HouseState { pub struct HouseState {
room: Room, current_room_id: usize,
room_count: usize,
rooms: Vec<Room>,
//grid: Grid, //grid: Grid,
player: Player player: Player
} }
pub fn setup(state: &mut crate::State, ctx: &mut EngineContext<'_>) { pub fn setup(state: &mut crate::State, ctx: &mut EngineContext<'_>) {
let house = { let house = {
let room = Room::new(ctx); let room_count = random_i32(2, MAX_ROOMS) as usize;
let player = Player::new(&room);
HouseState { room, player } 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); 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<'_>) { pub fn draw(state: &crate::State, _ctx: &comfy::EngineContext<'_>) {
if let Some(house) = &state.house { if let Some(house) = &state.house {
//Draw House //Draw House
house.room.draw(); house.rooms.get(house.current_room_id).unwrap().draw();
//Draw Grid //Draw Grid
//state.house.grid.draw(); //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<'_>) { pub fn update(state: &mut crate::State, ctx: &mut comfy::EngineContext<'_>) {
if let Some(house) = &mut state.house { 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) { if house.player.is_moving_to_right_room(current_room) {
house.room = Room::new(ctx); if house.current_room_id < (house.room_count - 1) {
house.player.reset_on_room(&house.room, true); house.current_room_id +=1;
} else if house.player.is_moving_to_left_room(&house.room) {
house.room = Room::new(ctx); let current_room = house.rooms.get(house.current_room_id).unwrap();
house.player.reset_on_room(&house.room, false); 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.position = vec2(x, room.grid.nodes.first().unwrap().y);
if place_left {
self.connection = 0; self.connection = 0;
self.next_connections = vec![1]; 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];
}
} }
} }