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;
|
2023-12-23 16:50:08 -06:00
|
|
|
|
2024-07-24 17:03:22 -05:00
|
|
|
pub struct CameraPlugin;
|
2023-12-20 13:24:16 -06:00
|
|
|
|
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 {
|
2023-12-20 13:24:16 -06:00
|
|
|
fn build(&self, app: &mut App) {
|
2023-12-23 16:50:08 -06:00
|
|
|
app.add_systems(Startup, spawn)
|
2024-07-29 14:33:14 -05:00
|
|
|
.add_systems(PostUpdate, follow);
|
2023-12-20 13:24:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-23 16:50:08 -06:00
|
|
|
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;
|
2023-12-20 13:24:16 -06:00
|
|
|
commands.spawn(bundle);
|
|
|
|
}
|
2023-12-23 16:50:08 -06:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|