149 lines
4.6 KiB
Rust
149 lines
4.6 KiB
Rust
use bevy::prelude::*;
|
|
|
|
use crate::{camera::MouseCoords, illumination::Illumination};
|
|
|
|
pub struct Player;
|
|
pub struct PlayerLight {
|
|
i: u8,
|
|
}
|
|
|
|
pub fn spawn_player(
|
|
commands: &mut Commands,
|
|
meshes: &mut Assets<Mesh>,
|
|
materials: &mut Assets<StandardMaterial>,
|
|
) {
|
|
commands
|
|
.spawn_bundle(PbrBundle {
|
|
mesh: meshes.add(Mesh::from(shape::Capsule {
|
|
radius: 0.5,
|
|
..Default::default()
|
|
})),
|
|
material: materials.add(Color::rgb(1.0, 1.0, 1.0).into()),
|
|
transform: Transform::from_xyz(0.0, 1.0, 0.0),
|
|
..Default::default()
|
|
})
|
|
.insert(Player);
|
|
|
|
// light
|
|
commands
|
|
.spawn()
|
|
.insert(LightFriends)
|
|
.insert(Transform::default())
|
|
.insert(GlobalTransform::default())
|
|
.with_children(|children| {
|
|
let count = 6;
|
|
for i in 0..count {
|
|
let color = Color::hsl(360.0 * i as f32 / count as f32, 0.5, 0.5);
|
|
|
|
let mut light_material: StandardMaterial = color.into();
|
|
light_material.metallic = 0.5;
|
|
light_material.reflectance = 0.5;
|
|
light_material.emissive = color.as_rgba() * 10.0;
|
|
let light_material = materials.add(light_material);
|
|
|
|
children
|
|
.spawn_bundle(PbrBundle {
|
|
mesh: meshes.add(Mesh::from(shape::Icosphere {
|
|
radius: 0.2,
|
|
subdivisions: 5,
|
|
})),
|
|
material: light_material.clone(),
|
|
transform: Transform::from_xyz(0.0, 0.5, 0.0),
|
|
..Default::default()
|
|
})
|
|
.insert_bundle(LightBundle {
|
|
transform: Transform::from_xyz(0.0, 0.0, 0.0),
|
|
light: Light {
|
|
color: color.as_rgba() * 10.0,
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
})
|
|
.insert(PlayerLight { i: i as u8 + 1 })
|
|
.insert(Illumination { radius: 13.0 });
|
|
}
|
|
});
|
|
}
|
|
|
|
pub struct LightFriends;
|
|
pub fn move_light_friends(
|
|
mut query: Query<(&mut Transform, &LightFriends)>,
|
|
player: Query<(&Transform, &Player), Without<LightFriends>>,
|
|
coords: Res<MouseCoords>,
|
|
interpolation: Res<FloatingOrbsInterpolationState>,
|
|
time: Res<Time>,
|
|
) {
|
|
let player_pos = if let Some(player) = player.iter().next() {
|
|
player.0.translation
|
|
} else {
|
|
return;
|
|
};
|
|
|
|
for (mut trans, _light) in query.iter_mut() {
|
|
// interpolate between player pos and mouse click pos
|
|
let i = interpolation.0;
|
|
let center = i * coords.0 + (1.0 - i) * player_pos;
|
|
|
|
let mut d = trans.translation - center;
|
|
d.y = 0.0;
|
|
trans.translation -= d * time.delta_seconds();
|
|
}
|
|
}
|
|
|
|
pub fn light_movement(
|
|
mut query: Query<(&mut Transform, &PlayerLight)>,
|
|
time: Res<Time>,
|
|
interpolation: Res<FloatingOrbsInterpolationState>,
|
|
) {
|
|
let t = time.seconds_since_startup();
|
|
// make the friends go further when the button is pressed
|
|
let width = 5.0 + interpolation.0 * 7.0;
|
|
|
|
for (mut trans, light) in query.iter_mut() {
|
|
let i = light.i as f64;
|
|
let i2 = i / 2.0;
|
|
trans.translation = Vec3::new(
|
|
width * i2 as f32 * (t * 0.4 * i2 + i).cos() as f32,
|
|
3.0 * (t * 1.1 * i2 + i).sin() as f32 + 4.0,
|
|
width * (t * 0.4 * i + i).sin() as f32,
|
|
);
|
|
}
|
|
}
|
|
|
|
#[derive(Default)]
|
|
pub struct FloatingOrbsInterpolationState(f32);
|
|
pub fn update_floating_orbs_interpolation(
|
|
mut state: ResMut<FloatingOrbsInterpolationState>,
|
|
input: Res<Input<MouseButton>>,
|
|
time: Res<Time>,
|
|
) {
|
|
let ds = time.delta_seconds();
|
|
if input.pressed(MouseButton::Left) {
|
|
state.0 = (state.0 + 0.5 * ds).clamp(0.0, 1.0);
|
|
} else {
|
|
state.0 = (state.0 - ds).clamp(0.0, 1.0);
|
|
}
|
|
}
|
|
|
|
pub fn move_player(
|
|
mut query: Query<(&mut Transform, &Player)>,
|
|
input: Res<Input<KeyCode>>,
|
|
time: Res<Time>,
|
|
) {
|
|
let ds = time.delta_seconds() * 5.0;
|
|
|
|
for (mut transform, _) in query.iter_mut() {
|
|
if input.pressed(KeyCode::W) {
|
|
transform.translation += Vec3::X * ds;
|
|
}
|
|
if input.pressed(KeyCode::S) {
|
|
transform.translation -= Vec3::X * ds;
|
|
}
|
|
if input.pressed(KeyCode::A) {
|
|
transform.translation -= Vec3::Z * ds;
|
|
}
|
|
if input.pressed(KeyCode::D) {
|
|
transform.translation += Vec3::Z * ds;
|
|
}
|
|
}
|
|
}
|