speed up agent following by not recalculating where agent is on every single event

This commit is contained in:
Dustin Carlino 2019-07-07 20:36:41 -07:00
parent ff0a6b7240
commit e9c27b4639

View File

@ -1,10 +1,11 @@
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,22 +34,31 @@ 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
.primary
.sim
.get_canonical_pt_per_trip(trip, &ui.primary.map)
{
ctx.canvas.center_on_map_pt(pt);
} else {
// TODO ideally they wouldnt vanish for so long according to
// get_canonical_point_for_trip
println!("{} is gone... temporarily or not?", trip);
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);
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_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;