Skip to content

Commit 2034b6e

Browse files
committed
fix: player controller shouldnt run if not ingame and playing
1 parent 680bf58 commit 2034b6e

File tree

1 file changed

+26
-11
lines changed
  • src/character_controller

1 file changed

+26
-11
lines changed

src/character_controller/mod.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use bevy::prelude::*;
33

44
use crate::{
55
GRAVITY,
6+
game_flow::states::InGameState,
67
player::{Player, camera::components::ViewModelCamera},
78
};
89

@@ -81,18 +82,31 @@ pub struct CharacterControllerPlugin;
8182

8283
impl Plugin for CharacterControllerPlugin {
8384
fn build(&self, app: &mut App) {
84-
app.add_message::<MovementAction>().add_systems(
85-
Update,
86-
(
87-
update_on_ground,
88-
apply_gravity_over_time,
89-
handle_keyboard_input_for_player,
90-
handle_movement_actions_for_player,
91-
),
92-
);
85+
app.add_message::<MovementAction>()
86+
.add_systems(
87+
Update,
88+
(
89+
update_on_ground,
90+
apply_gravity_over_time,
91+
handle_keyboard_input_for_player,
92+
handle_movement_actions_for_player,
93+
)
94+
.run_if(in_state(InGameState::Playing)),
95+
)
96+
.add_systems(
97+
OnEnter(InGameState::PlayerDead),
98+
handle_player_dead_velocity,
99+
);
93100
}
94101
}
95102

103+
fn handle_player_dead_velocity(
104+
mut player_velocity: Single<&mut LinearVelocity, With<Player>>,
105+
) {
106+
info!("Player entered state Dead, zeroeing velocity");
107+
player_velocity.0 = Vec3::ZERO;
108+
}
109+
96110
fn handle_keyboard_input_for_player(
97111
keyboard_input: Res<ButtonInput<KeyCode>>,
98112
mut movement_action_writer: MessageWriter<MovementAction>,
@@ -289,11 +303,12 @@ fn update_on_ground(
289303
}
290304

291305
fn apply_gravity_over_time(
292-
query: Query<(&Grounded, &mut LinearVelocity)>,
306+
query: Query<(&Grounded, &mut LinearVelocity, &Name)>,
293307
time: Res<Time>,
294308
) {
295-
for (grounded, mut velocity) in query {
309+
for (grounded, mut velocity, name) in query {
296310
if !grounded.0 {
311+
debug!("applying gravity for {}", name);
297312
velocity.y -= GRAVITY * time.delta_secs();
298313
}
299314
}

0 commit comments

Comments
 (0)