add a cozy fade-in from black on start

This commit is contained in:
yuni 2024-03-30 21:14:04 +01:00
parent 346239ae6d
commit 97640c383c
3 changed files with 102 additions and 0 deletions

92
src/effects.rs Normal file
View file

@ -0,0 +1,92 @@
use bevy::prelude::*;
use crate::settings;
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));
app.add_systems(Update, spawn_effects);
app.add_systems(Update, update_fadeblack);
app.add_event::<SpawnEffectEvent>();
}
}
#[derive(Clone)]
pub enum Effects {
FadeIn,
}
#[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, duration: 4.0 });
}
}
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 => {
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::rgba(0.0, 0.0, 0.0, 1.0).into(),
..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 = Color::rgba(0.0, 0.0, 0.0, alpha as f32);
}
}

View file

@ -4,6 +4,7 @@ mod world;
mod settings;
mod hud;
mod actor;
mod effects;
#[allow(dead_code)]
mod nature;
@ -53,6 +54,7 @@ impl Plugin for OutFlyPlugin {
hud::HudPlugin,
actor::ActorPlugin,
audio::AudioPlugin,
effects::EffectsPlugin,
));
}
}

View file

@ -3,6 +3,7 @@ use std::env;
#[derive(Resource)]
pub struct Settings {
pub dev_mode: bool,
pub mute_sfx: bool,
pub mute_music: bool,
pub volume_sfx: u8,
@ -48,17 +49,24 @@ pub struct Settings {
impl Default for Settings {
fn default() -> Self {
let dev_mode;
let default_mute_sfx = false;
let default_mute_music;
if let Ok(_) = env::var("CARGO") {
// Mute audio by default when run through `cargo`
default_mute_music = cfg!(debug_assertions);
// Enable dev mode when running `cargo run` without `--release`
dev_mode = cfg!(debug_assertions);
}
else {
default_mute_music = false;
dev_mode = false;
}
Settings {
dev_mode: dev_mode,
mute_sfx: default_mute_sfx,
mute_music: default_mute_music,
volume_sfx: 100,