29 lines
820 B
Rust
29 lines
820 B
Rust
use bevy::prelude::*;
|
|
|
|
use crate::player::Player;
|
|
use crate::player::move_player;
|
|
|
|
fn spawn_camera(mut commands: Commands) {
|
|
commands.spawn(Camera2d);
|
|
}
|
|
|
|
// Follow the player with the camera
|
|
fn follow_player(
|
|
player: Single<&Transform, (With<Player>, Without<Camera2d>)>,
|
|
mut camera: Single<&mut Transform, (With<Camera2d>, Without<Player>)>,
|
|
// time: Res<Time>,
|
|
) {
|
|
camera.translation.y = player.translation.y; // without delay
|
|
// camera.translation.y = camera.translation.y
|
|
// + (player.translation.y - camera.translation.y) * 3.0 * time.delta_secs(); // with delay
|
|
}
|
|
|
|
pub struct CameraPlugin;
|
|
|
|
impl Plugin for CameraPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.add_systems(Startup, spawn_camera)
|
|
.add_systems(Update, follow_player.after(move_player));
|
|
}
|
|
}
|