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
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;

use geom::{Circle, Distance, Duration, Pt2D, Time};
use widgetry::{Color, Drawable, EventCtx, GeomBatch, GfxCtx};

pub struct Animator {
    time: Time,
    active: Vec<Effect>,
    draw_current: Drawable,
}

struct Effect {
    start: Time,
    end: Time,
    orig: GeomBatch,
    center: Pt2D,
    // Scaling is the only transformation for now
    lerp_scale: (f64, f64),
}

impl Animator {
    pub fn new(ctx: &EventCtx) -> Animator {
        Animator {
            time: Time::START_OF_DAY,
            active: Vec::new(),
            draw_current: Drawable::empty(ctx),
        }
    }

    pub fn add(
        &mut self,
        duration: Duration,
        lerp_scale: (f64, f64),
        center: Pt2D,
        orig: GeomBatch,
    ) {
        self.active.push(Effect {
            start: self.time,
            end: self.time + duration,
            orig,
            lerp_scale,
            center,
        });
    }

    pub fn event(&mut self, ctx: &mut EventCtx) {
        if let Some(dt) = ctx.input.nonblocking_is_update_event() {
            self.time += dt;
            if self.active.is_empty() {
                return;
            }
            let mut batch = GeomBatch::new();
            let time = self.time;
            self.active.retain(|effect| effect.update(time, &mut batch));
            self.draw_current = ctx.upload(batch);
        }
    }

    pub fn draw(&self, g: &mut GfxCtx) {
        g.redraw(&self.draw_current);
    }
}

impl Effect {
    fn update(&self, time: Time, batch: &mut GeomBatch) -> bool {
        let pct = (time - self.start) / (self.end - self.start);
        if pct > 1.0 {
            return false;
        }
        let scale = self.lerp_scale.0 + pct * (self.lerp_scale.1 - self.lerp_scale.0);
        batch.append(self.orig.clone().scale(scale).centered_on(self.center));
        true
    }
}

pub struct SnowEffect {
    time: Time,
    rng: XorShiftRng,
    flakes: Vec<Snowflake>,

    draw: Drawable,
}

struct Snowflake {
    start: Time,
    top_left: Pt2D,
}

impl SnowEffect {
    pub fn new(ctx: &mut EventCtx) -> SnowEffect {
        let mut snow = SnowEffect {
            time: Time::START_OF_DAY,
            rng: XorShiftRng::seed_from_u64(42),
            flakes: Vec::new(),
            draw: Drawable::empty(ctx),
        };

        for _ in 0..60 {
            snow.spawn_new(ctx);
        }
        snow.update(ctx);

        snow
    }

    fn spawn_new(&mut self, ctx: &EventCtx) {
        let top_left = Pt2D::new(
            self.rng.gen_range(0.0, ctx.canvas.window_width),
            self.rng.gen_range(0.0, ctx.canvas.window_height),
        );
        self.flakes.push(Snowflake {
            start: self.time,
            top_left,
        });
    }

    fn update(&mut self, ctx: &mut EventCtx) {
        let shape = Circle::new(Pt2D::new(0.0, 0.0), Distance::meters(50.0)).to_polygon();

        let lifetime = Duration::seconds(5.0);
        let lerp_scale = (1.0, 0.1);

        let mut batch = GeomBatch::new();
        let time = self.time;
        self.flakes.retain(|flake| {
            let pct = (time - flake.start) / lifetime;
            if pct > 1.0 {
                false
            } else {
                let scale = lerp_scale.0 + pct * (lerp_scale.1 - lerp_scale.0);
                batch.push(
                    Color::WHITE.alpha(0.5),
                    shape
                        .scale(scale)
                        .translate(flake.top_left.x(), flake.top_left.y()),
                );
                true
            }
        });
        self.draw = ctx.upload(batch);
    }

    pub fn event(&mut self, ctx: &mut EventCtx) {
        if let Some(dt) = ctx.input.nonblocking_is_update_event() {
            self.time += dt;
            self.update(ctx);
        }
    }

    pub fn draw(&self, g: &mut GfxCtx) {
        g.fork_screenspace();
        g.redraw(&self.draw);
        g.unfork();
    }
}