| 1 | |
| 2 | |
| 3 | |
| 4 | use bevy::prelude::*; |
| 5 | |
| 6 | use crate::front::hourly_need; |
| 7 | use crate::model::{Good, Network, NodeId, NodeKind, Train}; |
| 8 | use crate::seasons::SeasonState; |
| 9 | |
| 10 | const INK: Color = Color::srgb(0.22, 0.15, 0.09); |
| 11 | const RED: Color = Color::srgb(0.78, 0.12, 0.10); |
| 12 | const UNSERVED: Color = Color::srgb(0.55, 0.55, 0.52); |
| 13 | |
| 14 | |
| 15 | const ICON: f32 = 14.0; |
| 16 | |
| 17 | const FILL_STEP: f32 = 1.5; |
| 18 | |
| 19 | const PRODUCER_OFFSET: Vec2 = Vec2::new(17.0, 17.0); |
| 20 | |
| 21 | const FRONT_ROW_Y: f32 = 44.0; |
| 22 | const FRONT_ROW_GAP: f32 = 20.0; |
| 23 | |
| 24 | const LOW_HOURS: f32 = 6.0; |
| 25 | |
| 26 | const CUT_RING_R: f32 = 26.0; |
| 27 | |
| 28 | |
| 29 | const FRONT_GOODS: [Good; 3] = [Good::Ammo, Good::Food, Good::Troops]; |
| 30 | |
| 31 | pub struct SignalsPlugin; |
| 32 | |
| 33 | impl Plugin for SignalsPlugin { |
| 34 | fn build(&self, app: &mut App) { |
| 35 | app.add_systems(Update, draw_signals); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | |
| 40 | fn 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 | |
| 50 | fn shape(g: Good) -> Vec<Vec2> { |
| 51 | match g { |
| 52 | |
| 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 | |
| 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 | |
| 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 | |
| 84 | fn icon_points(g: Good, center: Vec2) -> Vec<Vec2> { |
| 85 | shape(g).iter().map(|p| center + *p * ICON).collect() |
| 86 | } |
| 87 | |
| 88 | |
| 89 | fn 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 | |
| 108 | fn outline(gizmos: &mut Gizmos, pts: &[Vec2], color: Color) { |
| 109 | gizmos.linestrip_2d(pts.iter().copied().chain(pts.first().copied()), color); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | fn 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 | |
| 120 | fn 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 | |
| 126 | fn ring(gizmos: &mut Gizmos, center: Vec2, radius: f32, color: Color) { |
| 127 | gizmos.circle_2d(Isometry2d::from_translation(center), radius, color); |
| 128 | } |
| 129 | |
| 130 | fn 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 | |
| 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 | } |