Slightly better handling
This commit is contained in:
parent
accda086f4
commit
d54148e5db
28
src/main.rs
28
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,12 +190,13 @@ 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() {
|
||||||
|
if (ev.button == MouseButton::Right) && (ev.state == ButtonState::Released) {
|
||||||
let (cam_gt, cam) = camera_q.single();
|
let (cam_gt, cam) = camera_q.single();
|
||||||
let wnd = if let RenderTarget::Window(id) = cam.target {
|
let wnd = if let RenderTarget::Window(id) = cam.target {
|
||||||
wnds.get(id).unwrap()
|
wnds.get(id).unwrap()
|
||||||
@ -224,14 +226,18 @@ fn pawn_control(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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