From cd2a20f1f47143b7eed2c8bd5fe21662666db8d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 26 Jul 2024 18:13:35 +0200 Subject: [PATCH 1/7] Use `OnReset` function to reset damage indicators The additional indirection with the `CEffects::ResetDamageIndicator` and `CDamageInd::Reset` functions to reset damage indicators is confusing and unnecessary. Always reset the damage indicators when disconnecting (in `OnReset`) instead of when entering the next game. --- src/game/client/components/damageind.cpp | 2 +- src/game/client/components/damageind.h | 2 +- src/game/client/components/effects.cpp | 5 ----- src/game/client/components/effects.h | 1 - src/game/client/gameclient.cpp | 1 - 5 files changed, 2 insertions(+), 9 deletions(-) diff --git a/src/game/client/components/damageind.cpp b/src/game/client/components/damageind.cpp index 96a1358b2..f42c2e7c6 100644 --- a/src/game/client/components/damageind.cpp +++ b/src/game/client/components/damageind.cpp @@ -105,7 +105,7 @@ void CDamageInd::OnInit() Graphics()->QuadContainerUpload(m_DmgIndQuadContainerIndex); } -void CDamageInd::Reset() +void CDamageInd::OnReset() { for(int i = 0; i < m_NumItems;) { diff --git a/src/game/client/components/damageind.h b/src/game/client/components/damageind.h index 4944010ef..cb5bb7d4b 100644 --- a/src/game/client/components/damageind.h +++ b/src/game/client/components/damageind.h @@ -37,7 +37,7 @@ public: virtual int Sizeof() const override { return sizeof(*this); } void Create(vec2 Pos, vec2 Dir, float Alpha); - void Reset(); + virtual void OnReset() override; virtual void OnRender() override; virtual void OnInit() override; }; diff --git a/src/game/client/components/effects.cpp b/src/game/client/components/effects.cpp index f6389e924..1d6000baf 100644 --- a/src/game/client/components/effects.cpp +++ b/src/game/client/components/effects.cpp @@ -53,11 +53,6 @@ void CEffects::DamageIndicator(vec2 Pos, vec2 Dir, float Alpha) m_pClient->m_DamageInd.Create(Pos, Dir, Alpha); } -void CEffects::ResetDamageIndicator() -{ - m_pClient->m_DamageInd.Reset(); -} - void CEffects::PowerupShine(vec2 Pos, vec2 Size, float Alpha) { if(!m_Add50hz) diff --git a/src/game/client/components/effects.h b/src/game/client/components/effects.h index c88b691ef..f86c27cce 100644 --- a/src/game/client/components/effects.h +++ b/src/game/client/components/effects.h @@ -26,7 +26,6 @@ public: void HammerHit(vec2 Pos, float Alpha = 1.0f); void AirJump(vec2 Pos, float Alpha = 1.0f); void DamageIndicator(vec2 Pos, vec2 Dir, float Alpha = 1.0f); - void ResetDamageIndicator(); void PlayerSpawn(vec2 Pos, float Alpha = 1.0f); void PlayerDeath(vec2 Pos, int ClientId, float Alpha = 1.0f); void PowerupShine(vec2 Pos, vec2 Size, float Alpha = 1.0f); diff --git a/src/game/client/gameclient.cpp b/src/game/client/gameclient.cpp index af6ea217d..9dfdf5e5f 100644 --- a/src/game/client/gameclient.cpp +++ b/src/game/client/gameclient.cpp @@ -1010,7 +1010,6 @@ void CGameClient::OnShutdown() void CGameClient::OnEnterGame() { - m_Effects.ResetDamageIndicator(); } void CGameClient::OnGameOver() From f14f00da0928b670459e9fae69b3ba48017cc80a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 26 Jul 2024 18:14:12 +0200 Subject: [PATCH 2/7] Reset damage indicators when skipping in demo Prevent old damage indicators still persisting in demo player after skipping. --- src/game/client/components/menus_demo.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/game/client/components/menus_demo.cpp b/src/game/client/components/menus_demo.cpp index 4f61a7b83..8612e10f7 100644 --- a/src/game/client/components/menus_demo.cpp +++ b/src/game/client/components/menus_demo.cpp @@ -78,6 +78,7 @@ void CMenus::HandleDemoSeeking(float PositionToSeek, float TimeToSeek) if((PositionToSeek >= 0.0f && PositionToSeek <= 1.0f) || TimeToSeek != 0.0f) { m_pClient->m_Chat.Reset(); + m_pClient->m_DamageInd.OnReset(); m_pClient->m_InfoMessages.OnReset(); m_pClient->m_Particles.OnReset(); m_pClient->m_Sounds.OnReset(); From df0a9811d908c4efb865b74c9105d3d8ed40c2df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 26 Jul 2024 18:18:54 +0200 Subject: [PATCH 3/7] Remove unused `m_Lastupdate` variable --- src/game/client/components/damageind.cpp | 1 - src/game/client/components/damageind.h | 1 - 2 files changed, 2 deletions(-) diff --git a/src/game/client/components/damageind.cpp b/src/game/client/components/damageind.cpp index f42c2e7c6..90664bbf0 100644 --- a/src/game/client/components/damageind.cpp +++ b/src/game/client/components/damageind.cpp @@ -12,7 +12,6 @@ CDamageInd::CDamageInd() { - m_Lastupdate = 0; m_NumItems = 0; } diff --git a/src/game/client/components/damageind.h b/src/game/client/components/damageind.h index cb5bb7d4b..bf35bc920 100644 --- a/src/game/client/components/damageind.h +++ b/src/game/client/components/damageind.h @@ -8,7 +8,6 @@ class CDamageInd : public CComponent { - int64_t m_Lastupdate; struct CItem { vec2 m_Pos; From 690590ca03c4d1acc33e53faeaec1c0eea80fb6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 26 Jul 2024 18:23:38 +0200 Subject: [PATCH 4/7] Remove unnecessary `m_StartAlpha` variable The `m_Color` variable already stores the initial alpha value and should not be modified, so the separate `m_StartAlpha` variable is unnecessary. --- src/game/client/components/damageind.cpp | 8 ++------ src/game/client/components/damageind.h | 1 - 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/game/client/components/damageind.cpp b/src/game/client/components/damageind.cpp index 90664bbf0..0ce173f38 100644 --- a/src/game/client/components/damageind.cpp +++ b/src/game/client/components/damageind.cpp @@ -42,7 +42,6 @@ void CDamageInd::Create(vec2 Pos, vec2 Dir, float Alpha) pItem->m_Dir = -Dir; pItem->m_StartAngle = -random_angle(); pItem->m_Color = ColorRGBA(1.0f, 1.0f, 1.0f, Alpha); - pItem->m_StartAlpha = Alpha; } } @@ -75,11 +74,8 @@ void CDamageInd::OnRender() else { vec2 Pos = mix(m_aItems[i].m_Pos + m_aItems[i].m_Dir * 75.0f, m_aItems[i].m_Pos, clamp((Life - 0.60f) / 0.15f, 0.0f, 1.0f)); - ColorRGBA Color = m_aItems[i].m_Color; - - float LifeAlpha = Life / 0.1f; - Color.a = m_aItems[i].m_StartAlpha * LifeAlpha; - Graphics()->SetColor(Color); + const float LifeAlpha = Life / 0.1f; + Graphics()->SetColor(m_aItems[i].m_Color.WithMultipliedAlpha(LifeAlpha)); Graphics()->QuadsSetRotation(m_aItems[i].m_StartAngle + Life * 2.0f); Graphics()->RenderQuadContainerAsSprite(m_DmgIndQuadContainerIndex, 0, Pos.x, Pos.y); i++; diff --git a/src/game/client/components/damageind.h b/src/game/client/components/damageind.h index bf35bc920..75c3eec66 100644 --- a/src/game/client/components/damageind.h +++ b/src/game/client/components/damageind.h @@ -15,7 +15,6 @@ class CDamageInd : public CComponent float m_StartTime; float m_StartAngle; ColorRGBA m_Color; - float m_StartAlpha; }; enum From 90562b515764948f36fcc038cd0a26ecf7fd2ffd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 26 Jul 2024 18:25:51 +0200 Subject: [PATCH 5/7] Make `CDamageInd::OnReset` function more efficient There is no need to copy the items if we only want to delete all of them. --- src/game/client/components/damageind.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/game/client/components/damageind.cpp b/src/game/client/components/damageind.cpp index 0ce173f38..8ea48067f 100644 --- a/src/game/client/components/damageind.cpp +++ b/src/game/client/components/damageind.cpp @@ -102,8 +102,5 @@ void CDamageInd::OnInit() void CDamageInd::OnReset() { - for(int i = 0; i < m_NumItems;) - { - DestroyI(&m_aItems[i]); - } + m_NumItems = 0; } From 782c9a54bfa3df35a4a1281daf493a00e5713613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 26 Jul 2024 18:30:47 +0200 Subject: [PATCH 6/7] Remove separate `CreateI` and `DestroyI` functions Improve readability by removing the indirection when creating and removing damage indicators. --- src/game/client/components/damageind.cpp | 41 ++++++++---------------- src/game/client/components/damageind.h | 3 -- 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/src/game/client/components/damageind.cpp b/src/game/client/components/damageind.cpp index 8ea48067f..dfcba20e3 100644 --- a/src/game/client/components/damageind.cpp +++ b/src/game/client/components/damageind.cpp @@ -15,34 +15,18 @@ CDamageInd::CDamageInd() m_NumItems = 0; } -CDamageInd::CItem *CDamageInd::CreateI() -{ - if(m_NumItems < MAX_ITEMS) - { - CItem *p = &m_aItems[m_NumItems]; - m_NumItems++; - return p; - } - return 0; -} - -void CDamageInd::DestroyI(CDamageInd::CItem *pItem) -{ - m_NumItems--; - *pItem = m_aItems[m_NumItems]; -} - void CDamageInd::Create(vec2 Pos, vec2 Dir, float Alpha) { - CItem *pItem = CreateI(); - if(pItem) - { - pItem->m_Pos = Pos; - pItem->m_StartTime = LocalTime(); - pItem->m_Dir = -Dir; - pItem->m_StartAngle = -random_angle(); - pItem->m_Color = ColorRGBA(1.0f, 1.0f, 1.0f, Alpha); - } + if(m_NumItems >= MAX_ITEMS) + return; + + CItem *pItem = &m_aItems[m_NumItems]; + pItem->m_Pos = Pos; + pItem->m_StartTime = LocalTime(); + pItem->m_Dir = -Dir; + pItem->m_StartAngle = -random_angle(); + pItem->m_Color = ColorRGBA(1.0f, 1.0f, 1.0f, Alpha); + ++m_NumItems; } void CDamageInd::OnRender() @@ -70,7 +54,10 @@ void CDamageInd::OnRender() float Life = 0.75f - (LocalTime() - m_aItems[i].m_StartTime); if(Life < 0.0f) - DestroyI(&m_aItems[i]); + { + --m_NumItems; + m_aItems[i] = m_aItems[m_NumItems]; + } else { vec2 Pos = mix(m_aItems[i].m_Pos + m_aItems[i].m_Dir * 75.0f, m_aItems[i].m_Pos, clamp((Life - 0.60f) / 0.15f, 0.0f, 1.0f)); diff --git a/src/game/client/components/damageind.h b/src/game/client/components/damageind.h index 75c3eec66..bedae4ca6 100644 --- a/src/game/client/components/damageind.h +++ b/src/game/client/components/damageind.h @@ -25,9 +25,6 @@ class CDamageInd : public CComponent CItem m_aItems[MAX_ITEMS]; int m_NumItems; - CItem *CreateI(); - void DestroyI(CItem *pItem); - int m_DmgIndQuadContainerIndex; public: From 12aa79d6f82ae539179a62dcbfcf85721b5781a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 26 Jul 2024 18:39:45 +0200 Subject: [PATCH 7/7] Avoid duplicate calculation of damage indicator life Only calculate the passed time once to adjust damage indicators' remaining life instead calculating the time separately for each damage indicator. --- src/game/client/components/damageind.cpp | 48 +++++++++++++----------- src/game/client/components/damageind.h | 2 +- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/game/client/components/damageind.cpp b/src/game/client/components/damageind.cpp index dfcba20e3..0ac462da0 100644 --- a/src/game/client/components/damageind.cpp +++ b/src/game/client/components/damageind.cpp @@ -22,8 +22,8 @@ void CDamageInd::Create(vec2 Pos, vec2 Dir, float Alpha) CItem *pItem = &m_aItems[m_NumItems]; pItem->m_Pos = Pos; - pItem->m_StartTime = LocalTime(); pItem->m_Dir = -Dir; + pItem->m_RemainingLife = 0.75f; pItem->m_StartAngle = -random_angle(); pItem->m_Color = ColorRGBA(1.0f, 1.0f, 1.0f, Alpha); ++m_NumItems; @@ -34,41 +34,45 @@ void CDamageInd::OnRender() if(Client()->State() != IClient::STATE_ONLINE && Client()->State() != IClient::STATE_DEMOPLAYBACK) return; - Graphics()->TextureSet(GameClient()->m_GameSkin.m_aSpriteStars[0]); static float s_LastLocalTime = LocalTime(); + float LifeAdjustment; + if(Client()->State() == IClient::STATE_DEMOPLAYBACK) + { + const IDemoPlayer::CInfo *pInfo = DemoPlayer()->BaseInfo(); + if(pInfo->m_Paused) + LifeAdjustment = 0.0f; + else + LifeAdjustment = (LocalTime() - s_LastLocalTime) * pInfo->m_Speed; + } + else + { + const auto &pGameInfoObj = GameClient()->m_Snap.m_pGameInfoObj; + if(pGameInfoObj && pGameInfoObj->m_GameStateFlags & GAMESTATEFLAG_PAUSED) + LifeAdjustment = 0.0f; + else + LifeAdjustment = LocalTime() - s_LastLocalTime; + } + s_LastLocalTime = LocalTime(); + + Graphics()->TextureSet(GameClient()->m_GameSkin.m_aSpriteStars[0]); for(int i = 0; i < m_NumItems;) { - if(Client()->State() == IClient::STATE_DEMOPLAYBACK) - { - const IDemoPlayer::CInfo *pInfo = DemoPlayer()->BaseInfo(); - if(pInfo->m_Paused) - m_aItems[i].m_StartTime += LocalTime() - s_LastLocalTime; - else - m_aItems[i].m_StartTime += (LocalTime() - s_LastLocalTime) * (1.0f - pInfo->m_Speed); - } - else - { - if(m_pClient->m_Snap.m_pGameInfoObj && m_pClient->m_Snap.m_pGameInfoObj->m_GameStateFlags & GAMESTATEFLAG_PAUSED) - m_aItems[i].m_StartTime += LocalTime() - s_LastLocalTime; - } - - float Life = 0.75f - (LocalTime() - m_aItems[i].m_StartTime); - if(Life < 0.0f) + m_aItems[i].m_RemainingLife -= LifeAdjustment; + if(m_aItems[i].m_RemainingLife < 0.0f) { --m_NumItems; m_aItems[i] = m_aItems[m_NumItems]; } else { - vec2 Pos = mix(m_aItems[i].m_Pos + m_aItems[i].m_Dir * 75.0f, m_aItems[i].m_Pos, clamp((Life - 0.60f) / 0.15f, 0.0f, 1.0f)); - const float LifeAlpha = Life / 0.1f; + vec2 Pos = mix(m_aItems[i].m_Pos + m_aItems[i].m_Dir * 75.0f, m_aItems[i].m_Pos, clamp((m_aItems[i].m_RemainingLife - 0.60f) / 0.15f, 0.0f, 1.0f)); + const float LifeAlpha = m_aItems[i].m_RemainingLife / 0.1f; Graphics()->SetColor(m_aItems[i].m_Color.WithMultipliedAlpha(LifeAlpha)); - Graphics()->QuadsSetRotation(m_aItems[i].m_StartAngle + Life * 2.0f); + Graphics()->QuadsSetRotation(m_aItems[i].m_StartAngle + m_aItems[i].m_RemainingLife * 2.0f); Graphics()->RenderQuadContainerAsSprite(m_DmgIndQuadContainerIndex, 0, Pos.x, Pos.y); i++; } } - s_LastLocalTime = LocalTime(); Graphics()->QuadsSetRotation(0); Graphics()->SetColor(1.f, 1.f, 1.f, 1.f); diff --git a/src/game/client/components/damageind.h b/src/game/client/components/damageind.h index bedae4ca6..8b45acc8b 100644 --- a/src/game/client/components/damageind.h +++ b/src/game/client/components/damageind.h @@ -12,7 +12,7 @@ class CDamageInd : public CComponent { vec2 m_Pos; vec2 m_Dir; - float m_StartTime; + float m_RemainingLife; float m_StartAngle; ColorRGBA m_Color; };