From 7d2c0954cd95bdabcb7ecf26f9225382ab078289 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Wed, 24 Jul 2019 05:54:49 -0500 Subject: dialog box --- src/constants.rs | 5 ++++- src/dialogbox.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/world.rs | 6 ++++++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/dialogbox.rs (limited to 'src') diff --git a/src/constants.rs b/src/constants.rs index 1f6bb9f..8a873c6 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,6 +1,6 @@ pub const TILE_WIDTH: f32 = 16.0; pub const TILE_HEIGHT: f32 = 16.0; -pub const TILE_SCALE: f32 = 2.5; +pub const TILE_SCALE: f32 = 2.75; pub const PLAYER_SPEED: f32 = 3.0; pub const WANDER_DISTANCE: f32 = 200.0; @@ -13,3 +13,6 @@ pub const FLIP_H: usize = 0x8000_0000; pub const FLIP_V: usize = 0x4000_0000; pub const FLIP_D: usize = 0x2000_0000; pub const FLIP_A: usize = FLIP_D | FLIP_H | FLIP_V; + +use ggez::graphics::Color; +pub const PURPLE: Color = Color::new(0.4, 0.0, 0.2, 1.0); diff --git a/src/dialogbox.rs b/src/dialogbox.rs new file mode 100644 index 0000000..1f6415b --- /dev/null +++ b/src/dialogbox.rs @@ -0,0 +1,61 @@ +use ggez::conf::Conf; +use ggez::graphics::{ + self, DrawMode, DrawParam, Font, Mesh, MeshBuilder, Rect, Scale, Text, TextFragment, +}; +use ggez::nalgebra::Point2; +use ggez::{Context, GameResult}; + +use crate::constants; + +pub struct DialogBox { + 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)), + ), + mesh: MeshBuilder::new() + .rectangle( + DrawMode::fill(), + Rect::new( + conf.window_mode.width * 0.10, + 2.5 * conf.window_mode.height / 4.0, + conf.window_mode.width * 0.80, + conf.window_mode.height / 4.0, + ), + constants::PURPLE, + ) + .build(context) + .unwrap(), + visible: false, + conf, + } + } + + pub fn draw(&self, context: &mut Context) -> GameResult { + if self.visible { + graphics::draw(context, &self.mesh, DrawParam::default())?; + graphics::draw( + context, + &self.text, + DrawParam::default().dest(Point2::new( + self.conf.window_mode.width * 0.11, + 2.6 * self.conf.window_mode.height / 4.0, + )), + )?; + } + + Ok(()) + } +} diff --git a/src/lib.rs b/src/lib.rs index a95028d..be462c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ pub mod animations; pub mod camera; pub mod cell; pub mod constants; +pub mod dialogbox; pub mod entity; pub mod layer; pub mod map; diff --git a/src/world.rs b/src/world.rs index f89e9e7..abbb9c1 100644 --- a/src/world.rs +++ b/src/world.rs @@ -3,6 +3,7 @@ use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Imag use ggez::{filesystem, Context, GameResult}; use crate::camera::Camera; +use crate::dialogbox::DialogBox; use crate::entity::Operable; use crate::map::Map; use crate::npc::NPC; @@ -12,6 +13,7 @@ use crate::tileset::Tileset; pub struct World { map: Map, spritebatch: SpriteBatch, + dialogbox: DialogBox, camera: Camera, player: Player, npcs: Vec, @@ -30,6 +32,7 @@ impl World { Ok(World { map: map.clone(), spritebatch: SpriteBatch::new(image), + dialogbox: DialogBox::new(context), camera: Camera::new(map.get_dimensions()), player: Player::new( &tileset, @@ -68,6 +71,8 @@ impl EventHandler for World { DrawParam::default().dest(self.camera.draw), )?; + self.dialogbox.draw(context)?; + self.spritebatch.clear(); graphics::present(context)?; @@ -89,6 +94,7 @@ impl EventHandler for World { if !repeat { match keycode { KeyCode::Q => context.continuing = false, + KeyCode::E => self.dialogbox.visible = !self.dialogbox.visible, _ => self.player.give_key_down(keycode), } } -- cgit v1.2.3