outfly/src/effects.rs

93 lines
2.6 KiB
Rust
Raw Normal View History

2024-03-30 20:14:04 +00:00
use bevy::prelude::*;
use crate::{settings, camera};
2024-03-30 20:14:04 +00:00
pub struct EffectsPlugin;
impl Plugin for EffectsPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup);
app.add_systems(Startup, spawn_effects.after(setup).after(camera::setup_camera));
2024-03-30 20:14:04 +00:00
app.add_systems(Update, spawn_effects);
app.add_systems(Update, update_fadeblack);
app.add_event::<SpawnEffectEvent>();
}
}
#[derive(Clone)]
pub enum Effects {
FadeIn(Color),
2024-03-30 20:14:04 +00:00
}
#[derive(Component)] pub struct FadeBlack;
#[derive(Component)]
pub struct Effect {
pub class: Effects,
pub duration: f64,
pub start_time: f64,
}
#[derive(Event)]
pub struct SpawnEffectEvent {
pub class: Effects,
pub duration: f64,
}
pub fn setup(
settings: Res<settings::Settings>,
mut ew_effect: EventWriter<SpawnEffectEvent>,
) {
if !settings.dev_mode {
ew_effect.send(SpawnEffectEvent { class: Effects::FadeIn(Color::BLACK), duration: 4.0 });
2024-03-30 20:14:04 +00:00
}
}
pub fn spawn_effects(
mut commands: Commands,
mut er_effect: EventReader<SpawnEffectEvent>,
time: Res<Time>,
) {
let now = time.elapsed_seconds_f64();
for effect in er_effect.read() {
match effect.class {
Effects::FadeIn(color) => {
2024-03-30 20:14:04 +00:00
commands.spawn((
Effect {
class: effect.class.clone(),
duration: effect.duration,
start_time: now,
},
FadeBlack,
NodeBundle {
style: Style {
width: Val::Vw(100.0),
height: Val::Vh(100.0),
position_type: PositionType::Absolute,
top: Val::Px(0.0),
left: Val::Px(0.0),
..default()
},
background_color: color.into(),
2024-03-30 20:14:04 +00:00
..default()
},
));
},
//_ => {},
}
}
}
pub fn update_fadeblack(
mut commands: Commands,
mut q_effect: Query<(Entity, &Effect, &mut BackgroundColor), With<FadeBlack>>,
time: Res<Time>,
) {
for (entity, effect, mut bgcolor) in &mut q_effect {
let now = time.elapsed_seconds_f64();
if effect.start_time + effect.duration < now {
commands.entity(entity).despawn();
continue;
}
let alpha = (1.3 - 1.3 * (now - effect.start_time) / effect.duration).clamp(0.0, 1.0);
bgcolor.0.set_a(alpha as f32);
2024-03-30 20:14:04 +00:00
}
}