animate pedestrian feet smoothly regardless of sim speed

This commit is contained in:
Dustin Carlino 2019-05-08 18:24:57 -07:00
parent 60eea9b413
commit fce5d0f6dc
5 changed files with 21 additions and 5 deletions

View File

@ -1,7 +1,7 @@
use crate::helpers::{ColorScheme, ID};
use crate::render::{should_draw_blinkers, DrawCtx, DrawOptions, Renderable};
use ezgui::{Color, Drawable, GfxCtx, Prerender};
use geom::{Circle, Distance, Duration, PolyLine, Polygon};
use geom::{Circle, Distance, PolyLine, Polygon};
use map_model::{Map, LANE_THICKNESS};
use sim::{DrawPedestrianInput, PedestrianID};
@ -17,7 +17,7 @@ pub struct DrawPedestrian {
impl DrawPedestrian {
pub fn new(
input: DrawPedestrianInput,
time: Duration,
step_count: usize,
map: &Map,
prerender: &Prerender,
cs: &ColorScheme,
@ -59,11 +59,12 @@ impl DrawPedestrian {
);
let foot_color = cs.get_def("pedestrian foot", Color::BLACK);
// Jitter based on ID so we don't all walk synchronized.
let remainder = if input.id.0 % 2 == 0 { 0.0 } else { 1.0 };
let jitter = input.id.0 % 2 == 0;
let remainder = step_count % 6;
if input.waiting_for_turn.is_some() {
draw_default.push((foot_color, left_foot.to_polygon()));
draw_default.push((foot_color, right_foot.to_polygon()));
} else if time.inner_seconds() * 10.0 % 2.0 == remainder {
} else if (jitter && remainder < 3) || (!jitter && remainder >= 3) {
draw_default.push((foot_color, left_foot.to_polygon()));
draw_default.push((
foot_color,

View File

@ -124,6 +124,10 @@ impl GetDrawAgents for TimeTravel {
self.state_per_time[self.current_idx.unwrap()].time
}
fn step_count(&self) -> usize {
self.current_idx.unwrap()
}
fn get_draw_car(&self, id: CarID, _map: &Map) -> Option<DrawCarInput> {
self.get_current_state().cars.get(&id).cloned()
}

View File

@ -285,6 +285,7 @@ impl UI {
// Expand all of the Traversables into agents, populating the cache if needed.
{
let time = source.time();
let step_count = source.step_count();
for on in &agents_on {
if !agents.has(time, *on) {
@ -294,7 +295,7 @@ impl UI {
}
for p in source.get_draw_peds(*on, map).into_iter() {
list.push(Box::new(DrawPedestrian::new(
p, time, map, prerender, &self.cs,
p, step_count, map, prerender, &self.cs,
)));
}
agents.put(time, *on, list);

View File

@ -39,6 +39,8 @@ pub enum CarStatus {
// otherwise? except we don't know what to calculate. maybe cache it?
pub trait GetDrawAgents {
fn time(&self) -> Duration;
// Every time the time changes, this should increase. For smoothly animating stuff.
fn step_count(&self) -> usize;
fn get_draw_car(&self, id: CarID, map: &Map) -> Option<DrawCarInput>;
fn get_draw_ped(&self, id: PedestrianID, map: &Map) -> Option<DrawPedestrianInput>;
fn get_draw_cars(&self, on: Traversable, map: &Map) -> Vec<DrawCarInput>;

View File

@ -39,6 +39,8 @@ pub struct Sim {
// Some tests deliberately set different scenario names for comparisons.
#[derivative(PartialEq = "ignore")]
run_name: String,
#[derivative(PartialEq = "ignore")]
step_count: usize,
// Lazily computed.
#[derivative(PartialEq = "ignore")]
@ -75,6 +77,7 @@ impl Sim {
// TODO
edits_name: "no_edits".to_string(),
run_name,
step_count: 0,
stats: None,
events_since_last_step: Vec::new(),
}
@ -232,6 +235,10 @@ impl GetDrawAgents for Sim {
self.time
}
fn step_count(&self) -> usize {
self.step_count
}
fn get_draw_car(&self, id: CarID, map: &Map) -> Option<DrawCarInput> {
// TODO Faster
self.get_all_draw_cars(map).into_iter().find(|d| d.id == id)
@ -277,6 +284,7 @@ impl Sim {
// Running
impl Sim {
pub fn step(&mut self, map: &Map, dt: Duration) {
self.step_count += 1;
if !self.spawner.is_done() {
panic!("Forgot to call spawn_all_trips");
}