| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use bevy::prelude::*; |
| 6 | use bevy::time::{Real, Time}; |
| 7 | |
| 8 | use crate::economy::{advance_clock, economy_tick}; |
| 9 | use crate::front::front_hour_system; |
| 10 | use crate::map::unit_hash; |
| 11 | use crate::model::{Network, Notify, SimClock}; |
| 12 | |
| 13 | |
| 14 | const SEASON_HOURS: u32 = 24 * 14; |
| 15 | |
| 16 | const BLEND_HOURS: f32 = 24.0; |
| 17 | |
| 18 | const FOG_CHANCE: f64 = 0.4; |
| 19 | |
| 20 | const MAP_W: f32 = 1280.0; |
| 21 | const MAP_H: f32 = 720.0; |
| 22 | const SNOW_COUNT: u32 = 60; |
| 23 | const FOG_BANDS: u32 = 5; |
| 24 | const FOG_LEN: f32 = 320.0; |
| 25 | const FOG_HALF_H: f32 = 22.0; |
| 26 | |
| 27 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 28 | pub enum Season { |
| 29 | #[default] |
| 30 | Spring, |
| 31 | Summer, |
| 32 | Autumn, |
| 33 | Winter, |
| 34 | } |
| 35 | |
| 36 | impl Season { |
| 37 | pub const ALL: [Season; 4] = [Season::Spring, Season::Summer, Season::Autumn, Season::Winter]; |
| 38 | |
| 39 | |
| 40 | pub fn from_hour(hour: u32) -> Season { |
| 41 | Season::ALL[(hour / SEASON_HOURS) as usize % Season::ALL.len()] |
| 42 | } |
| 43 | |
| 44 | pub fn name(self) -> &'static str { |
| 45 | match self { |
| 46 | Season::Spring => "Spring", |
| 47 | Season::Summer => "Summer", |
| 48 | Season::Autumn => "Autumn", |
| 49 | Season::Winter => "Winter", |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | |
| 54 | pub fn speed_mult(self) -> f32 { |
| 55 | match self { |
| 56 | Season::Spring => 0.7, |
| 57 | Season::Summer => 1.0, |
| 58 | Season::Autumn | Season::Winter => 0.85, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | |
| 63 | pub fn raid_mult(self) -> f32 { |
| 64 | match self { |
| 65 | Season::Summer => 1.5, |
| 66 | _ => 1.0, |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | pub fn food_mult(self) -> f32 { |
| 72 | match self { |
| 73 | Season::Winter => 1.5, |
| 74 | _ => 1.0, |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | pub fn is_muddy(self) -> bool { |
| 80 | matches!(self, Season::Spring | Season::Autumn) |
| 81 | } |
| 82 | |
| 83 | |
| 84 | fn tint(self) -> [f32; 4] { |
| 85 | match self { |
| 86 | Season::Spring => [0.55, 0.70, 0.52, 0.22], |
| 87 | Season::Summer => [0.98, 0.88, 0.62, 0.20], |
| 88 | Season::Autumn => [0.66, 0.42, 0.18, 0.24], |
| 89 | Season::Winter => [0.84, 0.91, 0.98, 0.40], |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | |
| 94 | fn badge_color(self) -> Color { |
| 95 | match self { |
| 96 | Season::Spring => Color::srgb(0.55, 0.78, 0.45), |
| 97 | Season::Summer => Color::srgb(0.95, 0.78, 0.35), |
| 98 | Season::Autumn => Color::srgb(0.82, 0.48, 0.20), |
| 99 | Season::Winter => Color::srgb(0.75, 0.88, 1.00), |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | |
| 104 | fn change_text(self) -> &'static str { |
| 105 | match self { |
| 106 | Season::Spring => "Spring: mud slows trains", |
| 107 | Season::Summer => "Summer: clear skies, raids up", |
| 108 | Season::Autumn => "Autumn: mud and fog", |
| 109 | Season::Winter => "Winter: food needs up", |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | |
| 115 | |
| 116 | #[derive(Resource, Debug, Clone, Copy, Default)] |
| 117 | pub struct SeasonState { |
| 118 | pub season: Season, |
| 119 | |
| 120 | previous: Season, |
| 121 | |
| 122 | changed_at: u32, |
| 123 | pub fog: bool, |
| 124 | |
| 125 | fog_day: u32, |
| 126 | } |
| 127 | |
| 128 | impl SeasonState { |
| 129 | |
| 130 | fn tint_color(&self, hour: u32) -> Color { |
| 131 | let t = (hour.saturating_sub(self.changed_at) as f32 / BLEND_HOURS).min(1.0); |
| 132 | let (a, b) = (self.previous.tint(), self.season.tint()); |
| 133 | let c = |i: usize| a[i] + (b[i] - a[i]) * t; |
| 134 | Color::srgba(c(0), c(1), c(2), c(3)) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | pub struct SeasonsPlugin; |
| 139 | |
| 140 | impl Plugin for SeasonsPlugin { |
| 141 | fn build(&self, app: &mut App) { |
| 142 | app.init_resource::<SeasonState>() |
| 143 | .add_systems(Startup, spawn_tint) |
| 144 | .add_systems( |
| 145 | Update, |
| 146 | ( |
| 147 | |
| 148 | season_system |
| 149 | .after(advance_clock) |
| 150 | .before(economy_tick) |
| 151 | .before(front_hour_system), |
| 152 | (update_tint, draw_weather, update_badge), |
| 153 | ), |
| 154 | ); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | |
| 159 | #[derive(Component)] |
| 160 | struct MapTint; |
| 161 | |
| 162 | |
| 163 | #[derive(Component)] |
| 164 | struct SeasonBadgeTint; |
| 165 | |
| 166 | #[derive(Component)] |
| 167 | struct SeasonLabel; |
| 168 | |
| 169 | #[derive(Component)] |
| 170 | struct SeasonProgress; |
| 171 | |
| 172 | fn spawn_tint(mut commands: Commands, state: Res<SeasonState>) { |
| 173 | commands.spawn(( |
| 174 | MapTint, |
| 175 | Sprite::from_color(state.tint_color(0), Vec2::new(MAP_W, MAP_H)), |
| 176 | Transform::from_xyz(0.0, 0.0, -5.0), |
| 177 | )); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | |
| 182 | pub fn spawn_badge(bar: &mut ChildSpawnerCommands) { |
| 183 | bar.spawn(Node { |
| 184 | flex_direction: FlexDirection::Column, |
| 185 | row_gap: Val::Px(3.0), |
| 186 | ..default() |
| 187 | }) |
| 188 | .with_children(|col| { |
| 189 | col.spawn(Node { |
| 190 | flex_direction: FlexDirection::Row, |
| 191 | align_items: AlignItems::Center, |
| 192 | column_gap: Val::Px(6.0), |
| 193 | ..default() |
| 194 | }) |
| 195 | .with_children(|row| { |
| 196 | row.spawn(( |
| 197 | SeasonBadgeTint, |
| 198 | Node { |
| 199 | width: Val::Px(10.0), |
| 200 | height: Val::Px(10.0), |
| 201 | ..default() |
| 202 | }, |
| 203 | BackgroundColor(Color::WHITE), |
| 204 | )); |
| 205 | row.spawn(( |
| 206 | SeasonLabel, |
| 207 | Text::new(""), |
| 208 | TextFont { |
| 209 | font_size: FontSize::Px(14.0), |
| 210 | ..default() |
| 211 | }, |
| 212 | TextColor(Color::srgb(0.92, 0.88, 0.76)), |
| 213 | )); |
| 214 | }); |
| 215 | col.spawn(( |
| 216 | Node { |
| 217 | width: Val::Px(90.0), |
| 218 | height: Val::Px(3.0), |
| 219 | ..default() |
| 220 | }, |
| 221 | BackgroundColor(Color::srgba(0.0, 0.0, 0.0, 0.4)), |
| 222 | )) |
| 223 | .with_children(|track| { |
| 224 | track.spawn(( |
| 225 | SeasonProgress, |
| 226 | SeasonBadgeTint, |
| 227 | Node { |
| 228 | width: Val::Percent(0.0), |
| 229 | height: Val::Percent(100.0), |
| 230 | ..default() |
| 231 | }, |
| 232 | BackgroundColor(Color::WHITE), |
| 233 | )); |
| 234 | }); |
| 235 | }); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | |
| 240 | fn season_system( |
| 241 | clock: Res<SimClock>, |
| 242 | mut state: ResMut<SeasonState>, |
| 243 | mut notify: MessageWriter<Notify>, |
| 244 | ) { |
| 245 | let now = Season::from_hour(clock.hour); |
| 246 | if now != state.season { |
| 247 | state.previous = state.season; |
| 248 | state.season = now; |
| 249 | state.changed_at = clock.hour; |
| 250 | notify.write(Notify(now.change_text().to_string())); |
| 251 | } |
| 252 | if clock.day() != state.fog_day { |
| 253 | state.fog_day = clock.day(); |
| 254 | state.fog = now == Season::Autumn && rand::random_bool(FOG_CHANCE); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | fn update_tint( |
| 259 | state: Res<SeasonState>, |
| 260 | clock: Res<SimClock>, |
| 261 | mut tints: Query<&mut Sprite, With<MapTint>>, |
| 262 | ) { |
| 263 | let color = state.tint_color(clock.hour); |
| 264 | for mut sprite in &mut tints { |
| 265 | if sprite.color != color { |
| 266 | sprite.color = color; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | fn update_badge( |
| 272 | state: Res<SeasonState>, |
| 273 | clock: Res<SimClock>, |
| 274 | mut label: Query<(&mut Text, &mut TextColor), With<SeasonLabel>>, |
| 275 | mut tints: Query<&mut BackgroundColor, With<SeasonBadgeTint>>, |
| 276 | mut progress: Query<&mut Node, With<SeasonProgress>>, |
| 277 | ) { |
| 278 | let color = state.season.badge_color(); |
| 279 | for (mut text, mut text_color) in &mut label { |
| 280 | if text.0 != state.season.name() { |
| 281 | text.0 = state.season.name().to_string(); |
| 282 | } |
| 283 | text_color.0 = color; |
| 284 | } |
| 285 | for mut bg in &mut tints { |
| 286 | bg.0 = color; |
| 287 | } |
| 288 | let done = (clock.hour % SEASON_HOURS) as f32 / SEASON_HOURS as f32; |
| 289 | for mut node in &mut progress { |
| 290 | node.width = Val::Percent(done * 100.0); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | |
| 295 | fn draw_weather( |
| 296 | mut gizmos: Gizmos, |
| 297 | state: Res<SeasonState>, |
| 298 | net: Res<Network>, |
| 299 | time: Res<Time<Real>>, |
| 300 | ) { |
| 301 | let t = time.elapsed_secs(); |
| 302 | if state.season == Season::Winter { |
| 303 | draw_snow(&mut gizmos, t); |
| 304 | } |
| 305 | if state.fog { |
| 306 | draw_fog(&mut gizmos, t); |
| 307 | } |
| 308 | if state.season.is_muddy() { |
| 309 | draw_mud(&mut gizmos, &net); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | |
| 314 | fn draw_snow(gizmos: &mut Gizmos, t: f32) { |
| 315 | let color = Color::srgba(1.0, 1.0, 1.0, 0.85); |
| 316 | for i in 0..SNOW_COUNT { |
| 317 | let seed = i * 4; |
| 318 | let x0 = unit_hash(seed) * MAP_W - MAP_W * 0.5; |
| 319 | let speed = 10.0 + 12.0 * unit_hash(seed + 1); |
| 320 | let fall = (unit_hash(seed + 2) * MAP_H + t * speed) % MAP_H; |
| 321 | let x = x0 + (t * 0.8 + i as f32).sin() * 8.0; |
| 322 | let y = MAP_H * 0.5 - fall; |
| 323 | let radius = 1.5 + unit_hash(seed + 3); |
| 324 | gizmos.circle_2d(Isometry2d::from_translation(Vec2::new(x, y)), radius, color); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | |
| 329 | fn draw_fog(gizmos: &mut Gizmos, t: f32) { |
| 330 | let color = Color::srgba(0.94, 0.94, 0.90, 0.07); |
| 331 | for k in 0..FOG_BANDS { |
| 332 | let seed = 500 + k * 4; |
| 333 | let speed = 14.0 + 10.0 * unit_hash(seed); |
| 334 | let start = (unit_hash(seed + 1) * MAP_W + t * speed) % MAP_W - MAP_W * 0.5; |
| 335 | let (x0, x1) = (start.max(-MAP_W * 0.5), (start + FOG_LEN).min(MAP_W * 0.5)); |
| 336 | if x1 <= x0 { |
| 337 | continue; |
| 338 | } |
| 339 | let y = (unit_hash(seed + 2) * MAP_H - MAP_H * 0.5) + (t * 0.3 + k as f32).sin() * 12.0; |
| 340 | let mut dy = -FOG_HALF_H; |
| 341 | while dy <= FOG_HALF_H { |
| 342 | gizmos.line_2d(Vec2::new(x0, y + dy), Vec2::new(x1, y + dy), color); |
| 343 | dy += 3.0; |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | |
| 349 | fn draw_mud(gizmos: &mut Gizmos, net: &Network) { |
| 350 | let mud = Color::srgba(0.38, 0.25, 0.12, 0.45); |
| 351 | for (i, track) in net.tracks.iter().enumerate() { |
| 352 | let a = net.nodes[track.a.0].pos; |
| 353 | let b = net.nodes[track.b.0].pos; |
| 354 | let len = a.distance(b); |
| 355 | if len <= 0.0 { |
| 356 | continue; |
| 357 | } |
| 358 | let dir = (b - a) / len; |
| 359 | let perp = Vec2::new(-dir.y, dir.x); |
| 360 | for k in 0..3 { |
| 361 | let seed = 900 + i as u32 * 16 + k * 4; |
| 362 | let along = 0.2 + 0.6 * unit_hash(seed); |
| 363 | let side = if unit_hash(seed + 1) < 0.5 { -1.0 } else { 1.0 }; |
| 364 | let c = a.lerp(b, along) + perp * side * 5.0; |
| 365 | let half = 3.0 + 3.0 * unit_hash(seed + 2); |
| 366 | gizmos.line_2d(c - dir * half, c + dir * half, mud); |
| 367 | gizmos.line_2d(c - dir * half + perp, c + dir * half + perp, mud); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | #[cfg(test)] |
| 373 | mod tests { |
| 374 | use super::*; |
| 375 | |
| 376 | #[test] |
| 377 | fn season_follows_two_week_cycle() { |
| 378 | assert_eq!(Season::from_hour(0), Season::Spring); |
| 379 | assert_eq!(Season::from_hour(SEASON_HOURS - 1), Season::Spring); |
| 380 | assert_eq!(Season::from_hour(SEASON_HOURS), Season::Summer); |
| 381 | assert_eq!(Season::from_hour(4 * SEASON_HOURS), Season::Spring); |
| 382 | } |
| 383 | } |