bevy-playground/src/camera.rs

36 lines
989 B
Rust

use crate::prelude::*;
use bevy::render::camera::{Camera, ScalingMode};
// #[derive(Component)]
// struct GameCamera;
pub fn startup(mut commands: Commands, mut ui_scale: ResMut<UiScale>) {
let mut bundle = Camera2dBundle::default();
bundle.projection = OrthographicProjection {
near: f32::MIN,
far: f32::MAX,
scaling_mode: ScalingMode::FixedVertical(360.0 / 1.),
..default()
};
commands.spawn((bundle, IsDefaultUiCamera));
ui_scale.0 = 1.;
}
#[derive(Component, Debug)]
pub struct Watched;
pub fn update(
watched: Query<&Transform, With<Watched>>,
mut camera: Query<&mut Transform, (With<Camera>, Without<Watched>)>,
) {
if let Ok(mut camera) = camera.get_single_mut() {
if let Ok(watched) = watched.get_single() {
camera.translation.x = watched.translation.x;
camera.translation.y = watched.translation.y;
} else {
camera.translation = Vec3::ZERO;
}
}
}