diff --git a/game/game_logic/src/scenes/player_interaction.rs b/game/game_logic/src/scenes/player_interaction.rs index ae62da21..dfcb9708 100644 --- a/game/game_logic/src/scenes/player_interaction.rs +++ b/game/game_logic/src/scenes/player_interaction.rs @@ -44,13 +44,16 @@ impl PlayableScene { Self { has_updated_discord_rpc: false, - player: Player::new(na::Vector2::new(10.0, 10.0)), + player: Player::new(na::Vector2::new(10.0 * constants.tile_size as f32, -10.0 * constants.tile_size as f32)), world_map: map_renderer, camera: raylib::camera::Camera2D { - target: raylib::math::Vector2 { x: 0.0, y: 0.0 }, - offset: raylib::math::Vector2 { - x: (constants.base_window_size.0 as f32 / 2.0), - y: (constants.base_window_size.1 as f32 / 2.0), + target: raylib::math::Vector2 { + x: 0.0, + y: 0.0, + }, + offset: raylib::math::Vector2 { + x: 0.0, + y: 0.0 }, rotation: 0.0, zoom: 1.0, @@ -193,10 +196,47 @@ impl PlayableScene { self.update_camera(raylib); } - pub fn update_camera(&mut self, raylib: &raylib::RaylibHandle) { - self.camera.target = self.player.position.into(); - self.camera.target.y *= -1.0; - self.camera.offset.x = raylib.get_screen_width() as f32 / 2.0; - self.camera.offset.y = raylib.get_screen_height() as f32 / 2.0; + // Update the camera + pub fn update_camera( + &mut self, + raylib: & raylib::RaylibHandle, + ) { + + // Bounding box + let bbox = na::Vector2::new(0.2, 0.2); + + // Get bounding box dimensions on the screen + let bbox_screen_min: raylib::math::Vector2 = (((na::Vector2::new(1.0, 1.0) - bbox) * 0.5).component_mul( + &na::Vector2::new(raylib.get_screen_width() as f32, raylib.get_screen_height() as f32) + )).into(); + let bbox_screen_max: raylib::math::Vector2 = (((na::Vector2::new(1.0, 1.0) + bbox) * 0.5).component_mul( + &na::Vector2::new(raylib.get_screen_width() as f32, raylib.get_screen_height() as f32) + )).into(); + + // Get bounding box in world space + let mut bbox_world_min = raylib.get_screen_to_world2D(bbox_screen_min, self.camera); + let mut bbox_world_max = raylib.get_screen_to_world2D(bbox_screen_max, self.camera); + + // Invert y + bbox_world_min.y *= -1.0; + bbox_world_max.y *= -1.0; + + self.camera.offset = bbox_screen_min; + + if self.player.position.x < bbox_world_min.x { + self.camera.target.x = self.player.position.x; + } + + if self.player.position.y > bbox_world_min.y { + self.camera.target.y = -self.player.position.y; + } + + if self.player.position.x > bbox_world_max.x { + self.camera.target.x = bbox_world_min.x + (self.player.position.x - bbox_world_max.x); + } + + if self.player.position.y < bbox_world_max.y { + self.camera.target.y = bbox_world_max.y - (self.player.position.y + bbox_world_min.y); + } } }