Organize Monologue into more specific types

Monologue contains a vec of MonologueGroups
MonologueGroup contains a vec of MonologueLines

This would make it easier (ish) to add @directives like I've spec'd out
We can add a Vec of Directive enums at each level.
main
Elijah Voigt 4 months ago
parent a96e86ea9b
commit 1d31902312

@ -313,11 +313,11 @@ fn dialog_engine(
// Fetch this monologue from the assets // Fetch this monologue from the assets
if let Some(monologue) = monologues.get(handle.clone().id()) { if let Some(monologue) = monologues.get(handle.clone().id()) {
// Fetch this batch of options // 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 // Spawn the dialog options in the dialog box
options.iter().for_each(|option| { batch.lines.iter().for_each(|line| {
parent.spawn(( parent.spawn((
Text::new(option.clone()), Text::new(line.clone()),
DialogOption, DialogOption,
TextLayout::new(JustifyText::Left, LineBreak::NoWrap), TextLayout::new(JustifyText::Left, LineBreak::NoWrap),
)); ));
@ -615,9 +615,9 @@ fn preview_monologue(
// Spawn the monologue // Spawn the monologue
let mut i = 0; let mut i = 0;
commands.entity(*container).with_children(|parent| { 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)); 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)); parent.spawn((Text::new(format!("{i}.{n}: {item}")), MonologuePreview));
} }
// TODO: Just implement iter_batches or something // TODO: Just implement iter_batches or something

@ -1,13 +1,64 @@
use super::*; use super::*;
/// A monologue containing a list of optional lines
#[derive(Asset, TypePath, Debug, Deserialize, Default, Clone)] #[derive(Asset, TypePath, Debug, Deserialize, Default, Clone)]
pub(crate) struct Monologue { pub(crate) struct Monologue {
value: Vec<Vec<String>>, pub batches: Vec<MonologueLineBatch>,
} }
impl Monologue { impl Monologue {
pub fn get(&self, idx: usize) -> Option<&Vec<String>> { pub fn get(&self, idx: usize) -> Option<&MonologueLineBatch> {
self.value.get(idx) 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<MonologueLine>,
}
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<String> for MonologueLine {
fn from(value: String) -> Self {
MonologueLine {
value
}
}
}
impl Into<String> 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 raw_string = String::from_utf8(bytes)?;
let mut value: Vec<Vec<String>> = 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() { for line in raw_string.lines() {
// Break up into batches by --- separator
if line.starts_with("---") { 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() { } else if line.starts_with("#") || line.is_empty() {
// Skip comments and blank lines // Skip comments and blank lines
// everything else we read as a monologue line
} else { } else {
value.last_mut().unwrap().push(line.into()); monologue.batches.last_mut().unwrap().add_line(line.into());
} }
} }
// Clear empty batches // Clear empty batches
value.retain(|batch| !batch.is_empty()); monologue.batches.retain(|batch| !batch.lines.is_empty());
debug!("Monologue: {:#?}", value);
let thing = Monologue { value }; Ok(monologue)
Ok(thing)
} }
fn extensions(&self) -> &[&str] { fn extensions(&self) -> &[&str] {

Loading…
Cancel
Save