moria/src/pillar.rs

187 lines
5.6 KiB
Rust

use bevy::{
pbr::CubemapVisibleEntities,
prelude::*,
render::{primitives::CubemapFrusta, view::NoFrustumCulling},
};
use crate::{camera::MouseCoords, player::LightFriends};
// TODO maybe change these per pillar
const SIZE: f32 = 10.0;
const HEIGHT: f32 = 20.0;
/// how many seconds a pillar lasts activated
const PILLAR_DURATION: f32 = 30.0;
/// the range for the pillar
const MAX_RANGE: f32 = 40.0;
const INTENSITY: f32 = 2000.0;
#[derive(Component)]
pub struct UnlitPillar;
#[derive(Component)]
pub struct LitPillar {
created_at: f64,
}
pub fn spawn_pillar(
commands: &mut Commands,
pillar_mats: &PillarMaterials,
materials: &mut Assets<StandardMaterial>,
pos: Vec3,
) {
let mut unlit: StandardMaterial = Color::rgb(0.9, 0.9, 0.9).into();
unlit.metallic = 0.0;
unlit.reflectance = 0.0;
let unlit = materials.add(unlit);
commands
.spawn_bundle(PbrBundle {
mesh: pillar_mats.mesh.clone(),
material: unlit,
transform: Transform::from_translation(pos),
..Default::default()
})
// idk what these do
.insert(CubemapFrusta::default())
.insert(CubemapVisibleEntities::default())
// we want the light to keep working even if it's out of the camera
.insert(NoFrustumCulling)
.insert(UnlitPillar);
}
#[derive(Default)]
pub struct PillarActivationProgress(pub Option<(Entity, f32)>);
pub fn increase_progress_when_activating_pillar(
pillars: Query<(Entity, &Transform, &UnlitPillar)>,
friends: Query<(&Transform, &LightFriends)>,
mut progress: ResMut<PillarActivationProgress>,
input: Res<Input<MouseButton>>,
mouse: Res<MouseCoords>,
) {
if !input.pressed(MouseButton::Left) {
return;
}
let (friends_trans, _) = if let Some(f) = friends.iter().next() {
f
} else {
return;
};
for (entity, trans, _) in pillars.iter() {
let friends_d = trans.translation.distance(friends_trans.translation);
let mouse_d = trans.translation.distance(mouse.processed);
if friends_d < 10.0 && mouse_d < 1.0 {
// set as the one on progress and increase
if let Some(p) = &mut progress.0 {
if p.0 == entity {
p.1 = (p.1 + 0.008).clamp(0.0, 1.0);
} else {
*p = (entity, 0.0);
}
} else {
progress.0 = Some((entity, 0.0));
}
}
}
}
pub fn activate_pillar_when_progress_is_1(
mut commands: Commands,
mut progress_res: ResMut<PillarActivationProgress>,
query: Query<(&Handle<StandardMaterial>, &UnlitPillar)>,
mut materials: ResMut<Assets<StandardMaterial>>,
time: Res<Time>,
) {
if let Some((entity, progress)) = progress_res.0 {
if progress >= 1.0 {
progress_res.0 = None;
// activate pillar
commands
.entity(entity)
.remove::<UnlitPillar>()
.insert(LitPillar {
created_at: time.seconds_since_startup(),
})
.insert(PointLight {
color: Color::rgb(15.0, 15.0, 15.0),
range: 1.0,
intensity: INTENSITY,
..Default::default()
});
if let Ok((handle, _)) = query.get(entity) {
if let Some(mat) = materials.get_mut(handle) {
mat.base_color = Color::rgb(15.0, 15.0, 15.0);
mat.emissive = Color::rgb(15.0, 15.0, 15.0);
}
}
} else if let Ok((handle, _)) = query.get(entity) {
if let Some(mat) = materials.get_mut(handle) {
mat.base_color = Color::rgb(15.0, 15.0, 15.0) * progress;
mat.emissive = Color::rgb(15.0, 15.0, 15.0) * progress;
}
}
}
}
pub fn increase_range_up_to_max(mut query: Query<&mut PointLight, With<LitPillar>>) {
for mut light in &mut query {
light.range += (MAX_RANGE - light.range) / MAX_RANGE;
}
}
pub fn decrease_pillar_strength(
mut commands: Commands,
mut query: Query<(
Entity,
&mut PointLight,
&Handle<StandardMaterial>,
&LitPillar,
)>,
mut materials: ResMut<Assets<StandardMaterial>>,
time: Res<Time>,
) {
let t = time.seconds_since_startup();
for (entity, mut light, handle, pillar) in query.iter_mut() {
let d = t - pillar.created_at;
let i = (PILLAR_DURATION - d as f32) / PILLAR_DURATION;
if i < 0.0 {
// deactivate pillar
commands
.entity(entity)
.remove::<LitPillar>()
.remove::<PointLight>()
.insert(UnlitPillar);
if let Some(mat) = materials.get_mut(handle) {
mat.base_color = Color::rgb(0.9, 0.9, 0.9);
mat.emissive = Color::rgb(0., 0., 0.);
}
} else {
light.intensity = i * INTENSITY;
if let Some(mat) = materials.get_mut(handle) {
mat.base_color = Color::rgb(15.0, 15.0, 15.0) * i.powf(1.3);
mat.emissive = Color::rgb(15.0, 15.0, 15.0) * i.powf(1.3);
}
}
}
}
pub struct PillarMaterials {
mesh: Handle<Mesh>,
}
impl FromWorld for PillarMaterials {
fn from_world(world: &mut World) -> Self {
let world = world.cell();
let mut meshes = world.get_resource_mut::<Assets<Mesh>>().unwrap();
let mesh = meshes.add(Mesh::from(shape::Box::new(SIZE, HEIGHT, SIZE)));
Self { mesh }
}
}