diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/dialogbox.rs | 33 | ||||
-rw-r--r-- | src/game.rs | 5 | ||||
-rw-r--r-- | src/npc.rs | 13 | ||||
-rw-r--r-- | src/world.rs | 15 |
4 files changed, 51 insertions, 15 deletions
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<DialogTree>, + dialog: Option<Dialog>, font: Font, - text: Option<Text>, 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<DialogTree>) { 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(()) } @@ -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<NPC> { 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<DialogTree> { let player_position = self.player.entity.position; if let Some(npc) = self.npcs.iter_mut().find(|npc| { |