diff --git a/src/bin/trees/main.rs b/src/bin/trees/main.rs index 41e658b..d108963 100644 --- a/src/bin/trees/main.rs +++ b/src/bin/trees/main.rs @@ -313,11 +313,11 @@ fn dialog_engine( // Fetch this monologue from the assets if let Some(monologue) = monologues.get(handle.clone().id()) { // Fetch this batch of options - if let Some(options) = monologue.get(*idx) { + if let Some(batch) = monologue.get(*idx) { // Spawn the dialog options in the dialog box - options.iter().for_each(|option| { + batch.lines.iter().for_each(|line| { parent.spawn(( - Text::new(option.clone()), + Text::new(line.clone()), DialogOption, TextLayout::new(JustifyText::Left, LineBreak::NoWrap), )); @@ -615,9 +615,9 @@ fn preview_monologue( // Spawn the monologue let mut i = 0; commands.entity(*container).with_children(|parent| { - while let Some(options) = monologue.get(i) { + while let Some(batch) = monologue.get(i) { parent.spawn((Text::new("---"), MonologuePreview)); - for (n, item) in options.iter().enumerate() { + for (n, item) in batch.lines.iter().enumerate() { parent.spawn((Text::new(format!("{i}.{n}: {item}")), MonologuePreview)); } // TODO: Just implement iter_batches or something diff --git a/src/bin/trees/mono.rs b/src/bin/trees/mono.rs index 9859235..fd7c45e 100644 --- a/src/bin/trees/mono.rs +++ b/src/bin/trees/mono.rs @@ -1,13 +1,64 @@ use super::*; +/// A monologue containing a list of optional lines #[derive(Asset, TypePath, Debug, Deserialize, Default, Clone)] pub(crate) struct Monologue { - value: Vec>, + pub batches: Vec, } impl Monologue { - pub fn get(&self, idx: usize) -> Option<&Vec> { - self.value.get(idx) + pub fn get(&self, idx: usize) -> Option<&MonologueLineBatch> { + self.batches.get(idx) + } + + pub fn add_batch(&mut self, group: MonologueLineBatch) { + self.batches.push(group); + } +} + +/// A set of possible lines in a monologue +#[derive(Debug, Deserialize, Default, Clone)] +pub(crate) struct MonologueLineBatch { + pub lines: Vec, +} + +impl MonologueLineBatch { + fn add_line(&mut self, line: MonologueLine) { + self.lines.push(line); + } +} + +/// A single monologue line +#[derive(Debug, Deserialize, Default, Clone)] +pub(crate) struct MonologueLine { + pub value: String, +} + +impl Display for MonologueLine { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{}", self.value) + } +} + +impl From for MonologueLine { + fn from(value: String) -> Self { + MonologueLine { + value + } + } +} + +impl Into for MonologueLine { + fn into(self) -> String { + self.value + } +} + +impl From<&str> for MonologueLine { + fn from(value: &str) -> Self { + MonologueLine { + value: value.into() + } } } @@ -37,25 +88,30 @@ impl AssetLoader for MonologueLoader { let raw_string = String::from_utf8(bytes)?; - let mut value: Vec> = vec![vec![]]; + // Create a blank monologue to populate + let mut monologue = Monologue::default(); + + // Add an initial batch that may end up being empty + monologue.add_batch(MonologueLineBatch::default()); + // Iterate over raw string lines in the .mono file for line in raw_string.lines() { + // Break up into batches by --- separator if line.starts_with("---") { - value.push(Vec::new()); + monologue.add_batch(MonologueLineBatch::default()); + // Skip any empty lines or comments } else if line.starts_with("#") || line.is_empty() { // Skip comments and blank lines + // everything else we read as a monologue line } else { - value.last_mut().unwrap().push(line.into()); + monologue.batches.last_mut().unwrap().add_line(line.into()); } } // Clear empty batches - value.retain(|batch| !batch.is_empty()); - - debug!("Monologue: {:#?}", value); + monologue.batches.retain(|batch| !batch.lines.is_empty()); - let thing = Monologue { value }; - Ok(thing) + Ok(monologue) } fn extensions(&self) -> &[&str] {