forge
Explore
Log in

Don't you dare go Hollow

game_cargo

Clone
★ 0

0 watching0 forksRust201.2 KiB5226b95Tahinli1 month ago1 commit

signals.rs185 lines, 6.4 KiBHistoryRawDownload
1//! Map signals, drawn with gizmos each frame: a good icon beside each producer, a supply row
2//! above each front, and a slow red warning ring around any front with no rail line.
3
4use bevy::prelude::*;
5
6use crate::front::hourly_need;
7use crate::model::{Good, Network, NodeId, NodeKind, Train};
8use crate::seasons::SeasonState;
9
10const INK: Color = Color::srgb(0.22, 0.15, 0.09);
11const RED: Color = Color::srgb(0.78, 0.12, 0.10);
12const UNSERVED: Color = Color::srgb(0.55, 0.55, 0.52);
13
14/// Icon side in world units. At the default zoom a node is about 20 units across.
15const ICON: f32 = 14.0;
16/// Distance between scanlines when filling an icon.
17const FILL_STEP: f32 = 1.5;
18/// Producer icon offset from its node centre, top right, clear of the track ends.
19const PRODUCER_OFFSET: Vec2 = Vec2::new(17.0, 17.0);
20/// Height of the front's supply row above its centre, and the gap between its icons.
21const FRONT_ROW_Y: f32 = 44.0;
22const FRONT_ROW_GAP: f32 = 20.0;
23/// A front icon pulses red while its stock covers less than this many hours of need.
24const LOW_HOURS: f32 = 6.0;
25/// Radius of the warning ring around a front with no rail line.
26const CUT_RING_R: f32 = 26.0;
27
28/// The goods a front needs, in the order of its supply row.
29const FRONT_GOODS: [Good; 3] = [Good::Ammo, Good::Food, Good::Troops];
30
31pub struct SignalsPlugin;
32
33impl Plugin for SignalsPlugin {
34 fn build(&self, app: &mut App) {
35 app.add_systems(Update, draw_signals);
36 }
37}
38
39/// Same colours as the stock bars on the map.
40fn good_color(g: Good) -> Color {
41 match g {
42 Good::Ammo => Color::srgb(0.33, 0.36, 0.14),
43 Good::Food => Color::srgb(0.87, 0.73, 0.40),
44 Good::Troops => Color::srgb(0.10, 0.18, 0.42),
45 Good::Wounded => Color::srgb(0.72, 0.08, 0.22),
46 }
47}
48
49/// Outline of a good's icon, in a box of side 1 centred on the origin.
50fn shape(g: Good) -> Vec<Vec2> {
51 match g {
52 // Shell: a body with a pointed nose.
53 Good::Ammo => vec![
54 Vec2::new(-0.3, -0.5),
55 Vec2::new(0.3, -0.5),
56 Vec2::new(0.3, 0.1),
57 Vec2::new(0.0, 0.5),
58 Vec2::new(-0.3, 0.1),
59 ],
60 // Helmet: a dome sitting on a brim.
61 Good::Troops => {
62 let mut pts = vec![
63 Vec2::new(-0.5, -0.25),
64 Vec2::new(0.5, -0.25),
65 Vec2::new(0.5, -0.12),
66 ];
67 for i in 0..=12 {
68 let a = std::f32::consts::PI * i as f32 / 12.0;
69 pts.push(Vec2::new(0.45 * a.cos(), -0.05 + 0.45 * a.sin()));
70 }
71 pts.push(Vec2::new(-0.5, -0.12));
72 pts
73 }
74 // Food: a filled disc.
75 Good::Food | Good::Wounded => (0..16)
76 .map(|i| {
77 let a = std::f32::consts::TAU * i as f32 / 16.0;
78 Vec2::new(0.5 * a.cos(), 0.5 * a.sin())
79 })
80 .collect(),
81 }
82}
83
84fn icon_points(g: Good, center: Vec2) -> Vec<Vec2> {
85 shape(g).iter().map(|p| center + *p * ICON).collect()
86}
87
88/// Fills a simple polygon with horizontal scanlines.
89fn fill(gizmos: &mut Gizmos, pts: &[Vec2], color: Color) {
90 let lo = pts.iter().map(|p| p.y).fold(f32::INFINITY, f32::min);
91 let hi = pts.iter().map(|p| p.y).fold(f32::NEG_INFINITY, f32::max);
92 let mut y = lo + FILL_STEP * 0.5;
93 while y < hi {
94 let mut xs: Vec<f32> = Vec::new();
95 for (a, b) in pts.iter().zip(pts.iter().cycle().skip(1)) {
96 if (a.y <= y) != (b.y <= y) {
97 xs.push(a.x + (b.x - a.x) * (y - a.y) / (b.y - a.y));
98 }
99 }
100 xs.sort_by(f32::total_cmp);
101 for pair in xs.chunks_exact(2) {
102 gizmos.line_2d(Vec2::new(pair[0], y), Vec2::new(pair[1], y), color);
103 }
104 y += FILL_STEP;
105 }
106}
107
108fn outline(gizmos: &mut Gizmos, pts: &[Vec2], color: Color) {
109 gizmos.linestrip_2d(pts.iter().copied().chain(pts.first().copied()), color);
110}
111
112/// A filled good icon with an ink outline.
113fn draw_icon(gizmos: &mut Gizmos, g: Good, center: Vec2) {
114 let pts = icon_points(g, center);
115 fill(gizmos, &pts, good_color(g));
116 outline(gizmos, &pts, INK);
117}
118
119/// A good the front is not receiving: grey outline with a red slash.
120fn draw_unserved(gizmos: &mut Gizmos, g: Good, center: Vec2) {
121 outline(gizmos, &icon_points(g, center), UNSERVED);
122 let h = ICON * 0.5;
123 gizmos.line_2d(center + Vec2::new(-h, h), center + Vec2::new(h, -h), RED);
124}
125
126fn ring(gizmos: &mut Gizmos, center: Vec2, radius: f32, color: Color) {
127 gizmos.circle_2d(Isometry2d::from_translation(center), radius, color);
128}
129
130fn draw_signals(
131 mut gizmos: Gizmos,
132 net: Res<Network>,
133 trains: Query<&Train>,
134 season: Res<SeasonState>,
135 time: Res<Time>,
136) {
137 let t = time.elapsed_secs();
138 // Both pulses run 0..1. `fast` drives the low-stock ring, `slow` the rail-cut ring.
139 let fast = 0.5 + 0.5 * (t * 5.0).sin();
140 let slow = 0.5 + 0.5 * (t * 2.0).sin();
141 let capital = net
142 .nodes
143 .iter()
144 .position(|n| n.kind == NodeKind::Capital)
145 .map(NodeId);
146
147 for (i, node) in net.nodes.iter().enumerate() {
148 let c = node.pos;
149 let made = match node.kind {
150 NodeKind::Factory => Some(Good::Ammo),
151 NodeKind::Farm => Some(Good::Food),
152 NodeKind::Capital | NodeKind::City => Some(Good::Troops),
153 NodeKind::Depot | NodeKind::Front => None,
154 };
155 if let Some(g) = made {
156 draw_icon(&mut gizmos, g, c + PRODUCER_OFFSET);
157 }
158 if node.kind != NodeKind::Front {
159 continue;
160 }
161
162 let id = NodeId(i);
163 let connected = capital.is_some_and(|cap| net.shortest_path(cap, id).is_some());
164 if !connected {
165 for dr in [0.0, 1.0, 2.0] {
166 ring(&mut gizmos, c, CUT_RING_R + 3.0 * slow + dr, RED);
167 }
168 }
169
170 for (k, g) in FRONT_GOODS.iter().enumerate() {
171 let center = c + Vec2::new((k as f32 - 1.0) * FRONT_ROW_GAP, FRONT_ROW_Y);
172 let delivered = trains
173 .iter()
174 .any(|tr| tr.can_carry(*g) && tr.route.iter().any(|s| s.node == id));
175 if !delivered {
176 draw_unserved(&mut gizmos, *g, center);
177 continue;
178 }
179 draw_icon(&mut gizmos, *g, center);
180 if node.stock[g.idx()] < LOW_HOURS * hourly_need(*g, season.season.food_mult()) {
181 ring(&mut gizmos, center, ICON * 0.5 + 2.0 + 2.5 * fast, RED);
182 }
183 }
184 }
185}