implement deactivating pillars

main
annieversary 2021-10-16 22:48:37 +02:00
parent 2e8245eb92
commit fb419df631
3 changed files with 47 additions and 6 deletions

View File

@ -44,7 +44,9 @@ pub fn update_raycast_with_cursor(
#[derive(Default)]
pub struct MouseCoords {
/// The mouse coordinates on the floor, as they are
pub raw: Vec3,
/// The mouse coordinates, after being snapped to pillars and stuff
pub processed: Vec3,
}

View File

@ -78,6 +78,7 @@ fn main() {
.with_system(light_up_ball_when_close_to_player.system())
.with_system(increase_progress_when_activating_pillar.system())
.with_system(activate_pillar_when_progress_is_1.system())
.with_system(decrease_pillar_strength.system())
.with_system(toggle_debug.system()),
)
.add_system_set(

View File

@ -1,12 +1,14 @@
use bevy::prelude::*;
use crate::player::LightFriends;
use crate::{camera::MouseCoords, player::LightFriends};
const SIZE: f32 = 10.0;
const HEIGHT: f32 = 20.0;
pub struct UnlitPillar;
pub struct LitPillar;
pub struct LitPillar {
created_at: f64,
}
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();
@ -30,6 +32,7 @@ pub fn increase_progress_when_activating_pillar(
friends: Query<(&Transform, &LightFriends)>,
mut progress: ResMut<PillarActivationProgress>,
input: Res<Input<MouseButton>>,
mouse: Res<MouseCoords>,
) {
if !input.pressed(MouseButton::Left) {
return;
@ -42,8 +45,9 @@ pub fn increase_progress_when_activating_pillar(
};
for (entity, trans, _) in pillars.iter() {
let d = trans.translation.distance(friends_trans.translation);
if d < 5.0 {
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 {
@ -63,6 +67,7 @@ pub fn activate_pillar_when_progress_is_1(
mut progress: ResMut<PillarActivationProgress>,
mut query: Query<(&mut Handle<StandardMaterial>, &UnlitPillar)>,
materials: Res<PillarMaterials>,
time: Res<Time>,
) {
if let Some(p) = progress.0 {
if p.1 >= 1.0 {
@ -72,10 +77,12 @@ pub fn activate_pillar_when_progress_is_1(
commands
.entity(p.0)
.remove::<UnlitPillar>()
.insert(LitPillar)
.insert(LitPillar {
created_at: time.seconds_since_startup(),
})
.insert(Light {
color: Color::rgb(15.0, 15.0, 15.0),
intensity: 300.0,
intensity: 500.0,
..Default::default()
});
@ -86,6 +93,37 @@ pub fn activate_pillar_when_progress_is_1(
}
}
pub fn decrease_pillar_strength(
mut commands: Commands,
materials: Res<PillarMaterials>,
mut query: Query<(
Entity,
&mut Light,
&mut Handle<StandardMaterial>,
&LitPillar,
)>,
time: Res<Time>,
) {
let t = time.seconds_since_startup();
for (entity, mut light, mut handle, pillar) in query.iter_mut() {
let d = t - pillar.created_at;
if d > 30.0 {
// deactivate pillar
commands
.entity(entity)
.remove::<LitPillar>()
.remove::<Light>()
.insert(UnlitPillar);
*handle = materials.unlit.clone();
} else {
let i = (30.0 - d as f32) / 30.0;
light.intensity = i * 500.0;
}
}
}
pub struct PillarMaterials {
unlit: Handle<StandardMaterial>,
lit: Handle<StandardMaterial>,