Skip to main content

traaains/sim/track/segment/
signals_manager.rs

1use std::collections::BTreeMap;
2
3use bevy::ecs::{component::Component, entity::Entity};
4
5use crate::sim::signal::SignalPos;
6
7/// Takes care of signals placed along a single track segment. Stores entity references in a binary search tree
8/// ordered by the ratio of their track position (f32, yuck, but whatever). Allows for logarithmic lookup
9/// of closest pair of signals to a given ratio.
10#[derive(Component, Default)]
11pub struct SignalsManager {
12    pub inner: BTreeMap<SignalPos, Entity>,
13}
14
15impl SignalsManager {
16    /// First signal with respect to positive direction.
17    pub fn first(&self) -> Option<Entity> {
18        self.inner.first_key_value().map(|(_, e)| *e)
19    }
20
21    /// Last signal with respect to positive direction.
22    pub fn last(&self) -> Option<Entity> {
23        self.inner.last_key_value().map(|(_, e)| *e)
24    }
25}