-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfadeout.rs
More file actions
94 lines (84 loc) · 2.49 KB
/
fadeout.rs
File metadata and controls
94 lines (84 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use bevy::prelude::*;
use crate::{ascii::AsciiSheet, GameState};
pub struct FadeoutPlugin;
#[derive(Component)]
struct ScreenFade {
alpha: f32,
sent: bool,
next_state: Option<GameState>,
timer: Timer,
}
impl Plugin for FadeoutPlugin {
fn build(&self, app: &mut App) {
app.add_system(fadeout).add_system(ui_fadeout);
}
}
fn ui_fadeout(
fade_query: Query<&ScreenFade>,
mut ui_query: Query<&mut UiColor>,
mut text_query: Query<&mut Text>,
) {
if let Some(fade) = fade_query.iter().next() {
for mut ui_color in ui_query.iter_mut() {
ui_color.0.set_a(1.0 - fade.alpha);
}
for mut text in text_query.iter_mut() {
for section in text.sections.iter_mut() {
section.style.color.set_a(1.0 - fade.alpha);
}
}
}
}
fn fadeout(
mut commands: Commands,
mut fade_query: Query<(Entity, &mut ScreenFade, &mut TextureAtlasSprite)>,
mut state: ResMut<State<GameState>>,
time: Res<Time>,
) {
for (entity, mut fade, mut sprite) in fade_query.iter_mut() {
fade.timer.tick(time.delta());
if fade.timer.percent() < 0.5 {
fade.alpha = fade.timer.percent() * 2.0;
} else {
fade.alpha = fade.timer.percent_left() * 2.0;
}
sprite.color.set_a(fade.alpha);
if fade.timer.percent() > 0.5 && !fade.sent {
if let Some(next_state) = fade.next_state {
state.push(next_state).unwrap();
} else {
state.pop().unwrap();
}
fade.sent = true;
}
if fade.timer.just_finished() {
commands.entity(entity).despawn_recursive();
}
}
}
pub fn create_fadeout(
commands: &mut Commands,
next_state: Option<GameState>,
ascii: &Res<AsciiSheet>,
) {
let mut sprite = TextureAtlasSprite::new(0);
sprite.color = Color::rgba(0.1, 0.1, 0.15, 0.0);
sprite.custom_size = Some(Vec2::splat(100000.0));
commands
.spawn_bundle(SpriteSheetBundle {
sprite: sprite,
texture_atlas: ascii.0.clone(),
transform: Transform {
translation: Vec3::new(0.0, 0.0, 999.0),
..Default::default()
},
..Default::default()
})
.insert(ScreenFade {
alpha: 0.0,
sent: false,
next_state: next_state,
timer: Timer::from_seconds(1.0, false),
})
.insert(Name::new("Fadeout"));
}