Merge pull request #25 from Ewpratten/camera-movement
Camera Follow Deadzone
This commit is contained in:
commit
64a59daf8b
@ -44,13 +44,16 @@ impl PlayableScene {
|
|||||||
|
|
||||||
Self {
|
Self {
|
||||||
has_updated_discord_rpc: false,
|
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,
|
world_map: map_renderer,
|
||||||
camera: raylib::camera::Camera2D {
|
camera: raylib::camera::Camera2D {
|
||||||
target: raylib::math::Vector2 { x: 0.0, y: 0.0 },
|
target: raylib::math::Vector2 {
|
||||||
offset: raylib::math::Vector2 {
|
x: 0.0,
|
||||||
x: (constants.base_window_size.0 as f32 / 2.0),
|
y: 0.0,
|
||||||
y: (constants.base_window_size.1 as f32 / 2.0),
|
},
|
||||||
|
offset: raylib::math::Vector2 {
|
||||||
|
x: 0.0,
|
||||||
|
y: 0.0
|
||||||
},
|
},
|
||||||
rotation: 0.0,
|
rotation: 0.0,
|
||||||
zoom: 1.0,
|
zoom: 1.0,
|
||||||
@ -193,10 +196,47 @@ impl PlayableScene {
|
|||||||
self.update_camera(raylib);
|
self.update_camera(raylib);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_camera(&mut self, raylib: &raylib::RaylibHandle) {
|
// Update the camera
|
||||||
self.camera.target = self.player.position.into();
|
pub fn update_camera(
|
||||||
self.camera.target.y *= -1.0;
|
&mut self,
|
||||||
self.camera.offset.x = raylib.get_screen_width() as f32 / 2.0;
|
raylib: & raylib::RaylibHandle,
|
||||||
self.camera.offset.y = raylib.get_screen_height() as f32 / 2.0;
|
) {
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user