veloren/common/src/weather.rs

54 lines
1.5 KiB
Rust
Raw Normal View History

2022-02-20 16:04:58 +00:00
use std::fmt;
2021-11-17 17:09:51 +00:00
use serde::{Deserialize, Serialize};
use vek::Vec2;
2022-02-20 16:04:05 +00:00
pub const CHUNKS_PER_CELL: u32 = 16;
2021-11-17 17:09:51 +00:00
// Weather::default is Clear, 0 degrees C and no wind
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub struct Weather {
2022-02-20 16:04:58 +00:00
/// Clouds currently in the area between 0 and 1
2021-11-17 17:09:51 +00:00
pub cloud: f32,
2022-02-20 16:04:58 +00:00
/// Rain per time, between 0 and 1
2021-11-17 17:09:51 +00:00
pub rain: f32,
2022-02-20 16:04:58 +00:00
// Wind direction in block / second
2021-11-17 17:09:51 +00:00
pub wind: Vec2<f32>,
}
impl Weather {
pub fn new(cloud: f32, rain: f32, wind: Vec2<f32>) -> Self { Self { cloud, rain, wind } }
pub fn get_kind(&self) -> WeatherKind {
match (
(self.cloud * 10.0) as i32,
(self.rain * 10.0) as i32,
(self.wind.magnitude() * 10.0) as i32,
) {
2022-02-20 16:04:05 +00:00
// Over 24.5 m/s wind is a storm
(_, _, 245..) => WeatherKind::Storm,
2021-11-17 17:09:51 +00:00
(_, 1..=10, _) => WeatherKind::Rain,
(4..=10, _, _) => WeatherKind::Cloudy,
_ => WeatherKind::Clear,
}
}
}
2022-02-20 16:04:05 +00:00
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
2021-11-17 17:09:51 +00:00
pub enum WeatherKind {
Clear,
Cloudy,
Rain,
Storm,
}
2022-02-20 16:04:58 +00:00
impl fmt::Display for WeatherKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
WeatherKind::Clear => write!(f, "Clear"),
WeatherKind::Cloudy => write!(f, "Cloudy"),
WeatherKind::Rain => write!(f, "Rain"),
WeatherKind::Storm => write!(f, "Storm"),
}
}
}