moria/src/light_balls.rs

117 lines
3.5 KiB
Rust

use bevy::{pbr::CubemapVisibleEntities, prelude::*, render::primitives::CubemapFrusta};
use crate::{illumination::Illumination, player::Player};
const RANGE: f32 = 25.0;
#[derive(Component)]
pub struct LightBall;
pub fn light_up_ball_when_close_to_player(
mut commands: Commands,
player: Query<(&Transform, &Player), Without<LightBall>>,
mut thingies: Query<(
Entity,
&Transform,
&LightBall,
&mut Handle<StandardMaterial>,
Option<&mut PointLight>,
Option<&mut Illumination>,
)>,
materials: Res<LightBallMaterials>,
) {
let player_pos = if let Some(player) = player.iter().next() {
player.0.translation
} else {
return;
};
for (entity, trans, _, mut material, light, illumination) in thingies.iter_mut() {
let dis = trans.translation.distance(player_pos);
if dis < RANGE {
*material = materials.lit.clone();
// change intensity,
// add light if there isn't one
if let Some(mut l) = light {
l.intensity = 300.0 * (RANGE - dis) / RANGE;
} else {
commands.entity(entity).insert(PointLight {
color: Color::rgb(15.0, 15.0, 15.0),
intensity: 300.0 * (RANGE - dis) / RANGE,
..Default::default()
});
}
// same with illumination
if let Some(mut l) = illumination {
l.radius = 15.0 * ((RANGE - dis) / RANGE).sqrt();
} else {
commands.entity(entity).insert(Illumination {
radius: 15.0 * ((RANGE - dis) / RANGE).sqrt().sqrt(),
});
}
} else {
*material = materials.unlit.clone();
// remove light if there is one
if light.is_some() {
commands.entity(entity).remove::<PointLight>();
}
if illumination.is_some() {
commands.entity(entity).remove::<Illumination>();
}
}
}
}
pub fn spawn_light_ball(
commands: &mut Commands,
materials: &LightBallMaterials,
translation: Vec3,
) {
commands
.spawn_bundle(PbrBundle {
mesh: materials.mesh.clone(),
material: materials.unlit.clone(),
transform: Transform::from_translation(translation),
..Default::default()
})
.insert(CubemapFrusta::default())
.insert(CubemapVisibleEntities::default())
.insert(LightBall);
}
pub struct LightBallMaterials {
unlit: Handle<StandardMaterial>,
lit: Handle<StandardMaterial>,
mesh: Handle<Mesh>,
}
impl FromWorld for LightBallMaterials {
fn from_world(world: &mut World) -> Self {
let world = world.cell();
let mut materials = world
.get_resource_mut::<Assets<StandardMaterial>>()
.unwrap();
let mut meshes = world.get_resource_mut::<Assets<Mesh>>().unwrap();
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);
let mut lit: StandardMaterial = Color::rgb(15.0, 15.0, 15.0).into();
lit.metallic = 0.5;
lit.reflectance = 0.5;
lit.emissive = Color::rgb(15.0, 15.0, 15.0);
let lit = materials.add(lit);
let mesh = meshes.add(Mesh::from(shape::Icosphere {
radius: 2.0,
subdivisions: 5,
}));
Self { lit, unlit, mesh }
}
}