From 0f85752b1657e7b8024fb2715578712295979b3a Mon Sep 17 00:00:00 2001 From: tom barrett Date: Tue, 27 Aug 2019 03:33:50 -0500 Subject: cargo update and dialogbox now disappears and npc resumes wandering --- src/dialogbox.rs | 33 +++++++++++++++++++-------------- src/game.rs | 5 +++++ src/npc.rs | 13 ++++++++++++- src/world.rs | 15 +++++++++++++++ 4 files changed, 51 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/dialogbox.rs b/src/dialogbox.rs index 4bdce04..f2909d1 100644 --- a/src/dialogbox.rs +++ b/src/dialogbox.rs @@ -36,8 +36,8 @@ impl DialogTree { #[derive(Clone)] pub struct DialogBox { dialogtree: Option, + dialog: Option, font: Font, - text: Option, mesh: Mesh, conf: Conf, } @@ -48,8 +48,8 @@ impl DialogBox { DialogBox { dialogtree: None, + dialog: None, font: Font::new(context, "/fonts/SONORM__.ttf").unwrap(), - text: None, mesh: MeshBuilder::new() .rectangle( DrawMode::fill(), @@ -67,26 +67,28 @@ impl DialogBox { } } + pub fn is_visible(&self) -> bool { + self.dialogtree.is_some() + } + 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; + if self.dialogtree.is_none() { + self.dialog = None; } } pub fn draw(&self, context: &mut Context) -> GameResult { - if let Some(text) = &self.text { + if let Some(dialog) = &self.dialog { + let text = Text::new( + TextFragment::new(dialog.text.as_str()) + .font(self.font) + .scale(Scale::uniform(40.0)), + ); + graphics::draw(context, &self.mesh, DrawParam::default())?; graphics::draw( context, - text, + &text, DrawParam::default().dest(Point2::new( self.conf.window_mode.width * 0.11, 2.6 * self.conf.window_mode.height / 4.0, @@ -99,5 +101,8 @@ impl DialogBox { pub fn give_dialogtree(&mut self, dialogtree: Option) { self.dialogtree = dialogtree; + if let Some(dialogtree) = &self.dialogtree { + self.dialog = dialogtree.dialogs.get(&0).cloned(); + } } } diff --git a/src/game.rs b/src/game.rs index 2eabcb2..8e86b99 100644 --- a/src/game.rs +++ b/src/game.rs @@ -35,6 +35,11 @@ impl EventHandler for Game { fn update(&mut self, _context: &mut Context) -> GameResult { self.world.update(); self.camera.give_center(self.world.player.get_position()); + + if !self.world.player_in_talking_range() { + self.dialogbox.give_dialogtree(None); + } + self.dialogbox.update(); Ok(()) } diff --git a/src/npc.rs b/src/npc.rs index 29400a0..729aecd 100644 --- a/src/npc.rs +++ b/src/npc.rs @@ -116,6 +116,17 @@ impl NPC { self.dialogtree.clone() } + pub fn is_talking(&self) -> bool { + self.behavior == Behavior::Talking + } + + pub fn stop_talking(&mut self) { + self.behavior = Behavior::Wandering(random_nearby_point( + self.entity.spawn, + constants::WANDER_DISTANCE, + )); + } + pub fn build_npcs(context: &mut Context, tileset: &Tileset, map: &Map) -> Vec { let mut npcs = Vec::new(); @@ -134,7 +145,7 @@ impl NPC { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] enum Behavior { Talking, Waiting(Instant), diff --git a/src/world.rs b/src/world.rs index 76048b9..4e9a965 100644 --- a/src/world.rs +++ b/src/world.rs @@ -52,6 +52,21 @@ impl World { } } + pub fn player_in_talking_range(&mut self) -> bool { + if let Some(npc) = self.npcs.iter_mut().find(|npc| npc.is_talking()) { + if constants::INTERACT_DISTANCE + > distance(&self.player.entity.position, &npc.entity.position) + { + true + } else { + npc.stop_talking(); + false + } + } else { + false + } + } + pub fn get_dialogtree(&mut self) -> Option { let player_position = self.player.entity.position; if let Some(npc) = self.npcs.iter_mut().find(|npc| { -- cgit v1.2.3