cursor ui
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 4.3 KiB |
+1
-1
@@ -1,4 +1,4 @@
|
||||
use crate::game::PIXEL_RATIO;
|
||||
use crate::constants::*;
|
||||
use bevy::prelude::*;
|
||||
use rand::prelude::*;
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
pub const PIXEL_RATIO: f32 = 4.0;
|
||||
pub const TILE_SIZE: f32 = 16.0 * PIXEL_RATIO;
|
||||
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
+3
-4
@@ -1,11 +1,10 @@
|
||||
use crate::citizen::Citizen;
|
||||
use crate::item::{Item, ItemBundle};
|
||||
use crate::tiles::TilePrefab;
|
||||
use crate::constants::*;
|
||||
use bevy::prelude::*;
|
||||
use rand::prelude::*;
|
||||
|
||||
pub const PIXEL_RATIO: f32 = 4.0;
|
||||
|
||||
pub fn setup_level(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
setup_tilemap(&mut commands, &asset_server);
|
||||
|
||||
@@ -26,8 +25,8 @@ fn setup_tilemap(commands: &mut Commands, asset_server: &Res<AssetServer>) {
|
||||
for y in -25..25 {
|
||||
for z in 0..4 {
|
||||
let position = Vec3::new(
|
||||
x as f32 * 16. * PIXEL_RATIO,
|
||||
y as f32 * 16. * PIXEL_RATIO,
|
||||
x as f32 * TILE_SIZE,
|
||||
y as f32 * TILE_SIZE,
|
||||
-z as f32,
|
||||
);
|
||||
|
||||
|
||||
+4
-32
@@ -1,14 +1,13 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
mod constants;
|
||||
mod camera;
|
||||
mod citizen;
|
||||
mod game;
|
||||
mod item;
|
||||
mod tile;
|
||||
mod tiles;
|
||||
|
||||
use bevy::color::palettes::basic::WHITE;
|
||||
use game::PIXEL_RATIO;
|
||||
mod cursor;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
@@ -25,6 +24,7 @@ fn main() {
|
||||
)
|
||||
.insert_resource(ClearColor(Color::srgb(0.1, 0.1, 0.15)))
|
||||
.add_systems(Startup, (game::setup_level, camera::spawn_panning_camera))
|
||||
.add_systems(Startup, cursor::setup_curor)
|
||||
.add_systems(
|
||||
FixedUpdate,
|
||||
(
|
||||
@@ -33,34 +33,6 @@ fn main() {
|
||||
tile::tile_sprite_update,
|
||||
),
|
||||
)
|
||||
.add_systems(FixedUpdate, draw_cursor)
|
||||
.add_systems(FixedUpdate, cursor::move_cursor)
|
||||
.run();
|
||||
}
|
||||
|
||||
fn draw_cursor(
|
||||
camera_query: Single<(&Camera, &GlobalTransform)>,
|
||||
windows: Query<&Window>,
|
||||
mut gizmos: Gizmos,
|
||||
) {
|
||||
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;
|
||||
};
|
||||
println!(
|
||||
"pos: {}, {}",
|
||||
(point.x / (16. * PIXEL_RATIO)) as i32,
|
||||
(point.y / (16. * PIXEL_RATIO)) as i32
|
||||
);
|
||||
|
||||
gizmos.circle_2d(point, 10., WHITE);
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
use bevy::prelude::*;
|
||||
use crate::tile::{Tile, TileState};
|
||||
use crate::game::PIXEL_RATIO;
|
||||
use crate::constants::*;
|
||||
|
||||
#[derive(Bundle)]
|
||||
pub struct TilePrefab {
|
||||
|
||||
Reference in New Issue
Block a user