diff options
Diffstat (limited to 'src/dialogbox.rs')
-rw-r--r-- | src/dialogbox.rs | 66 |
1 files changed, 54 insertions, 12 deletions
diff --git a/src/dialogbox.rs b/src/dialogbox.rs index 1f6415b..4bdce04 100644 --- a/src/dialogbox.rs +++ b/src/dialogbox.rs @@ -3,28 +3,53 @@ use ggez::graphics::{ self, DrawMode, DrawParam, Font, Mesh, MeshBuilder, Rect, Scale, Text, TextFragment, }; use ggez::nalgebra::Point2; -use ggez::{Context, GameResult}; +use ggez::{filesystem, Context, GameResult}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; use crate::constants; +use crate::npc::Character; +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Dialog { + text: String, + responses: Vec<(usize, String)>, +} + +#[derive(Clone, Debug, Default)] +pub struct DialogTree { + dialogs: HashMap<usize, Dialog>, +} + +impl DialogTree { + pub fn new(context: &mut Context, character: Character) -> DialogTree { + DialogTree { + dialogs: serde_json::from_reader( + filesystem::open(context, "/dialogtrees/".to_string() + character.to_str()) + .unwrap(), + ) + .unwrap(), + } + } +} + +#[derive(Clone)] pub struct DialogBox { + dialogtree: Option<DialogTree>, + font: Font, + text: Option<Text>, mesh: Mesh, - text: Text, conf: Conf, - pub visible: bool, } impl DialogBox { pub fn new(context: &mut Context) -> DialogBox { let conf = Conf::new(); - let font = Font::new(context, "/fonts/SONORM__.ttf").unwrap(); DialogBox { - text: Text::new( - TextFragment::new("Ave !") - .font(font) - .scale(Scale::uniform(40.0)), - ), + dialogtree: None, + font: Font::new(context, "/fonts/SONORM__.ttf").unwrap(), + text: None, mesh: MeshBuilder::new() .rectangle( DrawMode::fill(), @@ -38,17 +63,30 @@ impl DialogBox { ) .build(context) .unwrap(), - visible: false, conf, } } + pub fn update(&mut self) { + if let Some(dialogtree) = &self.dialogtree { + if self.text.is_none() { + self.text = Some(Text::new( + TextFragment::new(dialogtree.dialogs.get(&0).unwrap().text.as_str()) + .font(self.font) + .scale(Scale::uniform(40.0)), + )); + } + } else { + self.text = None; + } + } + pub fn draw(&self, context: &mut Context) -> GameResult { - if self.visible { + if let Some(text) = &self.text { graphics::draw(context, &self.mesh, DrawParam::default())?; graphics::draw( context, - &self.text, + text, DrawParam::default().dest(Point2::new( self.conf.window_mode.width * 0.11, 2.6 * self.conf.window_mode.height / 4.0, @@ -58,4 +96,8 @@ impl DialogBox { Ok(()) } + + pub fn give_dialogtree(&mut self, dialogtree: Option<DialogTree>) { + self.dialogtree = dialogtree; + } } |