mouse lock
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
3504
Cargo.lock
generated
Normal file
3504
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "l_system_plants"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
bevy = "0.5"
|
||||
48
src/bundles/camera.rs
Normal file
48
src/bundles/camera.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use bevy::ecs::bundle::Bundle;
|
||||
use bevy::transform::components::{GlobalTransform, Transform};
|
||||
use bevy::render::{
|
||||
camera::{
|
||||
Camera, PerspectiveProjection,
|
||||
VisibleEntities,
|
||||
},
|
||||
render_graph::base,
|
||||
};
|
||||
use bevy::math::*;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct FPSCamera
|
||||
{
|
||||
pub yaw: f32,
|
||||
pub pitch: f32
|
||||
}
|
||||
|
||||
/// Component bundle for fps camera entities with perspective projection
|
||||
#[derive(Bundle)]
|
||||
pub struct FpsCameraBundle {
|
||||
pub camera: Camera,
|
||||
pub perspective_projection: PerspectiveProjection,
|
||||
pub visible_entities: VisibleEntities,
|
||||
pub transform: Transform,
|
||||
pub global_transform: GlobalTransform,
|
||||
pub fps_camera: FPSCamera,
|
||||
}
|
||||
|
||||
impl Default for FpsCameraBundle {
|
||||
fn default() -> Self {
|
||||
FpsCameraBundle {
|
||||
camera: Camera {
|
||||
name: Some(base::camera::CAMERA_3D.to_string()),
|
||||
..Default::default()
|
||||
},
|
||||
perspective_projection: PerspectiveProjection {
|
||||
near: 0.1,
|
||||
far: 1000.0,
|
||||
..Default::default()
|
||||
},
|
||||
visible_entities: Default::default(),
|
||||
transform: Default::default(),
|
||||
global_transform: Default::default(),
|
||||
fps_camera: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
1
src/bundles/mod.rs
Normal file
1
src/bundles/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod camera;
|
||||
127
src/main.rs
Normal file
127
src/main.rs
Normal file
@@ -0,0 +1,127 @@
|
||||
use bevy::input::mouse::MouseMotion;
|
||||
use bevy::prelude::*;
|
||||
|
||||
mod bundles;
|
||||
use bundles::camera::*;
|
||||
|
||||
|
||||
fn main() {
|
||||
App::build()
|
||||
.insert_resource(Msaa {samples: 4})
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_startup_system(startup.system())
|
||||
.add_startup_system(initial_grab_cursor.system())
|
||||
.add_system(camera_movement_system.system())
|
||||
.add_system(camera_update_system.system())
|
||||
.add_system(cursor_grab.system())
|
||||
.run();
|
||||
}
|
||||
|
||||
fn camera_movement_system(
|
||||
time: Res<Time>,
|
||||
keyboard_input: Res<Input<KeyCode>>,
|
||||
windows: Res<Windows>,
|
||||
mut mouse_input: EventReader<MouseMotion>,
|
||||
mut query: Query<(&mut Transform, &mut FPSCamera)>)
|
||||
{
|
||||
let window = windows.get_primary().unwrap();
|
||||
if !window.cursor_locked()
|
||||
{
|
||||
return;
|
||||
}
|
||||
for (mut transform, mut cam) in query.iter_mut() {
|
||||
let mouse_sensitivity = 25.;
|
||||
let walking_speed = 0.5;
|
||||
|
||||
for ev_m in mouse_input.iter()
|
||||
{
|
||||
cam.yaw += ev_m.delta.x * time.delta_seconds() * mouse_sensitivity;
|
||||
cam.pitch += ev_m.delta.y * time.delta_seconds() * mouse_sensitivity * -1.;
|
||||
if cam.pitch > 89.0
|
||||
{
|
||||
cam.pitch = 89.0;
|
||||
}
|
||||
if cam.pitch < -89.0
|
||||
{
|
||||
cam.pitch = -89.0;
|
||||
}
|
||||
}
|
||||
let local_z = transform.local_z();
|
||||
let forward = -Vec3::new(local_z.x, 0., local_z.z);
|
||||
let right = Vec3::new(local_z.z, 0., -local_z.x);
|
||||
|
||||
for key in keyboard_input.get_pressed()
|
||||
{
|
||||
match key {
|
||||
KeyCode::W => transform.translation += forward * time.delta_seconds() * walking_speed,
|
||||
KeyCode::A => transform.translation += -right * time.delta_seconds() * walking_speed,
|
||||
KeyCode::S => transform.translation += -forward * time.delta_seconds() * walking_speed,
|
||||
KeyCode::D => transform.translation += right * time.delta_seconds() * walking_speed,
|
||||
KeyCode::Space => transform.translation += Vec3::Y * time.delta_seconds() * walking_speed,
|
||||
KeyCode::C => transform.translation -= Vec3::Y * time.delta_seconds() * walking_speed,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn camera_update_system(
|
||||
mut query: Query<(&mut Transform, &FPSCamera)>
|
||||
)
|
||||
{
|
||||
if let Ok((mut trans, cam)) = query.single_mut() {
|
||||
let mut direction = Vec3::ZERO;
|
||||
|
||||
direction.x = cam.yaw.to_radians().cos() * cam.pitch.to_radians().cos();
|
||||
direction.y = cam.pitch.to_radians().sin();
|
||||
direction.z = cam.yaw.to_radians().sin() * cam.pitch.to_radians().cos();
|
||||
direction.normalize();
|
||||
|
||||
let pos = trans.translation;
|
||||
let transform = Transform::from_translation(pos).looking_at(pos + direction, Vec3::Y);
|
||||
trans.clone_from(&transform);
|
||||
}
|
||||
}
|
||||
|
||||
fn cursor_grab(keys: Res<Input<KeyCode>>, mut windows: ResMut<Windows>)
|
||||
{
|
||||
let window = windows.get_primary_mut().unwrap();
|
||||
if keys.just_pressed(KeyCode::Escape) {
|
||||
toggle_grab_cursor(window);
|
||||
}
|
||||
}
|
||||
|
||||
fn toggle_grab_cursor(window: &mut Window)
|
||||
{
|
||||
window.set_cursor_lock_mode(!window.cursor_locked());
|
||||
window.set_cursor_visibility(!window.cursor_visible());
|
||||
}
|
||||
|
||||
fn initial_grab_cursor(mut windows: ResMut<Windows>)
|
||||
{
|
||||
toggle_grab_cursor(windows.get_primary_mut().unwrap());
|
||||
}
|
||||
|
||||
fn startup(mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>)
|
||||
{
|
||||
//Plane
|
||||
commands.spawn_bundle(PbrBundle {
|
||||
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0})),
|
||||
material: materials.add(Color::rgb(0.3,0.5,0.3).into()),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
// light
|
||||
commands.spawn_bundle(LightBundle {
|
||||
transform: Transform::from_xyz(4.0,8.0,4.0),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
commands.spawn_bundle(FpsCameraBundle {
|
||||
transform: Transform::from_xyz(-2.0,2.5,5.0).looking_at(Vec3::ZERO, Vec3::Y),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user