forge
Explore
Log in

Don't you dare go Hollow

game_cargo

Clone
★ 0

0 watching0 forksRust201.2 KiB5226b95Tahinli1 month ago1 commit

seasons.rs383 lines, 11.7 KiBHistoryRawDownload
1//! Seasons: a cycle of Spring, Summer, Autumn and Winter, two game weeks each. Each season
2//! changes train speed, raid odds, food need and fog. The map tint, weather drawing and the
3//! top-bar badge show the season without text walls.
4
5use bevy::prelude::*;
6use bevy::time::{Real, Time};
7
8use crate::economy::{advance_clock, economy_tick};
9use crate::front::front_hour_system;
10use crate::map::unit_hash;
11use crate::model::{Network, Notify, SimClock};
12
13/// Game hours per season: two game weeks.
14const SEASON_HOURS: u32 = 24 * 14;
15/// Hours a season change takes to blend the map tint in.
16const BLEND_HOURS: f32 = 24.0;
17/// Chance that an Autumn day is a fog day.
18const FOG_CHANCE: f64 = 0.4;
19/// Map frame size, matching `map::draw_background`.
20const MAP_W: f32 = 1280.0;
21const MAP_H: f32 = 720.0;
22const SNOW_COUNT: u32 = 60;
23const FOG_BANDS: u32 = 5;
24const FOG_LEN: f32 = 320.0;
25const FOG_HALF_H: f32 = 22.0;
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
28pub enum Season {
29 #[default]
30 Spring,
31 Summer,
32 Autumn,
33 Winter,
34}
35
36impl Season {
37 pub const ALL: [Season; 4] = [Season::Spring, Season::Summer, Season::Autumn, Season::Winter];
38
39 /// The season in force at `hour` game hours.
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 /// Train speed multiplier: mud in Spring and Autumn, snow in Winter.
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 /// Air raid chance multiplier: clear skies in Summer.
63 pub fn raid_mult(self) -> f32 {
64 match self {
65 Season::Summer => 1.5,
66 _ => 1.0,
67 }
68 }
69
70 /// Food need of front sectors and food eaten by cities: higher in Winter.
71 pub fn food_mult(self) -> f32 {
72 match self {
73 Season::Winter => 1.5,
74 _ => 1.0,
75 }
76 }
77
78 /// Seasons with mud on the tracks.
79 pub fn is_muddy(self) -> bool {
80 matches!(self, Season::Spring | Season::Autumn)
81 }
82
83 /// Map background tint as RGBA; blended toward the next season over a game day.
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 /// Colour of the top-bar badge.
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 /// News line sent when this season starts.
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/// The operation's season. `season` is the one in force; `fog` is set on Autumn fog days,
115/// when no air raids happen.
116#[derive(Resource, Debug, Clone, Copy, Default)]
117pub struct SeasonState {
118 pub season: Season,
119 /// Season before the current one, for the tint blend.
120 previous: Season,
121 /// Game hour the current season began.
122 changed_at: u32,
123 pub fog: bool,
124 /// Game day the fog roll was made for.
125 fog_day: u32,
126}
127
128impl SeasonState {
129 /// Map tint at `hour`: the previous season's tint fading into the current one.
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
138pub struct SeasonsPlugin;
139
140impl 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 // Season and fog are settled before this hour's economy and raids run.
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/// Marks the full-map colour wash behind the drawn map.
159#[derive(Component)]
160struct MapTint;
161
162/// Top-bar season badge parts.
163#[derive(Component)]
164struct SeasonBadgeTint;
165
166#[derive(Component)]
167struct SeasonLabel;
168
169#[derive(Component)]
170struct SeasonProgress;
171
172fn 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/// Adds the season badge to the top bar: a coloured swatch, the season word, and a bar
181/// that fills as the season runs out.
182pub 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/// Tracks the clock: switches season at each boundary (with one news line), and rolls the
239/// fog once per game day in Autumn.
240fn 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
258fn 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
271fn 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/// Snow, fog and mud drawn over the map.
295fn 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/// Slow falling specks, each swaying a little.
314fn 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/// Pale translucent bands drifting east across the map.
329fn 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/// Brown mud dabs beside each track.
349fn 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)]
373mod 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}