122 lines
3.8 KiB
Rust
122 lines
3.8 KiB
Rust
use bevy::prelude::*;
|
|
|
|
use crate::Camera;
|
|
|
|
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(33.0, 1.0, 0.0),
|
|
..Default::default()
|
|
})
|
|
.insert(Player)
|
|
.with_children(|parent| {
|
|
// light
|
|
let mut light_material: StandardMaterial = Color::rgb(0.3, 0.5, 0.3).into();
|
|
light_material.metallic = 0.5;
|
|
light_material.reflectance = 0.5;
|
|
light_material.emissive = Color::rgb(15.0, 15.0, 15.0);
|
|
let light_material = materials.add(light_material);
|
|
|
|
for i in 0..5 {
|
|
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 {
|
|
..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 = 6.0 * (t * 1.1 * i2 + i).sin() as f32 + 7.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;
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn camera_follow_player(
|
|
player: Query<(&Transform, &Player), Without<Camera>>,
|
|
mut camera: Query<(&mut Transform, &Camera), Without<Player>>,
|
|
time: Res<Time>,
|
|
) {
|
|
let player_pos = if let Some(player) = player.iter().next() {
|
|
player.0.translation
|
|
} else {
|
|
return;
|
|
};
|
|
|
|
let ds = time.delta_seconds() * 5.0;
|
|
for (mut trans, _) in camera.iter_mut() {
|
|
trans.look_at(player_pos, Vec3::Y);
|
|
|
|
// keep a distance to the player
|
|
if trans.translation.distance(player_pos) > 170.0 {
|
|
let mut d = trans.rotation * Vec3::Z * ds;
|
|
d.y = 0.0;
|
|
trans.translation -= d;
|
|
}
|
|
if trans.translation.distance(player_pos) < 100.0 {
|
|
let mut d = trans.rotation * Vec3::Z * ds;
|
|
d.y = 0.0;
|
|
trans.translation += d;
|
|
}
|
|
}
|
|
}
|