(
&mut self,
lines: &[(usize, String)],
block_index: &mut usize,
)
| 151 | |
| 152 | for (index, (line_num, line)) in lines.iter().enumerate() { |
| 153 | if let Some(line) = line.strip_prefix('@') { |
| 154 | if let Some((cmd, arg)) = line.split_once(' ') { |
| 155 | let cmd = match cmd { |
| 156 | "bg" | "cg" => { |
| 157 | let mut parts = arg.split('|').map(str::trim); |
| 158 | let bg = Background { |
| 159 | name: parts.next().unwrap_or("").to_string(), |
| 160 | x_offset: parts.next().and_then(|s| s.parse::<f32>().ok()), |
| 161 | y_offset: parts.next().and_then(|s| s.parse::<f32>().ok()), |
| 162 | zoom: parts.next().and_then(|s| s.parse::<f32>().ok()), |
| 163 | is_cg: cmd == "cg", |
| 164 | }; |
| 165 | self.script.insert_background(self.block_index, bg.clone()); |
| 166 | bg |
| 167 | } |
| 168 | "bgm" => { |
| 169 | self.script.insert_bgm(self.block_index, arg.to_string()); |
| 170 | PlayBgm(arg.to_string()) |
| 171 | } |
| 172 | "choose" => { |
| 173 | let num = arg.parse::<usize>().map_err(ScriptError::from)?; |
| 174 | let mut choices_branch = HashMap::with_capacity(num); |
| 175 | let explain = lines[index + 1].1.clone(); |
| 176 | for (i, line) in lines.iter().take(index + num + 1 + 1).skip(index + 2) |
| 177 | { |
| 178 | if let Some((choice, script)) = line.split_once(' ') { |
| 179 | let (choice, label) = match script.split_once(":") { |
| 180 | Some((name, label)) |
| 181 | if !name.is_empty() && !label.is_empty() => |
| 182 | { |
| 183 | ( |
| 184 | choice.to_string(), |
| 185 | (name.to_string(), label.to_string()), |
| 186 | ) |
| 187 | } |
| 188 | Some((name, "")) if !name.is_empty() => ( |
| 189 | choice.to_string(), |
| 190 | (name.to_string(), "start".to_string()), |
| 191 | ), |
| 192 | Some(("", label)) => ( |
| 193 | choice.to_string(), |
| 194 | (self.script.name().to_string(), label.to_string()), |
| 195 | ), |
| 196 | None => ( |
| 197 | choice.to_string(), |
| 198 | (script.to_string(), "start".to_string()), |
| 199 | ), |
| 200 | _ => unreachable!(), |
| 201 | }; |
| 202 | choices_branch.insert(choice.clone(), label.clone()); |
| 203 | } else { |
| 204 | return Err(EngineError::from(ScriptError::Choice(format!( |
| 205 | "Invalid choice at line {i}: {line}" |
| 206 | )))); |
| 207 | } |
| 208 | } |
| 209 | self.script |
| 210 | .insert_choice(self.block_index, choices_branch.clone()); |
no test coverage detected