mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-12-25 07:25:47 +03:00
start animating the lines
This commit is contained in:
parent
282a4a6712
commit
daac00155e
@ -6,6 +6,7 @@ extern crate serde_json;
|
||||
mod abst_multimap;
|
||||
mod clone;
|
||||
mod io;
|
||||
mod time;
|
||||
|
||||
pub use abst_multimap::MultiMap;
|
||||
pub use clone::Cloneable;
|
||||
@ -13,3 +14,4 @@ pub use io::{
|
||||
deserialize_btreemap, deserialize_multimap, list_all_objects, load_all_objects, read_binary,
|
||||
read_json, serialize_btreemap, serialize_multimap, to_json, write_binary, write_json,
|
||||
};
|
||||
pub use time::elapsed_seconds;
|
||||
|
6
abstutil/src/time.rs
Normal file
6
abstutil/src/time.rs
Normal file
@ -0,0 +1,6 @@
|
||||
use std::time::Instant;
|
||||
|
||||
pub fn elapsed_seconds(since: Instant) -> f64 {
|
||||
let dt = since.elapsed();
|
||||
(dt.as_secs() as f64) + (f64::from(dt.subsec_nanos()) * 1e-9)
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
// Copyright 2018 Google LLC, licensed under http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
use abstutil::elapsed_seconds;
|
||||
use control::ControlMap;
|
||||
use ezgui::{Canvas, EventLoopMode, GfxCtx, Text, UserInput, TOP_RIGHT};
|
||||
use map_model::Map;
|
||||
@ -100,8 +101,7 @@ impl SimController {
|
||||
if input.is_update_event() {
|
||||
if let Some(tick) = self.last_step {
|
||||
// TODO https://gafferongames.com/post/fix_your_timestep/
|
||||
let dt = tick.elapsed();
|
||||
let dt_s = dt.as_secs() as f64 + f64::from(dt.subsec_nanos()) * 1e-9;
|
||||
let dt_s = elapsed_seconds(tick);
|
||||
if dt_s >= TIMESTEP.value_unsafe / self.desired_speed {
|
||||
sim.step(map, control_map);
|
||||
self.last_step = Some(Instant::now());
|
||||
|
@ -4,6 +4,7 @@ version = "0.1.0"
|
||||
authors = ["Dustin Carlino <dabreegster@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
abstutil = { path = "../abstutil" }
|
||||
csv = "1.0.1"
|
||||
failure = "0.1.2"
|
||||
geom = { path = "../geom" }
|
||||
|
@ -1,3 +1,4 @@
|
||||
extern crate abstutil;
|
||||
extern crate csv;
|
||||
extern crate failure;
|
||||
extern crate geom;
|
||||
@ -6,6 +7,7 @@ extern crate serde;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
use abstutil::elapsed_seconds;
|
||||
use failure::Error;
|
||||
use geom::LonLat;
|
||||
use itertools::Itertools;
|
||||
@ -70,8 +72,6 @@ pub fn load(dir_path: &str) -> Result<Vec<Route>, Error> {
|
||||
});
|
||||
}
|
||||
|
||||
let elapsed = timer.elapsed();
|
||||
let dt = elapsed.as_secs() as f64 + f64::from(elapsed.subsec_nanos()) * 1e-9;
|
||||
println!("Loading GTFS took {}s", dt);
|
||||
println!("Loading GTFS took {}s", elapsed_seconds(timer));
|
||||
Ok(results)
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ version = "0.1.0"
|
||||
authors = ["Dustin Carlino <dabreegster@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
abstutil = { path = "../abstutil" }
|
||||
ezgui = { path = "../ezgui" }
|
||||
geom = { path = "../geom" }
|
||||
map_model = { path = "../map_model" }
|
||||
|
@ -1,3 +1,4 @@
|
||||
extern crate abstutil;
|
||||
extern crate ezgui;
|
||||
extern crate geom;
|
||||
extern crate map_model;
|
||||
@ -6,6 +7,7 @@ extern crate piston;
|
||||
extern crate structopt;
|
||||
|
||||
mod render;
|
||||
mod timer;
|
||||
|
||||
use ezgui::{Canvas, EventLoopMode, GfxCtx, Text, UserInput, GUI};
|
||||
use map_model::{Map, RoadEdits};
|
||||
@ -13,6 +15,7 @@ use piston::input::Key;
|
||||
use render::DrawMap;
|
||||
use std::process;
|
||||
use structopt::StructOpt;
|
||||
use timer::Cycler;
|
||||
|
||||
#[derive(StructOpt)]
|
||||
#[structopt(name = "halloween")]
|
||||
@ -23,10 +26,12 @@ struct Flags {
|
||||
}
|
||||
|
||||
const KEY_CATEGORY: &str = "";
|
||||
const ANIMATION_PERIOD_S: f64 = 2.0;
|
||||
|
||||
struct UI {
|
||||
canvas: Canvas,
|
||||
draw_map: DrawMap,
|
||||
cycler: Cycler,
|
||||
}
|
||||
|
||||
impl UI {
|
||||
@ -35,6 +40,7 @@ impl UI {
|
||||
UI {
|
||||
canvas: Canvas::new(),
|
||||
draw_map: DrawMap::new(map),
|
||||
cycler: Cycler::new(ANIMATION_PERIOD_S),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -44,10 +50,8 @@ impl GUI for UI {
|
||||
if input.unimportant_key_pressed(Key::Escape, KEY_CATEGORY, "quit") {
|
||||
process::exit(0);
|
||||
}
|
||||
|
||||
self.canvas.handle_event(&mut input);
|
||||
|
||||
EventLoopMode::InputOnly
|
||||
EventLoopMode::Animation
|
||||
}
|
||||
|
||||
fn get_mut_canvas(&mut self) -> &mut Canvas {
|
||||
@ -55,7 +59,7 @@ impl GUI for UI {
|
||||
}
|
||||
|
||||
fn draw(&self, g: &mut GfxCtx, _osd: Text) {
|
||||
self.draw_map.draw(g);
|
||||
self.draw_map.draw(g, self.cycler.value());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
use ezgui::GfxCtx;
|
||||
use geom::{Line, Polygon};
|
||||
use geom::{Line, Polygon, Pt2D};
|
||||
use map_model::{Building, Map, Road, LANE_THICKNESS};
|
||||
|
||||
const WHITE: [f32; 4] = [1.0, 1.0, 1.0, 1.0];
|
||||
@ -26,14 +26,14 @@ impl DrawMap {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(&self, g: &mut GfxCtx) {
|
||||
pub fn draw(&self, g: &mut GfxCtx, timer: f64) {
|
||||
g.clear(WHITE);
|
||||
// TODO no pruning yet
|
||||
for r in &self.roads {
|
||||
r.draw(g);
|
||||
}
|
||||
for b in &self.buildings {
|
||||
b.draw(g);
|
||||
b.draw(g, timer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -60,6 +60,7 @@ impl DrawRoad {
|
||||
|
||||
struct DrawBuilding {
|
||||
polygon: Polygon,
|
||||
// pt1 is fixed, to the road
|
||||
line: Line,
|
||||
}
|
||||
|
||||
@ -67,12 +68,20 @@ impl DrawBuilding {
|
||||
fn new(b: &Building) -> DrawBuilding {
|
||||
DrawBuilding {
|
||||
polygon: Polygon::new(&b.points),
|
||||
line: b.front_path.line.clone(),
|
||||
line: b.front_path.line.reverse(),
|
||||
}
|
||||
}
|
||||
|
||||
fn draw(&self, g: &mut GfxCtx) {
|
||||
fn draw(&self, g: &mut GfxCtx, timer: f64) {
|
||||
g.draw_polygon(RED, &self.polygon);
|
||||
g.draw_rounded_line(BLUE, LINE_WIDTH, &self.line);
|
||||
let percent = timer;
|
||||
let new_line = Line::new(
|
||||
self.line.pt1(),
|
||||
Pt2D::new(
|
||||
self.line.pt1().x() + percent * (self.line.pt2().x() - self.line.pt1().x()),
|
||||
self.line.pt1().y() + percent * (self.line.pt2().y() - self.line.pt1().y()),
|
||||
),
|
||||
);
|
||||
g.draw_rounded_line(BLUE, LINE_WIDTH, &new_line);
|
||||
}
|
||||
}
|
||||
|
27
halloween/src/timer.rs
Normal file
27
halloween/src/timer.rs
Normal file
@ -0,0 +1,27 @@
|
||||
use abstutil::elapsed_seconds;
|
||||
use std::time::Instant;
|
||||
|
||||
pub struct Cycler {
|
||||
start: Instant,
|
||||
period_s: f64,
|
||||
}
|
||||
|
||||
impl Cycler {
|
||||
pub fn new(period_s: f64) -> Cycler {
|
||||
Cycler {
|
||||
start: Instant::now(),
|
||||
period_s,
|
||||
}
|
||||
}
|
||||
|
||||
// Returns [0.0, 1.0], going up from 0 to 1 and back down to 0 over the specified period.
|
||||
pub fn value(&self) -> f64 {
|
||||
let result: f64 = (elapsed_seconds(self.start) % self.period_s) / self.period_s;
|
||||
assert!(result >= 0.0 && result < 1.0);
|
||||
if result >= 0.5 {
|
||||
1.0 - result
|
||||
} else {
|
||||
result * 2.0
|
||||
}
|
||||
}
|
||||
}
|
@ -299,7 +299,7 @@ impl Sim {
|
||||
|
||||
pub fn measure_speed(&self, b: &mut Benchmark) -> f64 {
|
||||
let elapsed = b.last_real_time.elapsed();
|
||||
let dt = (elapsed.as_secs() as f64 + f64::from(elapsed.subsec_nanos()) * 1e-9) * si::S;
|
||||
let dt = abstutil::elapsed_seconds(b.last_real_time) * si::S;
|
||||
let speed = (self.time - b.last_sim_time).as_time() / dt;
|
||||
b.last_real_time = Instant::now();
|
||||
b.last_sim_time = self.time;
|
||||
|
@ -1,3 +1,4 @@
|
||||
use abstutil::elapsed_seconds;
|
||||
use driving::DrivingSimState;
|
||||
use geom::Polygon;
|
||||
use kinematics::Vehicle;
|
||||
@ -437,8 +438,10 @@ fn calculate_paths(
|
||||
.map(|(start, goal)| Pathfinder::shortest_distance(map, *start, *goal))
|
||||
.collect();
|
||||
|
||||
let elapsed = timer.elapsed();
|
||||
let dt = elapsed.as_secs() as f64 + f64::from(elapsed.subsec_nanos()) * 1e-9;
|
||||
debug!("Calculating {} paths took {}s", paths.len(), dt);
|
||||
debug!(
|
||||
"Calculating {} paths took {}s",
|
||||
paths.len(),
|
||||
elapsed_seconds(timer)
|
||||
);
|
||||
paths
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user