add dialogue name for 7.0

This commit is contained in:
Max Bradbury 2020-04-30 19:44:20 +01:00
parent ef80fc27db
commit ef310d6fb7
2 changed files with 36 additions and 9 deletions

View File

@ -1,24 +1,39 @@
use crate::optional_data_line;
#[derive(Debug, Eq, PartialEq)]
pub struct Dialogue {
pub id: String,
pub contents: String,
pub name: Option<String>,
}
impl From<String> for Dialogue {
#[inline]
fn from(string: String) -> Dialogue {
let lines: Vec<&str> = string.lines().collect();
let mut lines: Vec<&str> = string.lines().collect();
let id = lines[0].replace("DLG ", "").to_string();
let name = if lines.last().unwrap().starts_with("NAME ") {
Some(lines.pop().unwrap().replace("NAME ", ""))
} else {
None
};
let contents = lines[1..].join("\n");
Dialogue { id, contents }
Dialogue { id, contents, name }
}
}
impl ToString for Dialogue {
#[inline]
fn to_string(&self) -> String {
format!("DLG {}\n{}", self.id, self.contents)
format!(
"DLG {}\n{}{}",
self.id,
self.contents,
optional_data_line("NAME", self.name.as_ref())
)
}
}
@ -28,19 +43,29 @@ mod test {
#[test]
fn test_dialogue_from_string() {
assert_eq!(
Dialogue::from("DLG h\nhello\ngoodbye".to_string()),
Dialogue { id: "h".to_string(), contents: "hello\ngoodbye".to_string()}
)
let output = Dialogue::from(
"DLG h\nhello\nNAME not a dialogue name\nNAME a dialogue name".to_string()
);
let expected = Dialogue {
id: "h".to_string(),
contents: "hello\nNAME not a dialogue name".to_string(),
name: Some("a dialogue name".to_string())
};
assert_eq!(output, expected);
}
#[test]
fn test_dialogue_to_string() {
let output = Dialogue {
id: "y".to_string(),
contents: "This is a bit of dialogue,\nblah blah\nblah blah".to_string()
contents: "This is a bit of dialogue,\nblah blah\nblah blah".to_string(),
name: Some("a dialogue name".to_string())
}.to_string();
let expected = "DLG y\nThis is a bit of dialogue,\nblah blah\nblah blah".to_string();
let expected = "DLG y\nThis is a bit of dialogue,\nblah blah\nblah blah\nNAME a dialogue name".to_string();
assert_eq!(output, expected);
}
}

View File

@ -816,10 +816,12 @@ pub fn game_default() -> Game {
Dialogue {
id: "SPR_0".to_string(),
contents: "I'm a cat".to_string(),
name: None
},
Dialogue {
id: "ITM_0".to_string(),
contents: "You found a nice warm cup of tea".to_string(),
name: None
},
],
endings: vec![],