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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;
use geom::{Distance, Duration, PolyLine, Pt2D, Time};
use widgetry::{Color, Drawable, EventCtx, GeomBatch, GfxCtx, RewriteColor};
pub struct Animator {
active: Vec<Animation>,
draw_mapspace: Drawable,
draw_screenspace: Option<Drawable>,
}
struct Animation {
start: Time,
end: Time,
effect: Effect,
screenspace: bool,
}
pub enum Effect {
Scale {
orig: GeomBatch,
center: Pt2D,
lerp_scale: (f64, f64),
},
FollowPath {
color: Color,
width: Distance,
pl: PolyLine,
},
Flash {
orig: GeomBatch,
alpha_scale: (f32, f32),
cycles: usize,
},
}
impl Animator {
pub fn new(ctx: &EventCtx) -> Animator {
Animator {
active: Vec::new(),
draw_mapspace: Drawable::empty(ctx),
draw_screenspace: None,
}
}
pub fn add(&mut self, now: Time, duration: Duration, effect: Effect) {
self.active.push(Animation {
start: now,
end: now + duration,
effect,
screenspace: false,
});
}
pub fn add_screenspace(&mut self, now: Time, duration: Duration, effect: Effect) {
self.active.push(Animation {
start: now,
end: now + duration,
effect,
screenspace: true,
});
}
pub fn event(&mut self, ctx: &mut EventCtx, now: Time) {
if self.active.is_empty() {
return;
}
let mut mapspace = GeomBatch::new();
let mut screenspace = GeomBatch::new();
self.active.retain(|anim| {
let pct = (now - anim.start) / (anim.end - anim.start);
if pct < 0.0 {
true
} else if pct > 1.0 {
false
} else {
if anim.screenspace {
anim.effect.render(pct, &mut screenspace);
} else {
anim.effect.render(pct, &mut mapspace);
}
true
}
});
self.draw_mapspace = ctx.upload(mapspace);
if screenspace.is_empty() {
self.draw_screenspace = None;
} else {
self.draw_screenspace = Some(ctx.upload(screenspace));
}
}
pub fn draw(&self, g: &mut GfxCtx) {
g.redraw(&self.draw_mapspace);
if let Some(ref d) = self.draw_screenspace {
g.fork_screenspace();
g.redraw(d);
g.unfork();
}
}
pub fn is_done(&self) -> bool {
self.active.is_empty()
}
}
impl Effect {
fn render(&self, pct: f64, batch: &mut GeomBatch) {
match self {
Effect::Scale {
ref orig,
center,
lerp_scale,
} => {
let scale = lerp_scale.0 + pct * (lerp_scale.1 - lerp_scale.0);
batch.append(orig.clone().scale(scale).centered_on(*center));
}
Effect::FollowPath {
color,
width,
ref pl,
} => {
if let Ok(pl) = pl.maybe_exact_slice(Distance::ZERO, pct * pl.length()) {
batch.push(*color, pl.make_polygons(*width));
}
}
Effect::Flash {
ref orig,
alpha_scale,
cycles,
} => {
let shift = (pct * (*cycles as f64) * (2.0 * std::f64::consts::PI)).sin() as f32;
let midpt = (alpha_scale.0 + alpha_scale.1) / 2.0;
let half_range = (alpha_scale.1 - alpha_scale.0) / 2.0;
let alpha = midpt + shift * half_range;
batch.append(orig.clone().color(RewriteColor::ChangeAlpha(alpha)));
}
}
}
}
pub struct SnowEffect {
rng: XorShiftRng,
flakes: Vec<Snowflake>,
draw: Drawable,
}
struct Snowflake {
start: Time,
initial_pos: Pt2D,
fall_speed: f64,
swoop_period: f64,
max_swoop: f64,
}
impl Snowflake {
fn pos(&self, time: Time) -> Pt2D {
let arg =
(2.0 * std::f64::consts::PI) * (time - self.start).inner_seconds() / self.swoop_period;
let x = self.initial_pos.x() + self.max_swoop * arg.cos();
let y = self.initial_pos.y() + self.fall_speed * (time - self.start).inner_seconds();
Pt2D::new(x, y)
}
}
impl SnowEffect {
pub fn new(ctx: &mut EventCtx) -> SnowEffect {
let mut snow = SnowEffect {
rng: XorShiftRng::seed_from_u64(42),
flakes: Vec::new(),
draw: Drawable::empty(ctx),
};
let now = Time::START_OF_DAY;
for _ in 0..20 {
let initial_pos = Pt2D::new(
snow.rng.gen_range(0.0..ctx.canvas.window_width),
snow.rng.gen_range(0.0..ctx.canvas.window_height),
);
let flake = snow.spawn_new(now, initial_pos);
snow.flakes.push(flake);
}
snow.event(ctx, now);
snow
}
fn spawn_new(&mut self, now: Time, initial_pos: Pt2D) -> Snowflake {
Snowflake {
start: now,
initial_pos,
fall_speed: self.rng.gen_range(150.0..300.0),
swoop_period: self.rng.gen_range(1.0..5.0),
max_swoop: self.rng.gen_range(0.0..50.0),
}
}
pub fn event(&mut self, ctx: &mut EventCtx, now: Time) {
let shape = GeomBatch::load_svg(ctx, "system/assets/map/snowflake.svg").scale(0.1);
let mut batch = GeomBatch::new();
let prev_flakes = std::mem::take(&mut self.flakes);
let mut new_flakes = Vec::new();
for flake in prev_flakes {
let pt = flake.pos(now);
if pt.y() > ctx.canvas.window_height {
let initial_pos = Pt2D::new(self.rng.gen_range(0.0..ctx.canvas.window_width), 0.0);
new_flakes.push(self.spawn_new(now, initial_pos));
} else {
batch.append(shape.clone().translate(pt.x(), pt.y()));
new_flakes.push(flake);
}
}
self.flakes = new_flakes;
self.draw = ctx.upload(batch);
}
pub fn draw(&self, g: &mut GfxCtx) {
g.fork_screenspace();
g.redraw(&self.draw);
g.unfork();
}
}