1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
use ggez::conf::Conf;
use ggez::graphics::{
self, DrawMode, DrawParam, Font, Mesh, MeshBuilder, PxScale, Rect, Text, TextFragment,
};
use ggez::{filesystem, Context, GameResult};
use glam;
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 {
display: Option<(Dialog, DialogTree, usize)>,
font: Font,
mesh: Mesh,
conf: Conf,
}
impl DialogBox {
pub fn new(context: &mut Context) -> DialogBox {
let conf = Conf::new();
DialogBox {
display: None,
font: Font::new(context, "/fonts/SONORM__.ttf").unwrap(),
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,
)
.unwrap()
.build(context)
.unwrap(),
conf,
}
}
pub fn is_visible(&self) -> bool {
self.display.is_some()
}
pub fn update(&mut self) {}
pub fn draw(&self, context: &mut Context) -> GameResult {
if let Some((dialog, _, selected_response)) = &self.display {
let text = Text::new(
TextFragment::new(dialog.text.as_str())
.font(self.font)
.scale(PxScale::from(40.0)),
);
graphics::draw(context, &self.mesh, DrawParam::default())?;
graphics::draw(
context,
&text,
DrawParam::default().dest(glam::vec2(
self.conf.window_mode.width * 0.11,
2.6 * self.conf.window_mode.height / 4.0,
)),
)?;
for (i, response) in dialog.responses.iter().enumerate() {
let color = if &i == selected_response {
constants::GOLD
} else {
constants::WHITE
};
let text = Text::new(
TextFragment::new(response.1.as_str())
.font(self.font)
.scale(PxScale::from(40.0)),
);
graphics::draw(
context,
&text,
DrawParam::default()
.dest(glam::vec2(
self.conf.window_mode.width * 0.11,
(2.6 + (0.25 * (i + 1) as f32)) * self.conf.window_mode.height / 4.0,
))
.color(color),
)?;
}
}
Ok(())
}
pub fn populate_display(&mut self, dialogtree: Option<DialogTree>) {
if let Some(dialogtree) = &dialogtree {
let dialog = dialogtree.dialogs.get(&0).unwrap();
self.display = Some((dialog.clone(), dialogtree.clone(), 0));
} else {
self.display = None;
}
}
pub fn choose_reponse(&mut self) {
if let Some((dialog, dialogtree, selected_response)) = &self.display.clone() {
if let Some(selected_dialog) = dialog.responses.get(*selected_response) {
if let Some(new_dialog) = dialogtree.dialogs.get(&selected_dialog.0) {
self.display = Some((new_dialog.clone(), dialogtree.clone(), 0));
}
}
}
}
pub fn next_response(&mut self) {
if let Some((dialog, dialogtree, selected_response)) = &self.display.clone() {
let new_selected_response =
if Some(*selected_response) < dialog.responses.len().checked_sub(1) {
selected_response + 1
} else {
0
};
self.display = Some((dialog.clone(), dialogtree.clone(), new_selected_response));
}
}
pub fn prev_response(&mut self) {
if let Some((dialog, dialogtree, selected_response)) = &self.display.clone() {
let new_selected_response = if selected_response == &0 {
match dialog.responses.len().checked_sub(1) {
Some(i) => i,
None => 0,
}
} else {
selected_response - 1
};
self.display = Some((dialog.clone(), dialogtree.clone(), new_selected_response));
}
}
}
|