//! Map signals, drawn with gizmos each frame: a good icon beside each producer, a supply row //! above each front, and a slow red warning ring around any front with no rail line. use bevy::prelude::*; use crate::front::hourly_need; use crate::model::{Good, Network, NodeId, NodeKind, Train}; use crate::seasons::SeasonState; const INK: Color = Color::srgb(0.22, 0.15, 0.09); const RED: Color = Color::srgb(0.78, 0.12, 0.10); const UNSERVED: Color = Color::srgb(0.55, 0.55, 0.52); /// Icon side in world units. At the default zoom a node is about 20 units across. const ICON: f32 = 14.0; /// Distance between scanlines when filling an icon. const FILL_STEP: f32 = 1.5; /// Producer icon offset from its node centre, top right, clear of the track ends. const PRODUCER_OFFSET: Vec2 = Vec2::new(17.0, 17.0); /// Height of the front's supply row above its centre, and the gap between its icons. const FRONT_ROW_Y: f32 = 44.0; const FRONT_ROW_GAP: f32 = 20.0; /// A front icon pulses red while its stock covers less than this many hours of need. const LOW_HOURS: f32 = 6.0; /// Radius of the warning ring around a front with no rail line. const CUT_RING_R: f32 = 26.0; /// The goods a front needs, in the order of its supply row. const FRONT_GOODS: [Good; 3] = [Good::Ammo, Good::Food, Good::Troops]; pub struct SignalsPlugin; impl Plugin for SignalsPlugin { fn build(&self, app: &mut App) { app.add_systems(Update, draw_signals); } } /// Same colours as the stock bars on the map. fn good_color(g: Good) -> Color { match g { Good::Ammo => Color::srgb(0.33, 0.36, 0.14), Good::Food => Color::srgb(0.87, 0.73, 0.40), Good::Troops => Color::srgb(0.10, 0.18, 0.42), Good::Wounded => Color::srgb(0.72, 0.08, 0.22), } } /// Outline of a good's icon, in a box of side 1 centred on the origin. fn shape(g: Good) -> Vec { match g { // Shell: a body with a pointed nose. Good::Ammo => vec![ Vec2::new(-0.3, -0.5), Vec2::new(0.3, -0.5), Vec2::new(0.3, 0.1), Vec2::new(0.0, 0.5), Vec2::new(-0.3, 0.1), ], // Helmet: a dome sitting on a brim. Good::Troops => { let mut pts = vec![ Vec2::new(-0.5, -0.25), Vec2::new(0.5, -0.25), Vec2::new(0.5, -0.12), ]; for i in 0..=12 { let a = std::f32::consts::PI * i as f32 / 12.0; pts.push(Vec2::new(0.45 * a.cos(), -0.05 + 0.45 * a.sin())); } pts.push(Vec2::new(-0.5, -0.12)); pts } // Food: a filled disc. Good::Food | Good::Wounded => (0..16) .map(|i| { let a = std::f32::consts::TAU * i as f32 / 16.0; Vec2::new(0.5 * a.cos(), 0.5 * a.sin()) }) .collect(), } } fn icon_points(g: Good, center: Vec2) -> Vec { shape(g).iter().map(|p| center + *p * ICON).collect() } /// Fills a simple polygon with horizontal scanlines. fn fill(gizmos: &mut Gizmos, pts: &[Vec2], color: Color) { let lo = pts.iter().map(|p| p.y).fold(f32::INFINITY, f32::min); let hi = pts.iter().map(|p| p.y).fold(f32::NEG_INFINITY, f32::max); let mut y = lo + FILL_STEP * 0.5; while y < hi { let mut xs: Vec = Vec::new(); for (a, b) in pts.iter().zip(pts.iter().cycle().skip(1)) { if (a.y <= y) != (b.y <= y) { xs.push(a.x + (b.x - a.x) * (y - a.y) / (b.y - a.y)); } } xs.sort_by(f32::total_cmp); for pair in xs.chunks_exact(2) { gizmos.line_2d(Vec2::new(pair[0], y), Vec2::new(pair[1], y), color); } y += FILL_STEP; } } fn outline(gizmos: &mut Gizmos, pts: &[Vec2], color: Color) { gizmos.linestrip_2d(pts.iter().copied().chain(pts.first().copied()), color); } /// A filled good icon with an ink outline. fn draw_icon(gizmos: &mut Gizmos, g: Good, center: Vec2) { let pts = icon_points(g, center); fill(gizmos, &pts, good_color(g)); outline(gizmos, &pts, INK); } /// A good the front is not receiving: grey outline with a red slash. fn draw_unserved(gizmos: &mut Gizmos, g: Good, center: Vec2) { outline(gizmos, &icon_points(g, center), UNSERVED); let h = ICON * 0.5; gizmos.line_2d(center + Vec2::new(-h, h), center + Vec2::new(h, -h), RED); } fn ring(gizmos: &mut Gizmos, center: Vec2, radius: f32, color: Color) { gizmos.circle_2d(Isometry2d::from_translation(center), radius, color); } fn draw_signals( mut gizmos: Gizmos, net: Res, trains: Query<&Train>, season: Res, time: Res