| Name | Message | Date |
|---|---|---|
| 📁 physical_monitor | 1 day ago | |
| 📄 camera_controller.rs | 1 day ago | |
| 📄 main.rs | 1 day ago | |
| 📄 virtual_window.rs | 1 day ago |
📄
src/camera_controller.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use bevy::{ app::{App, Plugin, Update}, camera::Camera, ecs::{ query::With, schedule::IntoScheduleConfigs, system::{If, Res, Single}, }, input::{ ButtonInput, gamepad::{Gamepad, GamepadAxis, GamepadButton}, keyboard::KeyCode, }, math::Vec3, transform::components::Transform, }; use crate::virtual_window::VirtualWindow; pub struct CameraControllerPlugin; impl Plugin for CameraControllerPlugin { fn build(&self, app: &mut App) { app.add_systems( Update, (move_camera_with_keyboard, move_camera_with_controller).chain(), ); } } fn move_camera_with_controller( camera: Single<(&mut Transform, &VirtualWindow), With<Camera>>, gamepad: If<Single<&Gamepad>>, ) { let (mut camera, window) = camera.into_inner(); let mut origin = window.camera_position() + window.camera_normal().as_vec3(); camera.translation = origin; camera.look_at(window.center(), Vec3::Y); origin += camera.local_z().as_vec3() * (gamepad .get(GamepadButton::LeftTrigger2) .and_then(|r| gamepad.get(GamepadButton::RightTrigger2).map(|l| r - l))) .unwrap_or_default(); origin += camera.local_x().as_vec3() * gamepad.get(GamepadAxis::LeftStickX).unwrap_or_default(); origin += camera.local_y().as_vec3() * gamepad.get(GamepadAxis::LeftStickY).unwrap_or_default(); camera.translation = origin; camera.look_at(window.center(), Vec3::Y); } fn move_camera_with_keyboard( camera: Single<(&mut Transform, &VirtualWindow), With<Camera>>, input: Res<ButtonInput<KeyCode>>, ) { let (mut camera, window) = camera.into_inner(); let mut origin = window.camera_position() + window.camera_normal().as_vec3(); camera.translation = origin; camera.look_at(window.center(), Vec3::Y); if input.pressed(KeyCode::KeyS) { origin += camera.local_z().as_vec3(); } if input.pressed(KeyCode::KeyW) { origin -= camera.local_z().as_vec3(); } if input.pressed(KeyCode::KeyD) { origin += camera.local_x().as_vec3(); } if input.pressed(KeyCode::KeyA) { origin -= camera.local_x().as_vec3(); } if input.pressed(KeyCode::KeyE) { origin += camera.local_y().as_vec3(); } if input.pressed(KeyCode::KeyQ) { origin -= camera.local_y().as_vec3(); } camera.translation = origin; camera.look_at(window.center(), Vec3::Y); }