46 lines
1.3 KiB
Rust
46 lines
1.3 KiB
Rust
use crate::constants::*;
|
|
use bevy::prelude::*;
|
|
|
|
#[derive(Component)]
|
|
#[require(Transform)]
|
|
pub struct Cursor {}
|
|
|
|
pub fn setup_cursor(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 {},
|
|
));
|
|
}
|
|
|
|
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.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,
|
|
);
|
|
}
|
|
}
|