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
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use abstutil::Timer;
use widgetry::{Color, EventCtx};
use crate::levels::Level;
use crate::music::Music;
#[derive(Serialize, Deserialize)]
pub struct Session {
pub levels: Vec<Level>,
pub colors: ColorScheme,
pub high_scores: HashMap<String, Vec<usize>>,
pub levels_unlocked: usize,
pub current_vehicle: String,
pub vehicles_unlocked: Vec<String>,
pub upzones_unlocked: usize,
#[serde(skip_serializing, skip_deserializing)]
pub music: Music,
pub play_music: bool,
}
#[derive(Serialize, Deserialize)]
pub struct ColorScheme {
pub house: Color,
pub apartment: Color,
pub store: Color,
pub visited: Color,
pub score: Color,
pub energy: Color,
pub boost: Color,
}
impl Session {
pub fn load() -> Session {
let levels = Level::all();
if let Ok(session) = abstutil::maybe_read_json::<Session>(
abstutil::path_player("santa.json"),
&mut Timer::throwaway(),
) {
if session.levels == levels {
return session;
}
warn!("Loaded session data, but the levels have changed, so discarding!");
}
let mut high_scores = HashMap::new();
for level in &levels {
high_scores.insert(level.title.clone(), Vec::new());
}
Session {
levels,
colors: ColorScheme {
house: Color::hex("#688865"),
apartment: Color::hex("#C0F879"),
store: Color::hex("#F4DF4D"),
visited: Color::BLACK,
score: Color::hex("#83AA51"),
energy: Color::hex("#D8B830"),
boost: Color::hex("#A32015"),
},
high_scores,
levels_unlocked: 1,
current_vehicle: "sleigh".to_string(),
vehicles_unlocked: vec!["sleigh".to_string()],
upzones_unlocked: 0,
music: Music::empty(),
play_music: true,
}
}
pub fn record_score(&mut self, level: String, score: usize) -> Option<Vec<String>> {
let scores = self.high_scores.get_mut(&level).unwrap();
scores.push(score);
scores.sort();
scores.reverse();
scores.truncate(3);
let idx = self
.levels
.iter()
.position(|lvl| lvl.title == level)
.unwrap();
let level = &self.levels[idx];
let msg = if idx + 1 == self.levels_unlocked && score >= level.goal {
if idx + 1 == self.levels.len() {
Some(vec![
format!("All levels complete! Nice."),
format!("Can you improve your score on other levels?"),
])
} else {
self.levels_unlocked += 1;
let mut messages = vec![format!("New level unlocked!")];
if level.unlock_upzones > 0 {
self.upzones_unlocked += level.unlock_upzones;
messages.push(format!(
"Unlocked the ability to upzone {} buildings",
level.unlock_upzones
));
}
for x in &level.unlock_vehicles {
self.vehicles_unlocked.push(x.clone());
messages.push(format!("Unlocked the {}", x));
}
Some(messages)
}
} else {
None
};
abstutil::write_json(abstutil::path_player("santa.json"), self);
msg
}
pub fn unlock_all(&mut self) {
for level in &self.levels {
self.vehicles_unlocked.extend(level.unlock_vehicles.clone());
self.upzones_unlocked += level.unlock_upzones;
}
self.levels_unlocked = self.levels.len();
}
pub fn update_music(&mut self, ctx: &mut EventCtx) {
let play_music = self.play_music;
self.music.event(ctx, &mut self.play_music);
if play_music != self.play_music {
abstutil::write_json(abstutil::path_player("santa.json"), self);
}
}
}