speed up agent following by not recalculating where agent is on every single event
use crate::common::route_viewer::RouteViewer;
use crate::ui::UI;
use ezgui::{EventCtx, GfxCtx, Key, ModalMenu, Text};
+use geom::{Duration, Pt2D};
use sim::TripID;
pub struct AgentTools {
- following: Option<TripID>,
+ following: Option<(TripID, Option<Pt2D>, Duration)>,
route_viewer: RouteViewer,
}
@@ -17,7 +18,7 @@ impl AgentTools {
}
pub fn update_menu_info(&self, txt: &mut Text) {
- if let Some(trip) = self.following {
+ if let Some((trip, _, _)) = self.following {
txt.add_line(format!("Following {}", trip));
}
if let RouteViewer::Active(_, trip, _) = self.route_viewer {
@@ -33,23 +34,32 @@ impl AgentTools {
.input
.contextual_action(Key::F, &format!("follow {}", agent))
{
- self.following = Some(trip);
+ self.following = Some((
+ trip,
+ ui.primary
+ .sim
+ .get_canonical_pt_per_trip(trip, &ui.primary.map),
+ ui.primary.sim.time(),
+ ));
}
}
}
}
- if let Some(trip) = self.following {
- if let Some(pt) = ui
+ if let Some((trip, _, time)) = self.following {
+ if ui.primary.sim.time() != time {
+ let maybe_pt = ui
.primary
.sim
- .get_canonical_pt_per_trip(trip, &ui.primary.map)
- {
+ .get_canonical_pt_per_trip(trip, &ui.primary.map);
+ if let Some(pt) = maybe_pt {
ctx.canvas.center_on_map_pt(pt);
} else {
// TODO ideally they wouldnt vanish for so long according to
- // get_canonical_point_for_trip
+ // get_canonical_pt_per_trip
println!("{} is gone... temporarily or not?", trip);
}
+ self.following = Some((trip, maybe_pt, ui.primary.sim.time()));
+ }
if menu.action("stop following agent") {
self.following = None;
}
