Shrink planet, get rotating visible lights near the surface.

This commit is contained in:
Joe Ardent 2021-12-19 23:11:23 -08:00
parent d4f8c0ec56
commit 3f322ba0ea
2 changed files with 201 additions and 107 deletions

View file

@ -26,31 +26,20 @@ impl Default for MovementSettings {
fn default() -> Self { fn default() -> Self {
Self { Self {
sensitivity: 0.00012, sensitivity: 0.00012,
speed: 12., speed: 40.,
} }
} }
} }
/// Used in queries when you want flycams and not other cameras /// Used in queries when you want flycams and not other cameras
#[derive(Debug)] #[derive(Component, Debug)]
pub struct FlyCam; pub struct FlyCam;
/// Grabs/ungrabs mouse cursor
fn toggle_grab_cursor(window: &mut Window) {
window.set_cursor_lock_mode(!window.cursor_locked());
window.set_cursor_visibility(!window.cursor_visible());
}
/// Grabs the cursor when game first starts
fn initial_grab_cursor(mut windows: ResMut<Windows>) {
toggle_grab_cursor(windows.get_primary_mut().unwrap());
}
/// Spawns the `Camera3dBundle` to be controlled /// Spawns the `Camera3dBundle` to be controlled
fn setup_player(mut commands: Commands) { fn setup_player(mut commands: Commands) {
commands commands
.spawn_bundle(PerspectiveCameraBundle { .spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(0.0, 0.0, -1200.0).looking_at(Vec3::ZERO, Vec3::Y), transform: Transform::from_xyz(0.0, 100.0, -500.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default() ..Default::default()
}) })
.insert(FlyCam); .insert(FlyCam);
@ -60,11 +49,9 @@ fn setup_player(mut commands: Commands) {
fn player_move( fn player_move(
keys: Res<Input<KeyCode>>, keys: Res<Input<KeyCode>>,
time: Res<Time>, time: Res<Time>,
windows: Res<Windows>,
settings: Res<MovementSettings>, settings: Res<MovementSettings>,
mut query: Query<(&FlyCam, &mut Transform)>, mut query: Query<(&FlyCam, &mut Transform)>,
) { ) {
let window = windows.get_primary().unwrap();
for (_camera, mut transform) in query.iter_mut() { for (_camera, mut transform) in query.iter_mut() {
let mut velocity = Vec3::ZERO; let mut velocity = Vec3::ZERO;
let local_z = transform.local_z(); let local_z = transform.local_z();
@ -72,18 +59,16 @@ fn player_move(
let right = Vec3::new(local_z.z, 0., -local_z.x); let right = Vec3::new(local_z.z, 0., -local_z.x);
for key in keys.get_pressed() { for key in keys.get_pressed() {
if window.cursor_locked() {
match key { match key {
KeyCode::W => velocity += forward, KeyCode::Up => velocity += forward,
KeyCode::S => velocity -= forward, KeyCode::Down => velocity -= forward,
KeyCode::A => velocity -= right, KeyCode::Left => velocity -= right,
KeyCode::D => velocity += right, KeyCode::Right => velocity += right,
KeyCode::Space => velocity += Vec3::Y, KeyCode::Space => velocity += Vec3::Y,
KeyCode::Delete => velocity -= Vec3::Y, KeyCode::Back => velocity -= Vec3::Y,
_ => (), _ => (),
} }
} }
}
velocity = velocity.normalize(); velocity = velocity.normalize();
@ -122,10 +107,21 @@ fn player_look(
} }
} }
fn cursor_grab(keys: Res<Input<KeyCode>>, mut windows: ResMut<Windows>) { fn cursor_grab(
keys: Res<Input<KeyCode>>,
mut windows: ResMut<Windows>,
mut query: Query<(&FlyCam, &mut Transform)>,
) {
let window = windows.get_primary_mut().unwrap(); let window = windows.get_primary_mut().unwrap();
if keys.just_pressed(KeyCode::Escape) { if keys.just_pressed(KeyCode::L) {
toggle_grab_cursor(window); window.set_cursor_lock_mode(true);
window.set_cursor_visibility(true);
let (_, mut transform) = query.single_mut();
*transform.as_mut() = transform.looking_at(Vec3::ZERO, Vec3::Y);
}
if keys.just_pressed(KeyCode::U) {
window.set_cursor_lock_mode(false);
window.set_cursor_visibility(false);
} }
} }
@ -133,26 +129,12 @@ fn cursor_grab(keys: Res<Input<KeyCode>>, mut windows: ResMut<Windows>) {
/// game /// game
pub struct PlayerPlugin; pub struct PlayerPlugin;
impl Plugin for PlayerPlugin { impl Plugin for PlayerPlugin {
fn build(&self, app: &mut bevy::prelude::AppBuilder) { fn build(&self, app: &mut bevy::prelude::App) {
app.init_resource::<InputState>() app.init_resource::<InputState>()
.init_resource::<MovementSettings>() .init_resource::<MovementSettings>()
.add_startup_system(setup_player.system()) .add_startup_system(setup_player)
.add_startup_system(initial_grab_cursor.system()) .add_system(player_move)
.add_system(player_move.system()) .add_system(player_look)
.add_system(player_look.system()) .add_system(cursor_grab);
.add_system(cursor_grab.system());
}
}
/// Same as `PlayerPlugin` but does not spawn a camera
pub struct NoCameraPlayerPlugin;
impl Plugin for NoCameraPlayerPlugin {
fn build(&self, app: &mut bevy::prelude::AppBuilder) {
app.init_resource::<InputState>()
.init_resource::<MovementSettings>()
.add_startup_system(initial_grab_cursor.system())
.add_system(player_move.system())
.add_system(player_look.system())
.add_system(cursor_grab.system());
} }
} }

View file

@ -1,49 +1,63 @@
use bevy::prelude::*; use bevy::prelude::*;
use bevy_inspector_egui::WorldInspectorPlugin;
use cyber_rider::flycam::{MovementSettings, PlayerPlugin}; use cyber_rider::flycam::{MovementSettings, PlayerPlugin};
// stolen with neither shame nor pity from /*
// git@github.com:sburris0/bevy_flycam.git, b90f6fc, which is copyright 2020 stolen with neither shame nor pity from
// Spencer Burris git@github.com:sburris0/bevy_flycam.git, b90f6fc, which is copyright 2020
Spencer Burris
*/
const BIG_RADIUS: f32 = 150.0;
const LIGHT_RANGE: f32 = BIG_RADIUS * 3.0;
const LIGHT_DIST: f32 = BIG_RADIUS * 1.2;
fn main() { fn main() {
App::build() App::new()
.insert_resource(Msaa { samples: 4 }) .insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugin(PlayerPlugin) .add_plugin(PlayerPlugin)
.add_plugin(WorldInspectorPlugin::new())
.insert_resource(MovementSettings { .insert_resource(MovementSettings {
sensitivity: 0.00015, // default: 0.00012 sensitivity: 0.00015, // default: 0.00012
speed: 12.0, // default: 12.0 speed: 12.0, // default: 12.0
}) })
.add_startup_system(setup.system()) .add_startup_system(setup.label("world"))
.add_system(animate_lights.system()) .add_startup_system(disable_cursor.after("world"))
.add_system(animate_lights)
.add_system(bevy::input::system::exit_on_esc_system)
.run(); .run();
} }
/// set up a simple 3D scene fn disable_cursor(mut windows: ResMut<Windows>) {
let window = windows.get_primary_mut().unwrap();
window.set_cursor_lock_mode(false);
window.set_cursor_visibility(true);
}
#[derive(Component)]
struct Animate;
fn setup( fn setup(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) { ) {
let red_light = Light { let red_light = PointLight {
intensity: 50_000.0, intensity: 10_000.0,
range: 1800.0, range: LIGHT_RANGE,
color: Color::RED, color: Color::RED,
..Default::default() ..Default::default()
}; };
let blue_light = Light { let blue_light = PointLight {
intensity: 50_000.0, intensity: 10_000.0,
range: 1800.0, range: LIGHT_RANGE,
color: Color::BLUE, color: Color::BLUE,
..Default::default() ..Default::default()
}; };
let purple_light = Light { let purple_light = PointLight {
intensity: 50_000.0, intensity: 10_000.0,
range: 1800.0, range: LIGHT_RANGE,
color: Color::PURPLE, color: Color::PURPLE,
..Default::default() ..Default::default()
}; };
@ -51,70 +65,168 @@ fn setup(
// world // world
commands.spawn_bundle(PbrBundle { commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Icosphere { mesh: meshes.add(Mesh::from(shape::Icosphere {
radius: 300.0, radius: BIG_RADIUS,
subdivisions: 5, subdivisions: 6,
})), })),
material: materials.add(StandardMaterial { material: materials.add(StandardMaterial {
base_color: Color::GRAY, base_color: Color::GRAY,
metallic: 0.7, metallic: 0.7,
roughness: 0.2, perceptual_roughness: 0.5,
..Default::default() ..Default::default()
}), }),
..Default::default() ..Default::default()
}); });
// east light // east light
commands.spawn_bundle(LightBundle { commands
transform: Transform::from_xyz(750.0, 0.0, 0.0), .spawn_bundle(PointLightBundle {
light: purple_light.clone(), transform: Transform::from_xyz(LIGHT_DIST, 0.0, 0.0),
point_light: purple_light,
..Default::default()
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::UVSphere {
radius: 10.0,
..Default::default()
})),
material: materials.add(StandardMaterial {
base_color: Color::PURPLE,
emissive: Color::rgba_linear(50.0, 0.0, 50.0, 0.0),
..Default::default()
}),
..Default::default() ..Default::default()
}); });
})
.insert(Animate);
// west light // west light
commands.spawn_bundle(LightBundle { commands
transform: Transform::from_xyz(-750.0, 0.0, 0.0), .spawn_bundle(PointLightBundle {
light: purple_light.clone(), transform: Transform::from_xyz(-LIGHT_DIST, 0.0, 0.0),
point_light: purple_light,
..Default::default()
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::UVSphere {
radius: 10.0,
..Default::default()
})),
material: materials.add(StandardMaterial {
base_color: Color::PURPLE,
emissive: Color::rgba_linear(50.0, 0.0, 50.0, 0.0),
..Default::default()
}),
..Default::default() ..Default::default()
}); });
})
.insert(Animate);
// north light // north light
commands.spawn_bundle(LightBundle { commands
transform: Transform::from_xyz(0.0, 0.0, 750.0), .spawn_bundle(PointLightBundle {
light: purple_light.clone(), transform: Transform::from_xyz(0.0, 0.0, LIGHT_DIST),
point_light: purple_light,
..Default::default()
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::UVSphere {
radius: 10.0,
..Default::default()
})),
material: materials.add(StandardMaterial {
base_color: Color::PURPLE,
emissive: Color::rgba_linear(50.0, 0.0, 50.0, 0.0),
..Default::default()
}),
..Default::default() ..Default::default()
}); });
})
.insert(Animate);
// south light // south light
commands.spawn_bundle(LightBundle { commands
transform: Transform::from_xyz(0.0, 0.0, -750.0), .spawn_bundle(PointLightBundle {
light: purple_light, transform: Transform::from_xyz(0.0, 0.0, -LIGHT_DIST),
point_light: purple_light,
..Default::default()
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::UVSphere {
radius: 10.0,
..Default::default()
})),
material: materials.add(StandardMaterial {
base_color: Color::PURPLE,
emissive: Color::rgba_linear(50.0, 0.0, 50.0, 0.0),
..Default::default()
}),
..Default::default() ..Default::default()
}); });
})
.insert(Animate);
// up light // up light
commands.spawn_bundle(LightBundle { commands
transform: Transform::from_xyz(0.0, 750.0, 0.0), .spawn_bundle(PointLightBundle {
light: red_light, transform: Transform::from_xyz(0.0, LIGHT_DIST, 0.0),
point_light: red_light,
..Default::default() ..Default::default()
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Icosphere {
radius: 10.0,
subdivisions: 2,
})),
material: materials.add(StandardMaterial {
base_color: Color::RED,
emissive: Color::rgba_linear(100.0, 0.0, 0.0, 0.0),
..Default::default()
}),
..Default::default()
});
}); });
// down light // down light
commands.spawn_bundle(LightBundle { commands
transform: Transform::from_xyz(0.0, 750.0, 0.0), .spawn_bundle(PointLightBundle {
light: blue_light, transform: Transform::from_xyz(0.0, -LIGHT_DIST, 0.0),
point_light: blue_light,
..Default::default() ..Default::default()
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Icosphere {
radius: 10.0,
subdivisions: 2,
})),
material: materials.add(StandardMaterial {
base_color: Color::BLUE,
emissive: Color::rgba_linear(0.0, 0.0, 100.0, 0.0),
..Default::default()
}),
..Default::default()
});
}); });
} }
fn animate_lights(mut query: Query<&mut Transform, With<Light>>) { fn animate_lights(mut query: Query<&mut Transform, (With<PointLight>, With<Animate>)>) {
for mut transform in query.iter_mut() { for mut transform in query.iter_mut() {
let translation = &transform.translation; let translation = &transform.translation;
let x = translation.x; let x = translation.x;
let y = if translation.y.abs() < 0.1 { let y = translation.y;
translation.y + 0.15 let z = if translation.z.abs() < 0.1 {
translation.z + 0.15
} else { } else {
translation.y translation.z
}; };
let theta = y.atan2(x) + 0.174533; // 10 degrees == 0.174533 radians // 10 degrees == 0.174533 radians
let x_new = 750.0 * theta.cos(); let anim_rate = 0.174533 / 5.0;
let y_new = 750.0 * theta.sin(); let theta = z.atan2(x) + anim_rate;
let x_new = LIGHT_DIST * theta.cos();
let z_new = LIGHT_DIST * theta.sin();
*transform = Transform::from_xyz(x_new, y_new, 0.0); *transform = Transform::from_xyz(x_new, y, z_new);
} }
} }