moria/src/pillar.rs

187 lines
5.6 KiB
Rust
Raw Normal View History

2022-08-13 11:34:51 +00:00
use bevy::{
pbr::CubemapVisibleEntities,
prelude::*,
render::{primitives::CubemapFrusta, view::NoFrustumCulling},
};
2021-10-16 20:31:32 +00:00
2021-10-16 20:48:37 +00:00
use crate::{camera::MouseCoords, player::LightFriends};
2021-10-16 20:31:32 +00:00
2022-08-13 11:34:51 +00:00
// TODO maybe change these per pillar
2021-10-16 20:31:32 +00:00
const SIZE: f32 = 10.0;
const HEIGHT: f32 = 20.0;
2022-08-13 11:34:51 +00:00
/// how many seconds a pillar lasts activated
2022-07-07 09:18:41 +00:00
const PILLAR_DURATION: f32 = 30.0;
2022-08-13 11:34:51 +00:00
/// the range for the pillar
const MAX_RANGE: f32 = 40.0;
const INTENSITY: f32 = 2000.0;
2021-10-16 20:31:32 +00:00
2022-07-06 23:12:08 +00:00
#[derive(Component)]
2021-10-16 20:31:32 +00:00
pub struct UnlitPillar;
2022-07-06 23:12:08 +00:00
#[derive(Component)]
2021-10-16 20:48:37 +00:00
pub struct LitPillar {
created_at: f64,
}
2021-10-16 20:31:32 +00:00
2022-07-06 23:12:08 +00:00
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);
2021-10-16 20:31:32 +00:00
commands
.spawn_bundle(PbrBundle {
2022-07-06 23:12:08 +00:00
mesh: pillar_mats.mesh.clone(),
material: unlit,
2021-10-16 20:31:32 +00:00
transform: Transform::from_translation(pos),
..Default::default()
})
2022-08-13 11:34:51 +00:00
// idk what these do
2022-07-07 09:18:41 +00:00
.insert(CubemapFrusta::default())
.insert(CubemapVisibleEntities::default())
2022-08-13 11:34:51 +00:00
// we want the light to keep working even if it's out of the camera
.insert(NoFrustumCulling)
2021-10-16 20:31:32 +00:00
.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>>,
2021-10-16 20:48:37 +00:00
mouse: Res<MouseCoords>,
2021-10-16 20:31:32 +00:00
) {
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() {
2021-10-16 20:48:37 +00:00
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 {
2021-10-16 20:31:32 +00:00
// 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,
2022-07-06 23:12:08 +00:00
mut progress_res: ResMut<PillarActivationProgress>,
query: Query<(&Handle<StandardMaterial>, &UnlitPillar)>,
mut materials: ResMut<Assets<StandardMaterial>>,
2021-10-16 20:48:37 +00:00
time: Res<Time>,
2021-10-16 20:31:32 +00:00
) {
2022-07-06 23:12:08 +00:00
if let Some((entity, progress)) = progress_res.0 {
if progress >= 1.0 {
progress_res.0 = None;
2021-10-16 20:31:32 +00:00
// activate pillar
commands
2022-07-06 23:12:08 +00:00
.entity(entity)
2021-10-16 20:31:32 +00:00
.remove::<UnlitPillar>()
2021-10-16 20:48:37 +00:00
.insert(LitPillar {
created_at: time.seconds_since_startup(),
})
2022-07-06 23:12:08 +00:00
.insert(PointLight {
2021-10-16 20:31:32 +00:00
color: Color::rgb(15.0, 15.0, 15.0),
2022-08-13 11:34:51 +00:00
range: 1.0,
intensity: INTENSITY,
2021-10-16 20:31:32 +00:00
..Default::default()
});
2022-07-06 23:12:08 +00:00
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);
}
}
2022-08-13 11:34:51 +00:00
} 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;
2021-10-16 20:31:32 +00:00
}
}
}
}
2022-08-13 11:34:51 +00:00
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;
}
}
2021-10-16 20:48:37 +00:00
pub fn decrease_pillar_strength(
mut commands: Commands,
mut query: Query<(
Entity,
2022-07-06 23:12:08 +00:00
&mut PointLight,
&Handle<StandardMaterial>,
2021-10-16 20:48:37 +00:00
&LitPillar,
)>,
2022-07-06 23:12:08 +00:00
mut materials: ResMut<Assets<StandardMaterial>>,
2021-10-16 20:48:37 +00:00
time: Res<Time>,
) {
let t = time.seconds_since_startup();
2022-07-06 23:12:08 +00:00
for (entity, mut light, handle, pillar) in query.iter_mut() {
2021-10-16 20:48:37 +00:00
let d = t - pillar.created_at;
2022-07-07 09:18:41 +00:00
let i = (PILLAR_DURATION - d as f32) / PILLAR_DURATION;
2021-10-16 20:48:37 +00:00
2022-07-06 23:12:08 +00:00
if i < 0.0 {
2021-10-16 20:48:37 +00:00
// deactivate pillar
commands
.entity(entity)
.remove::<LitPillar>()
2022-07-06 23:12:08 +00:00
.remove::<PointLight>()
2021-10-16 20:48:37 +00:00
.insert(UnlitPillar);
2022-07-06 23:12:08 +00:00
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.);
}
2021-10-16 20:48:37 +00:00
} else {
2022-08-13 11:34:51 +00:00
light.intensity = i * INTENSITY;
2022-07-06 23:12:08 +00:00
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);
}
2021-10-16 20:48:37 +00:00
}
}
}
2021-10-16 20:31:32 +00:00
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)));
2022-07-06 23:12:08 +00:00
Self { mesh }
2021-10-16 20:31:32 +00:00
}
}