Slightly better handling
This commit is contained in:
parent
accda086f4
commit
d54148e5db
78
src/main.rs
78
src/main.rs
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
use bevy::{
|
use bevy::{
|
||||||
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
|
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
|
||||||
input::mouse::MouseMotion,
|
input::{mouse::{MouseMotion, MouseButtonInput}, ButtonState},
|
||||||
math::Vec4Swizzles,
|
math::Vec4Swizzles,
|
||||||
prelude::*,
|
prelude::*,
|
||||||
render::camera::RenderTarget,
|
render::camera::RenderTarget,
|
||||||
@ -176,8 +176,9 @@ fn mouse_motion(
|
|||||||
buttons: Res<Input<MouseButton>>,
|
buttons: Res<Input<MouseButton>>,
|
||||||
mut query: Query<(&mut Transform, &mut OrthographicProjection), With<Camera>>,
|
mut query: Query<(&mut Transform, &mut OrthographicProjection), With<Camera>>,
|
||||||
) {
|
) {
|
||||||
if buttons.pressed(MouseButton::Middle) {
|
|
||||||
for ev in motion_evr.iter() {
|
for ev in motion_evr.iter() {
|
||||||
|
if buttons.pressed(MouseButton::Middle) {
|
||||||
for (mut transform, mut _ortho) in query.iter_mut() {
|
for (mut transform, mut _ortho) in query.iter_mut() {
|
||||||
let direction = Vec3::new(ev.delta.x, ev.delta.y * -1.0, 0.0);
|
let direction = Vec3::new(ev.delta.x, ev.delta.y * -1.0, 0.0);
|
||||||
transform.translation += direction;
|
transform.translation += direction;
|
||||||
@ -189,49 +190,54 @@ fn mouse_motion(
|
|||||||
fn pawn_control(
|
fn pawn_control(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
wnds: Res<Windows>,
|
wnds: Res<Windows>,
|
||||||
buttons: Res<Input<MouseButton>>,
|
|
||||||
query: Query<Entity, With<PlayerCharacter>>,
|
query: Query<Entity, With<PlayerCharacter>>,
|
||||||
|
mut buttons_evr: EventReader<MouseButtonInput>,
|
||||||
camera_q: Query<(&GlobalTransform, &Camera)>,
|
camera_q: Query<(&GlobalTransform, &Camera)>,
|
||||||
tilemap_q: Query<(&TilemapSize, &TilemapGridSize, &TilemapType, &Transform)>,
|
tilemap_q: Query<(&TilemapSize, &TilemapGridSize, &TilemapType, &Transform)>,
|
||||||
) {
|
) {
|
||||||
if buttons.just_released(MouseButton::Right) {
|
for ev in buttons_evr.iter() {
|
||||||
let (cam_gt, cam) = camera_q.single();
|
if (ev.button == MouseButton::Right) && (ev.state == ButtonState::Released) {
|
||||||
let wnd = if let RenderTarget::Window(id) = cam.target {
|
let (cam_gt, cam) = camera_q.single();
|
||||||
wnds.get(id).unwrap()
|
let wnd = if let RenderTarget::Window(id) = cam.target {
|
||||||
} else {
|
wnds.get(id).unwrap()
|
||||||
wnds.get_primary().unwrap()
|
} else {
|
||||||
};
|
wnds.get_primary().unwrap()
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(screen_pos) = wnd.cursor_position() {
|
if let Some(screen_pos) = wnd.cursor_position() {
|
||||||
query.for_each(|entity| {
|
query.for_each(|entity| {
|
||||||
if let Some(world_ray) = cam.viewport_to_world(cam_gt, screen_pos) {
|
if let Some(world_ray) = cam.viewport_to_world(cam_gt, screen_pos) {
|
||||||
let (map_size, map_grid_size, map_type, map_t) = tilemap_q.single();
|
let (map_size, map_grid_size, map_type, map_t) = tilemap_q.single();
|
||||||
|
|
||||||
let tilemap_pos = (map_t.compute_matrix().inverse()
|
let tilemap_pos = (map_t.compute_matrix().inverse()
|
||||||
* Vec4::from((world_ray.origin, 1.0)))
|
* Vec4::from((world_ray.origin, 1.0)))
|
||||||
.xy();
|
.xy();
|
||||||
info!("WP {:?} {:?}", world_ray.origin, tilemap_pos);
|
info!("WP {:?} {:?}", world_ray.origin, tilemap_pos);
|
||||||
if let Some(tile_pos) =
|
if let Some(tile_pos) =
|
||||||
TilePos::from_world_pos(&tilemap_pos, map_size, map_grid_size, map_type)
|
TilePos::from_world_pos(&tilemap_pos, map_size, map_grid_size, map_type)
|
||||||
{
|
{
|
||||||
info!("{:?}", tile_pos);
|
info!("{:?}", tile_pos);
|
||||||
commands.entity(entity).insert(PawnDest {
|
commands.entity(entity).insert(PawnDest {
|
||||||
x: tile_pos.x,
|
x: tile_pos.x,
|
||||||
y: tile_pos.y,
|
y: tile_pos.y,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ChangedPawns = Or<(Changed<PawnPos>, With<PawnDest>)>;
|
||||||
|
|
||||||
fn pawn_motion(
|
fn pawn_motion(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut pawn_q: Query<(Entity, &mut PawnPos, &mut Transform, Option<&PawnDest>)>,
|
mut pawn_q: Query<(Entity, &mut PawnPos, &mut Transform, Option<&PawnDest>), ChangedPawns>,
|
||||||
tilemap_q: Query<(&TilemapGridSize, &TilemapTileSize, &TilemapType)>,
|
tilemap_q: Query<&TilemapTileSize>,
|
||||||
) {
|
) {
|
||||||
let (map_grid_size, map_tile_size, map_type) = tilemap_q.single();
|
let map_tile_size = tilemap_q.single();
|
||||||
pawn_q.for_each_mut(|(entity, mut pos, mut transform, dest)| match dest {
|
pawn_q.for_each_mut(|(entity, mut pos, mut transform, dest)| match dest {
|
||||||
Some(d) => {
|
Some(d) => {
|
||||||
pos.x = d.x;
|
pos.x = d.x;
|
||||||
@ -244,9 +250,11 @@ fn pawn_motion(
|
|||||||
commands.entity(entity).remove::<PawnDest>();
|
commands.entity(entity).remove::<PawnDest>();
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let world_pos = TilePos::new(pos.x, pos.y).center_in_world(map_grid_size, map_type);
|
transform.translation = Vec3::from((
|
||||||
transform.translation.x = world_pos.x;
|
(pos.x as f32) * map_tile_size.x,
|
||||||
transform.translation.y = world_pos.y;
|
(pos.y as f32) * map_tile_size.y,
|
||||||
|
1.0,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user