-
Notifications
You must be signed in to change notification settings - Fork 54
/
spatial.rs
211 lines (191 loc) · 7.36 KB
/
spatial.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use bevy::input::mouse::MouseMotion;
use bevy::prelude::*;
use bevy::window::{CursorGrabMode, PrimaryWindow};
use bevy_kira_audio::prelude::*;
fn main() {
App::new()
.insert_resource(SpatialAudio { max_distance: 25. })
.add_plugins((DefaultPlugins, AudioPlugin, CameraPlugin))
.add_systems(Startup, setup)
.run();
}
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
audio: Res<Audio>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// Emitter Nr. 1
let cooking = audio
.play(asset_server.load("sounds/cooking.ogg"))
.looped()
.handle();
commands
.spawn((
SceneRoot(asset_server.load("models/panStew.glb#Scene0")),
Transform::from_xyz(-5.0, 0., 0.),
))
.insert(AudioEmitter {
instances: vec![cooking],
});
// Emitter Nr. 2
let elevator_music = audio
.play(asset_server.load("sounds/loop.ogg"))
.looped()
.handle();
commands
.spawn((
SceneRoot(asset_server.load("models/boxOpen.glb#Scene0")),
Transform::from_xyz(10., 0., 0.),
))
.insert(AudioEmitter {
instances: vec![elevator_music],
});
// Our camera will be the receiver
commands
.spawn((Camera3d::default(), Transform::from_xyz(0.0, 0.5, 10.0)))
.insert(AudioReceiver)
.insert(FlyCam);
// Other scene setup...
commands.spawn((
PointLight {
intensity: 1500.0,
shadows_enabled: true,
..default()
},
Transform::from_xyz(4.0, 8.0, 4.0),
));
commands.spawn( (Text::new("WASD to move horizontally\nSPACE to ascend\nLSHIFT to descend\nESC to grab/release cursor."), TextFont{
font:asset_server.load("fonts/monogram.ttf"),
font_size: 40.0,
..default()
}, TextColor(Color::linear_rgb(0.9, 0.9, 0.9)),
Node {margin: UiRect::all(Val::Px(15.)), ..default()}));
commands.spawn((
Mesh3d(meshes.add(Cuboid {
half_size: Vec3::new(25., 0., 25.),
})),
MeshMaterial3d(materials.add(StandardMaterial::from_color(LinearRgba::GREEN))),
));
}
// only camera handling code from here on...
// Code modified from https://github.com/sburris0/bevy_flycam under the ISC License
// Copyright 2020 Spencer Burris
//
// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
struct CameraPlugin;
const SENSITIVITY: f32 = 0.00012;
const SPEED: f32 = 12.;
#[derive(Component)]
pub struct FlyCam;
impl Plugin for CameraPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<InputState>()
.add_systems(Startup, initial_grab_cursor)
.add_systems(Update, (player_move, player_look, cursor_grab));
}
}
fn initial_grab_cursor(mut primary_window: Query<&mut Window, With<PrimaryWindow>>) {
if let Ok(mut window) = primary_window.get_single_mut() {
toggle_grab_cursor(&mut window);
} else {
warn!("Primary window not found for `initial_grab_cursor`!");
}
}
fn cursor_grab(
keys: Res<ButtonInput<KeyCode>>,
mut primary_window: Query<&mut Window, With<PrimaryWindow>>,
) {
if let Ok(mut window) = primary_window.get_single_mut() {
if keys.just_pressed(KeyCode::Escape) {
toggle_grab_cursor(&mut window);
}
} else {
warn!("Primary window not found for `cursor_grab`!");
}
}
/// Grabs/ungrabs mouse cursor
fn toggle_grab_cursor(window: &mut Window) {
match window.cursor_options.grab_mode {
CursorGrabMode::None => {
window.cursor_options.grab_mode = CursorGrabMode::Confined;
window.cursor_options.visible = false;
}
_ => {
window.cursor_options.grab_mode = CursorGrabMode::None;
window.cursor_options.visible = true;
}
}
}
/// Handles keyboard input and movement
fn player_move(
keys: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
primary_window: Query<&Window, With<PrimaryWindow>>,
mut query: Query<&mut Transform, With<FlyCam>>,
) {
if let Ok(window) = primary_window.get_single() {
for mut transform in query.iter_mut() {
let mut velocity = Vec3::ZERO;
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 keys.get_pressed() {
match window.cursor_options.grab_mode {
CursorGrabMode::None => (),
_ => match key {
KeyCode::KeyW => velocity += forward,
KeyCode::KeyS => velocity -= forward,
KeyCode::KeyA => velocity -= right,
KeyCode::KeyD => velocity += right,
KeyCode::Space => velocity += Vec3::Y,
KeyCode::ShiftLeft => velocity -= Vec3::Y,
_ => (),
},
}
}
velocity = velocity.normalize_or_zero();
transform.translation += velocity * time.delta_secs() * SPEED;
}
} else {
warn!("Primary window not found for `player_move`!");
}
}
#[derive(Resource, Default)]
struct InputState {
pitch: f32,
yaw: f32,
}
/// Handles looking around if cursor is locked
fn player_look(
primary_window: Query<&Window, With<PrimaryWindow>>,
mut state: ResMut<InputState>,
mut motion: EventReader<MouseMotion>,
mut query: Query<&mut Transform, With<FlyCam>>,
) {
if let Ok(window) = primary_window.get_single() {
let delta_state = state.as_mut();
for mut transform in query.iter_mut() {
for ev in motion.read() {
match window.cursor_options.grab_mode {
CursorGrabMode::None => (),
_ => {
// Using smallest of height or width ensures equal vertical and horizontal sensitivity
let window_scale = window.height().min(window.width());
delta_state.pitch -= (SENSITIVITY * ev.delta.y * window_scale).to_radians();
delta_state.yaw -= (SENSITIVITY * ev.delta.x * window_scale).to_radians();
}
}
delta_state.pitch = delta_state.pitch.clamp(-1.54, 1.54);
// Order is important to prevent unintended roll
transform.rotation = Quat::from_axis_angle(Vec3::Y, delta_state.yaw)
* Quat::from_axis_angle(Vec3::X, delta_state.pitch);
}
}
} else {
warn!("Primary window not found for `player_look`!");
}
}