diff --git a/src/activities/house/grid.rs b/src/activities/house/grid.rs index 17bac26..55839f0 100644 --- a/src/activities/house/grid.rs +++ b/src/activities/house/grid.rs @@ -14,7 +14,10 @@ impl Default for Grid { impl Grid { pub fn new(nodes: Vec, connections: Vec<(usize, usize)>) -> Self { - Grid { nodes, connections } + let mut grid = Grid { nodes, connections }; + grid.sanitize(); + + grid } fn load() -> Self { diff --git a/src/activities/house/mod.rs b/src/activities/house/mod.rs index 76daac1..f695945 100644 --- a/src/activities/house/mod.rs +++ b/src/activities/house/mod.rs @@ -6,13 +6,21 @@ use grid::Grid; use player::Player; use room::Room; -#[derive(Debug, Default)] +#[derive(Debug)] pub struct HouseState { room: Room, //grid: Grid, player: Player } +impl Default for HouseState { + fn default() -> Self { + let room = Room::default(); + let player = Player::new(&room); + Self { room, player} + } +} + pub fn draw(state: &crate::State, _engine: &comfy::EngineContext<'_>) { //Draw House state.house.room.draw(); @@ -26,4 +34,12 @@ pub fn draw(state: &crate::State, _engine: &comfy::EngineContext<'_>) { 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); + } } diff --git a/src/activities/house/player.rs b/src/activities/house/player.rs index e7bc726..7ba89fb 100644 --- a/src/activities/house/player.rs +++ b/src/activities/house/player.rs @@ -1,4 +1,4 @@ -use super::Grid; +use super::{room::Room, Grid}; use comfy::{delta, draw_circle, is_key_down, vec2, KeyCode, Vec2, RED}; use std::collections::HashSet; @@ -10,20 +10,21 @@ pub struct Player { next_connections: Vec } -impl Default for Player { - fn default() -> Self { - Self { - position: Default::default(), - speed: 10.0, - connection: 0, - next_connections: vec![1, 2, 3] - } - } -} 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), + speed: 10.0, + connection: 0, + next_connections: vec![1], + } + } + pub fn draw(&self) { draw_circle(self.position, 0.5, RED, 0); } + pub fn update(&mut self, grid: &Grid) { let allowed_movement = get_allowed_movement(self, grid); @@ -33,6 +34,32 @@ impl Player { snap_to_closest_node(self, grid); } } + + 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 + } + + 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 + } + + 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 + } else { + (room.size.0 as f32 / 2.0 - offset) * scale + }; + + self.position = vec2(x, room.grid.nodes.get(0).unwrap().y); + self.connection = 0; + self.next_connections = vec![1]; + } } fn move_player(player: &mut Player, allowed_movement: (bool, bool, bool, bool)) { diff --git a/src/activities/house/room.rs b/src/activities/house/room.rs index fd9dbb2..60e3cde 100644 --- a/src/activities/house/room.rs +++ b/src/activities/house/room.rs @@ -14,7 +14,7 @@ enum RoomType { #[derive(Debug)] pub struct Room { room_type: RoomType, - size: (u8, u8), //(width, height) + pub size: (u8, u8), //(width, height) pub grid: Grid } @@ -42,8 +42,6 @@ impl Room { let room_type = RoomType::random(); let size = Self::random_size(&room_type); - - Room { room_type, size, grid: Self::create_grid(size.0, size.1)} } @@ -56,12 +54,13 @@ impl Room { (random_i32(4, 6) as u8, 3) } RoomType::Toilett => { - (random_i32(1, 3) as u8, 3) + (random_i32(2, 3) as u8, 3) } } } fn create_grid(width: u8, height: u8) -> Grid{ + error!("START GRID CREATION!"); let left_border = width as f32 / 2.0; let lower_border = height as f32 / 2.0; let scale = 4.0; @@ -71,9 +70,11 @@ impl Room { let mut current_x = -0.5; let mut nodes = Vec::new(); + 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)); - current_x += random_i32(1, (width / 2) as i32) as f32; + 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)); diff --git a/src/activities/mod.rs b/src/activities/mod.rs index b3ad872..dfa2b9e 100644 --- a/src/activities/mod.rs +++ b/src/activities/mod.rs @@ -3,8 +3,8 @@ pub mod overworld; #[derive(Debug, Default)] pub enum Activity { - #[allow(dead_code)] - House, #[default] + House, + #[allow(dead_code)] Overworld }