cyber_rider/src/main.rs

248 lines
7.7 KiB
Rust
Raw Normal View History

2021-11-06 05:32:55 +00:00
use bevy::prelude::*;
use cyber_rider::flycam::{MovementSettings, PlayerPlugin, PLANET_RADIUS};
use heron::prelude::*;
2021-11-06 05:32:55 +00:00
const LIGHT_RANGE: f32 = PLANET_RADIUS * 0.6;
const LIGHT_DIST: f32 = PLANET_RADIUS * 1.2;
2021-11-06 05:32:55 +00:00
fn main() {
App::new()
2021-11-06 05:32:55 +00:00
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins)
.add_plugin(PlayerPlugin)
.add_plugin(PhysicsPlugin::default())
2021-11-06 05:32:55 +00:00
.insert_resource(MovementSettings {
sensitivity: 0.3, // default: 1.0
2021-12-25 02:07:09 +00:00
accel: 20.0, // default: 40.0
2022-01-07 08:45:05 +00:00
drag: 0.01, // default: 0.0005
2021-12-25 02:07:09 +00:00
gravity: 10.0, // default: 10.0
2021-11-06 05:32:55 +00:00
})
.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)
2021-11-06 05:32:55 +00:00
.run();
}
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;
2021-11-06 05:32:55 +00:00
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let red_light = PointLight {
intensity: 1_000.0,
range: LIGHT_RANGE,
color: Color::RED,
2022-01-07 08:45:05 +00:00
radius: 10.0,
..Default::default()
};
let blue_light = PointLight {
intensity: 1_000.0,
range: LIGHT_RANGE,
color: Color::BLUE,
2022-01-07 08:45:05 +00:00
radius: 10.0,
..Default::default()
};
let purple_light = PointLight {
intensity: 1_000.0,
range: LIGHT_RANGE,
color: Color::PURPLE,
2022-01-07 08:45:05 +00:00
radius: 10.0,
..Default::default()
};
2021-12-25 02:07:09 +00:00
commands.insert_resource(AmbientLight {
color: Color::WHITE,
2022-01-07 08:45:05 +00:00
brightness: 0.32,
2021-12-25 02:07:09 +00:00
});
// world
commands
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Icosphere {
radius: PLANET_RADIUS,
subdivisions: 6,
})),
material: materials.add(StandardMaterial {
base_color: Color::GRAY,
metallic: 0.7,
perceptual_roughness: 0.5,
..Default::default()
}),
..Default::default()
})
.insert(RigidBody::Static)
.insert(CollisionShape::Sphere {
radius: PLANET_RADIUS,
});
// east light
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(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(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(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(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(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()
});
});
}
2022-01-07 08:45:05 +00:00
fn animate_lights(
time: Res<Time>,
mut query: Query<&mut Transform, (With<PointLight>, With<Animate>)>,
) {
let dt = time.delta_seconds();
for mut transform in query.iter_mut() {
let translation = &transform.translation;
let x = translation.x;
let y = translation.y;
let z = if translation.z.abs() < 0.1 {
translation.z + 0.15
} else {
translation.z
};
// 10 degrees == 0.174533 radians
2022-01-07 08:45:05 +00:00
let anim_rate = 0.174533 * dt;
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, z_new);
}
2021-11-06 05:32:55 +00:00
}