Merge branch 'camera-fuckery'
This commit is contained in:
@@ -69,33 +69,57 @@ pub fn camera_fit_inside_current_level(
|
||||
}
|
||||
}
|
||||
}
|
||||
const CAMERA_DECAY_RATE: f32 = 2.;
|
||||
fn update_camera(
|
||||
mut camera_query: Query<
|
||||
(
|
||||
&mut bevy::render::camera::OrthographicProjection,
|
||||
&mut Transform,
|
||||
),
|
||||
Without<Player>,
|
||||
>,
|
||||
player_query: Query<&Transform, With<Player>>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
|
||||
if let Ok(Transform {
|
||||
translation: player_translation,
|
||||
..
|
||||
}) = player_query.get_single()
|
||||
{
|
||||
let player_translation = *player_translation;
|
||||
use crate::player::Player;
|
||||
|
||||
let (mut orthographic_projection, mut camera_transform) = camera_query.single_mut();
|
||||
let Vec3 { x, y, .. } = player_translation;
|
||||
let direction = Vec3::new(x, y, camera_transform.translation.z);
|
||||
pub struct CameraPlugin;
|
||||
|
||||
// Applies a smooth effect to camera movement using stable interpolation
|
||||
// between the camera position and the player position on the x and y axes.
|
||||
camera_transform
|
||||
.translation += direction;
|
||||
impl Plugin for CameraPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
// Spawn in the camera on startup, only 1 instance
|
||||
app.add_systems(Startup, spawn_camera);
|
||||
|
||||
// We're so good..
|
||||
// Update the camera position ever game loop iteration
|
||||
app.add_systems(Update, move_camera);
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_camera(mut commands: Commands) {
|
||||
let mut camera = Camera2dBundle::default();
|
||||
// Bigger decimal, further it zooms in
|
||||
// 0.5 is closer to level than 1.0
|
||||
camera.projection.scale = 0.3;
|
||||
|
||||
commands.spawn(camera);
|
||||
}
|
||||
|
||||
// THIS NEEDS TO BE IN THE UPDATE LOOP LOL
|
||||
// We don't need to get player position to snap
|
||||
// when spawning, it does that here lol
|
||||
// - Mjork
|
||||
fn move_camera(
|
||||
mut camera: Query<&mut Transform, (With<Camera2d>, Without<Player>)>,
|
||||
player: Query<&Transform, (With<Player>, Without<Camera2d>)>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
// Get the only instance of camera
|
||||
let Ok(mut camera) = camera.get_single_mut() else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Get the only instance of player MIGUEL
|
||||
let Ok(player) = player.get_single() else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Smoothly follow the player
|
||||
let player_position = Vec3::new(
|
||||
player.translation.x,
|
||||
player.translation.y,
|
||||
camera.translation.z,
|
||||
);
|
||||
camera.translation = camera
|
||||
.translation
|
||||
.lerp(player_position, time.delta_seconds() * 2.0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user