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, dt: f32, ) -> anyhow::Result<()> { if self.next_event < Instant::now() { self.next_event += Duration::from_secs_f32(30. + random::() * 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(()) } }