From 7b6d9242e4217c8229cb546de495cd2670b97f2d Mon Sep 17 00:00:00 2001
From: Alexander Foremny <aforemny@posteo.de>
Date: Tue, 19 Mar 2024 14:56:30 +0100
Subject: feat: render buffs in client

---
 src/client.rs         | 25 +++++++++++++++++++++++++
 src/protocol.rs       |  2 ++
 src/server.rs         | 34 +++++++++++++++++-----------------
 src/shared/ability.rs | 10 +++++-----
 src/shared/buffs.rs   | 10 +++++-----
 5 files changed, 54 insertions(+), 27 deletions(-)

(limited to 'src')

diff --git a/src/client.rs b/src/client.rs
index 7e0cebf..91d8c7f 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -3,6 +3,7 @@ use crate::protocol::*;
 use crate::shared::ability::*;
 use crate::shared::activation::*;
 use crate::shared::area_of_effect::*;
+use crate::shared::buffs::*;
 use crate::shared::champion::*;
 use crate::shared::cooldown::*;
 use crate::shared::health::*;
@@ -79,6 +80,7 @@ impl Plugin for ClientPlugin {
                     render_projectiles.after(move_projectiles),
                     render_area_of_effects,
                     render_health,
+                    render_buffs,
                 ),
             )
             .add_systems(
@@ -559,6 +561,29 @@ fn render_health(players: Query<(&Health, &PlayerPosition, &EffectiveStats)>, mu
     }
 }
 
+fn render_buffs(players: Query<(&Buffs, &PlayerPosition)>, mut gizmos: Gizmos) {
+    for (buffs, position) in players.iter() {
+        let mut start =
+            position.0 + Vec2::new(-PLAYER_RADIUS + 1., PLAYER_RADIUS + HEALTH_OFFSET + 2.);
+        if buffs.haste.is_some() {
+            gizmos.rect_2d(start, 0., Vec2::new(2., 2.), Color::RED);
+            start = start + Vec2::new(3., 0.);
+        }
+        if buffs.shield.is_some() {
+            gizmos.rect_2d(start, 0., Vec2::new(2., 2.), Color::GRAY);
+            start = start + Vec2::new(3., 0.);
+        }
+        if buffs.slow.is_some() {
+            gizmos.rect_2d(start, 0., Vec2::new(2., 2.), Color::BLUE);
+            start = start + Vec2::new(3., 0.);
+        }
+        if buffs.speed.is_some() {
+            gizmos.rect_2d(start, 0., Vec2::new(2., 2.), Color::GREEN);
+            //start = start + Vec2::new(3., 0.);
+        }
+    }
+}
+
 fn hotbar_cooldown(
     client_id: Res<ClientId>,
     cooldowns: Query<(&PlayerId, &Cooldown)>,
diff --git a/src/protocol.rs b/src/protocol.rs
index 646974c..2a5b846 100644
--- a/src/protocol.rs
+++ b/src/protocol.rs
@@ -1,5 +1,6 @@
 use crate::shared::activation::*;
 use crate::shared::area_of_effect::*;
+use crate::shared::buffs::*;
 use crate::shared::champion::*;
 use crate::shared::cooldown::*;
 use crate::shared::health::*;
@@ -44,6 +45,7 @@ pub enum Components {
     Champion(Champion),
     EffectiveStats(EffectiveStats),
     AreaOfEffect(AreaOfEffect),
+    Buffs(Buffs),
 }
 
 #[derive(Channel)]
diff --git a/src/server.rs b/src/server.rs
index 4593cfa..aa3e3dc 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -549,42 +549,42 @@ fn effective_stats(
 }
 
 fn buffs_tick(mut buffses: Query<&mut Buffs>, time: Res<Time>) {
-    let dt = time.delta();
+    let dt = time.delta().as_secs_f32();
     for mut buffs in buffses.iter_mut() {
-        if let Some(ref mut timer) = &mut buffs.haste {
-            timer.tick(dt);
+        if let Some(ref mut duration) = &mut buffs.haste {
+            *duration = (*duration - dt).max(0.);
         }
-        if let Some(ref mut timer) = &mut buffs.shield {
-            timer.tick(dt);
+        if let Some(ref mut duration) = &mut buffs.shield {
+            *duration = (*duration - dt).max(0.);
         }
-        if let Some(ref mut timer) = &mut buffs.slow {
-            timer.tick(dt);
+        if let Some(ref mut duration) = &mut buffs.slow {
+            *duration = (*duration - dt).max(0.);
         }
-        if let Some(ref mut timer) = &mut buffs.speed {
-            timer.tick(dt);
+        if let Some(ref mut duration) = &mut buffs.speed {
+            *duration = (*duration - dt).max(0.);
         }
     }
 }
 
 fn buffs_despawn(mut buffses: Query<&mut Buffs>) {
     for mut buffs in buffses.iter_mut() {
-        if let Some(timer) = &buffs.haste {
-            if timer.finished() {
+        if let Some(duration) = &buffs.haste {
+            if *duration <= 0. {
                 buffs.haste = None;
             }
         }
-        if let Some(timer) = &buffs.shield {
-            if timer.finished() {
+        if let Some(duration) = &buffs.shield {
+            if *duration <= 0. {
                 buffs.shield = None;
             }
         }
-        if let Some(timer) = &buffs.slow {
-            if timer.finished() {
+        if let Some(duration) = &buffs.slow {
+            if *duration <= 0. {
                 buffs.slow = None;
             }
         }
-        if let Some(timer) = &buffs.speed {
-            if timer.finished() {
+        if let Some(duration) = &buffs.speed {
+            if *duration <= 0. {
                 buffs.speed = None;
             }
         }
diff --git a/src/shared/ability.rs b/src/shared/ability.rs
index 2cd44c4..d88b63b 100644
--- a/src/shared/ability.rs
+++ b/src/shared/ability.rs
@@ -109,8 +109,8 @@ fn focus_activation(focus: Focus) -> ActivatedAbilityActivation {
                     let Ok(mut buffs) = buffses.get_mut(*entity_id) else {
                         return;
                     };
-                    buffs.haste = Some(Timer::from_seconds(focus.duration, TimerMode::Once));
-                    buffs.speed = Some(Timer::from_seconds(focus.duration, TimerMode::Once));
+                    buffs.haste = Some(focus.duration);
+                    buffs.speed = Some(focus.duration);
                 },
             )
         });
@@ -128,7 +128,7 @@ fn haste_activation(haste: Haste) -> ActivatedAbilityActivation {
                     let Ok(mut buffs) = buffses.get_mut(*entity_id) else {
                         return;
                     };
-                    buffs.haste = Some(Timer::from_seconds(haste.duration, TimerMode::Once));
+                    buffs.haste = Some(haste.duration);
                 },
             )
         });
@@ -152,7 +152,7 @@ fn shield_activation(shield: Shield) -> ActivatedAbilityActivation {
                         return;
                     };
                     health.shield = health.shield + shield.blocked_damage;
-                    buffs.shield = Some(Timer::from_seconds(shield.duration, TimerMode::Once));
+                    buffs.shield = Some(shield.duration);
                 },
             )
         });
@@ -170,7 +170,7 @@ fn speed_activation(speed: Speed) -> ActivatedAbilityActivation {
                     let Ok(mut buffs) = buffses.get_mut(*entity_id) else {
                         return;
                     };
-                    buffs.speed = Some(Timer::from_seconds(speed.duration, TimerMode::Once));
+                    buffs.speed = Some(speed.duration);
                 },
             )
         });
diff --git a/src/shared/buffs.rs b/src/shared/buffs.rs
index 6b518a5..868f3a3 100644
--- a/src/shared/buffs.rs
+++ b/src/shared/buffs.rs
@@ -1,9 +1,9 @@
 use crate::shared::*;
 
-#[derive(Clone, Component, Default, Debug)]
+#[derive(Clone, Message, Component, Default, Debug, PartialEq, Serialize, Deserialize)]
 pub struct Buffs {
-    pub haste: Option<Timer>,
-    pub shield: Option<Timer>,
-    pub slow: Option<Timer>,
-    pub speed: Option<Timer>,
+    pub haste: Option<f32>,
+    pub shield: Option<f32>,
+    pub slow: Option<f32>,
+    pub speed: Option<f32>,
 }
-- 
cgit v1.2.3