1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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)
}
}