bevy-playground/src/camera.rs

30 lines
749 B
Rust
Raw Normal View History

2024-07-29 14:33:14 -05:00
use bevy::prelude::*;
2023-12-25 15:35:10 -06:00
use bevy::render::camera::Camera as BevyCamera;
2024-07-24 17:03:22 -05:00
pub struct CameraPlugin;
2024-07-29 14:33:14 -05:00
#[derive(Component, Debug)]
pub struct CameraTarget;
2024-07-24 17:03:22 -05:00
impl Plugin for CameraPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, spawn)
2024-07-29 14:33:14 -05:00
.add_systems(PostUpdate, follow);
}
}
fn spawn(mut commands: Commands) {
2024-07-29 14:33:14 -05:00
let mut bundle = (Camera2dBundle::default(), IsDefaultUiCamera);
bundle.0.projection.scale = 0.5;
commands.spawn(bundle);
}
2024-07-29 14:33:14 -05:00
fn follow(
player: Query<&Transform, With<CameraTarget>>,
mut camera: Query<&mut Transform, (With<BevyCamera>, Without<CameraTarget>)>,
2024-07-24 17:03:22 -05:00
) {
2024-07-29 14:33:14 -05:00
if let Ok(player) = player.get_single() {
camera.single_mut().translation = player.translation
2024-07-24 17:03:22 -05:00
}
}