-
Notifications
You must be signed in to change notification settings - Fork 1
/
portraits.rs
231 lines (205 loc) · 7.1 KB
/
portraits.rs
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use bevy::prelude::*;
use bevy_mod_yarn::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
resolution: (800., 600.).into(),
..default()
}),
..default()
}))
.add_plugins(YarnPlugin)
.init_resource::<State>()
.add_systems(Startup, setup)
.add_systems(
Update,
(dialogue_init, dialogue_navigation, dialogue_display),
)
.run();
}
#[derive(Resource, Default)]
struct State {
handle: Handle<YarnAsset>,
done: bool,
}
#[derive(Component)]
struct CharacterName(String);
#[derive(Component)]
struct CharacterPortraitPath(String);
#[derive(Component)]
struct CharacterPortrait(Handle<Image>);
// marker components
#[derive(Component)]
struct DialogueTextMarker;
#[derive(Component)]
struct DialogueNameMarker;
fn setup(
mut state: ResMut<State>,
asset_server: Res<AssetServer>,
mut commands: bevy::prelude::Commands,
) {
// load the yarn file
state.handle = asset_server.load("dialogues/two_nodes_jump_nested_choices.yarn");
// setup a simple 2d camera
commands.spawn(Camera2dBundle {
transform: Transform::from_xyz(0.0, 5.0, 0.0),
..default()
});
// for character dialogue text
commands.spawn((
TextBundle::from_section(
"",
TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 18.0,
color: Color::WHITE,
},
)
.with_style(Style {
position_type: PositionType::Absolute,
bottom: Val::Px(30.0),
left: Val::Px(80.0),
..default()
}),
DialogueTextMarker,
));
// for character names
commands.spawn((
TextBundle::from_section(
"",
TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 18.0,
color: Color::GOLD,
},
)
.with_style(Style {
position_type: PositionType::Absolute,
bottom: Val::Px(60.0),
left: Val::Px(80.0),
..default()
}),
DialogueNameMarker,
));
commands
.spawn(NodeBundle {
style: Style {
position_type: PositionType::Absolute,
bottom: Val::Px(10.0),
left: Val::Px(10.0),
..default()
},
..default()
})
.with_children(|parent| {
// bevy logo (image)
parent
.spawn(ImageBundle {
style: Style {
min_width: Val::Px(64.),
min_height: Val::Px(64.),
..default()
},
image: asset_server.load("textures/portrait0.png").into(),
..default()
})
.with_children(|parent| {
// alt text
parent.spawn(TextBundle::from_section("portraits", TextStyle::default()));
});
});
// spawn our characters
commands.spawn((
CharacterName("Lamik".to_string()),
CharacterPortraitPath("textures/portrait1.png".to_string()),
CharacterPortrait(asset_server.load("textures/portrait1.png").into()),
));
commands.spawn((
CharacterName("Dona".to_string()),
CharacterPortraitPath("textures/portrait2.png".to_string()),
CharacterPortrait(asset_server.load("textures/portrait2.png").into()),
));
}
fn dialogue_init(
mut state: ResMut<State>,
dialogues: Res<Assets<YarnAsset>>,
mut commands: bevy::prelude::Commands,
) {
if let Some(dialogues) = dialogues.get(&state.handle) {
if !state.done {
commands.spawn(DialogueRunner::new(dialogues.clone(), "Test_node"));
state.done = true;
}
}
}
fn dialogue_navigation(keys: Res<Input<KeyCode>>, mut runners: Query<&mut DialogueRunner>) {
if let Ok(mut runner) = runners.get_single_mut() {
if keys.just_pressed(KeyCode::Return) {
runner.next_entry();
}
if keys.just_pressed(KeyCode::Down) {
println!("next choice");
runner.next_choice()
}
if keys.just_pressed(KeyCode::Up) {
println!("prev choice");
runner.prev_choice()
}
}
}
fn dialogue_display(
runners: Query<&DialogueRunner>,
mut name_display: Query<&mut Text, (With<DialogueNameMarker>, Without<DialogueTextMarker>)>,
mut text: Query<&mut Text, (With<DialogueTextMarker>, Without<DialogueNameMarker>)>,
mut portrait: Query<&mut UiImage>,
characters: Query<(&CharacterName, &CharacterPortrait)>,
) {
let mut text = text.single_mut();
let text = &mut text.sections[0].value;
*text = "".to_string();
text.push_str("------------------------------\n");
let mut name_display = name_display.single_mut();
let name_display = &mut name_display.sections[0].value;
*name_display = "".to_string();
let mut portrait = portrait.single_mut();
if let Ok(runner) = runners.get_single() {
match runner.current_statement() {
Statements::Dialogue(dialogue) => {
// println!("{:?}: {:?}", dialogue.who, dialogue.what);
text.push_str(&format!("{}\n", dialogue.what));
// FIXME: very inneficient, but does the job, perhaps switch to for each to break out early
for (name, portrait_img) in characters.iter() {
if name.0 == dialogue.who {
portrait.texture = portrait_img.0.clone();
name_display.push_str(&name.0);
}
}
}
Statements::Choice(_) => {
let (choices, current_choice_index) = runner.get_current_choices();
for (index, dialogue) in choices.iter().enumerate() {
*name_display = "".to_string();
if index == current_choice_index {
// text.push_str(&format!("--> {:?}: {:?}\n", dialogue.who, dialogue.what));
text.push_str(&format!("--> {}\n", dialogue.what));
} else {
// text.push_str(&format!("{:?}: {:?}\n", dialogue.who, dialogue.what));
text.push_str(&format!("{}\n", dialogue.what));
}
// FIXME: very inneficient, but does the job, perhaps switch to for each to break out early
for (name, portrait_img) in characters.iter() {
if name.0 == dialogue.who {
portrait.texture = portrait_img.0.clone();
name_display.push_str(&name.0);
}
}
}
}
Statements::Exit => {
text.push_str("end of the dialogue! (Exit)");
}
_ => {}
}
}
}