mirror of
https://codeberg.org/schrottkatze/mgd2-tram-championships.git
synced 2025-05-20 09:38:00 +00:00
66 lines
2.1 KiB
Rust
66 lines
2.1 KiB
Rust
|
//! Sets up the game's camera controls etc.
|
||
|
|
||
|
use bevy::{
|
||
|
input::mouse::{AccumulatedMouseMotion, MouseButtonInput},
|
||
|
prelude::*,
|
||
|
};
|
||
|
|
||
|
use super::GameplaySet;
|
||
|
|
||
|
pub fn plugin(app: &mut App) {
|
||
|
app.init_resource::<MouseButtonsPressed>().add_systems(
|
||
|
Update,
|
||
|
(
|
||
|
detect_mouse_button_press,
|
||
|
panning_basic.after(detect_mouse_button_press),
|
||
|
)
|
||
|
.in_set(GameplaySet),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
/// Resource to store current mouse button state.
|
||
|
#[derive(Resource, Default)]
|
||
|
struct MouseButtonsPressed {
|
||
|
left: bool,
|
||
|
}
|
||
|
|
||
|
/// Detect whether mouse buttons (currently only left) are pressed.
|
||
|
fn detect_mouse_button_press(
|
||
|
mut button_events: EventReader<MouseButtonInput>,
|
||
|
mut pressed: ResMut<MouseButtonsPressed>,
|
||
|
) {
|
||
|
for event in button_events.read() {
|
||
|
if event.button == MouseButton::Left {
|
||
|
pressed.left = dbg!(event.state.is_pressed());
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// is SUPPOSED to do proper panning
|
||
|
/// i hate 3d math
|
||
|
fn panning_basic(
|
||
|
accumulated_mouse_motion: Res<AccumulatedMouseMotion>,
|
||
|
mut cam_transform: Single<&mut Transform, With<Camera>>,
|
||
|
pressed: Res<MouseButtonsPressed>,
|
||
|
) {
|
||
|
if pressed.left && accumulated_mouse_motion.delta != Vec2::ZERO {
|
||
|
let yaw = Quat::from_rotation_y(accumulated_mouse_motion.delta.x / 150.);
|
||
|
// cam_transform.rotate_around(Vec3::ZERO, yaw);
|
||
|
cam_transform.rotation *= yaw;
|
||
|
|
||
|
let pitch = Quat::from_rotation_x(accumulated_mouse_motion.delta.y / 150.);
|
||
|
let new_rot = cam_transform.rotation * pitch;
|
||
|
// cam_transform.rotate_around(Vec3::ZERO, pitch);
|
||
|
// cam_transform.rotation *= pitch;
|
||
|
let up_vector = new_rot * Vec3::Y;
|
||
|
if up_vector.y > 0.0 {
|
||
|
cam_transform.rotation = new_rot;
|
||
|
}
|
||
|
}
|
||
|
// ???????
|
||
|
// TODO: figure out/understand how they do it https://github.com/The-DevBlog/bevy_third_person_camera/blob/a7c6b458573fcb0730b65eda6507ca27fb58f571/src/mouse.rs#L29-L74
|
||
|
//
|
||
|
let rot_matrix = Mat3::from_quat(cam_transform.rotation);
|
||
|
cam_transform.translation = rot_matrix.mul_vec3(Vec3::new(0.0, 0.0, 20.));
|
||
|
}
|