recenter when loading a different map

This commit is contained in:
Dustin Carlino 2019-06-20 15:15:14 -07:00
parent 6738de2488
commit 1ce616b0be
2 changed files with 38 additions and 40 deletions

View File

@ -1,7 +1,6 @@
use crate::abtest::ABTestMode; use crate::abtest::ABTestMode;
use crate::debug::DebugMode; use crate::debug::DebugMode;
use crate::edit::EditMode; use crate::edit::EditMode;
use crate::helpers::ID;
use crate::mission::MissionEditMode; use crate::mission::MissionEditMode;
use crate::render::DrawOptions; use crate::render::DrawOptions;
use crate::sandbox::SandboxMode; use crate::sandbox::SandboxMode;
@ -11,7 +10,6 @@ use abstutil::elapsed_seconds;
use ezgui::{hotkey, Canvas, EventCtx, EventLoopMode, GfxCtx, Key, UserInput, Wizard, GUI}; use ezgui::{hotkey, Canvas, EventCtx, EventLoopMode, GfxCtx, Key, UserInput, Wizard, GUI};
use geom::{Duration, Line, Pt2D, Speed}; use geom::{Duration, Line, Pt2D, Speed};
use map_model::Map; use map_model::Map;
use rand::seq::SliceRandom;
use rand::Rng; use rand::Rng;
use rand_xorshift::XorShiftRng; use rand_xorshift::XorShiftRng;
use std::path::PathBuf; use std::path::PathBuf;
@ -42,32 +40,12 @@ impl GameState {
pub fn new(flags: Flags, ctx: &mut EventCtx) -> GameState { pub fn new(flags: Flags, ctx: &mut EventCtx) -> GameState {
let splash = !flags.no_splash let splash = !flags.no_splash
&& !format!("{}", flags.sim_flags.load.display()).contains("data/save"); && !format!("{}", flags.sim_flags.load.display()).contains("data/save");
let mut rng = flags.sim_flags.make_rng();
let mut game = GameState { let mut game = GameState {
mode: Mode::Sandbox(SandboxMode::new(ctx)), mode: Mode::Sandbox(SandboxMode::new(ctx)),
ui: UI::new(flags, ctx), ui: UI::new(flags, ctx, splash),
}; };
let rand_focus_pt = game
.ui
.primary
.map
.all_buildings()
.choose(&mut rng)
.and_then(|b| ID::Building(b.id).canonical_point(&game.ui.primary))
.or_else(|| {
game.ui
.primary
.map
.all_lanes()
.choose(&mut rng)
.and_then(|l| ID::Lane(l.id).canonical_point(&game.ui.primary))
})
.expect("Can't get canonical_point of a random building or lane");
if splash { if splash {
ctx.canvas.center_on_map_pt(rand_focus_pt); let mut rng = game.ui.primary.current_flags.sim_flags.make_rng();
game.mode = Mode::SplashScreen( game.mode = Mode::SplashScreen(
Wizard::new(), Wizard::new(),
Some(( Some((
@ -75,21 +53,7 @@ impl GameState {
rng, rng,
)), )),
); );
} else {
match abstutil::read_json::<EditorState>("../editor_state.json") {
Ok(ref loaded) if game.ui.primary.map.get_name() == &loaded.map_name => {
println!("Loaded previous editor_state.json");
ctx.canvas.cam_x = loaded.cam_x;
ctx.canvas.cam_y = loaded.cam_y;
ctx.canvas.cam_zoom = loaded.cam_zoom;
}
_ => {
println!("Couldn't load editor_state.json or it's for a different map, so just focusing on an arbitrary building");
ctx.canvas.center_on_map_pt(rand_focus_pt);
}
}
} }
game game
} }
@ -286,7 +250,7 @@ fn splash_screen(
// This retains no state, but that's probably fine. // This retains no state, but that's probably fine.
let mut flags = ui.primary.current_flags.clone(); let mut flags = ui.primary.current_flags.clone();
flags.sim_flags.load = PathBuf::from(format!("../data/maps/{}.bin", name)); flags.sim_flags.load = PathBuf::from(format!("../data/maps/{}.bin", name));
*ui = UI::new(flags, ctx); *ui = UI::new(flags, ctx, false);
break Some(Mode::Sandbox(SandboxMode::new(ctx))); break Some(Mode::Sandbox(SandboxMode::new(ctx)));
} else if wizard.aborted() { } else if wizard.aborted() {
break Some(Mode::SplashScreen(Wizard::new(), maybe_screensaver.take())); break Some(Mode::SplashScreen(Wizard::new(), maybe_screensaver.take()));

View File

@ -8,6 +8,7 @@ use abstutil::{MeasureMemory, Timer};
use ezgui::{Color, EventCtx, GeomBatch, GfxCtx, Prerender}; use ezgui::{Color, EventCtx, GeomBatch, GfxCtx, Prerender};
use geom::{Bounds, Circle, Distance, Duration}; use geom::{Bounds, Circle, Distance, Duration};
use map_model::{Map, Traversable}; use map_model::{Map, Traversable};
use rand::seq::SliceRandom;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use sim::{GetDrawAgents, Sim, SimFlags}; use sim::{GetDrawAgents, Sim, SimFlags};
use structopt::StructOpt; use structopt::StructOpt;
@ -19,11 +20,44 @@ pub struct UI {
} }
impl UI { impl UI {
pub fn new(flags: Flags, ctx: &mut EventCtx) -> UI { pub fn new(flags: Flags, ctx: &mut EventCtx, splash: bool) -> UI {
let cs = ColorScheme::load().unwrap(); let cs = ColorScheme::load().unwrap();
let primary = ctx.loading_screen("load map", |ctx, mut timer| { let primary = ctx.loading_screen("load map", |ctx, mut timer| {
PerMapUI::new(flags, &cs, ctx, &mut timer) PerMapUI::new(flags, &cs, ctx, &mut timer)
}); });
let mut rng = primary.current_flags.sim_flags.make_rng();
let rand_focus_pt = primary
.map
.all_buildings()
.choose(&mut rng)
.and_then(|b| ID::Building(b.id).canonical_point(&primary))
.or_else(|| {
primary
.map
.all_lanes()
.choose(&mut rng)
.and_then(|l| ID::Lane(l.id).canonical_point(&primary))
})
.expect("Can't get canonical_point of a random building or lane");
if splash {
ctx.canvas.center_on_map_pt(rand_focus_pt);
} else {
match abstutil::read_json::<EditorState>("../editor_state.json") {
Ok(ref loaded) if primary.map.get_name() == &loaded.map_name => {
println!("Loaded previous editor_state.json");
ctx.canvas.cam_x = loaded.cam_x;
ctx.canvas.cam_y = loaded.cam_y;
ctx.canvas.cam_zoom = loaded.cam_zoom;
}
_ => {
println!("Couldn't load editor_state.json or it's for a different map, so just focusing on an arbitrary building");
ctx.canvas.center_on_map_pt(rand_focus_pt);
}
}
}
UI { primary, cs } UI { primary, cs }
} }