//! Seasons: a cycle of Spring, Summer, Autumn and Winter, two game weeks each. Each season //! changes train speed, raid odds, food need and fog. The map tint, weather drawing and the //! top-bar badge show the season without text walls. use bevy::prelude::*; use bevy::time::{Real, Time}; use crate::economy::{advance_clock, economy_tick}; use crate::front::front_hour_system; use crate::map::unit_hash; use crate::model::{Network, Notify, SimClock}; /// Game hours per season: two game weeks. const SEASON_HOURS: u32 = 24 * 14; /// Hours a season change takes to blend the map tint in. const BLEND_HOURS: f32 = 24.0; /// Chance that an Autumn day is a fog day. const FOG_CHANCE: f64 = 0.4; /// Map frame size, matching `map::draw_background`. const MAP_W: f32 = 1280.0; const MAP_H: f32 = 720.0; const SNOW_COUNT: u32 = 60; const FOG_BANDS: u32 = 5; const FOG_LEN: f32 = 320.0; const FOG_HALF_H: f32 = 22.0; #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub enum Season { #[default] Spring, Summer, Autumn, Winter, } impl Season { pub const ALL: [Season; 4] = [Season::Spring, Season::Summer, Season::Autumn, Season::Winter]; /// The season in force at `hour` game hours. pub fn from_hour(hour: u32) -> Season { Season::ALL[(hour / SEASON_HOURS) as usize % Season::ALL.len()] } pub fn name(self) -> &'static str { match self { Season::Spring => "Spring", Season::Summer => "Summer", Season::Autumn => "Autumn", Season::Winter => "Winter", } } /// Train speed multiplier: mud in Spring and Autumn, snow in Winter. pub fn speed_mult(self) -> f32 { match self { Season::Spring => 0.7, Season::Summer => 1.0, Season::Autumn | Season::Winter => 0.85, } } /// Air raid chance multiplier: clear skies in Summer. pub fn raid_mult(self) -> f32 { match self { Season::Summer => 1.5, _ => 1.0, } } /// Food need of front sectors and food eaten by cities: higher in Winter. pub fn food_mult(self) -> f32 { match self { Season::Winter => 1.5, _ => 1.0, } } /// Seasons with mud on the tracks. pub fn is_muddy(self) -> bool { matches!(self, Season::Spring | Season::Autumn) } /// Map background tint as RGBA; blended toward the next season over a game day. fn tint(self) -> [f32; 4] { match self { Season::Spring => [0.55, 0.70, 0.52, 0.22], Season::Summer => [0.98, 0.88, 0.62, 0.20], Season::Autumn => [0.66, 0.42, 0.18, 0.24], Season::Winter => [0.84, 0.91, 0.98, 0.40], } } /// Colour of the top-bar badge. fn badge_color(self) -> Color { match self { Season::Spring => Color::srgb(0.55, 0.78, 0.45), Season::Summer => Color::srgb(0.95, 0.78, 0.35), Season::Autumn => Color::srgb(0.82, 0.48, 0.20), Season::Winter => Color::srgb(0.75, 0.88, 1.00), } } /// News line sent when this season starts. fn change_text(self) -> &'static str { match self { Season::Spring => "Spring: mud slows trains", Season::Summer => "Summer: clear skies, raids up", Season::Autumn => "Autumn: mud and fog", Season::Winter => "Winter: food needs up", } } } /// The operation's season. `season` is the one in force; `fog` is set on Autumn fog days, /// when no air raids happen. #[derive(Resource, Debug, Clone, Copy, Default)] pub struct SeasonState { pub season: Season, /// Season before the current one, for the tint blend. previous: Season, /// Game hour the current season began. changed_at: u32, pub fog: bool, /// Game day the fog roll was made for. fog_day: u32, } impl SeasonState { /// Map tint at `hour`: the previous season's tint fading into the current one. fn tint_color(&self, hour: u32) -> Color { let t = (hour.saturating_sub(self.changed_at) as f32 / BLEND_HOURS).min(1.0); let (a, b) = (self.previous.tint(), self.season.tint()); let c = |i: usize| a[i] + (b[i] - a[i]) * t; Color::srgba(c(0), c(1), c(2), c(3)) } } pub struct SeasonsPlugin; impl Plugin for SeasonsPlugin { fn build(&self, app: &mut App) { app.init_resource::() .add_systems(Startup, spawn_tint) .add_systems( Update, ( // Season and fog are settled before this hour's economy and raids run. season_system .after(advance_clock) .before(economy_tick) .before(front_hour_system), (update_tint, draw_weather, update_badge), ), ); } } /// Marks the full-map colour wash behind the drawn map. #[derive(Component)] struct MapTint; /// Top-bar season badge parts. #[derive(Component)] struct SeasonBadgeTint; #[derive(Component)] struct SeasonLabel; #[derive(Component)] struct SeasonProgress; fn spawn_tint(mut commands: Commands, state: Res) { commands.spawn(( MapTint, Sprite::from_color(state.tint_color(0), Vec2::new(MAP_W, MAP_H)), Transform::from_xyz(0.0, 0.0, -5.0), )); } /// Adds the season badge to the top bar: a coloured swatch, the season word, and a bar /// that fills as the season runs out. pub fn spawn_badge(bar: &mut ChildSpawnerCommands) { bar.spawn(Node { flex_direction: FlexDirection::Column, row_gap: Val::Px(3.0), ..default() }) .with_children(|col| { col.spawn(Node { flex_direction: FlexDirection::Row, align_items: AlignItems::Center, column_gap: Val::Px(6.0), ..default() }) .with_children(|row| { row.spawn(( SeasonBadgeTint, Node { width: Val::Px(10.0), height: Val::Px(10.0), ..default() }, BackgroundColor(Color::WHITE), )); row.spawn(( SeasonLabel, Text::new(""), TextFont { font_size: FontSize::Px(14.0), ..default() }, TextColor(Color::srgb(0.92, 0.88, 0.76)), )); }); col.spawn(( Node { width: Val::Px(90.0), height: Val::Px(3.0), ..default() }, BackgroundColor(Color::srgba(0.0, 0.0, 0.0, 0.4)), )) .with_children(|track| { track.spawn(( SeasonProgress, SeasonBadgeTint, Node { width: Val::Percent(0.0), height: Val::Percent(100.0), ..default() }, BackgroundColor(Color::WHITE), )); }); }); } /// Tracks the clock: switches season at each boundary (with one news line), and rolls the /// fog once per game day in Autumn. fn season_system( clock: Res, mut state: ResMut, mut notify: MessageWriter, ) { let now = Season::from_hour(clock.hour); if now != state.season { state.previous = state.season; state.season = now; state.changed_at = clock.hour; notify.write(Notify(now.change_text().to_string())); } if clock.day() != state.fog_day { state.fog_day = clock.day(); state.fog = now == Season::Autumn && rand::random_bool(FOG_CHANCE); } } fn update_tint( state: Res, clock: Res, mut tints: Query<&mut Sprite, With>, ) { let color = state.tint_color(clock.hour); for mut sprite in &mut tints { if sprite.color != color { sprite.color = color; } } } fn update_badge( state: Res, clock: Res, mut label: Query<(&mut Text, &mut TextColor), With>, mut tints: Query<&mut BackgroundColor, With>, mut progress: Query<&mut Node, With>, ) { let color = state.season.badge_color(); for (mut text, mut text_color) in &mut label { if text.0 != state.season.name() { text.0 = state.season.name().to_string(); } text_color.0 = color; } for mut bg in &mut tints { bg.0 = color; } let done = (clock.hour % SEASON_HOURS) as f32 / SEASON_HOURS as f32; for mut node in &mut progress { node.width = Val::Percent(done * 100.0); } } /// Snow, fog and mud drawn over the map. fn draw_weather( mut gizmos: Gizmos, state: Res, net: Res, time: Res>, ) { let t = time.elapsed_secs(); if state.season == Season::Winter { draw_snow(&mut gizmos, t); } if state.fog { draw_fog(&mut gizmos, t); } if state.season.is_muddy() { draw_mud(&mut gizmos, &net); } } /// Slow falling specks, each swaying a little. fn draw_snow(gizmos: &mut Gizmos, t: f32) { let color = Color::srgba(1.0, 1.0, 1.0, 0.85); for i in 0..SNOW_COUNT { let seed = i * 4; let x0 = unit_hash(seed) * MAP_W - MAP_W * 0.5; let speed = 10.0 + 12.0 * unit_hash(seed + 1); let fall = (unit_hash(seed + 2) * MAP_H + t * speed) % MAP_H; let x = x0 + (t * 0.8 + i as f32).sin() * 8.0; let y = MAP_H * 0.5 - fall; let radius = 1.5 + unit_hash(seed + 3); gizmos.circle_2d(Isometry2d::from_translation(Vec2::new(x, y)), radius, color); } } /// Pale translucent bands drifting east across the map. fn draw_fog(gizmos: &mut Gizmos, t: f32) { let color = Color::srgba(0.94, 0.94, 0.90, 0.07); for k in 0..FOG_BANDS { let seed = 500 + k * 4; let speed = 14.0 + 10.0 * unit_hash(seed); let start = (unit_hash(seed + 1) * MAP_W + t * speed) % MAP_W - MAP_W * 0.5; let (x0, x1) = (start.max(-MAP_W * 0.5), (start + FOG_LEN).min(MAP_W * 0.5)); if x1 <= x0 { continue; } let y = (unit_hash(seed + 2) * MAP_H - MAP_H * 0.5) + (t * 0.3 + k as f32).sin() * 12.0; let mut dy = -FOG_HALF_H; while dy <= FOG_HALF_H { gizmos.line_2d(Vec2::new(x0, y + dy), Vec2::new(x1, y + dy), color); dy += 3.0; } } } /// Brown mud dabs beside each track. fn draw_mud(gizmos: &mut Gizmos, net: &Network) { let mud = Color::srgba(0.38, 0.25, 0.12, 0.45); for (i, track) in net.tracks.iter().enumerate() { let a = net.nodes[track.a.0].pos; let b = net.nodes[track.b.0].pos; let len = a.distance(b); if len <= 0.0 { continue; } let dir = (b - a) / len; let perp = Vec2::new(-dir.y, dir.x); for k in 0..3 { let seed = 900 + i as u32 * 16 + k * 4; let along = 0.2 + 0.6 * unit_hash(seed); let side = if unit_hash(seed + 1) < 0.5 { -1.0 } else { 1.0 }; let c = a.lerp(b, along) + perp * side * 5.0; let half = 3.0 + 3.0 * unit_hash(seed + 2); gizmos.line_2d(c - dir * half, c + dir * half, mud); gizmos.line_2d(c - dir * half + perp, c + dir * half + perp, mud); } } } #[cfg(test)] mod tests { use super::*; #[test] fn season_follows_two_week_cycle() { assert_eq!(Season::from_hour(0), Season::Spring); assert_eq!(Season::from_hour(SEASON_HOURS - 1), Season::Spring); assert_eq!(Season::from_hour(SEASON_HOURS), Season::Summer); assert_eq!(Season::from_hour(4 * SEASON_HOURS), Season::Spring); } }