Merge pull request #8669 from Robyt3/Client-Damageind-Refactoring

Refactor and optimize damage indicators, reset damage indicators when skipping in demo
This commit is contained in:
Dennis Felsing 2024-07-31 21:48:00 +00:00 committed by GitHub
commit 2c61177745
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 43 additions and 71 deletions

View file

@ -12,39 +12,21 @@
CDamageInd::CDamageInd()
{
m_Lastupdate = 0;
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)
{
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_RemainingLife = 0.75f;
pItem->m_StartAngle = -random_angle();
pItem->m_Color = ColorRGBA(1.0f, 1.0f, 1.0f, Alpha);
pItem->m_StartAlpha = Alpha;
}
++m_NumItems;
}
void CDamageInd::OnRender()
@ -52,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();
for(int i = 0; i < m_NumItems;)
{
float LifeAdjustment;
if(Client()->State() == IClient::STATE_DEMOPLAYBACK)
{
const IDemoPlayer::CInfo *pInfo = DemoPlayer()->BaseInfo();
if(pInfo->m_Paused)
m_aItems[i].m_StartTime += LocalTime() - s_LastLocalTime;
LifeAdjustment = 0.0f;
else
m_aItems[i].m_StartTime += (LocalTime() - s_LastLocalTime) * (1.0f - pInfo->m_Speed);
LifeAdjustment = (LocalTime() - s_LastLocalTime) * 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;
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();
float Life = 0.75f - (LocalTime() - m_aItems[i].m_StartTime);
if(Life < 0.0f)
DestroyI(&m_aItems[i]);
Graphics()->TextureSet(GameClient()->m_GameSkin.m_aSpriteStars[0]);
for(int i = 0; i < m_NumItems;)
{
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));
ColorRGBA Color = m_aItems[i].m_Color;
float LifeAlpha = Life / 0.1f;
Color.a = m_aItems[i].m_StartAlpha * LifeAlpha;
Graphics()->SetColor(Color);
Graphics()->QuadsSetRotation(m_aItems[i].m_StartAngle + Life * 2.0f);
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 + 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);
@ -105,10 +91,7 @@ void CDamageInd::OnInit()
Graphics()->QuadContainerUpload(m_DmgIndQuadContainerIndex);
}
void CDamageInd::Reset()
void CDamageInd::OnReset()
{
for(int i = 0; i < m_NumItems;)
{
DestroyI(&m_aItems[i]);
}
m_NumItems = 0;
}

View file

@ -8,15 +8,13 @@
class CDamageInd : public CComponent
{
int64_t m_Lastupdate;
struct CItem
{
vec2 m_Pos;
vec2 m_Dir;
float m_StartTime;
float m_RemainingLife;
float m_StartAngle;
ColorRGBA m_Color;
float m_StartAlpha;
};
enum
@ -27,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:
@ -37,7 +32,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;
};

View file

@ -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)

View file

@ -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);

View file

@ -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();

View file

@ -1010,7 +1010,6 @@ void CGameClient::OnShutdown()
void CGameClient::OnEnterGame()
{
m_Effects.ResetDamageIndicator();
}
void CGameClient::OnGameOver()