cursor ui

This commit is contained in:
StephenAdamson
2024-12-13 23:07:08 +00:00
parent 89567d4ce7
commit 7363ec9d39
7 changed files with 58 additions and 38 deletions
+47
View File
@@ -0,0 +1,47 @@
use crate::constants::*;
use bevy::{prelude::*, winit::cursor};
#[derive(Component)]
#[require(Transform)]
pub struct Cursor {
id: f32,
}
pub fn setup_curor(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn((
Transform::from_xyz(0., 0., 10.).with_scale(Vec3::splat(PIXEL_RATIO)),
Sprite {
image: asset_server.load("cursor.png"),
..Default::default()
},
Cursor { id: 1. },
));
}
pub fn move_cursor(
camera_query: Single<(&Camera, &GlobalTransform)>,
windows: Query<&Window>,
mut query: Query<(&Cursor, &mut Transform), With<Transform>>,
) {
for mut transform in query.iter_mut() {
let (camera, camera_transform) = *camera_query;
let Ok(window) = windows.get_single() else {
return;
};
let Some(cursor_position) = window.cursor_position() else {
return;
};
// Calculate a world position based on the cursor's position.
let Ok(point) = camera.viewport_to_world_2d(camera_transform, cursor_position) else {
return;
};
transform.1.translation = Vec3::new(
((point.x / (TILE_SIZE)+0.5).floor()) * TILE_SIZE,
((point.y / (TILE_SIZE)+0.5).floor()) * TILE_SIZE,
100.0,
);
}
}