add blip to item

This commit is contained in:
Max Bradbury 2022-09-27 08:41:03 +01:00
parent b8c30fe873
commit 8148e3f725
1 changed files with 20 additions and 7 deletions

View File

@ -9,6 +9,7 @@ pub struct Item {
pub name: Option<String>,
pub dialogue_id: Option<String>,
pub colour_id: Option<u64>,
pub blip: Option<String>,
}
impl Item {
@ -24,7 +25,11 @@ impl Item {
optional_data_line("COL", self.colour_id.as_ref())
}
pub fn from_str(str: &str) -> Result<Item, crate::Error> {
fn blip_line(&self) -> String {
optional_data_line("BLIP", self.blip.as_ref())
}
pub fn from_str(str: &str) -> Result<(Item, Vec<crate::Error>), crate::Error> {
let mut lines: Vec<&str> = str.lines().collect();
if lines.is_empty() || !lines[0].starts_with("ITM ") {
@ -35,11 +40,16 @@ impl Item {
let mut name = None;
let mut dialogue_id = None;
let mut colour_id: Option<u64> = None;
let mut blip = None;
let mut warnings = Vec::new();
loop {
let last_line = lines.pop().unwrap();
if last_line.starts_with("NAME") {
if last_line.starts_with("BLIP") {
blip = Some(last_line.replace("BLIP ", "").to_string());
} else if last_line.starts_with("NAME") {
name = Some(last_line.replace("NAME ", "").to_string());
} else if last_line.starts_with("DLG") {
dialogue_id = Some(last_line.replace("DLG ", "").to_string());
@ -51,11 +61,13 @@ impl Item {
}
}
let animation_frames = animation_frames_from_str(
let (animation_frames, mut animation_warnings) = animation_frames_from_str(
&lines[1..].join("\n")
);
).unwrap();
Ok(Item { id, name, animation_frames, dialogue_id, colour_id })
warnings.append(&mut animation_warnings);
Ok((Item { id, name, animation_frames, dialogue_id, colour_id, blip }, warnings))
}
}
@ -63,12 +75,13 @@ impl fmt::Display for Item {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"ITM {}\n{}{}{}{}",
"ITM {}\n{}{}{}{}{}",
self.id,
self.animation_frames.to_string(),
self.name_line(),
self.dialogue_line(),
self.colour_line(),
self.blip_line(),
)
}
}
@ -79,7 +92,7 @@ mod test {
#[test]
fn item_from_string() {
let output = Item::from_str(include_str!("test-resources/item")).unwrap();
let (output, _err) = Item::from_str(include_str!("test-resources/item")).unwrap();
let expected = mock::item();
assert_eq!(output, expected);
}