use abstutil::prettyprint_usize;
use geom::{Distance, PolyLine, Polygon, Pt2D};
use map_gui::tools::{ColorLegend, PopupMsg};
use widgetry::{
Color, Drawable, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Key, Line, Panel,
SimpleState, State, Text, VerticalAlignment, Widget,
};
use crate::buildings::{BldgState, Buildings};
use crate::levels::Level;
use crate::title::TitleScreen;
use crate::{App, Transition};
const ZOOM: f64 = 2.0;
pub struct Strategize {
unlock_messages: Option<Vec<String>>,
draw_all: Drawable,
}
impl Strategize {
pub fn new_state(
ctx: &mut EventCtx,
app: &mut App,
score: usize,
level: &Level,
bldgs: &Buildings,
path: RecordPath,
) -> Box<dyn State<App>> {
ctx.canvas.cam_zoom = ZOOM;
let start = app
.map
.get_i(app.map.find_i_by_osm_id(level.start).unwrap())
.polygon
.center();
ctx.canvas.center_on_map_pt(start);
let unlock_messages = app.session.record_score(level.title.clone(), score);
let mut txt = Text::new();
txt.add_line(Line(format!("Results for {}", level.title)).small_heading());
txt.add_line(format!(
"You delivered {} presents",
prettyprint_usize(score)
));
txt.add_line("");
txt.add_line("High scores:");
for (idx, score) in app.session.high_scores[&level.title].iter().enumerate() {
txt.add_line(format!("{}) {}", idx + 1, prettyprint_usize(*score)));
}
let mut batch = GeomBatch::new();
for b in app.map.all_buildings() {
match bldgs.buildings[&b.id] {
BldgState::Undelivered(num_housing_units) => {
batch.push(
if num_housing_units > 5 {
app.session.colors.apartment
} else {
app.session.colors.house
},
b.polygon.clone(),
);
if num_housing_units > 1 {
batch.append(
Text::from(Line(num_housing_units.to_string()).fg(Color::RED))
.render_autocropped(ctx)
.scale(0.2)
.centered_on(b.label_center),
);
}
}
BldgState::Store => {
batch.push(
if bldgs.upzones.contains(&b.id) {
Color::PINK
} else {
app.session.colors.store
},
b.polygon.clone(),
);
}
BldgState::Done => {
batch.push(Color::RED, b.polygon.clone());
}
BldgState::Ignore => {
batch.push(app.session.colors.visited, b.polygon.clone());
}
}
}
batch.push(Color::CYAN, path.render(Distance::meters(2.0)));
let panel = Panel::new_builder(Widget::col(vec![
txt.into_widget(ctx),
ctx.style()
.btn_outline
.text("Back to title screen")
.hotkey(Key::Enter)
.build_def(ctx),
Widget::row(vec![
ColorLegend::row(ctx, app.session.colors.house, "house"),
ColorLegend::row(ctx, app.session.colors.apartment, "apartment"),
ColorLegend::row(ctx, app.session.colors.store, "store"),
]),
Widget::row(vec![
ColorLegend::row(ctx, Color::PINK, "upzoned store"),
ColorLegend::row(ctx, Color::RED, "delivered!"),
])
.evenly_spaced(),
]))
.aligned(HorizontalAlignment::Right, VerticalAlignment::Top)
.build(ctx);
<dyn SimpleState<_>>::new_state(
panel,
Box::new(Strategize {
unlock_messages,
draw_all: ctx.upload(batch),
}),
)
}
}
impl SimpleState<App> for Strategize {
fn on_click(&mut self, ctx: &mut EventCtx, app: &mut App, x: &str, _: &Panel) -> Transition {
match x {
"Back to title screen" => {
let mut transitions = vec![
Transition::Pop,
Transition::Replace(TitleScreen::new_state(ctx, app)),
];
if let Some(msgs) = self.unlock_messages.take() {
transitions.push(Transition::Push(PopupMsg::new_state(
ctx,
"Level complete!",
msgs,
)));
}
Transition::Multi(transitions)
}
_ => unreachable!(),
}
}
fn other_event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
ctx.canvas_movement();
app.session.update_music(ctx);
Transition::Keep
}
fn draw(&self, g: &mut GfxCtx, app: &App) {
g.redraw(&self.draw_all);
app.session.music.draw(g);
}
}
pub struct Results;
impl Results {
pub fn new_state(
ctx: &mut EventCtx,
app: &mut App,
score: usize,
level: &Level,
) -> Box<dyn State<App>> {
let mut txt = Text::new();
if score < level.goal {
txt.add_line(Line("Not quite...").small_heading());
txt.add_line(format!(
"You only delivered {} / {} presents",
prettyprint_usize(score),
prettyprint_usize(level.goal)
));
txt.add_line("Review your route and try again.");
txt.add_line("");
txt.add_line("Hint: look for any apartments you missed!");
} else {
txt.add_line(Line("Thank you, Santa!").small_heading());
txt.add_line(format!(
"You delivered {} presents, more than the goal of {}!",
prettyprint_usize(score),
prettyprint_usize(level.goal)
));
let high_score = app.session.high_scores[&level.title][0];
if high_score == score {
txt.add_line("Wow, a new high score!");
} else {
txt.add_line(format!(
"But can you beat the high score of {}?",
prettyprint_usize(high_score)
));
}
}
<dyn SimpleState<_>>::new_state(
Panel::new_builder(Widget::col(vec![
txt.into_widget(ctx),
ctx.style()
.btn_solid_primary
.text("OK")
.hotkey(Key::Enter)
.build_def(ctx),
]))
.build(ctx),
Box::new(Results),
)
}
}
impl SimpleState<App> for Results {
fn on_click(&mut self, _: &mut EventCtx, _: &mut App, x: &str, _: &Panel) -> Transition {
match x {
"OK" => Transition::Pop,
_ => unreachable!(),
}
}
fn other_event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
app.session.update_music(ctx);
Transition::Keep
}
fn draw(&self, g: &mut GfxCtx, app: &App) {
app.session.music.draw(g);
}
}
pub struct RecordPath {
pts: Vec<Pt2D>,
}
impl RecordPath {
pub fn new() -> RecordPath {
RecordPath { pts: Vec::new() }
}
pub fn add_pt(&mut self, pt: Pt2D) {
let len = self.pts.len();
if len >= 2 {
let same_line = self.pts[len - 2]
.angle_to(self.pts[len - 1])
.approx_eq(self.pts[len - 1].angle_to(pt), 0.1);
if same_line {
self.pts.pop();
}
}
self.pts.push(pt);
}
pub fn render(mut self, thickness: Distance) -> Polygon {
self.pts.dedup();
PolyLine::unchecked_new(self.pts).make_polygons(thickness)
}
}