moria/src/player.rs

93 lines
3.0 KiB
Rust

use bevy::prelude::*;
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)
.with_children(|parent| {
// 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);
for i in 0..6 {
parent
.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 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.y = 3.0 * (t * 1.1 * i2 + i).sin() as f32 + 4.0;
trans.translation.x = 5.0 * i2 as f32 * (t * 0.4 * i2 + i).cos() as f32;
trans.translation.z = 5.0 * (t * 0.4 * i + i).sin() as f32;
}
}
pub fn move_player(
mut query: Query<(&mut Transform, &Player)>,
input: Res<Input<KeyCode>>,
time: Res<Time>,
) {
let ds = time.delta_seconds() * 5.0;
// TODO rotate according to camera position
// but make it snap to coord system
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;
}
}
}