Movement in update loop
This commit is contained in:
@@ -6,38 +6,50 @@ pub struct CameraPlugin;
|
|||||||
|
|
||||||
impl Plugin for CameraPlugin {
|
impl Plugin for CameraPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(
|
// Spawn in the camera on startup, only 1 instance
|
||||||
Startup,
|
app.add_systems(Startup, spawn_camera);
|
||||||
(spawn_camera, move_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) {
|
fn spawn_camera(mut commands: Commands) {
|
||||||
let mut camera = Camera2dBundle::default();
|
let mut camera = Camera2dBundle::default();
|
||||||
camera.projection.scale = 0.5;
|
// Bigger decimal, further it zooms in
|
||||||
camera.transform.translation.x += 1280.0 / 4.0;
|
// 0.5 is closer to level than 1.0
|
||||||
camera.transform.translation.y += 720.0 / 4.0;
|
camera.projection.scale = 0.3;
|
||||||
|
|
||||||
commands.spawn(camera);
|
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(
|
fn move_camera(
|
||||||
mut camera: Query<&mut Transform, (With<Camera2d>, Without<Player>)>,
|
mut camera: Query<&mut Transform, (With<Camera2d>, Without<Player>)>,
|
||||||
player: Query<&Transform, (With<Player>, Without<Camera2d>)>,
|
player: Query<&Transform, (With<Player>, Without<Camera2d>)>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
) {
|
) {
|
||||||
|
// Get the only instance of camera
|
||||||
let Ok(mut camera) = camera.get_single_mut() else {
|
let Ok(mut camera) = camera.get_single_mut() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Get the only instance of player MIGUEL
|
||||||
let Ok(player) = player.get_single() else {
|
let Ok(player) = player.get_single() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let Vec3 { x, y, .. } = player.translation;
|
// Smoothly follow the player
|
||||||
let direction = Vec3::new(x, y, camera.translation.z);
|
let player_position = Vec3::new(
|
||||||
|
player.translation.x,
|
||||||
|
player.translation.y,
|
||||||
|
camera.translation.z,
|
||||||
|
);
|
||||||
camera.translation = camera
|
camera.translation = camera
|
||||||
.translation
|
.translation
|
||||||
.lerp(direction, time.delta_seconds() * 2.);
|
.lerp(player_position, time.delta_seconds() * 2.0);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user