moria/src/player.rs

139 lines
4.2 KiB
Rust

use bevy::prelude::*;
use crate::camera::MouseCoords;
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
let mut light_material: StandardMaterial = Color::rgb(0.737255, 0.560784, 0.560784).into();
light_material.metallic = 0.5;
light_material.reflectance = 0.5;
light_material.emissive = Color::rgb(7.52, 5.72, 5.72);
let light_material = materials.add(light_material);
commands
.spawn()
.insert(LightFriends)
.insert(Transform::default())
.insert(GlobalTransform::default())
.with_children(|children| {
for i in 0..6 {
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::rgb(7.52, 5.72, 5.72),
..Default::default()
},
..Default::default()
})
.insert(PlayerLight { i: i as u8 + 1 });
}
});
}
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>) {
let t = time.seconds_since_startup();
for (mut trans, light) in query.iter_mut() {
let i = light.i as f64;
let i2 = i / 2.0;
trans.translation = Vec3::new(
5.0 * i2 as f32 * (t * 0.4 * i2 + i).cos() as f32,
3.0 * (t * 1.1 * i2 + i).sin() as f32 + 4.0,
5.0 * (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;
}
}
}