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 {
Self {
sensitivity: 0.00012,
speed: 12.,
speed: 40.,
}
}
}
/// Used in queries when you want flycams and not other cameras
#[derive(Debug)]
#[derive(Component, Debug)]
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
fn setup_player(mut commands: Commands) {
commands
.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()
})
.insert(FlyCam);
@ -60,11 +49,9 @@ fn setup_player(mut commands: Commands) {
fn player_move(
keys: Res<Input<KeyCode>>,
time: Res<Time>,
windows: Res<Windows>,
settings: Res<MovementSettings>,
mut query: Query<(&FlyCam, &mut Transform)>,
) {
let window = windows.get_primary().unwrap();
for (_camera, mut transform) in query.iter_mut() {
let mut velocity = Vec3::ZERO;
let local_z = transform.local_z();
@ -72,18 +59,16 @@ fn player_move(
let right = Vec3::new(local_z.z, 0., -local_z.x);
for key in keys.get_pressed() {
if window.cursor_locked() {
match key {
KeyCode::W => velocity += forward,
KeyCode::S => velocity -= forward,
KeyCode::A => velocity -= right,
KeyCode::D => velocity += right,
KeyCode::Up => velocity += forward,
KeyCode::Down => velocity -= forward,
KeyCode::Left => velocity -= right,
KeyCode::Right => velocity += right,
KeyCode::Space => velocity += Vec3::Y,
KeyCode::Delete => velocity -= Vec3::Y,
KeyCode::Back => velocity -= Vec3::Y,
_ => (),
}
}
}
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();
if keys.just_pressed(KeyCode::Escape) {
toggle_grab_cursor(window);
if keys.just_pressed(KeyCode::L) {
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
pub struct 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>()
.init_resource::<MovementSettings>()
.add_startup_system(setup_player.system())
.add_startup_system(initial_grab_cursor.system())
.add_system(player_move.system())
.add_system(player_look.system())
.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());
.add_startup_system(setup_player)
.add_system(player_move)
.add_system(player_look)
.add_system(cursor_grab);
}
}

View file

@ -1,49 +1,63 @@
use bevy::prelude::*;
use bevy_inspector_egui::WorldInspectorPlugin;
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
// Spencer Burris
/*
stolen with neither shame nor pity from
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() {
App::build()
App::new()
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins)
.add_plugin(PlayerPlugin)
.add_plugin(WorldInspectorPlugin::new())
.insert_resource(MovementSettings {
sensitivity: 0.00015, // default: 0.00012
speed: 12.0, // default: 12.0
})
.add_startup_system(setup.system())
.add_system(animate_lights.system())
.add_startup_system(setup.label("world"))
.add_startup_system(disable_cursor.after("world"))
.add_system(animate_lights)
.add_system(bevy::input::system::exit_on_esc_system)
.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(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let red_light = Light {
intensity: 50_000.0,
range: 1800.0,
let red_light = PointLight {
intensity: 10_000.0,
range: LIGHT_RANGE,
color: Color::RED,
..Default::default()
};
let blue_light = Light {
intensity: 50_000.0,
range: 1800.0,
let blue_light = PointLight {
intensity: 10_000.0,
range: LIGHT_RANGE,
color: Color::BLUE,
..Default::default()
};
let purple_light = Light {
intensity: 50_000.0,
range: 1800.0,
let purple_light = PointLight {
intensity: 10_000.0,
range: LIGHT_RANGE,
color: Color::PURPLE,
..Default::default()
};
@ -51,70 +65,168 @@ fn setup(
// world
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Icosphere {
radius: 300.0,
subdivisions: 5,
radius: BIG_RADIUS,
subdivisions: 6,
})),
material: materials.add(StandardMaterial {
base_color: Color::GRAY,
metallic: 0.7,
roughness: 0.2,
perceptual_roughness: 0.5,
..Default::default()
}),
..Default::default()
});
// east light
commands.spawn_bundle(LightBundle {
transform: Transform::from_xyz(750.0, 0.0, 0.0),
light: purple_light.clone(),
commands
.spawn_bundle(PointLightBundle {
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()
});
})
.insert(Animate);
// west light
commands.spawn_bundle(LightBundle {
transform: Transform::from_xyz(-750.0, 0.0, 0.0),
light: purple_light.clone(),
commands
.spawn_bundle(PointLightBundle {
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()
});
})
.insert(Animate);
// north light
commands.spawn_bundle(LightBundle {
transform: Transform::from_xyz(0.0, 0.0, 750.0),
light: purple_light.clone(),
commands
.spawn_bundle(PointLightBundle {
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()
});
})
.insert(Animate);
// south light
commands.spawn_bundle(LightBundle {
transform: Transform::from_xyz(0.0, 0.0, -750.0),
light: purple_light,
commands
.spawn_bundle(PointLightBundle {
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()
});
})
.insert(Animate);
// up light
commands.spawn_bundle(LightBundle {
transform: Transform::from_xyz(0.0, 750.0, 0.0),
light: red_light,
commands
.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(0.0, LIGHT_DIST, 0.0),
point_light: red_light,
..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
commands.spawn_bundle(LightBundle {
transform: Transform::from_xyz(0.0, 750.0, 0.0),
light: blue_light,
commands
.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(0.0, -LIGHT_DIST, 0.0),
point_light: blue_light,
..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() {
let translation = &transform.translation;
let x = translation.x;
let y = if translation.y.abs() < 0.1 {
translation.y + 0.15
let y = translation.y;
let z = if translation.z.abs() < 0.1 {
translation.z + 0.15
} else {
translation.y
translation.z
};
let theta = y.atan2(x) + 0.174533; // 10 degrees == 0.174533 radians
let x_new = 750.0 * theta.cos();
let y_new = 750.0 * theta.sin();
// 10 degrees == 0.174533 radians
let anim_rate = 0.174533 / 5.0;
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);
}
}