bevy-playground/src/camera.rs

32 lines
783 B
Rust
Raw Normal View History

use bevy::prelude::*;
2023-12-25 15:35:10 -06:00
use crate::player::Player;
use bevy::render::camera::Camera as BevyCamera;
pub struct Camera;
impl Plugin for Camera {
fn build(&self, app: &mut App) {
app.add_systems(Startup, spawn)
2023-12-25 15:35:10 -06:00
.add_systems(PostUpdate, focus)
.add_systems(Update, y_sort);
}
}
fn spawn(mut commands: Commands) {
let mut bundle = Camera2dBundle::default();
2023-12-25 15:22:15 -06:00
bundle.projection.scale = 1. / 2.;
commands.spawn(bundle);
}
fn focus(
2023-12-25 15:35:10 -06:00
player: Query<&Transform, With<Player>>,
mut camera: Query<&mut Transform, (With<BevyCamera>, Without<Player>)>,
) {
2023-12-25 15:35:10 -06:00
camera.single_mut().translation = player.single().translation
}
fn y_sort(mut q: Query<&mut Transform>) {
q.for_each_mut(|mut tf| tf.translation.z = -tf.translation.y)
}