2023-12-20 13:24:16 -06:00
|
|
|
use bevy::prelude::*;
|
|
|
|
|
2023-12-25 15:35:10 -06:00
|
|
|
use crate::player::Player;
|
|
|
|
use bevy::render::camera::Camera as BevyCamera;
|
2023-12-23 16:50:08 -06:00
|
|
|
|
2023-12-20 13:24:16 -06:00
|
|
|
pub struct Camera;
|
|
|
|
|
|
|
|
impl Plugin for Camera {
|
|
|
|
fn build(&self, app: &mut App) {
|
2023-12-23 16:50:08 -06:00
|
|
|
app.add_systems(Startup, spawn)
|
2023-12-25 15:35:10 -06:00
|
|
|
.add_systems(PostUpdate, focus)
|
|
|
|
.add_systems(Update, y_sort);
|
2023-12-20 13:24:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-23 16:50:08 -06:00
|
|
|
fn spawn(mut commands: Commands) {
|
2023-12-20 13:24:16 -06:00
|
|
|
let mut bundle = Camera2dBundle::default();
|
2023-12-25 15:22:15 -06:00
|
|
|
bundle.projection.scale = 1. / 2.;
|
2023-12-20 13:24:16 -06:00
|
|
|
commands.spawn(bundle);
|
|
|
|
}
|
2023-12-23 16:50:08 -06:00
|
|
|
|
|
|
|
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-23 16:50:08 -06:00
|
|
|
) {
|
2024-02-03 21:15:30 -06:00
|
|
|
let newpos = player.single().translation;
|
|
|
|
// println!("Cam pos: {newpos}");
|
|
|
|
camera.single_mut().translation = newpos
|
2023-12-25 15:35:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn y_sort(mut q: Query<&mut Transform>) {
|
2024-05-17 09:57:24 -05:00
|
|
|
q.iter_mut()
|
|
|
|
.for_each(|mut tf| tf.translation.z = -tf.translation.y)
|
2023-12-23 16:50:08 -06:00
|
|
|
}
|