outfly/src/visual.rs

210 lines
7 KiB
Rust
Raw Normal View History

2024-04-21 16:23:40 +00:00
// ▄████████▄ + ███ + ▄█████████ ███ +
// ███▀ ▀███ + + ███ ███▀ + ███ + +
// ███ + ███ ███ ███ █████████ ███ ███ ███ ███
2024-04-21 17:34:00 +00:00
// ███ +███ ███ ███ ███ ███▐██████ ███ ███ ███
2024-04-21 16:23:40 +00:00
// ███ + ███ ███+ ███ +███ ███ + ███ ███ + ███
// ███▄ ▄███ ███▄ ███ ███ + ███ + ███ ███▄ ███
// ▀████████▀ + ▀███████ ███▄ ███▄ ▀████ ▀███████
// + + + ███
// + ▀████████████████████████████████████████████████████▀
2024-04-23 15:33:36 +00:00
//
// This module manages visual effects.
2024-04-21 16:23:40 +00:00
2024-05-12 21:42:56 +00:00
use crate::prelude::*;
2024-05-22 02:57:20 +00:00
use bevy::prelude::*;
2024-07-09 01:32:03 +00:00
use std::time::Duration;
2024-03-30 20:14:04 +00:00
2024-05-10 08:37:40 +00:00
pub struct VisualPlugin;
2024-03-30 20:14:04 +00:00
2024-05-10 08:37:40 +00:00
impl Plugin for VisualPlugin {
2024-03-30 20:14:04 +00:00
fn build(&self, app: &mut App) {
2024-05-14 16:11:18 +00:00
app.add_systems(Startup, setup.after(menu::setup).after(hud::setup));
2024-05-22 02:57:20 +00:00
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);
2024-04-10 23:12:07 +00:00
app.add_systems(Update, update_fadein);
app.add_systems(Update, update_fadeout);
2024-05-10 08:25:03 +00:00
app.add_systems(Update, play_animations);
// Blackout disabled for now
//app.add_systems(Update, update_blackout);
2024-03-30 20:14:04 +00:00
app.add_event::<SpawnEffectEvent>();
}
}
#[derive(Clone)]
pub enum Effects {
FadeIn(Color),
2024-04-10 23:12:07 +00:00
FadeOut(Color),
2024-03-30 20:14:04 +00:00
}
// Blackout disabled for now
//#[derive(Component)] pub struct BlackOutOverlay;
2024-05-22 02:57:20 +00:00
#[derive(Component)]
pub struct FadeIn;
#[derive(Component)]
pub struct FadeOut;
2024-03-30 20:14:04 +00:00
#[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,
}
2024-07-09 01:32:03 +00:00
#[derive(Resource)]
pub struct SuitAnimation {
index: AnimationNodeIndex,
graph: Handle<AnimationGraph>
}
pub fn setup(
settings: Res<var::Settings>,
asset_server: Res<AssetServer>,
mut commands: Commands,
mut ew_effect: EventWriter<SpawnEffectEvent>,
mut graphs: ResMut<Assets<AnimationGraph>>,
) {
2024-03-30 20:14:04 +00:00
if !settings.dev_mode {
2024-05-22 02:57:20 +00:00
ew_effect.send(SpawnEffectEvent {
class: Effects::FadeIn(Color::BLACK),
duration: 4.0,
});
2024-03-30 20:14:04 +00:00
}
2024-07-09 01:32:03 +00:00
let mut graph = AnimationGraph::new();
let index = graph.add_clip(
asset_server.load(GltfAssetLabel::Animation(0).from_asset("models/suit_v2/suit_v2.glb")),
1.0,
graph.root,
);
let graph = graphs.add(graph);
commands.insert_resource(SuitAnimation { index, graph });
2024-05-22 02:57:20 +00:00
// Blackout disabled for now
// commands.spawn((
// BlackOutOverlay,
// 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::BLACK.into(),
// ..default()
// },
// ));
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,
},
2024-04-10 23:12:07 +00:00
FadeIn,
2024-03-30 20:14:04 +00:00
NodeBundle {
2024-05-12 22:44:03 +00:00
style: style_fullscreen(),
background_color: color.into(),
2024-03-30 20:14:04 +00:00
..default()
},
));
2024-05-22 02:57:20 +00:00
}
2024-04-10 23:12:07 +00:00
Effects::FadeOut(color) => {
commands.spawn((
Effect {
class: effect.class.clone(),
duration: effect.duration,
start_time: now,
},
FadeOut,
NodeBundle {
2024-05-12 22:44:03 +00:00
style: style_fullscreen(),
background_color: color.with_alpha(0.0).into(),
2024-04-10 23:12:07 +00:00
..default()
},
));
2024-05-22 02:57:20 +00:00
}
2024-03-30 20:14:04 +00:00
}
}
}
2024-04-10 23:12:07 +00:00
pub fn update_fadein(
2024-03-30 20:14:04 +00:00
mut commands: Commands,
2024-04-10 23:12:07 +00:00
mut q_effect: Query<(Entity, &Effect, &mut BackgroundColor), With<FadeIn>>,
2024-03-30 20:14:04 +00:00
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_alpha(alpha as f32);
2024-03-30 20:14:04 +00:00
}
}
2024-04-05 23:11:11 +00:00
2024-04-10 23:12:07 +00:00
pub fn update_fadeout(
mut commands: Commands,
mut q_effect: Query<(Entity, &Effect, &mut BackgroundColor), With<FadeOut>>,
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 = ((now - effect.start_time) / effect.duration).clamp(0.0, 1.0);
bgcolor.0.set_alpha(alpha as f32);
2024-04-10 23:12:07 +00:00
}
}
2024-05-10 08:25:03 +00:00
fn play_animations(
2024-07-09 01:32:03 +00:00
mut commands: Commands,
mut players: Query<(Entity, &mut AnimationPlayer), Added<AnimationPlayer>>,
suit_animation: Res<SuitAnimation>,
2024-05-10 08:25:03 +00:00
) {
2024-07-09 01:32:03 +00:00
for (entity, mut player) in &mut players {
let mut transitions = AnimationTransitions::new();
transitions
.play(&mut player, suit_animation.index, Duration::ZERO)
.repeat();
commands.entity(entity).insert(suit_animation.graph.clone()).insert(transitions);
//player.play(suit_ani_node_index.0).repeat();
2024-05-10 08:25:03 +00:00
}
}
// Blackout disabled for now
//pub fn update_blackout(
// mut q_effect: Query<&mut BackgroundColor, With<BlackOutOverlay>>,
// q_player: Query<&actor::ExperiencesGForce, With<actor::Player>>
//) {
// if let (Ok(gforce), Ok(mut bgcolor)) = (q_player.get_single(), q_effect.get_single_mut()) {
// let threshold = 0.3;
// let factor = 1.0 / (1.0 - threshold);
// let alpha = (factor * (gforce.blackout - threshold)).clamp(0.0, 1.0);
// bgcolor.0.set_alpha(alpha as f32);
// }
//}