This commit is contained in:
Daniel Flanagan 2024-07-24 17:03:22 -05:00
parent 33d893ace9
commit 01251d735a
3 changed files with 66 additions and 6 deletions

View file

@ -1,15 +1,21 @@
use bevy::prelude::*; use bevy::{
prelude::*,
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
window::PrimaryWindow,
};
use crate::player::Player; use crate::player::Player;
use bevy::render::camera::Camera as BevyCamera; use bevy::render::camera::Camera as BevyCamera;
pub struct Camera; pub struct CameraPlugin;
impl Plugin for Camera { impl Plugin for CameraPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, spawn) app.add_systems(Startup, spawn)
.add_systems(PostUpdate, focus) .add_systems(PostUpdate, focus)
.add_systems(Update, y_sort); .add_systems(Update, y_sort)
.add_systems(Startup, spawn_crosshair)
.add_systems(Update, rotate_crosshair);
} }
} }
@ -19,6 +25,42 @@ fn spawn(mut commands: Commands) {
commands.spawn(bundle); commands.spawn(bundle);
} }
#[derive(Component, Debug)]
pub struct Crosshair;
fn spawn_crosshair(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let mesh = Mesh2dHandle(meshes.add(Capsule2d::new(3.0, 25.0)));
let material = materials.add(Color::hsl(360. * 1 as f32 / 3 as f32, 0.95, 0.7));
// let global_transform = GlobalTransform::from_xyz(
// 0.0, 20.0,
// // TODO: need some way to ensure this draws above everything else?
// // a UI layer or something?
// 1000.,
// );
let transform = Transform::from_xyz(
0.0, 0.0,
// TODO: need some way to ensure this draws above everything else?
// a UI layer or something?
1000.,
);
commands.spawn((
Crosshair,
MaterialMesh2dBundle {
// global_transform,
mesh,
material,
transform,
..default()
},
));
}
fn focus( fn focus(
player: Query<&Transform, With<Player>>, player: Query<&Transform, With<Player>>,
mut camera: Query<&mut Transform, (With<BevyCamera>, Without<Player>)>, mut camera: Query<&mut Transform, (With<BevyCamera>, Without<Player>)>,
@ -32,3 +74,21 @@ fn y_sort(mut q: Query<&mut Transform>) {
q.iter_mut() q.iter_mut()
.for_each(|mut tf| tf.translation.z = -tf.translation.y) .for_each(|mut tf| tf.translation.z = -tf.translation.y)
} }
fn rotate_crosshair(
mut q: Query<&mut Transform, With<Crosshair>>,
win: Query<&Window, With<PrimaryWindow>>,
q_cam: Query<(&Camera, &GlobalTransform), With<Camera>>,
) {
let (camera, camera_transform) = q_cam.single();
if let Some(world_position) = win
.single()
.cursor_position()
.and_then(|cursor| camera.viewport_to_world(&GlobalTransform::from_xyz(0., 0., 0.), cursor))
.map(|ray| ray.origin.truncate())
{
let mut t = q.single_mut();
t.look_at(Vec3::ZERO, world_position.extend(0.));
t.translation = camera_transform.translation();
}
}

View file

@ -30,7 +30,7 @@ fn main() {
) )
.add_plugins(( .add_plugins((
statue::Statue, statue::Statue,
camera::Camera, camera::CameraPlugin,
assets::AssetPlugin, assets::AssetPlugin,
player::Player, player::Player,
main_menu::MainMenu, main_menu::MainMenu,

View file

@ -29,7 +29,7 @@ fn spawn_player(mut commands: Commands, assets: Res<AssetLoader>, asset_server:
texture, texture,
atlas: assets.images.player.clone().into(), atlas: assets.images.player.clone().into(),
sprite: Sprite::default(), sprite: Sprite::default(),
..Default::default() ..default()
}, },
Mover { Mover {
velocity: Velocity(Vec2::ZERO), velocity: Velocity(Vec2::ZERO),