moria/src/pillar.rs

118 lines
3.4 KiB
Rust

use bevy::prelude::*;
use crate::player::LightFriends;
const SIZE: f32 = 10.0;
const HEIGHT: f32 = 20.0;
pub struct UnlitPillar;
pub struct LitPillar;
pub fn spawn_pillar(commands: &mut Commands, materials: &PillarMaterials, pos: Vec3) {
let mut column_material: StandardMaterial = Color::rgb(0.7, 0.7, 0.7).into();
column_material.metallic = 0.0;
column_material.reflectance = 0.0;
commands
.spawn_bundle(PbrBundle {
mesh: materials.mesh.clone(),
material: materials.unlit.clone(),
transform: Transform::from_translation(pos),
..Default::default()
})
.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>>,
) {
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 d = trans.translation.distance(friends_trans.translation);
if d < 5.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: ResMut<PillarActivationProgress>,
mut query: Query<(&mut Handle<StandardMaterial>, &UnlitPillar)>,
materials: Res<PillarMaterials>,
) {
if let Some(p) = progress.0 {
if p.1 >= 1.0 {
progress.0 = None;
// activate pillar
commands
.entity(p.0)
.remove::<UnlitPillar>()
.insert(LitPillar)
.insert(Light {
color: Color::rgb(15.0, 15.0, 15.0),
intensity: 300.0,
..Default::default()
});
if let Ok((mut handle, _)) = query.get_mut(p.0) {
*handle = materials.lit.clone();
}
}
}
}
pub struct PillarMaterials {
unlit: Handle<StandardMaterial>,
lit: Handle<StandardMaterial>,
mesh: Handle<Mesh>,
}
impl FromWorld for PillarMaterials {
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::Box::new(SIZE, HEIGHT, SIZE)));
Self { lit, unlit, mesh }
}
}