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
95
use amethyst::{
core::Time,
ecs::{
Entity,
Entities,
Join,
Read,
ReadExpect,
SystemData,
world::EntitiesRes,
World,
WorldExt,
WriteStorage,
},
ui::UiTransform,
};
use crate::{
config::GameConfig,
events::fade_out_event::Fade,
};
use super::{BoxedGameEvent, ExecutionConditions, GameEvent};
#[derive(Clone, Default)]
pub struct FadeInEvent {
top_fade: Option<Entity>,
bottom_fade: Option<Entity>,
elapsed_time: f32,
completed: bool,
}
impl GameEvent for FadeInEvent {
fn boxed_clone(&self) -> BoxedGameEvent {
Box::new(self.clone())
}
fn get_execution_conditions(&self) -> ExecutionConditions {
ExecutionConditions {
requires_disabled_input: true,
}
}
fn start(&mut self, world: &mut World) {
let entities = world.read_resource::<EntitiesRes>();
let fades = world.read_component::<Fade>();
for (entity, fade) in (&entities, &fades).join() {
match fade.id {
0 => self.top_fade = Some(entity),
1 => self.bottom_fade = Some(entity),
_ => panic!("Invalid Fade ID"),
}
}
}
fn tick(&mut self, world: &mut World, _disabled_inputs: bool) {
let (mut ui_transforms, entities, game_config, time) = <(
WriteStorage<UiTransform>,
Entities,
ReadExpect<GameConfig>,
Read<Time>,
)>::fetch(world);
let fade_duration = game_config.fade_duration;
let top_fade = self.top_fade.as_mut().unwrap();
let bottom_fade = self.bottom_fade.as_mut().unwrap();
self.elapsed_time += time.delta_seconds();
ui_transforms
.get_mut(*top_fade)
.expect("Failed to retrieve UiTransform")
.height = 300. * (1. - self.elapsed_time / fade_duration);
ui_transforms
.get_mut(*bottom_fade)
.expect("Failed to retrieve UiTransform")
.height = 300. * (1. - self.elapsed_time / fade_duration);
if self.elapsed_time >= fade_duration {
entities.delete(*top_fade).expect("Failed to delete top fade");
entities.delete(*bottom_fade).expect("Failed to delete bottom fade");
self.completed = true;
}
}
fn is_complete(&self, _world: &mut World) -> bool {
self.completed
}
}