This commit is like Batman, It has no parents.
This commit is contained in:
52
src/helpers/camera.rs
Normal file
52
src/helpers/camera.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use bevy::{input::ButtonInput, math::Vec3, prelude::*, render::camera::Camera};
|
||||
|
||||
use crate::TileOffset;
|
||||
|
||||
// Todo: Middle mouse button should drag the playfield around
|
||||
// Zoom with + and -
|
||||
#[allow(dead_code)]
|
||||
pub fn movement(
|
||||
time: Res<Time>,
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
mut query: Query<(&mut Transform, &mut TileOffset, &mut OrthographicProjection), With<Camera>>,
|
||||
) {
|
||||
for (mut transform, mut offset, mut ortho) in query.iter_mut() {
|
||||
let mut direction = Vec3::ZERO;
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyA) {
|
||||
direction -= Vec3::new(1.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyD) {
|
||||
direction += Vec3::new(1.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyW) {
|
||||
direction += Vec3::new(0.0, 1.0, 0.0);
|
||||
}
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyS) {
|
||||
direction -= Vec3::new(0.0, 1.0, 0.0);
|
||||
}
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyZ) {
|
||||
ortho.scale += time.delta_seconds();
|
||||
}
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyX) {
|
||||
ortho.scale -= time.delta_seconds();
|
||||
}
|
||||
|
||||
ortho.scale = ortho.scale.clamp(0.5, 1.0);
|
||||
|
||||
let z = transform.translation.z;
|
||||
offset.translation += time.delta_seconds() * direction * (16.0 * 8.0);
|
||||
|
||||
offset.x = (offset.translation.x / 16.0) as isize;
|
||||
offset.y = (offset.translation.y / 16.0) as isize;
|
||||
|
||||
transform.translation = offset.translation % 16.0;
|
||||
|
||||
transform.translation.z = z;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user