diff options
author | Alexander Foremny <aforemny@posteo.de> | 2024-03-18 07:06:13 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-03-18 07:08:00 +0100 |
commit | 193cb6670addf16bb18b76a1badc00f648150d0b (patch) | |
tree | 98123c3d7264a398a1d810625dc3de1a7971661f | |
parent | ec17616f8d6041ac5d93a786edec0f5d0f969a46 (diff) |
fix: show health regen in ui
-rw-r--r-- | src/client.rs | 6 | ||||
-rw-r--r-- | src/server.rs | 22 |
2 files changed, 22 insertions, 6 deletions
diff --git a/src/client.rs b/src/client.rs index 3679b85..277fc2c 100644 --- a/src/client.rs +++ b/src/client.rs @@ -585,7 +585,11 @@ fn health_indicator_spawn( format!("{health_gained_or_lost}"), TextStyle { font_size: 6., - color: Color::RED, + color: if *health_gained < 0. { + Color::RED + } else { + Color::GREEN + }, ..Default::default() }, ) diff --git a/src/server.rs b/src/server.rs index 0b15c5f..55e7ebc 100644 --- a/src/server.rs +++ b/src/server.rs @@ -34,7 +34,7 @@ struct ServerPlugin { pub transport: TransportConfig, } -const HEALTH_REGEN_RATE: f32 = 1.; +const HEALTH_REGEN_RATE: f32 = 5.; #[derive(Resource)] struct HealthRegenTimer(pub Timer); @@ -472,6 +472,7 @@ fn projectile_despawn( if let Some(target_player) = maybe_target_player { if let Some(target_entity) = entity_map.0.get(&target_player.0) { if let Ok(mut health) = healths.get_mut(*target_entity) { + health.0 = (health.0 - projectile.damage).max(0.); let _ = connection_manager .send_message_to_target::<Channel1, HealthChanged>( HealthChanged(HealthEvent { @@ -480,7 +481,6 @@ fn projectile_despawn( }), NetworkTarget::All, ); - health.0 = (health.0 - projectile.damage).max(0.); } } } @@ -502,10 +502,22 @@ fn timers_tick(mut health_regen_timer: ResMut<HealthRegenTimer>, time: Res<Time> health_regen_timer.0.tick(time.delta()); } -fn health_regen(health_regen_timer: Res<HealthRegenTimer>, mut healths: Query<&mut Health>) { +const HEALTH_REGEN: f32 = 5.; +fn health_regen( + health_regen_timer: Res<HealthRegenTimer>, + mut connection_manager: ResMut<ServerConnectionManager>, + mut healths: Query<(&PlayerId, &mut Health)>, +) { if health_regen_timer.0.just_finished() { - for mut health in healths.iter_mut() { - health.0 = (health.0 + 1.).min(MAX_HEALTH); + for (target_player, mut health) in healths.iter_mut() { + health.0 = (health.0 + HEALTH_REGEN).min(MAX_HEALTH); + let _ = connection_manager.send_message_to_target::<Channel1, HealthChanged>( + HealthChanged(HealthEvent { + target_player: *target_player, + health_gained: HEALTH_REGEN, + }), + NetworkTarget::All, + ); } } } |