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)] #[derive(Debug, Eq, PartialEq)]
pub struct Dialogue { pub struct Dialogue {
pub id: String, pub id: String,
pub contents: String, pub contents: String,
pub name: Option<String>,
} }
impl From<String> for Dialogue { impl From<String> for Dialogue {
#[inline] #[inline]
fn from(string: String) -> Dialogue { 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 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"); let contents = lines[1..].join("\n");
Dialogue { id, contents } Dialogue { id, contents, name }
} }
} }
impl ToString for Dialogue { impl ToString for Dialogue {
#[inline] #[inline]
fn to_string(&self) -> String { 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] #[test]
fn test_dialogue_from_string() { fn test_dialogue_from_string() {
assert_eq!( let output = Dialogue::from(
Dialogue::from("DLG h\nhello\ngoodbye".to_string()), "DLG h\nhello\nNAME not a dialogue name\nNAME a dialogue name".to_string()
Dialogue { id: "h".to_string(), contents: "hello\ngoodbye".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] #[test]
fn test_dialogue_to_string() { fn test_dialogue_to_string() {
let output = Dialogue { let output = Dialogue {
id: "y".to_string(), 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(); }.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); assert_eq!(output, expected);
} }
} }

View File

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