@@ -5,13 +5,14 @@ use crate::player_control::actions::{DualAxisDataExt, PlayerAction};
5
5
use crate :: player_control:: camera:: { CameraUpdateSystemSet , IngameCamera , IngameCameraKind } ;
6
6
use crate :: util:: smoothness_to_lerp_factor;
7
7
use crate :: util:: trait_extension:: { F32Ext , TransformExt , Vec3Ext } ;
8
- use crate :: world_interaction:: dialog:: CurrentDialog ;
8
+ use crate :: world_interaction:: dialog:: DialogTarget ;
9
9
use crate :: GameState ;
10
10
use anyhow:: { Context , Result } ;
11
11
use bevy:: prelude:: * ;
12
12
use bevy_kira_audio:: AudioInstance ;
13
13
use bevy_mod_sysfail:: * ;
14
14
use bevy_rapier3d:: prelude:: * ;
15
+ use bevy_yarn_slinger_example_dialogue_view:: SpeakerChangeEvent ;
15
16
use leafwing_input_manager:: prelude:: ActionState ;
16
17
use serde:: { Deserialize , Serialize } ;
17
18
use std:: ops:: DerefMut ;
@@ -27,7 +28,7 @@ pub(crate) fn player_embodiment_plugin(app: &mut App) {
27
28
handle_jump,
28
29
handle_horizontal_movement,
29
30
handle_speed_effects,
30
- rotate_to_speaker. run_if ( resource_exists :: < CurrentDialog > ( ) ) ,
31
+ rotate_to_speaker,
31
32
control_walking_sound,
32
33
handle_camera_kind,
33
34
)
@@ -147,28 +148,35 @@ fn handle_speed_effects(
147
148
fn rotate_to_speaker (
148
149
time : Res < Time > ,
149
150
mut with_player : Query < ( & mut Transform , & Velocity ) , With < Player > > ,
150
- without_player : Query < & Transform , Without < Player > > ,
151
- current_dialog : Res < CurrentDialog > ,
151
+ speakers : Query < ( & Transform , & DialogTarget ) , Without < Player > > ,
152
+ mut speaker_change_event : EventReader < SpeakerChangeEvent > ,
152
153
config : Res < GameConfig > ,
153
154
) {
154
155
#[ cfg( feature = "tracing" ) ]
155
156
let _span = info_span ! ( "rotate_to_speaker" ) . entered ( ) ;
156
- let Ok ( speaker_transform) = without_player. get ( current_dialog. source ) else {
157
- return ;
158
- } ;
159
- let dt = time. delta_seconds ( ) ;
157
+ for speaker_change in speaker_change_event. read ( ) {
158
+ if !speaker_change. speaking {
159
+ continue ;
160
+ }
161
+ let dt = time. delta_seconds ( ) ;
160
162
161
- for ( mut transform, velocity) in with_player. iter_mut ( ) {
162
- let horizontal_velocity = velocity. linvel . split ( transform. up ( ) ) . horizontal ;
163
- if horizontal_velocity. is_approx_zero ( ) {
164
- let up = transform. up ( ) ;
165
- let target_rotation = transform
166
- . horizontally_looking_at ( speaker_transform. translation , up)
167
- . rotation ;
168
- let smoothness = config. player . rotate_to_speaker_smoothness ;
169
- let factor = smoothness_to_lerp_factor ( smoothness, dt) ;
170
- let rotation = transform. rotation . slerp ( target_rotation, factor) ;
171
- transform. rotation = rotation;
163
+ for ( mut transform, velocity) in with_player. iter_mut ( ) {
164
+ for ( speaker_transform, dialog_target) in speakers. iter ( ) {
165
+ if dialog_target. speaker != speaker_change. character_name {
166
+ continue ;
167
+ }
168
+ let horizontal_velocity = velocity. linvel . split ( transform. up ( ) ) . horizontal ;
169
+ if horizontal_velocity. is_approx_zero ( ) {
170
+ let up = transform. up ( ) ;
171
+ let target_rotation = transform
172
+ . horizontally_looking_at ( speaker_transform. translation , up)
173
+ . rotation ;
174
+ let smoothness = config. player . rotate_to_speaker_smoothness ;
175
+ let factor = smoothness_to_lerp_factor ( smoothness, dt) ;
176
+ let rotation = transform. rotation . slerp ( target_rotation, factor) ;
177
+ transform. rotation = rotation;
178
+ }
179
+ }
172
180
}
173
181
}
174
182
}
0 commit comments