Secondary ammo always displays in red in HUD, thought it should only if secondary attack is not readyI made a custom grenade launcher for my weapon_smg1 that:
1) fires if bool SMG1_GL_Loaded is true
2) reloads if bool SMG1_GL_Loaded is false and player has ammo for it.
I'd like to display the secondary ammo section in red if the grenade launcher is not ready to fire (i.e.bool SMG1_GL_Loadedisfalseor there's no ammo) and in regular HUD color in other cases, but for me it's always red.
Here's my src/game/client/hl2/hud.ammo.cpp
//-----------------------------------------------------------------------------
// Purpose: Displays the secondary ammunition level
//-----------------------------------------------------------------------------
class CHudSecondaryAmmo : public CHudNumericDisplay, public CHudElement
{
DECLARE_CLASS_SIMPLE( CHudSecondaryAmmo, CHudNumericDisplay );
public:
CHudSecondaryAmmo( const char *pElementName ) : BaseClass( NULL, "HudAmmoSecondary" ), CHudElement( pElementName )
{
m_iAmmo = -1;
m_iSMG1_GL_Loaded = false;
m_iAR1M1_GL_Loaded = false;
SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_WEAPONSELECTION | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );
}
void Init( void )
{
m_iSMG1_GL_Loaded = false;
m_iAR1M1_GL_Loaded = false;
#ifndef HL2MP
wchar_t *tempString = g_pVGuiLocalize->Find("#Valve_Hud_AMMO_ALT");
if (tempString)
{
SetLabelText(tempString);
}
else
{
SetLabelText(L"ALT");
}
#endif // HL2MP
}
void VidInit( void )
{
}
void SetAmmo(int ammo, bool playAnimation, bool SMG1_GL_Loaded)
{
C_BaseCombatWeapon *wpn = GetActiveWeapon();
const char* ActiveWeaponName = wpn->GetName();
if (ammo != m_iAmmo || m_iSMG1_GL_Loaded != SMG1_GL_Loaded) // some activity in secondary attacks
{
if (ammo == 0)
{
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("AmmoSecondaryEmpty");
}
if (ammo < m_iAmmo && strcmp(ActiveWeaponName, "weapon_smg1") == 0)
{
// ammo has decreased and current weapon is SMG1 means that its grenade launcher is unloaded
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("AmmoSecondaryDecreasedUnloaded");
}
else if (ammo < m_iAmmo)
{
// ammo has decreased
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("AmmoSecondaryDecreased");
}
else if (SMG1_GL_Loaded == false && strcmp(ActiveWeaponName, "weapon_smg1") == 0)
{
// ammunition has increased but active weapon is SMG1 and its grenade launcher is unloaded
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("AmmoSecondaryIncreasedUnloaded");
}
else
{
// ammunition has increased or grenade launcher has been loaded
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("AmmoSecondaryIncreased");
}
}
if (m_iAmmo != ammo)
{
m_iAmmo = ammo;
}
if (m_iSMG1_GL_Loaded != SMG1_GL_Loaded)
{
m_iSMG1_GL_Loaded = SMG1_GL_Loaded;
}
SetDisplayValue(ammo);
}
void Reset()
{
// hud reset, update ammo state
BaseClass::Reset();
m_iAmmo = 0;
m_hCurrentActiveWeapon = NULL;
SetAlpha( 0 );
UpdateAmmoState();
}
virtual void Paint( void )
{
BaseClass::Paint();
#ifndef HL2MP
if ( m_iconSecondaryAmmo )
{
int nLabelHeight;
int nLabelWidth;
surface()->GetTextSize( m_hTextFont, m_LabelText, nLabelWidth, nLabelHeight );
// Figure out where we're going to put this
int x = text_xpos + ( nLabelWidth - m_iconSecondaryAmmo->Width() ) / 2;
int y = text_ypos - ( nLabelHeight + ( m_iconSecondaryAmmo->Height() / 2 ) );
/*C_BaseCombatWeapon *wpn = GetActiveWeapon();
const char* ActiveWeaponName = wpn->GetName();
if (strcmp (ActiveWeaponName,"weapon_smg1") == 0 && !SMG1_GL_Loaded)
m_iconSecondaryAmmo->DrawSelf( x, y, Color (255, 0, 0, 255) );
else
*/
m_iconSecondaryAmmo->DrawSelf(x, y, GetFgColor());
}
#endif // HL2MP
}
protected:
virtual void OnThink()
{
// set whether or not the panel draws based on if we have a weapon that supports secondary ammo
C_BaseCombatWeapon *wpn = GetActiveWeapon();
C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();
IClientVehicle *pVehicle = player ? player->GetVehicle() : NULL;
if (!wpn || !player || pVehicle)
{
m_hCurrentActiveWeapon = NULL;
SetPaintEnabled(false);
SetPaintBackgroundEnabled(false);
return;
}
else
{
SetPaintEnabled(true);
SetPaintBackgroundEnabled(true);
}
UpdateAmmoState();
}
void UpdateAmmoState()
{
C_BaseCombatWeapon *wpn = GetActiveWeapon();
C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();
if (player && wpn && wpn->UsesSecondaryAmmo())
{
//SetAmmo(player->GetAmmoCount(wpn->GetSecondaryAmmoType()), true, SMG1_GL_Loaded, AR1M1_GL_Loaded);
SetAmmo(player->GetAmmoCount(wpn->GetSecondaryAmmoType()), true, SMG1_GL_Loaded);
}
if ( m_hCurrentActiveWeapon != wpn )
{
if ( wpn->UsesSecondaryAmmo() )
{
// we've changed to a weapon that uses secondary ammo
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("WeaponUsesSecondaryAmmo");
}
else
{
// we've changed away from a weapon that uses secondary ammo
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("WeaponDoesNotUseSecondaryAmmo");
}
m_hCurrentActiveWeapon = wpn;
// Get the icon we should be displaying
m_iconSecondaryAmmo = gWR.GetAmmoIconFromWeapon( m_hCurrentActiveWeapon->GetSecondaryAmmoType() );
}
}
private:
CHandle< C_BaseCombatWeapon > m_hCurrentActiveWeapon;
CHudTexture *m_iconSecondaryAmmo;
int m_iAmmo;
bool m_iSMG1_GL_Loaded;
bool m_iAR1M1_GL_Loaded;
};
DECLARE_HUDELEMENT( CHudSecondaryAmmo );
and scripts/hudanimations.txt
event AmmoSecondaryIncreased
{
StopEvent AmmoSecondaryDecreased 0.0
StopEvent AmmoSecondaryDecreasedUnloaded 0.0
StopEvent AmmoSecondaryIncreasedUnloaded 0.0
StopEvent AmmoSecondaryEmpty 0.0
Animate HudAmmoSecondary FgColor "BrightFg" Linear 0.0 0.15
Animate HudAmmoSecondary FgColor "FgColor" Deaccel 0.15 1.5
Animate HudAmmoSecondary Blur "5" Linear 0.0 0.0
Animate HudAmmoSecondary Blur "0" Accel 0.01 1.5
}
event AmmoSecondaryDecreased
{
StopEvent AmmoSecondaryIncreased 0.0
Animate HudAmmoSecondary Blur "7" Linear 0.0 0.0
Animate HudAmmoSecondary Blur "0" Deaccel 0.1 1.5
Animate HudAmmoSecondary TextColor "BrightFg" Linear 0.0 0.1
Animate HudAmmoSecondary TextColor "FgColor" Deaccel 0.1 0.75
}
event AmmoSecondaryIncreasedUnloaded
{
StopEvent AmmoSecondaryEmpty 0.0
Animate HudAmmoSecondary FgColor "BrightDamagedFg" Linear 0.0 0.2
Animate HudAmmoSecondary FgColor "DamagedFg" Accel 0.2 1.2
Animate HudAmmoSecondary Blur "7" Linear 0.0 0.0
Animate HudAmmoSecondary Blur "0" Deaccel 0.1 1.5
}
event AmmoSecondaryDecreasedUnloaded
{
StopEvent AmmoSecondaryIncreased 0.0
Animate HudAmmoSecondary FgColor "BrightDamagedFg" Linear 0.0 0.2
Animate HudAmmoSecondary FgColor "DamagedFg" Accel 0.2 1.2
Animate HudAmmoSecondary Blur "7" Linear 0.0 0.0
Animate HudAmmoSecondary Blur "0" Deaccel 0.1 1.5
}
event AmmoSecondaryEmpty
{
Animate HudAmmoSecondary FgColor "BrightDamagedFg" Linear 0.0 0.2
Animate HudAmmoSecondary FgColor "DamagedFg" Accel 0.2 1.2
Animate HudAmmoSecondary Blur "7" Linear 0.0 0.0
Animate HudAmmoSecondary Blur "0" Deaccel 0.1 1.5
}
Thanks a lot in advance!
Edited by: HEVcrab