moria/src/player.rs

246 lines
7.6 KiB
Rust

use bevy::{prelude::*, render::view::NoFrustumCulling};
use crate::{
camera::MouseCoords, illumination::Illumination, pillar::PillarActivationProgress,
LIGHT_FRIEND_COLOR_INDICES, PALETTE_HEX,
};
#[derive(Component)]
pub struct Player;
#[derive(Component)]
pub struct PlayerLight {
i: u8,
}
pub struct PlayerAnimations {
idle: Handle<AnimationClip>,
run: Handle<AnimationClip>,
}
pub fn spawn_player(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// Insert a resource with the current scene information
commands.insert_resource(PlayerAnimations {
idle: asset_server.load("models/scout_girl.glb#Animation0"),
run: asset_server.load("models/scout_girl.glb#Animation1"),
});
commands
.spawn_bundle(SpatialBundle {
transform: Transform::from_xyz(0.0, 1.0, 0.0),
..Default::default()
})
.insert(Player)
.add_children(|parent| {
parent.spawn_bundle(SceneBundle {
scene: asset_server.load("models/scout_girl.glb#Scene0"),
..default()
});
});
// light
commands
.spawn_bundle(SpatialBundle::default())
.insert(LightFriends)
.insert(NoFrustumCulling)
.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 color = PALETTE_HEX[LIGHT_FRIEND_COLOR_INDICES[i]];
let color = Color::hex(color).unwrap();
// let c = color.as_rgba_f32();
// println!(
// "\x1b[38;2;{};{};{}2m {} {} {} \x1b[m",
// (c[0] * 255.0) as u8,
// (c[1] * 255.0) as u8,
// (c[2] * 255.0) as u8,
// (c[0] * 255.0) as u8,
// (c[1] * 255.0) as u8,
// (c[2] * 255.0) as u8,
// );
let mut light_material: StandardMaterial = color.into();
light_material.metallic = 0.0;
light_material.reflectance = 0.0;
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(PointLightBundle {
transform: Transform::from_xyz(0.0, 0.0, 0.0),
point_light: PointLight {
color: color.as_rgba() * 10.0,
..Default::default()
},
..Default::default()
})
.insert(NoFrustumCulling)
.insert(PlayerLight { i: i as u8 + 1 })
.insert(Illumination { radius: 13.0 });
}
});
}
pub fn setup_scene_once_loaded(
animations: Res<PlayerAnimations>,
mut player: Query<&mut AnimationPlayer>,
mut done: Local<bool>,
) {
if !*done {
if let Ok(mut player) = player.get_single_mut() {
player.play(animations.idle.clone_weak()).repeat();
*done = true;
}
}
}
#[derive(Component)]
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.processed + (1.0 - i) * player_pos;
let mut d = trans.translation - center;
d.y = 0.0;
trans.translation -= d * time.delta_seconds();
}
}
#[derive(Default)]
pub struct LightExcitationState(f32);
pub fn update_light_excitation_state(
pillar_progress: Res<PillarActivationProgress>,
mut excitation: ResMut<LightExcitationState>,
) {
// light pillar activation progress
let p = pillar_progress.0.map(|(_, p)| p).unwrap_or(0.0);
if p > excitation.0 {
excitation.0 = (excitation.0 + 0.008).clamp(0.0, 1.0);
} else {
excitation.0 = (excitation.0 - 0.008).clamp(0.0, 1.0);
}
}
pub fn light_movement(
mut query: Query<(&mut Transform, &PlayerLight)>,
time: Res<Time>,
interpolation: Res<FloatingOrbsInterpolationState>,
excitation: Res<LightExcitationState>,
) {
let t = time.seconds_since_startup();
let p = excitation.0;
// make the friends go further when the button is pressed, but close in when activating a pillar
let width = (5.0 + interpolation.0 * 7.0) * (1.0 - p);
let p = 1.0 + 0.8 * p as f64;
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 * p).cos() as f32,
3.0 * (t * 1.1 * i2 + i * p).sin() as f32 + 4.0,
width * (t * 0.4 * i + i * p).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>,
animations: Res<PlayerAnimations>,
mut animation_player: Query<&mut AnimationPlayer>,
mut is_walking: Local<bool>,
) {
let ds = time.delta_seconds() * 7.0;
let mut anim = if let Ok(player) = animation_player.get_single_mut() {
player
} else {
return;
};
for (mut transform, _) in query.iter_mut() {
let mut delta = Vec3::ZERO;
if input.pressed(KeyCode::W) {
delta += Vec3::X;
}
if input.pressed(KeyCode::S) {
delta -= Vec3::X;
}
if input.pressed(KeyCode::A) {
delta -= Vec3::Z;
}
if input.pressed(KeyCode::D) {
delta += Vec3::Z;
}
if !*is_walking && delta.length_squared() > 0.2 {
anim.play(animations.run.clone_weak()).repeat();
anim.set_speed(0.7);
*is_walking = true;
}
if *is_walking && delta.length_squared() < 0.2 {
anim.play(animations.idle.clone_weak()).repeat();
anim.set_speed(1.0);
*is_walking = false;
}
if delta.length_squared() > 0.2 {
let mut ct = *transform;
ct.look_at(transform.translation - delta, Vec3::Y);
let rot = transform.rotation.slerp(ct.rotation, ds * 2.0);
transform.rotation = rot;
}
transform.translation += delta * ds;
}
}