aboutsummaryrefslogtreecommitdiff
path: root/server/src/entity/weather.rs
blob: 79ac364f16ce79d8811cf20397a6ef20d1874664 (plain)
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
use super::EntityT;
use crate::{game::Game, InterpolateExt};
use hurrycurry_protocol::PacketC;
use rand::{random, seq::IndexedRandom, thread_rng};
use std::{
    collections::VecDeque,
    time::{Duration, Instant},
};

#[derive(Clone, Debug)]
pub struct WeatherController {
    next_event: Instant,
    event: usize,
}

impl Default for WeatherController {
    fn default() -> Self {
        Self {
            next_event: Instant::now() + Duration::from_secs(10),
            event: Default::default(),
        }
    }
}
impl EntityT for WeatherController {
    fn tick(
        &mut self,
        game: &mut Game,
        _packet_out: &mut VecDeque<PacketC>,
        dt: f32,
    ) -> anyhow::Result<()> {
        if self.next_event < Instant::now() {
            self.next_event += Duration::from_secs_f32(30. + random::<f32>() * 40.);
            if self.event > 0 {
                self.event = 0;
            } else {
                self.event = *[0, 0, 1, 2].choose(&mut thread_rng()).unwrap();
            }
        }

        let (rain, wind) = match self.event {
            0 => (0., 0.),
            1 => (0.7, 0.),
            2 => (0., 0.7),
            _ => (1., 1.),
        };
        game.environment.wind.exp_to(wind, dt * 0.05);
        game.environment.rain.exp_to(rain, dt * 0.15);
        eprintln!("{:#?}", game.environment);
        Ok(())
    }
}