From 2e39423d11a6cd1e25b54c30d9afd22e8eff9dfe Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Tue, 19 Mar 2024 07:36:54 +0100 Subject: feat: shield ability --- src/client.rs | 12 +++++++----- src/server.rs | 19 +++++++++++++++---- src/shared/ability.rs | 42 +++++++++++++++++++++++++++++++++++++----- src/shared/buffs.rs | 1 + src/shared/champion.rs | 4 ++++ src/shared/health.rs | 18 +++++++++++++++++- src/shared/player.rs | 5 ++++- 7 files changed, 85 insertions(+), 16 deletions(-) diff --git a/src/client.rs b/src/client.rs index 5084eaa..8da77dc 100644 --- a/src/client.rs +++ b/src/client.rs @@ -522,11 +522,13 @@ fn render_health(players: Query<(&Health, &PlayerPosition, &EffectiveStats)>, mu for (health, position, effective_stats) in players.iter() { let start = position.0 + Vec2::new(-PLAYER_RADIUS, PLAYER_RADIUS + HEALTH_OFFSET); let end = position.0 + Vec2::new(PLAYER_RADIUS, PLAYER_RADIUS + HEALTH_OFFSET); - gizmos.line_2d( - start, - start.lerp(end, health.0 / effective_stats.0.max_health), - Color::RED, - ); + let health_start = start; + let health_end = start.lerp(end, health.health / effective_stats.0.max_health); + let px_per_health = (end.x - start.x) / effective_stats.0.max_health; + let shield_start = health_end; + let shield_end = health_end + Vec2::new(health.shield * px_per_health, 0.); + gizmos.line_2d(health_start, health_end, Color::RED); + gizmos.line_2d(shield_start, shield_end, Color::GRAY); } } diff --git a/src/server.rs b/src/server.rs index 483314d..fc44120 100644 --- a/src/server.rs +++ b/src/server.rs @@ -55,7 +55,7 @@ impl Plugin for ServerPlugin { .add_systems(Update, (connects, disconnects)) .add_systems(Update, receive_message) .add_systems(FixedUpdate, timers_tick) - .add_systems(FixedUpdate, effective_stats) + .add_systems(FixedUpdate, effective_stats.after(buffs_despawn)) .add_systems(FixedUpdate, health_regen.after(timers_tick)) .add_systems( FixedUpdate, @@ -466,7 +466,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.); + health.apply_damage(projectile.damage); let _ = connection_manager .send_message_to_target::( HealthChanged(HealthEvent { @@ -504,7 +504,7 @@ fn health_regen( ) { if health_regen_timer.0.just_finished() { for (target_player, mut health, effective_stats) in healths.iter_mut() { - health.0 = (health.0 + HEALTH_REGEN).min(effective_stats.0.max_health); + health.heal(HEALTH_REGEN, effective_stats.0.max_health); let _ = connection_manager.send_message_to_target::( HealthChanged(HealthEvent { target_player: *target_player, @@ -531,7 +531,10 @@ fn effective_stats( stats.attack_speed *= 0.5; } effective_stats.0 = stats; - health.0 = health.0.min(effective_stats.0.max_health); + health.health = health.health.min(effective_stats.0.max_health); + if buffs.shield.is_none() { + health.shield = 0.; + } } } @@ -541,6 +544,9 @@ fn buffs_tick(mut buffses: Query<&mut Buffs>, time: Res